@paosder/vector-map
Version:
vector-like map class using es6 map with array.
233 lines (230 loc) • 6.56 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
VectorMap: () => VectorMap
});
module.exports = __toCommonJS(src_exports);
// src/map.ts
var VectorMap = class {
constructor(arr) {
var _a;
this.source = (_a = arr == null ? void 0 : arr.map((el) => {
if (typeof el === "object" && el[0] && el[1]) {
return {
key: el[0],
value: el[1]
};
}
throw Error("wrong type of object");
})) != null ? _a : [];
this.pointer = new Map(arr == null ? void 0 : arr.map((el, i) => [el[0], i]));
this.forEach = this.forEach.bind(this);
this.map = this.map.bind(this);
this.reduce = this.reduce.bind(this);
this.delete = this.delete.bind(this);
this.clear = this.clear.bind(this);
this.swap = this.swap.bind(this);
this.swapIndex = this.swapIndex.bind(this);
this.swapPointer = this.swapPointer.bind(this);
this.get = this.get.bind(this);
this.has = this.has.bind(this);
this.set = this.set.bind(this);
this.getIndex = this.getIndex.bind(this);
this.pop = this.pop.bind(this);
this.clone = this.clone.bind(this);
this.insertInto = this.insertInto.bind(this);
this.from = this.from.bind(this);
}
forEach(callback) {
this.source.forEach(({ key, value }, index, arr) => callback(value, key, index, arr));
}
map(mapFunc) {
return this.source.map(({ key, value }, index, arr) => mapFunc(value, key, index, arr));
}
some(someFunc) {
return this.source.some(({ key, value }, index, arr) => someFunc(value, key, index, arr));
}
get head() {
if (this.source.length > 0) {
return this.source[0];
}
return void 0;
}
get tail() {
if (this.source.length > 0) {
return this.source[this.source.length - 1];
}
return void 0;
}
reduce(reducer, accumulator) {
return this.source.reduce((acc, { key, value }, index, arr) => reducer(acc, value, key, index, arr), accumulator);
}
filterMap(filterFunc) {
return this.source.reduce((acc, { key, value }, index, arr) => {
const filterResult = filterFunc(value, key, index, arr);
if (filterResult !== void 0) {
acc.push(filterResult);
}
return acc;
}, []);
}
*[Symbol.iterator]() {
for (let i = 0; i < this.source.length; i += 1) {
yield this.source[i].value;
}
}
delete(key, callback) {
const targetIndex = this.pointer.get(key);
let swapped = false;
if (targetIndex === void 0) {
return false;
}
if (targetIndex < this.source.length - 1) {
this.swapIndex(key);
swapped = true;
}
const removed = this.source.pop();
if (callback && swapped) {
callback(this.source[targetIndex], removed);
}
removed.key = void 0;
this.pointer.delete(key);
return true;
}
clear() {
this.pointer.clear();
this.source = [];
}
swap(key1, key2) {
const i1 = this.pointer.get(key1);
if (i1 === void 0) {
throw new Error(`Key does not exists: '${key1}'`);
}
const i2 = this.pointer.get(key2);
if (i2 === void 0) {
throw new Error(`Key does not exists: '${key2}'`);
}
const temp = this.source[i1];
this.source[i1] = this.source[i2];
this.source[i1].key = key1;
this.source[i2] = temp;
this.source[i2].key = key2;
}
swapPointer(key1, key2) {
const i1 = this.pointer.get(key1);
if (i1 === void 0) {
throw new Error(`Key does not exists: '${key1}'`);
}
const i2 = this.pointer.get(key2);
if (i2 === void 0) {
throw new Error(`Key does not exists: '${key2}'`);
}
this.source[i1].key = key2;
this.source[i2].key = key1;
this.pointer.set(key1, i2);
this.pointer.set(key2, i1);
}
swapIndex(key1, key2) {
const i1 = this.pointer.get(key1);
if (i1 === void 0) {
throw new Error(`Key does not exists: '${key1}'`);
}
let i2;
if (key2 !== void 0) {
i2 = this.pointer.get(key2);
if (i2 === void 0) {
throw new Error(`Key does not exists: '${key2}'`);
}
} else {
i2 = this.source.length - 1;
}
this.pointer.set(this.source[i2].key, i1);
this.pointer.set(this.source[i1].key, i2);
const temp = this.source[i1];
this.source[i1] = this.source[i2];
this.source[i2] = temp;
}
has(key) {
return this.pointer.has(key);
}
get(key) {
const targetIndex = this.pointer.get(key);
if (targetIndex === void 0) {
return void 0;
}
if (!this.source[targetIndex]) {
throw new Error(`mangling pointer detected.
key: '${key}', pointer: '${targetIndex}'.`);
}
return this.source[targetIndex].value;
}
getIndex(key) {
return this.pointer.get(key);
}
set(key, value) {
let index = this.pointer.get(key);
if (index !== void 0) {
this.source[index].value = value;
return index;
}
index = this.source.length;
this.pointer.set(key, index);
this.source.push({
key,
value
});
return index;
}
pop() {
if (this.source.length > 0) {
const removed = this.source.pop();
this.pointer.delete(removed.key);
return removed;
}
return void 0;
}
reverse() {
this.source.reverse();
this.source.forEach(({ key }, i) => {
this.pointer.set(key, i);
});
}
clone() {
const newMap = new VectorMap();
newMap.pointer = this.pointer;
newMap.source = this.source;
return newMap;
}
get size() {
return this.source.length;
}
insertInto(target) {
this.forEach((value, key) => {
target.set(key, value);
});
}
from(target) {
target.forEach((value, key) => {
this.set(key, value);
});
}
};
//# sourceMappingURL=index.js.map