2021-08-31 22:59:32 +03:00
|
|
|
import produce, { immerable, castDraft, setAutoFreeze, enablePatches } from 'immer';
|
|
|
|
import bigInt, { BigInteger } from 'big-integer';
|
2021-04-20 06:31:57 +03:00
|
|
|
|
2021-04-26 09:59:38 +03:00
|
|
|
setAutoFreeze(false);
|
|
|
|
|
|
|
|
enablePatches();
|
|
|
|
|
2021-04-20 06:31:57 +03:00
|
|
|
function sortBigInt(a: BigInteger, b: BigInteger) {
|
|
|
|
if (a.lt(b)) {
|
|
|
|
return 1;
|
|
|
|
} else if (a.eq(b)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
2021-11-10 22:52:08 +03:00
|
|
|
export class BigIntOrderedMap<V> implements Iterable<[BigInteger, V]> {
|
2021-04-26 09:59:38 +03:00
|
|
|
root: Record<string, V> = {}
|
2021-08-31 22:59:32 +03:00
|
|
|
cachedIter: [BigInteger, V][] | null = null;
|
2021-04-20 11:39:56 +03:00
|
|
|
[immerable] = true;
|
2021-01-27 08:45:18 +03:00
|
|
|
|
2021-04-20 06:31:57 +03:00
|
|
|
constructor(items: [BigInteger, V][] = []) {
|
2021-04-21 09:39:35 +03:00
|
|
|
items.forEach(([key, val]) => {
|
2021-01-27 08:45:18 +03:00
|
|
|
this.set(key, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-20 06:31:57 +03:00
|
|
|
get size() {
|
2021-06-15 03:48:42 +03:00
|
|
|
if(this.cachedIter) {
|
|
|
|
return this.cachedIter.length;
|
|
|
|
}
|
|
|
|
return this.generateCachedIter().length;
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 06:31:57 +03:00
|
|
|
get(key: BigInteger) {
|
|
|
|
return this.root[key.toString()] ?? null;
|
|
|
|
}
|
2021-01-27 08:45:18 +03:00
|
|
|
|
2021-04-26 09:59:38 +03:00
|
|
|
gas(items: [BigInteger, V][]) {
|
2021-08-31 22:59:32 +03:00
|
|
|
return produce(this, (draft) => {
|
2021-04-26 09:59:38 +03:00
|
|
|
items.forEach(([key, value]) => {
|
|
|
|
draft.root[key.toString()] = castDraft(value);
|
|
|
|
});
|
2021-05-05 06:47:27 +03:00
|
|
|
draft.cachedIter = null;
|
2021-08-31 22:59:32 +03:00
|
|
|
},
|
2021-04-26 09:59:38 +03:00
|
|
|
(patches) => {
|
2021-08-31 22:59:32 +03:00
|
|
|
// console.log(`gassed with ${JSON.stringify(patches, null, 2)}`);
|
2021-04-26 09:59:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-20 06:31:57 +03:00
|
|
|
set(key: BigInteger, value: V) {
|
2021-08-31 22:59:32 +03:00
|
|
|
return produce(this, (draft) => {
|
2021-04-26 09:59:38 +03:00
|
|
|
draft.root[key.toString()] = castDraft(value);
|
2021-05-05 06:47:27 +03:00
|
|
|
draft.cachedIter = null;
|
2021-04-26 09:59:38 +03:00
|
|
|
});
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
2021-08-31 22:59:32 +03:00
|
|
|
return produce(this, (draft) => {
|
2021-04-26 09:59:38 +03:00
|
|
|
draft.cachedIter = [];
|
2021-08-31 22:59:32 +03:00
|
|
|
draft.root = {};
|
2021-04-26 09:59:38 +03:00
|
|
|
});
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 06:31:57 +03:00
|
|
|
has(key: BigInteger) {
|
|
|
|
return key.toString() in this.root;
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(key: BigInteger) {
|
2021-08-31 22:59:32 +03:00
|
|
|
const result = produce(this, (draft) => {
|
2021-04-26 09:59:38 +03:00
|
|
|
delete draft.root[key.toString()];
|
2021-05-05 06:47:27 +03:00
|
|
|
draft.cachedIter = null;
|
2021-04-26 09:59:38 +03:00
|
|
|
});
|
2021-05-06 04:09:01 +03:00
|
|
|
return result;
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.iterator](): IterableIterator<[BigInteger, V]> {
|
|
|
|
let idx = 0;
|
2021-08-31 22:59:32 +03:00
|
|
|
const result = this.generateCachedIter();
|
2021-01-27 08:45:18 +03:00
|
|
|
return {
|
|
|
|
[Symbol.iterator]: this[Symbol.iterator],
|
|
|
|
next: (): IteratorResult<[BigInteger, V]> => {
|
|
|
|
if (idx < result.length) {
|
|
|
|
return { value: result[idx++], done: false };
|
|
|
|
}
|
|
|
|
return { done: true, value: null };
|
2021-08-31 22:59:32 +03:00
|
|
|
}
|
2021-01-27 08:45:18 +03:00
|
|
|
};
|
|
|
|
}
|
2021-04-20 06:31:57 +03:00
|
|
|
|
|
|
|
peekLargest() {
|
|
|
|
const sorted = Array.from(this);
|
|
|
|
return sorted[0] as [BigInteger, V] | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
peekSmallest() {
|
|
|
|
const sorted = Array.from(this);
|
|
|
|
return sorted[sorted.length - 1] as [BigInteger, V] | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys() {
|
2021-04-26 09:59:38 +03:00
|
|
|
return Array.from(this).map(([k,v]) => k);
|
2021-04-20 06:31:57 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 09:59:38 +03:00
|
|
|
generateCachedIter() {
|
2021-05-05 06:47:27 +03:00
|
|
|
if(this.cachedIter) {
|
|
|
|
return [...this.cachedIter];
|
|
|
|
}
|
2021-08-31 22:59:32 +03:00
|
|
|
const result = Object.keys(this.root).map((key) => {
|
2021-04-20 06:31:57 +03:00
|
|
|
const num = bigInt(key);
|
|
|
|
return [num, this.root[key]] as [BigInteger, V];
|
|
|
|
}).sort(([a], [b]) => sortBigInt(a,b));
|
|
|
|
this.cachedIter = result;
|
2021-05-06 04:09:01 +03:00
|
|
|
return [...result];
|
2021-04-20 06:31:57 +03:00
|
|
|
}
|
2021-01-27 08:45:18 +03:00
|
|
|
}
|
2021-04-20 06:31:57 +03:00
|
|
|
|