@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
93 lines • 2.26 kB
JavaScript
export class FastMap {
_keys = [];
_values = {};
constructor(iterable) {
if (iterable instanceof FastMap) {
this.setAll(iterable);
}
else {
if (iterable) {
for (const [key, value] of iterable) {
this.set(key, value);
}
}
}
}
get size() {
return this._keys.length;
}
setAll(map) {
this._keys = [...map._keys];
this._values = { ...map._values };
}
addAll(map) {
for (const [key, value] of map.entries()) {
this.set(key, value);
}
}
*keys() {
yield* this._keys;
}
*values() {
for (const key of this._keys) {
yield this._values[key];
}
}
*entries() {
for (const key of this._keys) {
yield [key, this._values[key]];
}
}
set(key, value) {
if (!this.has(key)) {
this._keys.push(key);
}
this._values[key] = value;
return this;
}
indexOf(key) {
if (!this.has(key)) {
return -1;
}
for (let i = 0; i < this._keys.length; i++) {
if (this._keys[i] === key) {
return i;
}
}
throw new Error('Key not found, this should not happen.');
}
get(key) {
return this._values[key];
}
has(key) {
return Object.prototype.hasOwnProperty.call(this._values, key);
}
delete(key) {
if (!this.has(key)) {
return false;
}
const index = this.indexOf(key);
this._keys.splice(index, 1);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this._values[key];
return true;
}
clear() {
this._keys = [];
this._values = {};
}
[Symbol.dispose]() {
this.clear();
}
forEach(callback, thisArg) {
for (const key of this._keys) {
callback.call(thisArg, this._values[key], key, this);
}
}
*[Symbol.iterator]() {
for (const key of this._keys) {
yield [key, this._values[key]];
}
}
}
//# sourceMappingURL=FastMap.js.map