UNPKG

low-level

Version:
199 lines (177 loc) 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UintMap = exports.BasicBinaryMap = void 0; const uint_js_1 = require("./uint.js"); const utils_js_1 = require("./utils.js"); class BMapEntriesIterator extends utils_js_1.AbstractIterator { CLS; constructor(CLS, mapEntries) { super(mapEntries); this.CLS = CLS; } _next(value) { return [this.CLS.from(value[0], "hex"), value[1]]; } } class BMapKeysIterator extends utils_js_1.AbstractIterator { CLS; constructor(CLS, mapEntries) { super(mapEntries); this.CLS = CLS; } _next(value) { return this.CLS.from(value, "hex"); } } class BMapValuesIterator extends utils_js_1.AbstractIterator { constructor(mapEntries) { super(mapEntries); } _next(value) { return value; } } /* export abstract class AbstractBinaryMap<K extends Uint, V> { protected readonly store: {[key: string]: V} = {}; protected constructor( protected readonly CLS: BasicUintConstructable<K>, entries?: readonly (readonly [K, V])[] ) { if (entries) { for (const [key, value] of entries) { this.set(key, value); } } } protected get size() { return Object.keys(this.store).length; } protected get(key: K): V | undefined { return this.store[key.toHex()]; } protected set(key: K, value: V) { return this.store[key.toHex()] = value; } protected delete(key: K) { return delete this.store[key.toHex()]; } protected has(key: K) { return key.toHex() in this.store; } protected entries() { return new BMapEntriesIterator(this.CLS, Object.entries(this.store).values()); } protected keys() { return new BMapKeysIterator(this.CLS, Object.keys(this.store).values()); } protected values() { return new BMapValuesIterator(Object.values(this.store).values()); } protected forEach(callbackfn: (value: V, key: K) => void, thisArg?: any) { for (const [key, value] of this.entries()) { callbackfn.call(thisArg, value, key); }; } protected clear() { for (const key of this.keys()) { this.delete(key); } } protected getStringTag() { return this.constructor.name; } } export class BasicBinaryMap<K extends Uint, V> extends AbstractBinaryMap<K, V> { constructor( CLS: BasicUintConstructable<K>, entries?: readonly (readonly [K, V])[] ) { super(CLS, entries); } public get size() { return super.size; } public get(key: K) { return super.get(key); } public set(key: K, value: V) { return super.set(key, value); } public delete(key: K) { return super.delete(key); } public has(key: K) { return super.has(key); } public [Symbol.iterator]() { return this.entries(); } public entries() { return super.entries(); } public keys() { return super.keys(); } public values() { return super.values(); } public forEach(callbackfn: (value: V, key: K) => void, thisArg?: any) { super.forEach(callbackfn, thisArg); } public clear() { super.clear(); } public get [Symbol.toStringTag]() { return this.getStringTag(); } }*/ class BasicBinaryMap { CLS; store = {}; constructor(CLS, entries) { this.CLS = CLS; if (entries) { for (const [key, value] of entries) { this.set(key, value); } } } get size() { return Object.keys(this.store).length; } get(key) { return this.store[key.toHex()]; } set(key, value) { return this.store[key.toHex()] = value; } /** * Deletes the key from the map. * @param key The key-value pair to delete * @returns true if the key was present and deleted, false otherwise */ delete(key) { const hasKey = this.has(key); delete this.store[key.toHex()]; return hasKey; } has(key) { return key.toHex() in this.store; } [Symbol.iterator]() { return this.entries(); } entries() { return new BMapEntriesIterator(this.CLS, Object.entries(this.store).values()); } keys() { return new BMapKeysIterator(this.CLS, Object.keys(this.store).values()); } values() { return new BMapValuesIterator(Object.values(this.store).values()); } forEach(callbackfn, thisArg) { for (const [key, value] of this.entries()) { callbackfn.call(thisArg, value, key); } ; } clear() { for (const key of this.keys()) { this.delete(key); } } getStringTag() { return this.constructor.name; } get [Symbol.toStringTag]() { return this.getStringTag(); } } exports.BasicBinaryMap = BasicBinaryMap; class UintMap extends BasicBinaryMap { constructor(entries) { super(uint_js_1.Uint, entries); } } exports.UintMap = UintMap; //# sourceMappingURL=map.js.map