1 | import { HashTable } from "../hashtable/HashTable";
|
2 | import { AbstractSet } from "../interface/AbstractSet";
|
3 |
|
4 |
|
5 |
|
6 | export class HashSet<T= string> extends AbstractSet<T>{
|
7 | private hashtable: HashTable<boolean>;
|
8 | private static DEFAULT_TABLE_SIZE = 11;
|
9 |
|
10 | public get Size(){
|
11 | return this.hashtable.Count;
|
12 | }
|
13 |
|
14 | constructor(capacity = HashSet.DEFAULT_TABLE_SIZE){
|
15 | super();
|
16 | this.hashtable = new HashTable(capacity);
|
17 | }
|
18 | add(item: T){
|
19 |
|
20 |
|
21 | this.hashtable.put(item, true);
|
22 | return this;
|
23 | }
|
24 |
|
25 | has(element: T){
|
26 | return this.hashtable.contains(element);
|
27 | }
|
28 |
|
29 | remove(element: T){
|
30 | return this.hashtable.remove(element);
|
31 | }
|
32 |
|
33 | clear(){
|
34 | this.hashtable.clear();
|
35 | }
|
36 |
|
37 | |
38 |
|
39 |
|
40 | entries(){
|
41 | return this.hashtable.getOrignalKeys();
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | diff(set: AbstractSet<T>): Array<T>{
|
50 | return super.diff(set);
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | union(set: AbstractSet<T>): Array<T>{
|
59 | return super.union(set);
|
60 | }
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | intersect(set: AbstractSet<T>): Array<T>{
|
68 | return super.intersect(set);
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 | static fromArray<T>(array: Array<T>){
|
76 | const set = new HashSet<T>(array.length);
|
77 | array.forEach(item => set.add(item));
|
78 | return set;
|
79 | }
|
80 | }
|