forked from BorisKozo/jsHash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashes.js
826 lines (694 loc) · 30 KB
/
hashes.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//A key-value pair constructor function for internal use
var KeyValuePair = function (key, value) {
this._key = key;
this._value = value;
};
///Various static methods. Override these methods to change the behavior of all hashes objects
var statics = (function () {
return {
//Makes sure that the key is valid for the HashTable
verifyKey: function (key) {
if (key === undefined || key === null) {
throw new Error("Key cannot be undefined or null");
}
return true;
},
///Converts a string to a numeric hash
stringToHash: function stringToHash(s) {
var i, stringData = s, result = 0;
if (s === null || s === undefined) {
return 0;
}
if (typeof (s) !== "string") {
stringData = s.toString();
}
if (!stringData) {
return 0;
}
if (typeof (stringData) !== "string") {
throw new Error("stringToHash: A string argument expected or any object that implements toString()");
}
i = stringData.length - 1;
while (i > 0) {
result = stringData.charCodeAt(i) * 31 + result;
result = result & result;
i -= 1;
}
return result & result;
},
/// Compares the first item to the second items and returns true if the two items are equal and false otherwise
defaultEqual: function (first, second) {
return first === second;
},
/// Returns the hash of a given key based on the provided options
getHash: function (key, options) {
if (options && options.hasOwnProperty("getHashCode")) {
return options.getHashCode(key);
}
if (key.getHashCode !== undefined && typeof (key.getHashCode) === "function") {
return key.getHashCode();
}
return key.toString();
},
/// Returns the most appropriate equal function based on the options and the key
getEqual: function (key, options) {
if (options && options.equal) {
return options.equal;
} else {
if (key.equal !== undefined && typeof (key.equal) === "function") {
return function(other, unused) { return key.equal(other); };
} else {
return statics.defaultEqual;
}
}
},
/// Shallow copies only the relevant properties and functions from the options object
copyOptions: function (options) {
if (options === undefined || options === null) {
return undefined;
}
var result = {};
if (options.hasOwnProperty("getHashCode")) {
result.getHashCode = options.getHashCode;
}
if (options.hasOwnProperty("equal")) {
result.equal = options.equal;
}
return result;
}
};
})();
var HashTable = (function () {
function HashTable(options) {
this._buckets = {
};
this._count = 0;
this._options = statics.copyOptions(options);
}
//Static functions
//Searches the given key within the given bucket. If the key is found then returns the index in the bucket,
//otherwise returns -1
HashTable.getKeyIndex = function (bucket, key, options) {
var i, bucketLength = bucket.length, equality, currentItem;
equality = statics.getEqual(key, options);
for (i = 0; i < bucketLength; i += 1) {
currentItem = bucket[i];
if (currentItem !== undefined && equality(currentItem._key, key)) {
return i;
}
}
return -1;
};
///A static function that adds the given key value pair to the given bucket
HashTable.addToBucket = function (bucket, key, value, options) {
bucket.push(new KeyValuePair(key, value));
};
//Removes the key from the given bucket. Returns false if the key was not found in the bucket
HashTable.removeKey = function (bucket, key, options) {
var index = HashTable.getKeyIndex(bucket, key, options), bucketLength = bucket.length;
if (index < 0) {
return false;
}
if (bucketLength > 1 && index !== (bucketLength - 1)) {
bucket[index] = bucket[bucketLength - 1];
}
bucket.length = bucketLength - 1;
return true;
};
///Creates a new HashTable which is a union of the first and second HashTables. You may specify an optional options parameter and
///an optional overwriteIfExists parameter. The options are used to create the result HashTable and all the key-value pairs are added accordingly.
//When overwriteIfExists is true and a key from the second HashTable already exists in the first HashTable the entire key-value pair will be overwritten in the result.
///If overwriteIfExists is false then the key-value pair is ignored.
HashTable.union = function (first, second, options, overwriteIfExists) {
var result = new HashTable(options),
keyValuePairs;
keyValuePairs = first.getKeyValuePairs();
result.addRange(keyValuePairs, overwriteIfExists);
keyValuePairs = second.getKeyValuePairs();
result.addRange(keyValuePairs, overwriteIfExists);
return result;
};
///Creates a new HashTable which is an intersection of the first and second HashTables. You may specify an optional options parameter and
///an optional overwriteIfExists parameter. The options are used to create the result HashTable and all the key-value pairs are added accordingly.
///overwriteIfExists is used to add the elements to the resulting HashTable that might have different options (see the add function for details).
///In any case the key-value pairs from the first HashTable are in the result.
HashTable.intersection = function (first, second, options, overwriteIfExists) {
var firstLength = first.count(),
secondLength = second.count(),
result, i, keyValuePairs, tempPair;
result = new HashTable(options);
if (firstLength < secondLength) {
keyValuePairs = first.getKeyValuePairs();
for (i = 0; i < firstLength; i += 1) {
if (second.contains(keyValuePairs[i].key)) {
result.add(keyValuePairs[i].key, keyValuePairs[i].value, overwriteIfExists);
}
}
} else {
keyValuePairs = second.getKeyValuePairs();
for (i = 0; i < secondLength; i += 1) {
tempPair = first.get(keyValuePairs[i].key);
if (tempPair !== null) {
result.add(tempPair.key, tempPair.value, overwriteIfExists);
}
}
}
return result;
};
///Creates a new HashTable which is the difference of the first and second HashTables (i.e. all the key-value pairs which are in the first HashTable but not in the second HashTable).
///You may specify an optional options parameter and an optional overwriteIfExists parameter. The options are used to create the result HashTable and all the key-value pairs are added accordingly.
///overwriteIfExists is used to add the elements to the resulting HashTable that might have different options (see the add function for details).
///In any case the key-value pairs from the first HashTable are in the result.
HashTable.difference = function (first, second, options, overwriteIfExists) {
var i, length, keyValuePairs, result = new HashTable(options), pair;
keyValuePairs = first.getKeyValuePairs();
length = first.count();
for (i = 0; i < length; i += 1) {
pair = keyValuePairs[i];
if (!second.contains(pair.key, pair.value)) {
result.add(pair.key, pair.value, overwriteIfExists);
}
}
return result;
};
///Creates a new HashTable which is the symmetric difference of the first and second HashTables (i.e. all the key-value pairs which are in the first HashTable but not in the second HashTable
//or in the second HashTable but not in the first).
///You may specify an optional options parameter and an optional overwriteIfExists parameter. The options are used to create the result HashTable and all the key-value pairs are added accordingly.
///overwriteIfExists is used to add the elements to the resulting HashTable that might have different options (see the add function for details).
HashTable.symmetricDifference = function (first, second, options, overwriteIfExists) {
var i, length, keyValuePairs, result = new HashTable(options), pair;
keyValuePairs = first.getKeyValuePairs();
length = first.count();
for (i = 0; i < length; i += 1) {
pair = keyValuePairs[i];
if (!second.contains(pair.key, pair.value)) {
result.add(pair.key, pair.value, overwriteIfExists);
}
}
keyValuePairs = second.getKeyValuePairs();
length = second.count();
for (i = 0; i < length; i += 1) {
pair = keyValuePairs[i];
if (!first.contains(pair.key, pair.value)) {
result.add(pair.key, pair.value, false);
}
}
return result;
};
//Prototype functions
///Adds a key value pair to the HashTable, returns true if any item was added and false otherwise.
///you may specify the overwriteIfExists flag. When overwriteIfExists is true the value of the given key will be replaced (the key will also be replaced)
///if this key already exists in the HashTable. When overwriteIfExists is false and the key already exists then nothing will happen but the
///function will return false (since nothing was added)
HashTable.prototype.add = function (key, value, overwriteIfExists) {
var hash, addedItem, bucket, itemIndex;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
this._buckets[hash] = [];
}
bucket = this._buckets[hash];
itemIndex = HashTable.getKeyIndex(bucket, key, this._options);
if (itemIndex >= 0) {
if (overwriteIfExists) {
bucket[itemIndex] = new KeyValuePair(key, value);
return true;
}
return false;
} else {
addedItem = HashTable.addToBucket(this._buckets[hash], key, value, this._options);
this._count += 1;
return true;
}
};
///Adds a collection of keys and values to the HashTable, returns the number of items added.
///There are two possible uses:
///1) addRange(keyValuePairs,[overwriteIfExists]) - provide a collection of objects that have key and value property and an optional overwriteIfExists argument (see add for details)
///2) addRange(keys, values, [overwriteIfExists]) - provide two collections (one of keys and the other of values) and an optional overwriteIfExists argument (see add for details)
HashTable.prototype.addRange = function (arg1, arg2, arg3) {
var i, keysLength, valuesLength, minLength, result = 0;
if (arguments.length === 1 || (arguments.length === 2 && (typeof (arguments[1]) === "boolean" || arguments[1] === undefined))) {
for (i = 0, keysLength = arg1.length; i < keysLength; i += 1) {
if (this.add(arg1[i].key, arg1[i].value, arg2)) {
result += 1;
}
}
} else {
keysLength = arg1.length;
valuesLength = arg2.length;
minLength = Math.min(keysLength, valuesLength);
for (i = 0; i < minLength; i += 1) {
if (this.add(arg1[i], arg2[i], arg3)) {
result += 1;
}
}
}
return result;
};
///Retrieves the value associated with the given key. If the key doesn't exist null is returned.
HashTable.prototype.get = function (key) {
var hash, bucket, itemIndex;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
return null;
}
bucket = this._buckets[hash];
itemIndex = HashTable.getKeyIndex(bucket, key, this._options);
if (itemIndex < 0) {
return null;
}
return {
value: bucket[itemIndex]._value,
key: bucket[itemIndex]._key
};
};
/// Removes the key-value pair with the given key. Returns false if the key wasn't found in the HashTable
HashTable.prototype.remove = function (key) {
var hash, bucket, keyRemoved;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
return false;
}
bucket = this._buckets[hash];
keyRemoved = HashTable.removeKey(bucket, key, this._options);
if (keyRemoved) {
this._count -= 1;
if (bucket.length === 0) {
delete (this._buckets[hash]);
}
}
return keyRemoved;
};
/// Returns true if the HashTable contains the key and false otherwise
HashTable.prototype.contains = function (key) {
var hash, bucket, itemIndex;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
return false;
}
bucket = this._buckets[hash];
itemIndex = HashTable.getKeyIndex(bucket, key, this._options);
if (itemIndex < 0) {
return false;
}
return true;
};
///Returns all the hashes that are currently in the HashTable
HashTable.prototype.getHashes = function () {
var result = [], hash;
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
result.push(hash);
}
}
return result;
};
///Returns an array of all the key-value pairs in the HashTable
HashTable.prototype.getKeyValuePairs = function () {
var result = [], hash, bucket, i, bucketLength;
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
bucket = this._buckets[hash];
for (i = 0, bucketLength = bucket.length; i < bucketLength; i += 1) {
result.push({
key: bucket[i]._key,
value: bucket[i]._value
});
}
}
}
return result;
};
///Returns the total number of items in the HashTable
HashTable.prototype.count = function () {
return this._count;
};
///Removes all the items from the HashTable
HashTable.prototype.clear = function () {
this._count = 0;
this._buckets = {};
};
///Returns a new HashTable which is a shallow copy of this HashTable
HashTable.prototype.clone = function () {
var result = new HashTable(statics.copyOptions(this._options)),
hash, bucket, newBucket, i, bucketLength;
result._count = this._count;
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
bucket = this._buckets[hash];
bucketLength = bucket.length;
newBucket = [];
newBucket.length = bucketLength;
result._buckets[hash] = newBucket;
for (i = 0; i < bucketLength; i += 1) {
newBucket[i] = new KeyValuePair(bucket[i]._key, bucket[i]._value);
}
}
}
return result;
};
///Returns a new HashTable where all the key value pairs are rehashed according to the new options
HashTable.prototype.rehash = function (options, overwriteIfExists) {
var result = new HashTable(options),
pairs = this.getKeyValuePairs(),
i, length = pairs.length, pair;
for (i = 0; i < length; i += 1) {
pair = pairs[i];
result.add(pair.key, pair.value, overwriteIfExists);
}
return result;
};
///Prints the content of the HashTable to the console. This is used for debugging
HashTable.prototype.print = function () {
var hash, bucket, i, length;
console.log("Count: ", this._count);
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
console.log("*");
console.log("Bucket:", hash);
bucket = this._buckets[hash];
length = bucket.length;
console.log("There are", length, "item slots");
for (i = 0; i < length; i += 1) {
if (bucket[i] === undefined) {
console.log(" ", i, ":", undefined);
} else {
console.log(" ", i, ":", "Key:", bucket[i]._key, "Value:", bucket[i]._value);
}
}
}
}
};
return HashTable;
})();
var HashSet = (function () {
function HashSet(options) {
this._buckets = {
};
this._count = 0;
this._options = statics.copyOptions(options);
}
//Static functions
//Searches the given key within the given bucket. If the key is found then returns the index in the bucket,
//otherwise returns -1
HashSet.getKeyIndex = function (bucket, key, options) {
var i, bucketLength = bucket.length, equality, currentItem;
equality = statics.getEqual(key, options);
for (i = 0; i < bucketLength; i += 1) {
currentItem = bucket[i];
if (currentItem !== undefined && equality(currentItem, key)) {
return i;
}
}
return -1;
};
///A static function that adds the given key to the given bucket
HashSet.addToBucket = function (bucket, key, options) {
bucket.push(key);
};
//Removes the key from the given bucket. Returns false if the key was not found in the bucket
HashSet.removeKey = function (bucket, key, options) {
var index = HashSet.getKeyIndex(bucket, key, options), bucketLength = bucket.length;
if (index < 0) {
return false;
}
if (bucketLength > 1 && index !== (bucketLength - 1)) {
bucket[index] = bucket[bucketLength - 1];
}
bucket.length = bucketLength - 1;
return true;
};
///Creates a new HashSet which is a union of the first and second HashSet. You may specify an optional options parameter and
///an optional overwriteIfExists parameter. The options are used to create the result HashSet and all the keys added accordingly.
//When overwriteIfExists is true and a key from the second HashSet already exists in the first HashTable it will be overwritten in the result.
///If overwriteIfExists is false then the key is ignored.
HashSet.union = function (first, second, options, overwriteIfExists) {
var result = new HashSet(options),
keys;
keys = first.getKeys();
result.addRange(keys, overwriteIfExists);
keys = second.getKeys();
result.addRange(keys);
return result;
};
///Creates a new HashSet which is an intersection of the first and second HashSets. You may specify an optional options parameter and
///an optional overwriteIfExists parameter. The options are used to create the result HashSet and all the keys are added accordingly.
///overwriteIfExists is used to add the elements to the resulting HashSet that might have different options (see the add function for details).
///In any case the keys from the first HashSet are in the result.
HashSet.intersection = function (first, second, options, overwriteIfExists) {
var firstLength = first.count(),
secondLength = second.count(),
result, i, keys, tempKey;
result = new HashSet(options);
if (firstLength < secondLength) {
keys = first.getKeys();
for (i = 0; i < firstLength; i += 1) {
tempKey = keys[i];
if (second.contains(tempKey)) {
result.add(tempKey, overwriteIfExists);
}
}
} else {
keys = second.getKeys();
for (i = 0; i < secondLength; i += 1) {
tempKey = first.get(keys[i]);
if (tempKey !== null) {
result.add(tempKey, overwriteIfExists);
}
}
}
return result;
};
///Creates a new HashSet which is the difference of the first and second HashSets (i.e. all the keys which are in the first HashSet but not in the second HashSet).
///You may specify an optional options parameter and an optional overwriteIfExists parameter. The options are used to create the result HashSet and all the keys are added accordingly.
///overwriteIfExists is used to add the elements to the resulting HashSet that might have different options (see the add function for details).
///In any case the keys from the first HashSet are in the result.
HashSet.difference = function (first, second, options, overwriteIfExists) {
var i, length, keys, result = new HashSet(options), tempKey;
keys = first.getKeys();
length = first.count();
for (i = 0; i < length; i += 1) {
tempKey = keys[i];
if (!second.contains(tempKey)) {
result.add(tempKey, overwriteIfExists);
}
}
return result;
};
///Creates a new HashSet which is the symmetric difference of the first and second HashSets (i.e. all the keys which are in the first HashSet but not in the second HashSet
//or in the second HashSet but not in the first).
///You may specify an optional options parameter and an optional overwriteIfExists parameter. The options are used to create the result HashSet and all the keys are added accordingly.
///overwriteIfExists is used to add the elements to the resulting HashSet that might have different options (see the add function for details).
HashSet.symmetricDifference = function (first, second, options, overwriteIfExists) {
var i, length, keys, result = new HashSet(options), tempKey;
keys = first.getKeys();
length = first.count();
for (i = 0; i < length; i += 1) {
tempKey = keys[i];
if (!second.contains(tempKey)) {
result.add(tempKey, overwriteIfExists);
}
}
keys = second.getKeys();
length = second.count();
for (i = 0; i < length; i += 1) {
tempKey = keys[i];
if (!first.contains(tempKey)) {
result.add(tempKey, false);
}
}
return result;
};
//Prototype functions
///Adds a key to the HashSet, returns true if any item was added and false otherwise.
///you may specify the overwriteIfExists flag. When overwriteIfExists is true the key will be replaced
///if this key already exists in the HashSet. When overwriteIfExists is false and the key already exists then nothing
///will happen but the function will return false (since nothing was added)
HashSet.prototype.add = function (key, overwriteIfExists) {
var hash, addedItem, bucket, itemIndex;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
this._buckets[hash] = [];
}
bucket = this._buckets[hash];
itemIndex = HashSet.getKeyIndex(bucket, key, this._options);
if (itemIndex >= 0) {
if (overwriteIfExists) {
bucket[itemIndex] = key;
return true;
}
return false;
} else {
addedItem = HashSet.addToBucket(this._buckets[hash], key, this._options);
this._count += 1;
return true;
}
};
///Adds a collection of keys to the HashSet, returns the number of keys added.
///you may specify the overwriteIfExists flag. When overwriteIfExists is true the key will be replaced
///if this key already exists in the HashSet. When overwriteIfExists is false and the key already exists then nothing
///will happen.
HashSet.prototype.addRange = function (keys, overwriteIfExists) {
var i, length, result = 0;
for (i = 0, length = keys.length; i < length; i += 1) {
if (this.add(keys[i], overwriteIfExists)) {
result += 1;
}
}
return result;
};
///Retrieves the key which is equal to the given key. If the key doesn't exist null is returned.
HashSet.prototype.get = function (key) {
var hash, bucket, itemIndex;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
return null;
}
bucket = this._buckets[hash];
itemIndex = HashSet.getKeyIndex(bucket, key, this._options);
if (itemIndex < 0) {
return null;
}
return bucket[itemIndex];
};
/// Removes the key. Returns false if the key wasn't found in the HashSet
HashSet.prototype.remove = function (key) {
var hash, bucket, keyRemoved;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
return false;
}
bucket = this._buckets[hash];
keyRemoved = HashSet.removeKey(bucket, key, this._options);
if (keyRemoved) {
this._count -= 1;
if (bucket.length === 0) {
delete (this._buckets[hash]);
}
}
return keyRemoved;
};
/// Returns true if the HashSet contains the key and false otherwise
HashSet.prototype.contains = function (key) {
var hash, bucket, itemIndex;
if (!statics.verifyKey) {
return false;
}
hash = statics.getHash(key, this._options).toString();
if (!this._buckets.hasOwnProperty(hash)) {
return false;
}
bucket = this._buckets[hash];
itemIndex = HashSet.getKeyIndex(bucket, key, this._options);
if (itemIndex < 0) {
return false;
}
return true;
};
///Returns all the hashes that are currently in the HashSet
HashSet.prototype.getHashes = function () {
var result = [], hash;
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
result.push(hash);
}
}
return result;
};
///Returns an array of all the keys in the HashSet
HashSet.prototype.getKeys = function () {
var result = [], hash, bucket, i, bucketLength;
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
bucket = this._buckets[hash];
for (i = 0, bucketLength = bucket.length; i < bucketLength; i += 1) {
result.push(bucket[i]);
}
}
}
return result;
};
///Returns the total number of items in the HashSet
HashSet.prototype.count = function () {
return this._count;
};
///Removes all the items from the HashSet
HashSet.prototype.clear = function () {
this._count = 0;
this._buckets = {};
};
///Returns a new HashSet which is a shallow copy of this HashSet
HashSet.prototype.clone = function () {
var result = new HashSet(statics.copyOptions(this._options)),
hash, bucket, newBucket, i, bucketLength;
result._count = this._count;
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
bucket = this._buckets[hash];
bucketLength = bucket.length;
newBucket = [];
newBucket.length = bucketLength;
result._buckets[hash] = newBucket;
for (i = 0; i < bucketLength; i += 1) {
newBucket[i] = bucket[i];
}
}
}
return result;
};
///Returns a new HashSet where all the keys are rehashed according to the new options
HashSet.prototype.rehash = function (options, overwriteIfExists) {
var result = new HashSet(options),
keys = this.getKeys(),
i, length = keys.length;
for (i = 0; i < length; i += 1) {
result.add(keys[i], overwriteIfExists);
}
return result;
};
///Prints the content of the HashSet to the console. This is used for debugging
HashSet.prototype.print = function () {
var hash, bucket, i, length;
console.log("Count: ", this._count);
for (hash in this._buckets) {
if (this._buckets.hasOwnProperty(hash)) {
console.log("*");
console.log("Bucket:", hash);
bucket = this._buckets[hash];
length = bucket.length;
console.log("There are", length, "item slots");
for (i = 0; i < length; i += 1) {
if (bucket[i] === undefined) {
console.log(" ", i, ":", undefined);
} else {
console.log(" ", i, ":", "Key:", bucket[i]);
}
}
}
}
};
return HashSet;
})();
exports.statics = statics;
exports.HashTable = HashTable;
exports.HashSet = HashSet;