@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
63 lines • 1.45 kB
JavaScript
import { OrderedSet } from "./set.js";
/**
* An implementation of Map that support getting first/last key and value.
*/
export class OrderedMap {
constructor() {
this._set = new OrderedSet();
this.map = new Map();
}
get(key) {
return this.map.get(key);
}
set(key, value) {
this._set.add(key);
this.map.set(key, value);
return this;
}
delete(key, searchFromHead) {
if (this.map.has(key)) {
this._set.delete(key, searchFromHead);
return this.map.delete(key);
}
return false;
}
keys() {
return this._set.values();
}
lastKey() {
return this._set.last();
}
firstKey() {
return this._set.first();
}
values() {
const _self = this;
return (function* generateValues() {
for (const key of _self.keys()) {
yield _self.get(key);
}
})();
}
lastValue() {
const lastKey = this._set.last();
if (lastKey === null) {
return null;
}
return this.get(lastKey);
}
firstValue() {
const firstKey = this._set.first();
if (firstKey === null) {
return null;
}
return this.get(firstKey);
}
size() {
return this._set.size;
}
has(key) {
return this.map.has(key);
}
}
//# sourceMappingURL=map.js.map