-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmapvariants.mjs
More file actions
103 lines (85 loc) · 1.85 KB
/
mapvariants.mjs
File metadata and controls
103 lines (85 loc) · 1.85 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
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
export class CountingMap {
#hits = 0; // Private class field for hits
#misses = 0; // Private class field for misses
#map; // Private class field for the internal Map
#enabled;
constructor({ iterable, enabled }) {
this.#map = new Map(iterable);
if (enabled) {
this.enable();
} else {
this.disable();
}
}
// Override the get() method to count hits and misses
get(key) {
if (this.disabled) {
return undefined;
}
if (this.#map.has(key)) {
this.#hits++; // Accessing private field
return this.#map.get(key);
} else {
this.#misses++; // Accessing private field
return undefined;
}
}
// Delegate other Map methods to the internal #map
set(key, value) {
if (this.enabled) {
return this.#map.set(key, value);
} else {
return undefined;
}
}
has(key) {
return this.enabled && this.#map.has(key);
}
delete(key) {
return this.#map.delete(key);
}
clear() {
this.#map.clear();
this.#hits = 0; // Reset private counts on clear
this.#misses = 0; // Reset private counts on clear
}
get size() {
return this.#map.size;
}
// Public getters to expose the private counts
get hits() {
return this.#hits;
}
get misses() {
return this.#misses;
}
enable() {
this.#enabled = true;
}
disable() {
this.#enabled = false;
}
get enabled() {
return this.#enabled;
}
get disabled() {
return !this.#enabled;
}
// Iteration methods (delegate to the internal #map's iterators)
forEach(callbackFn, thisArg) {
this.#map.forEach(callbackFn, thisArg);
}
keys() {
return this.#map.keys();
}
values() {
return this.#map.values();
}
entries() {
return this.#map.entries();
}
// Make it iterable
[Symbol.iterator]() {
return this.#map[Symbol.iterator]();
}
}