-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.js
More file actions
254 lines (223 loc) · 6.31 KB
/
set.js
File metadata and controls
254 lines (223 loc) · 6.31 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* Created by Mahir Shah on 9/22/14.
*/
var JsSet = (function (undefined) {
'use strict';
/**
* Constructor for Set and scope for private methods
*
* @param hashFunction
* @constructor
*/
function Set(hashFunction) {
//custom hash function or default to JSON.stringify
this.hashFunction = hashFunction || JSON.stringify;
//initialize private variables: empty set with length 0
var _length = 0,
_set = {};
/**
* Simple function which finds a value in the set if it exists and returns undefined if not in the set
*
* @param value
* @returns {*}
*/
this.get = function (value) {
return _set[this.hashFunction(value)];
};
/**
* Helper function to return the set object
*
* @returns {{}}
*/
this.getSet = function () {
return _set;
};
/**
* Function which takes in a list of values to put into the set and adds them if they don't exist.
*
* @param {...value} var_args
*/
this.add = function () {
function addOne(value) {
if (this.get(value) === undefined) {
_length++;
_set[this.hashFunction(value)] = value;
}
}
Array.prototype.forEach.call(arguments, function (val) {
addOne.call(this, val);
}, this);
};
/**
* returns the number of items in the set
*
* @returns {number}
*/
this.size = function () {
return _length;
};
/**
* Deletes a given element from the set if it exists. Returns true if the element was deleted, returns false if
* the element was not found in the set.
*
* @param element
*/
this.remove = function (element) {
if (this.isInSet(element)) {
delete _set[this.hashFunction(element)];
_length -= 1;
return true;
}
return false;
};
/**
* For debuging: prints the set out to the console
*/
this.print = function () {
console.log(_set);
};
/**
* Converts the set to an array
*
* @returns {Array}
*/
this.toArray = function () {
var array = [];
for (var element in _set) {
if (_set.hasOwnProperty(element))
array.push(_set[element])
}
return array;
}
}
/**
* Alias function for size
*
* @returns {number}
*/
Set.prototype.cardinality = function () {
return this.size();
};
/**
* Returns a boolean checking if the set is empty
*
* @returns {boolean}
*/
Set.prototype.isEmpty = function () {
return !this.size();
};
/**
* Returns a boolean checking if a given element is in the set
*
* @param element
* @returns {boolean}
*/
Set.prototype.isInSet = function (element) {
return this.get(element) !== undefined;
};
/**
* Iterates over the elements in the set using a provided function with optional arguments value, index
*
* @param fn - callback to be invoked each iteration, with parameters value and index
* @param self - this function context for callback
*/
Set.prototype.iterate = function (fn, self) {
var set = this.getSet(),
index = 0;
for (var element in set) {
fn.call(self, set[element], index);
index += 1;
}
};
/**
* Iterates over the elements in the set using fn callback, if callback is true the function immediately returns true
* else it returns false. The callback is invoked with value and index parameters.
*
* @param fn
* @param self
* @returns {boolean}
*/
Set.prototype.some = function (fn, self) {
var set = this.getSet(),
index = 0;
for (var element in set) {
if (fn.call(self, set[element], index))
return true;
index += 1;
}
return false;
};
/**
* Returns the union set between this set and set2
*
* @param set2
* @returns {JsSet}
*/
Set.prototype.union = function (set2) {
set2.iterate(function (val) {
this.add(val);
}, this);
return this; //return this to make it chainable
};
/**
* Returns the smaller set between this and set2
*
* @param set2
* @returns {JsSet}
*/
Set.prototype.getSmallerSet = function (set2) {
return this.size() <= set2.size() ? this : set2;
};
/**
* Returns the intersection set between this and set2
*
* @param set2
* @returns {Set}
*/
Set.prototype.intersection = function (set2) {
//initialize new set to return intersection
var intersectionSet = new Set(this.hashFunction),
smallerSet = this.getSmallerSet(set2), //find the smaller set to iterate over
largerSet = smallerSet === this ? set2 : this;
smallerSet.iterate(function (val) {
if (largerSet.isInSet(val)) {
intersectionSet.add(val);
}
});
return intersectionSet;
};
/**
* Return the difference set between this and set2
*
* @param set2
* @returns {JsSet}
*/
Set.prototype.difference = function (set2) {
set2.iterate(function (val) {
this.remove(val);
}, this);
return this; //return this to make it chainable
};
/**
* Checks if subset is subset of this
*
* @param subset
*/
Set.prototype.subset = function (subset) {
return subset.some(function (val) {
return this.isInSet(val);
}, this);
};
/**
*
* @returns {*}
*/
Set.prototype.pop = function () {
var set = this.getSet(),
keys = Object.keys(set);
var random = set[keys[Math.floor(Math.random() * keys.length)]];
this.remove(random);
return random;
};
return Set;
})();