jsketcher/modules/gems/linkedMap.js
2022-06-25 15:19:47 -07:00

44 lines
658 B
JavaScript

export class OrderedMap {
constructor() {
this.order = [];
this.map = new Map();
}
forEach(cb) {
this.order.forEach(key => cb(this.map.get(key), key));
}
set(key, value) {
if (!this.map.has(key)) {
this.map.set(key, value);
this.order.push(key)
}
}
get(key) {
return this.map(key);
}
has(key) {
return this.map.has(key);
}
delete(key) {
this.map.delete(key);
let index = this.order.indexOf(key);
if (index !== -1) {
this.order.splice(index, 1);
}
}
clear() {
this.map.clear();
this.order.length = 0;
}
get size() {
return this.map.size;
}
}