-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
multi-set.js
445 lines (359 loc) · 8.85 KB
/
multi-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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Mnemonist MultiSet
* ====================
*
* JavaScript implementation of a MultiSet.
*/
var Iterator = require('obliterator/iterator'),
forEach = require('obliterator/foreach'),
FixedReverseHeap = require('./fixed-reverse-heap.js');
/**
* Helpers.
*/
var MULTISET_ITEM_COMPARATOR = function(a, b) {
if (a[1] > b[1])
return -1;
if (a[1] < b[1])
return 1;
return 0;
};
// TODO: helper functions: union, intersection, sum, difference, subtract
/**
* MultiSet.
*
* @constructor
*/
function MultiSet() {
this.items = new Map();
Object.defineProperty(this.items, 'constructor', {
value: MultiSet,
enumerable: false
});
this.clear();
}
/**
* Method used to clear the structure.
*
* @return {undefined}
*/
MultiSet.prototype.clear = function() {
// Properties
this.size = 0;
this.dimension = 0;
this.items.clear();
};
/**
* Method used to add an item to the set.
*
* @param {any} item - Item to add.
* @param {number} count - Optional count.
* @return {MultiSet}
*/
MultiSet.prototype.add = function(item, count) {
if (count === 0)
return this;
if (count < 0)
return this.remove(item, -count);
count = count || 1;
if (typeof count !== 'number')
throw new Error('mnemonist/multi-set.add: given count should be a number.');
this.size += count;
const currentCount = this.items.get(item);
if (currentCount === undefined)
this.dimension++;
else
count += currentCount;
this.items.set(item, count);
return this;
};
/**
* Method used to set the multiplicity of an item in the set.
*
* @param {any} item - Target item.
* @param {number} count - Desired multiplicity.
* @return {MultiSet}
*/
MultiSet.prototype.set = function(item, count) {
var currentCount;
if (typeof count !== 'number')
throw new Error('mnemonist/multi-set.set: given count should be a number.');
// Setting an item to 0 or to a negative number means deleting it from the set
if (count <= 0) {
currentCount = this.items.get(item);
if (typeof currentCount !== 'undefined') {
this.size -= currentCount;
this.dimension--;
}
this.items.delete(item);
return this;
}
count = count || 1;
currentCount = this.items.get(item);
if (typeof currentCount === 'number') {
this.items.set(item, currentCount + count);
}
else {
this.dimension++;
this.items.set(item, count);
}
this.size += count;
return this;
};
/**
* Method used to return whether the item exists in the set.
*
* @param {any} item - Item to check.
* @return {boolan}
*/
MultiSet.prototype.has = function(item) {
return this.items.has(item);
};
/**
* Method used to delete an item from the set.
*
* @param {any} item - Item to delete.
* @return {boolan}
*/
MultiSet.prototype.delete = function(item) {
var count = this.items.get(item);
if (count === 0)
return false;
this.size -= count;
this.dimension--;
this.items.delete(item);
return true;
};
/**
* Method used to remove an item from the set.
*
* @param {any} item - Item to delete.
* @param {number} count - Optional count.
* @return {undefined}
*/
MultiSet.prototype.remove = function(item, count) {
if (count === 0)
return;
if (count < 0)
return this.add(item, -count);
count = count || 1;
if (typeof count !== 'number')
throw new Error('mnemonist/multi-set.remove: given count should be a number.');
var currentCount = this.items.get(item);
if (typeof currentCount === 'undefined') return;
var newCount = Math.max(0, currentCount - count);
if (newCount === 0) {
this.items.delete(item);
this.size -= currentCount;
this.dimension--;
}
else {
this.items.set(item, newCount);
this.size -= count;
}
return;
};
/**
* Method used to change a key into another one, merging counts if the target
* key already exists.
*
* @param {any} a - From key.
* @param {any} b - To key.
* @return {MultiSet}
*/
MultiSet.prototype.edit = function(a, b) {
var am = this.multiplicity(a);
// If a does not exist in the set, we can stop right there
if (am === 0)
return;
var bm = this.multiplicity(b);
this.items.set(b, am + bm);
this.items.delete(a);
return this;
};
/**
* Method used to return the multiplicity of the given item.
*
* @param {any} item - Item to get.
* @return {number}
*/
MultiSet.prototype.multiplicity = function(item) {
var count = this.items.get(item);
if (typeof count === 'undefined')
return 0;
return count;
};
MultiSet.prototype.get = MultiSet.prototype.multiplicity;
MultiSet.prototype.count = MultiSet.prototype.multiplicity;
/**
* Method used to return the frequency of the given item in the set.
*
* @param {any} item - Item to get.
* @return {number}
*/
MultiSet.prototype.frequency = function(item) {
if (this.size === 0)
return 0;
var count = this.multiplicity(item);
return count / this.size;
};
/**
* Method used to return the n most common items from the set.
*
* @param {number} n - Number of items to retrieve.
* @return {array}
*/
MultiSet.prototype.top = function(n) {
if (typeof n !== 'number' || n <= 0)
throw new Error('mnemonist/multi-set.top: n must be a number > 0.');
var heap = new FixedReverseHeap(Array, MULTISET_ITEM_COMPARATOR, n);
var iterator = this.items.entries(),
step;
while ((step = iterator.next(), !step.done))
heap.push(step.value);
return heap.consume();
};
/**
* Method used to iterate over the set's values.
*
* @param {function} callback - Function to call for each item.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
MultiSet.prototype.forEach = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;
var i;
this.items.forEach(function(multiplicity, value) {
for (i = 0; i < multiplicity; i++)
callback.call(scope, value, value);
});
};
/**
* Method used to iterate over the set's multiplicities.
*
* @param {function} callback - Function to call for each multiplicity.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
MultiSet.prototype.forEachMultiplicity = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;
this.items.forEach(callback, scope);
};
/**
* Method returning an iterator over the set's keys. I.e. its unique values,
* in a sense.
*
* @return {Iterator}
*/
MultiSet.prototype.keys = function() {
return this.items.keys();
};
/**
* Method returning an iterator over the set's values.
*
* @return {Iterator}
*/
MultiSet.prototype.values = function() {
var iterator = this.items.entries(),
inContainer = false,
step,
value,
multiplicity,
i;
return new Iterator(function next() {
if (!inContainer) {
step = iterator.next();
if (step.done)
return {done: true};
inContainer = true;
value = step.value[0];
multiplicity = step.value[1];
i = 0;
}
if (i >= multiplicity) {
inContainer = false;
return next();
}
i++;
return {
done: false,
value: value
};
});
};
/**
* Method returning an iterator over the set's multiplicities.
*
* @return {Iterator}
*/
MultiSet.prototype.multiplicities = function() {
return this.items.entries();
};
/**
* Attaching the #.entries method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
MultiSet.prototype[Symbol.iterator] = MultiSet.prototype.values;
/**
* Convenience known methods.
*/
MultiSet.prototype.inspect = function() {
return this.items;
};
if (typeof Symbol !== 'undefined')
MultiSet.prototype[Symbol.for('nodejs.util.inspect.custom')] = MultiSet.prototype.inspect;
MultiSet.prototype.toJSON = function() {
return this.items;
};
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a structure.
*
* @param {Iterable} iterable - Target iterable.
* @return {MultiSet}
*/
MultiSet.from = function(iterable) {
var set = new MultiSet();
forEach(iterable, function(value) {
set.add(value);
});
return set;
};
/**
* Function returning whether the multiset A is a subset of the multiset B.
*
* @param {MultiSet} A - First set.
* @param {MultiSet} B - Second set.
* @return {boolean}
*/
MultiSet.isSubset = function(A, B) {
var iterator = A.multiplicities(),
step,
key,
mA;
// Shortcuts
if (A === B)
return true;
if (A.dimension > B.dimension)
return false;
while ((step = iterator.next(), !step.done)) {
key = step.value[0];
mA = step.value[1];
if (B.multiplicity(key) < mA)
return false;
}
return true;
};
/**
* Function returning whether the multiset A is a superset of the multiset B.
*
* @param {MultiSet} A - First set.
* @param {MultiSet} B - Second set.
* @return {boolean}
*/
MultiSet.isSuperset = function(A, B) {
return MultiSet.isSubset(B, A);
};
/**
* Exporting.
*/
module.exports = MultiSet;