@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
135 lines (117 loc) • 3.47 kB
text/typescript
export class FastBigIntMap {
private items: Record<string, bigint>;
private keyOrder: bigint[];
constructor(iterable?: ReadonlyArray<readonly [bigint, bigint]> | null | FastBigIntMap) {
this.items = {};
this.keyOrder = [];
if (iterable instanceof FastBigIntMap) {
this.setAll(iterable);
} else {
if (iterable) {
for (const [key, value] of iterable) {
this.set(key, value);
}
}
}
}
/**
* Number of entries in the map.
*/
get size(): number {
return this.keyOrder.length;
}
public setAll(map: FastBigIntMap): void {
this.items = { ...map.items };
this.keyOrder = [...map.keyOrder];
}
public addAll(map: FastBigIntMap): void {
for (const [key, value] of map.entries()) {
this.set(key, value);
}
}
/**
* Inserts or updates the key/value. Returns `this` to allow chaining.
*/
set(key: bigint, value: bigint): this {
const keyStr = key.toString();
// If key is new, push to keyOrder
if (!this.has(key)) {
this.keyOrder.push(key);
}
// Store value in the record
this.items[keyStr] = value;
return this;
}
/**
* Retrieves the value for the given key. Returns undefined if key not found.
*/
get(key: bigint): bigint | undefined {
return this.items[key.toString()];
}
/**
* Checks if a key exists in the map.
*/
has(key: bigint): boolean {
return Object.prototype.hasOwnProperty.call(this.items, key.toString());
}
/**
* Deletes a key if it exists. Returns boolean indicating success.
*/
delete(key: bigint): boolean {
const keyStr = key.toString();
if (this.has(key)) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.items[keyStr];
// Remove from keyOrder
this.keyOrder = this.keyOrder.filter((k) => k !== key);
return true;
}
return false;
}
/**
* Removes all keys and values.
*/
clear(): void {
this.items = {};
this.keyOrder = [];
}
/**
* Iterates over [key, value] pairs in insertion order.
*/
*entries(): IterableIterator<[bigint, bigint]> {
for (const key of this.keyOrder) {
yield [key, this.items[key.toString()] as bigint];
}
}
/**
* Iterates over keys in insertion order.
*/
*keys(): IterableIterator<bigint> {
yield* this.keyOrder;
}
/**
* Iterates over values in insertion order.
*/
*values(): IterableIterator<bigint> {
for (const key of this.keyOrder) {
yield this.items[key.toString()] as bigint;
}
}
/**
* forEach callback in insertion order, similar to JS Map.
*/
forEach(
callback: (value: bigint, key: bigint, map: FastBigIntMap) => void,
thisArg?: unknown,
): void {
for (const key of this.keyOrder) {
callback.call(thisArg, this.items[key.toString()] as bigint, key, this);
}
}
/**
* Makes the map iterable with `for...of`, yielding [key, value] pairs.
*/
[Symbol.iterator](): IterableIterator<[bigint, bigint]> {
return this.entries();
}
}