forked from mauriciosantos/Buckets-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.js
188 lines (170 loc) · 5.48 KB
/
set.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Creates an empty set.
* @class <p>A set is a data structure that contains no duplicate items.</p>
* <p>If the inserted elements are custom objects, a function
* that converts elements to unique strings must be provided at construction time.
* <p>Example:</p>
* <pre>
* function petToString(pet) {
* return pet.type + ' ' + pet.name;
* }
* </pre>
*
* @param {function(Object):string=} toStringFunction Optional function used
* to convert elements to unique strings. If the elements aren't strings or if toString()
* is not appropriate, a custom function which receives an object and returns a
* unique string must be provided.
*/
buckets.Set = function (toStringFunction) {
/**
* @exports theSet as buckets.Set
* @private
*/
var theSet = {},
// Underlying storage.
dictionary = new buckets.Dictionary(toStringFunction);
/**
* Returns true if the set contains the specified element.
* @param {Object} element Element to search for.
* @return {boolean} True if the set contains the specified element,
* false otherwise.
*/
theSet.contains = function (element) {
return dictionary.containsKey(element);
};
/**
* Adds the specified element to the set if it's not already present.
* @param {Object} element The element to insert.
* @return {boolean} True if the set did not already contain the specified element.
*/
theSet.add = function (element) {
if (theSet.contains(element) || buckets.isUndefined(element)) {
return false;
}
dictionary.set(element, element);
return true;
};
/**
* Performs an intersection between this and another set.
* Removes all values that are not present in this set and the given set.
* @param {buckets.Set} otherSet Other set.
*/
theSet.intersection = function (otherSet) {
theSet.forEach(function (element) {
if (!otherSet.contains(element)) {
theSet.remove(element);
}
});
};
/**
* Performs a union between this and another set.
* Adds all values from the given set to this set.
* @param {buckets.Set} otherSet Other set.
*/
theSet.union = function (otherSet) {
otherSet.forEach(function (element) {
theSet.add(element);
});
};
/**
* Performs a difference between this and another set.
* Removes all the values that are present in the given set from this set.
* @param {buckets.Set} otherSet other set.
*/
theSet.difference = function (otherSet) {
otherSet.forEach(function (element) {
theSet.remove(element);
});
};
/**
* Checks whether the given set contains all the elements of this set.
* @param {buckets.Set} otherSet Other set.
* @return {boolean} True if this set is a subset of the given set.
*/
theSet.isSubsetOf = function (otherSet) {
var isSub = true;
if (theSet.size() > otherSet.size()) {
return false;
}
theSet.forEach(function (element) {
if (!otherSet.contains(element)) {
isSub = false;
return false;
}
});
return isSub;
};
/**
* Removes the specified element from the set.
* @return {boolean} True if the set contained the specified element, false
* otherwise.
*/
theSet.remove = function (element) {
if (!theSet.contains(element)) {
return false;
}
dictionary.remove(element);
return true;
};
/**
* Executes the provided function once per element
* present in the set.
* @param {function(Object):*} callback Function to execute, it's
* invoked an element as argument. To break the iteration you can
* optionally return false inside the callback.
*/
theSet.forEach = function (callback) {
dictionary.forEach(function (k, v) {
return callback(v);
});
};
/**
* Returns an array containing all the elements in the set in no particular order.
* @return {Array} An array containing all the elements in the set.
*/
theSet.toArray = function () {
return dictionary.values();
};
/**
* Returns true if the set contains no elements.
* @return {boolean} True if the set contains no elements.
*/
theSet.isEmpty = function () {
return dictionary.isEmpty();
};
/**
* Returns the number of elements in the set.
* @return {number} The number of elements in the set.
*/
theSet.size = function () {
return dictionary.size();
};
/**
* Removes all the elements from the set.
*/
theSet.clear = function () {
dictionary.clear();
};
/**
* Returns true if the set is equal to another set.
* Two sets are equal if they have the same elements.
* @param {buckets.Set} other The other set.
* @return {boolean} True if the set is equal to the given set.
*/
theSet.equals = function (other) {
var isEqual;
if (buckets.isUndefined(other) || typeof other.isSubsetOf !== 'function') {
return false;
}
if (theSet.size() !== other.size()) {
return false;
}
isEqual = true;
other.forEach(function (element) {
isEqual = theSet.contains(element);
return isEqual;
});
return isEqual;
};
return theSet;
};