UNPKG

1.05 kBJavaScriptView Raw
1import { HashTable } from "../hashtable/HashTable";
2import { AbstractSet } from "../interface/AbstractSet";
3export class HashSet extends AbstractSet {
4 constructor(capacity = HashSet.DEFAULT_TABLE_SIZE) {
5 super();
6 this.hashtable = new HashTable(capacity);
7 }
8 get Size() {
9 return this.hashtable.Count;
10 }
11 add(item) {
12 this.hashtable.put(item, true);
13 return this;
14 }
15 has(element) {
16 return this.hashtable.contains(element);
17 }
18 remove(element) {
19 return this.hashtable.remove(element);
20 }
21 clear() {
22 this.hashtable.clear();
23 }
24 entries() {
25 return this.hashtable.getOrignalKeys();
26 }
27 diff(set) {
28 return super.diff(set);
29 }
30 union(set) {
31 return super.union(set);
32 }
33 intersect(set) {
34 return super.intersect(set);
35 }
36 static fromArray(array) {
37 const set = new HashSet(array.length);
38 array.forEach(item => set.add(item));
39 return set;
40 }
41}
42HashSet.DEFAULT_TABLE_SIZE = 11;