set.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Datastructure: Set.
  16. *
  17. * @author arv@google.com (Erik Arvidsson)
  18. *
  19. * This class implements a set data structure. Adding and removing is O(1). It
  20. * supports both object and primitive values. Be careful because you can add
  21. * both 1 and new Number(1), because these are not the same. You can even add
  22. * multiple new Number(1) because these are not equal.
  23. */
  24. goog.provide('goog.structs.Set');
  25. goog.require('goog.structs');
  26. goog.require('goog.structs.Collection');
  27. goog.require('goog.structs.Map');
  28. /**
  29. * A set that can contain both primitives and objects. Adding and removing
  30. * elements is O(1). Primitives are treated as identical if they have the same
  31. * type and convert to the same string. Objects are treated as identical only
  32. * if they are references to the same object. WARNING: A goog.structs.Set can
  33. * contain both 1 and (new Number(1)), because they are not the same. WARNING:
  34. * Adding (new Number(1)) twice will yield two distinct elements, because they
  35. * are two different objects. WARNING: Any object that is added to a
  36. * goog.structs.Set will be modified! Because goog.getUid() is used to
  37. * identify objects, every object in the set will be mutated.
  38. * @param {Array<T>|Object<?,T>=} opt_values Initial values to start with.
  39. * @constructor
  40. * @implements {goog.structs.Collection<T>}
  41. * @final
  42. * @template T
  43. * @deprecated This type is misleading: use ES6 Set instead.
  44. */
  45. goog.structs.Set = function(opt_values) {
  46. this.map_ = new goog.structs.Map;
  47. if (opt_values) {
  48. this.addAll(opt_values);
  49. }
  50. };
  51. /**
  52. * Obtains a unique key for an element of the set. Primitives will yield the
  53. * same key if they have the same type and convert to the same string. Object
  54. * references will yield the same key only if they refer to the same object.
  55. * @param {*} val Object or primitive value to get a key for.
  56. * @return {string} A unique key for this value/object.
  57. * @private
  58. */
  59. goog.structs.Set.getKey_ = function(val) {
  60. var type = typeof val;
  61. if (type == 'object' && val || type == 'function') {
  62. return 'o' + goog.getUid(/** @type {Object} */ (val));
  63. } else {
  64. return type.substr(0, 1) + val;
  65. }
  66. };
  67. /**
  68. * @return {number} The number of elements in the set.
  69. * @override
  70. */
  71. goog.structs.Set.prototype.getCount = function() {
  72. return this.map_.getCount();
  73. };
  74. /**
  75. * Add a primitive or an object to the set.
  76. * @param {T} element The primitive or object to add.
  77. * @override
  78. */
  79. goog.structs.Set.prototype.add = function(element) {
  80. this.map_.set(goog.structs.Set.getKey_(element), element);
  81. };
  82. /**
  83. * Adds all the values in the given collection to this set.
  84. * @param {Array<T>|goog.structs.Collection<T>|Object<?,T>} col A collection
  85. * containing the elements to add.
  86. */
  87. goog.structs.Set.prototype.addAll = function(col) {
  88. var values = goog.structs.getValues(col);
  89. var l = values.length;
  90. for (var i = 0; i < l; i++) {
  91. this.add(values[i]);
  92. }
  93. };
  94. /**
  95. * Removes all values in the given collection from this set.
  96. * @param {Array<T>|goog.structs.Collection<T>|Object<?,T>} col A collection
  97. * containing the elements to remove.
  98. */
  99. goog.structs.Set.prototype.removeAll = function(col) {
  100. var values = goog.structs.getValues(col);
  101. var l = values.length;
  102. for (var i = 0; i < l; i++) {
  103. this.remove(values[i]);
  104. }
  105. };
  106. /**
  107. * Removes the given element from this set.
  108. * @param {T} element The primitive or object to remove.
  109. * @return {boolean} Whether the element was found and removed.
  110. * @override
  111. */
  112. goog.structs.Set.prototype.remove = function(element) {
  113. return this.map_.remove(goog.structs.Set.getKey_(element));
  114. };
  115. /**
  116. * Removes all elements from this set.
  117. */
  118. goog.structs.Set.prototype.clear = function() {
  119. this.map_.clear();
  120. };
  121. /**
  122. * Tests whether this set is empty.
  123. * @return {boolean} True if there are no elements in this set.
  124. */
  125. goog.structs.Set.prototype.isEmpty = function() {
  126. return this.map_.isEmpty();
  127. };
  128. /**
  129. * Tests whether this set contains the given element.
  130. * @param {T} element The primitive or object to test for.
  131. * @return {boolean} True if this set contains the given element.
  132. * @override
  133. */
  134. goog.structs.Set.prototype.contains = function(element) {
  135. return this.map_.containsKey(goog.structs.Set.getKey_(element));
  136. };
  137. /**
  138. * Tests whether this set contains all the values in a given collection.
  139. * Repeated elements in the collection are ignored, e.g. (new
  140. * goog.structs.Set([1, 2])).containsAll([1, 1]) is True.
  141. * @param {goog.structs.Collection<T>|Object} col A collection-like object.
  142. * @return {boolean} True if the set contains all elements.
  143. */
  144. goog.structs.Set.prototype.containsAll = function(col) {
  145. return goog.structs.every(col, this.contains, this);
  146. };
  147. /**
  148. * Finds all values that are present in both this set and the given collection.
  149. * @param {Array<S>|Object<?,S>} col A collection.
  150. * @return {!goog.structs.Set<T|S>} A new set containing all the values
  151. * (primitives or objects) present in both this set and the given
  152. * collection.
  153. * @template S
  154. */
  155. goog.structs.Set.prototype.intersection = function(col) {
  156. var result = new goog.structs.Set();
  157. var values = goog.structs.getValues(col);
  158. for (var i = 0; i < values.length; i++) {
  159. var value = values[i];
  160. if (this.contains(value)) {
  161. result.add(value);
  162. }
  163. }
  164. return result;
  165. };
  166. /**
  167. * Finds all values that are present in this set and not in the given
  168. * collection.
  169. * @param {Array<T>|goog.structs.Collection<T>|Object<?,T>} col A collection.
  170. * @return {!goog.structs.Set} A new set containing all the values
  171. * (primitives or objects) present in this set but not in the given
  172. * collection.
  173. */
  174. goog.structs.Set.prototype.difference = function(col) {
  175. var result = this.clone();
  176. result.removeAll(col);
  177. return result;
  178. };
  179. /**
  180. * Returns an array containing all the elements in this set.
  181. * @return {!Array<T>} An array containing all the elements in this set.
  182. */
  183. goog.structs.Set.prototype.getValues = function() {
  184. return this.map_.getValues();
  185. };
  186. /**
  187. * Creates a shallow clone of this set.
  188. * @return {!goog.structs.Set<T>} A new set containing all the same elements as
  189. * this set.
  190. */
  191. goog.structs.Set.prototype.clone = function() {
  192. return new goog.structs.Set(this);
  193. };
  194. /**
  195. * Tests whether the given collection consists of the same elements as this set,
  196. * regardless of order, without repetition. Primitives are treated as equal if
  197. * they have the same type and convert to the same string; objects are treated
  198. * as equal if they are references to the same object. This operation is O(n).
  199. * @param {goog.structs.Collection<T>|Object} col A collection.
  200. * @return {boolean} True if the given collection consists of the same elements
  201. * as this set, regardless of order, without repetition.
  202. */
  203. goog.structs.Set.prototype.equals = function(col) {
  204. return this.getCount() == goog.structs.getCount(col) && this.isSubsetOf(col);
  205. };
  206. /**
  207. * Tests whether the given collection contains all the elements in this set.
  208. * Primitives are treated as equal if they have the same type and convert to the
  209. * same string; objects are treated as equal if they are references to the same
  210. * object. This operation is O(n).
  211. * @param {goog.structs.Collection<T>|Object} col A collection.
  212. * @return {boolean} True if this set is a subset of the given collection.
  213. */
  214. goog.structs.Set.prototype.isSubsetOf = function(col) {
  215. var colCount = goog.structs.getCount(col);
  216. if (this.getCount() > colCount) {
  217. return false;
  218. }
  219. // TODO(user) Find the minimal collection size where the conversion makes
  220. // the contains() method faster.
  221. if (!(col instanceof goog.structs.Set) && colCount > 5) {
  222. // Convert to a goog.structs.Set so that goog.structs.contains runs in
  223. // O(1) time instead of O(n) time.
  224. col = new goog.structs.Set(col);
  225. }
  226. return goog.structs.every(
  227. this, function(value) { return goog.structs.contains(col, value); });
  228. };
  229. /**
  230. * Returns an iterator that iterates over the elements in this set.
  231. * @param {boolean=} opt_keys This argument is ignored.
  232. * @return {!goog.iter.Iterator} An iterator over the elements in this set.
  233. */
  234. goog.structs.Set.prototype.__iterator__ = function(opt_keys) {
  235. return this.map_.__iterator__(false);
  236. };