Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 10x 10x 21x 2x 2x 4x 4x 4x 15x 9x 12x 15x 15x 15x 1x 1x 1x 2x 10x 8x 8x 14x 8x 8x 2x 5x 7x 3x 2x 4x 2x 2x | /**
* A custom map implementation that extends the native `Map` interface with
* additional utility methods. Internally it stores its data in a standard
* `Map` instance and keeps its `size` property in sync with that store.
*
* @template K The type of keys maintained by this map.
* @template V The type of values stored in this map.
*/
export default class CMap<K, V> implements Map<K, V> {
private readonly store;
constructor(arr?: [K, V][]) {
this.store = new Map<K, V>(arr);
this.size = this.store.size;
}
private updateSize() {
this.size = this.store.size;
}
clear(): void {
this.store.clear();
this.updateSize();
}
delete(key: K): boolean {
const x = this.store.delete(key);
this.updateSize();
return x;
}
forEach(
callbackfn: (value: V, key: K, map: Map<K, V>) => void,
thisArg?: any,
): void {
this.store.forEach(callbackfn, thisArg);
}
get(key: K): V | undefined {
return this.store.get(key);
}
has(key: K): boolean {
return this.store.has(key);
}
set(key: K, value: V): this {
this.store.set(key, value);
this.updateSize();
return this;
}
size: number;
entries(): MapIterator<[K, V]> {
return this.store.entries();
}
keys(): MapIterator<K> {
return this.store.keys();
}
values(): MapIterator<V> {
return this.store.values();
}
[Symbol.iterator](): MapIterator<[K, V]> {
return this.store[Symbol.iterator]();
}
[Symbol.toStringTag] = 'CoreMap';
/**
* Return a array based on each element in storage
* @param mapper mapping function
*/
map<T>(mapper: (value: V, key: K) => T): T[] {
const out: T[] = [];
this.forEach((v, k) => {
out.push(mapper(v, k));
});
return out;
}
/**
* Return an array with the values only
*/
toValueArray(): V[] {
return this.map<V>((v) => v);
}
/**
* Return an array with the keys only
*/
toKeyArray(): K[] {
return this.map<K>((v, k) => k);
}
/**
* Delete selected elements from the storage
*/
deleteSelected(selector: (value: V, key: K) => boolean) {
this.forEach((v, k) => {
if (selector(v, k)) {
this.delete(k);
}
});
}
/**
* Merge another core-map in the existing, duplicated keys in the executing map will be overwritten
*/
merge(map: CMap<K, V>, skipOverwrite = false) {
map.forEach((value, key) => {
if (!(skipOverwrite && this.has(key))) {
this.set(key, value);
}
});
return this;
}
}
|