add equality collection element iterators
This commit is contained in:
@@ -8,6 +8,9 @@ export interface EqualityMap<K, V> extends Iterable<[K, V]> {
|
||||
contains(key: K): boolean;
|
||||
remove(key: K): V | undefined;
|
||||
clear(): void;
|
||||
keys(): IterableIterator<K>;
|
||||
values(): IterableIterator<V>;
|
||||
entries(): IterableIterator<[K, V]>;
|
||||
}
|
||||
|
||||
class NativeEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
@@ -41,6 +44,18 @@ class NativeEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
this.#map.clear();
|
||||
}
|
||||
|
||||
keys() {
|
||||
return this.#map.keys();
|
||||
}
|
||||
|
||||
values() {
|
||||
return this.#map.values();
|
||||
}
|
||||
|
||||
entries() {
|
||||
return this.#map.entries();
|
||||
}
|
||||
|
||||
[Symbol.iterator]() {
|
||||
return this.#map[Symbol.iterator]();
|
||||
}
|
||||
@@ -107,8 +122,26 @@ class CustomEqualityMap<K, V> implements EqualityMap<K, V> {
|
||||
this.#list.length = 0;
|
||||
}
|
||||
|
||||
[Symbol.iterator]() {
|
||||
return this.#list[Symbol.iterator]();
|
||||
*keys() {
|
||||
for (const entry of this.#list) {
|
||||
yield entry[0];
|
||||
}
|
||||
}
|
||||
|
||||
*values() {
|
||||
for (const entry of this.#list) {
|
||||
yield entry[1];
|
||||
}
|
||||
}
|
||||
|
||||
entries() {
|
||||
return this[Symbol.iterator]();
|
||||
}
|
||||
|
||||
*[Symbol.iterator]() {
|
||||
for (const entry of this.#list) {
|
||||
yield [entry[0], entry[1]] as [K, V]; // no entry mutation allowed!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user