123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- goog.provide('goog.labs.structs.Multimap');
- goog.require('goog.array');
- goog.require('goog.labs.structs.Map');
- goog.require('goog.object');
- goog.labs.structs.Multimap = function() {
- this.clear();
- };
- goog.labs.structs.Multimap.prototype.map_;
- goog.labs.structs.Multimap.prototype.count_ = 0;
- goog.labs.structs.Multimap.prototype.clear = function() {
- this.count_ = 0;
- this.map_ = new goog.labs.structs.Map();
- };
- goog.labs.structs.Multimap.prototype.clone = function() {
- var map = new goog.labs.structs.Multimap();
- map.addAllFromMultimap(this);
- return map;
- };
- goog.labs.structs.Multimap.prototype.add = function(key, value) {
- var values = this.map_.get(key);
- if (!values) {
- this.map_.set(key, (values = []));
- }
- values.push(value);
- this.count_++;
- };
- goog.labs.structs.Multimap.prototype.addAllValues = function(key, values) {
- goog.array.forEach(values, function(v) { this.add(key, v); }, this);
- };
- goog.labs.structs.Multimap.prototype.addAllFromMultimap = function(map) {
- goog.array.forEach(map.getEntries(), function(entry) {
- this.add(entry[0], entry[1]);
- }, this);
- };
- goog.labs.structs.Multimap.prototype.replaceValues = function(key, values) {
- this.removeAll(key);
- this.addAllValues(key, values);
- };
- goog.labs.structs.Multimap.prototype.get = function(key) {
- var values = (this.map_.get(key));
- return values ? goog.array.clone(values) : [];
- };
- goog.labs.structs.Multimap.prototype.remove = function(key, value) {
- var values = (this.map_.get(key));
- if (!values) {
- return false;
- }
- var removed = goog.array.removeIf(
- values, function(v) { return goog.object.is(value, v); });
- if (removed) {
- this.count_--;
- if (values.length == 0) {
- this.map_.remove(key);
- }
- }
- return removed;
- };
- goog.labs.structs.Multimap.prototype.removeAll = function(key) {
-
-
-
- var values = this.map_.get(key);
- if (this.map_.remove(key)) {
- this.count_ -= values.length;
- return true;
- }
- return false;
- };
- goog.labs.structs.Multimap.prototype.isEmpty = function() {
- return !this.count_;
- };
- goog.labs.structs.Multimap.prototype.getCount = function() {
- return this.count_;
- };
- goog.labs.structs.Multimap.prototype.containsEntry = function(key, value) {
- var values = (this.map_.get(key));
- if (!values) {
- return false;
- }
- var index = goog.array.findIndex(
- values, function(v) { return goog.object.is(v, value); });
- return index >= 0;
- };
- goog.labs.structs.Multimap.prototype.containsKey = function(key) {
- return this.map_.containsKey(key);
- };
- goog.labs.structs.Multimap.prototype.containsValue = function(value) {
- return goog.array.some(this.map_.getValues(), function(values) {
- return goog.array.some( (values), function(v) {
- return goog.object.is(v, value);
- });
- });
- };
- goog.labs.structs.Multimap.prototype.getKeys = function() {
- return this.map_.getKeys();
- };
- goog.labs.structs.Multimap.prototype.getValues = function() {
- return goog.array.flatten(this.map_.getValues());
- };
- goog.labs.structs.Multimap.prototype.getEntries = function() {
- var keys = this.getKeys();
- var entries = [];
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i];
- var values = this.get(key);
- for (var j = 0; j < values.length; j++) {
- entries.push([key, values[j]]);
- }
- }
- return entries;
- };
|