123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- goog.provide('goog.structs.StringSet');
- goog.require('goog.asserts');
- goog.require('goog.iter');
- goog.structs.StringSet = function(opt_elements) {
-
- this.elements_ = {};
- if (opt_elements) {
- for (var i = 0; i < opt_elements.length; i++) {
- this.elements_[goog.structs.StringSet.encode_(opt_elements[i])] = null;
- }
- }
- goog.asserts.assertObjectPrototypeIsIntact();
- };
- goog.structs.StringSet.EMPTY_OBJECT_ = {};
- goog.structs.StringSet.encode_ = function(element) {
- return element in goog.structs.StringSet.EMPTY_OBJECT_ ||
- String(element).charCodeAt(0) == 32 ?
- ' ' + element :
- element;
- };
- goog.structs.StringSet.decode_ = function(key) {
- return key.charCodeAt(0) == 32 ? key.substr(1) : key;
- };
- goog.structs.StringSet.prototype.add = function(element) {
- this.elements_[goog.structs.StringSet.encode_(element)] = null;
- };
- goog.structs.StringSet.prototype.addArray = function(arr) {
- for (var i = 0; i < arr.length; i++) {
- this.elements_[goog.structs.StringSet.encode_(arr[i])] = null;
- }
- };
- goog.structs.StringSet.prototype.addDifference_ = function(set1, set2) {
- for (var key in set1.elements_) {
- if (!(key in set2.elements_)) {
- this.elements_[key] = null;
- }
- }
- };
- goog.structs.StringSet.prototype.addSet = function(stringSet) {
- for (var key in stringSet.elements_) {
- this.elements_[key] = null;
- }
- };
- goog.structs.StringSet.prototype.clear = function() {
- this.elements_ = {};
- };
- goog.structs.StringSet.prototype.clone = function() {
- var ret = new goog.structs.StringSet;
- ret.addSet(this);
- return ret;
- };
- goog.structs.StringSet.prototype.contains = function(element) {
- return goog.structs.StringSet.encode_(element) in this.elements_;
- };
- goog.structs.StringSet.prototype.containsArray = function(arr) {
- for (var i = 0; i < arr.length; i++) {
- if (!(goog.structs.StringSet.encode_(arr[i]) in this.elements_)) {
- return false;
- }
- }
- return true;
- };
- goog.structs.StringSet.prototype.equals = function(stringSet) {
- return this.isSubsetOf(stringSet) && stringSet.isSubsetOf(this);
- };
- goog.structs.StringSet.prototype.forEach = function(f, opt_obj) {
- for (var key in this.elements_) {
- f.call(opt_obj, goog.structs.StringSet.decode_(key), undefined, this);
- }
- };
- goog.structs.StringSet.prototype.getCount = Object.keys ? function() {
- return Object.keys(this.elements_).length;
- } : function() {
- var count = 0;
- for (var key in this.elements_) {
- count++;
- }
- return count;
- };
- goog.structs.StringSet.prototype.getDifference = function(stringSet) {
- var ret = new goog.structs.StringSet;
- ret.addDifference_(this, stringSet);
- return ret;
- };
- goog.structs.StringSet.prototype.getIntersection = function(stringSet) {
- var ret = new goog.structs.StringSet;
- for (var key in this.elements_) {
- if (key in stringSet.elements_) {
- ret.elements_[key] = null;
- }
- }
- return ret;
- };
- goog.structs.StringSet.prototype.getSymmetricDifference = function(stringSet) {
- var ret = new goog.structs.StringSet;
- ret.addDifference_(this, stringSet);
- ret.addDifference_(stringSet, this);
- return ret;
- };
- goog.structs.StringSet.prototype.getUnion = function(stringSet) {
- var ret = this.clone();
- ret.addSet(stringSet);
- return ret;
- };
- goog.structs.StringSet.prototype.getValues = Object.keys ? function() {
-
- return Object.keys(this.elements_).map(goog.structs.StringSet.decode_, this);
- } : function() {
- var ret = [];
- for (var key in this.elements_) {
- ret.push(goog.structs.StringSet.decode_(key));
- }
- return ret;
- };
- goog.structs.StringSet.prototype.isDisjoint = function(stringSet) {
- for (var key in this.elements_) {
- if (key in stringSet.elements_) {
- return false;
- }
- }
- return true;
- };
- goog.structs.StringSet.prototype.isEmpty = function() {
- for (var key in this.elements_) {
- return false;
- }
- return true;
- };
- goog.structs.StringSet.prototype.isSubsetOf = function(stringSet) {
- for (var key in this.elements_) {
- if (!(key in stringSet.elements_)) {
- return false;
- }
- }
- return true;
- };
- goog.structs.StringSet.prototype.isSupersetOf = function(stringSet) {
- return stringSet.isSubsetOf(this);
- };
- goog.structs.StringSet.prototype.remove = function(element) {
- var key = goog.structs.StringSet.encode_(element);
- if (key in this.elements_) {
- delete this.elements_[key];
- return true;
- }
- return false;
- };
- goog.structs.StringSet.prototype.removeArray = function(arr) {
- for (var i = 0; i < arr.length; i++) {
- delete this.elements_[goog.structs.StringSet.encode_(arr[i])];
- }
- };
- goog.structs.StringSet.prototype.removeSet = function(stringSet) {
- for (var key in stringSet.elements_) {
- delete this.elements_[key];
- }
- };
- goog.structs.StringSet.prototype.__iterator__ = function(opt_keys) {
- return goog.iter.toIterator(this.getValues());
- };
|