forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_set.ts
More file actions
75 lines (66 loc) · 1.45 KB
/
map_set.ts
File metadata and controls
75 lines (66 loc) · 1.45 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
import { Map } from './map'
import { Set } from './set'
/**
* This class is a representation of the Set data structure based on a hash map.
*
* @template K The value type.
* @implements Set<K>
* @property {Map<K, null>} map The map used to store the set.
*/
export abstract class MapSet<K> implements Set<K> {
private map: Map<K, null>
constructor() {
this.map = this.initMap()
}
/**
* Initializes the map used to store the set.
*/
protected abstract initMap(): Map<K, null>
/**
* Adds a new element to the set.
*
* @param value The value to add to the set.
*/
add(value: K): void {
this.map.set(value, null)
}
/**
* Removes an element from the set.
*
* @param value The value to remove from the set.
*/
delete(value: K): void {
this.map.delete(value)
}
/**
* Checks if the set contains a given value.
*
* @param value The value to check for.
* @returns Whether the set contains the value.
*/
has(value: K): boolean {
return this.map.has(value)
}
/**
* Removes all elements from the set.
*/
clear(): void {
this.map.clear()
}
/**
* Returns an array of all the values in the set.
*
* @returns An array of all the values in the set.
*/
values(): K[] {
return this.map.keys()
}
/**
* Returns the number of elements in the set.
*
* @returns The number of elements in the set.
*/
getSize(): number {
return this.map.getSize()
}
}