UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

108 lines (107 loc) 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../linq/index"); class SortedMap { constructor(comparator, enumerable) { this.comparator = comparator; this.arr = []; if (enumerable && enumerable[Symbol.iterator]) { for (const x of enumerable) { this.set(x[0], x[1]); } } } [Symbol.iterator]() { return this.arr[Symbol.iterator](); } entries() { return this.arr[Symbol.iterator](); } keys() { const that = this; const itr = { [Symbol.iterator]: () => { const enb = that[Symbol.iterator](); return { next: () => { const item = enb.next(); return { value: item.value[0], done: item.done }; }, }; }, }; return itr; } values() { const that = this; const itr = { [Symbol.iterator]: () => { const enb = that[Symbol.iterator](); return { next: () => { const item = enb.next(); return { value: item.value[1], done: item.done }; }, }; }, }; return itr; } isEmpty() { return this.size === 0; } clear() { this.arr.splice(0, this.arr.length); } delete(key) { const foundedIndex = this.arr.findIndex(item => key === item[0]); if (foundedIndex > -1) { this.arr.splice(foundedIndex, 1); this.arr = this.arr.sort(this.comparator); return true; } return false; } forEach(callbackfn, thisArg) { this.arr.forEach((item, index, col) => { callbackfn(item[1], item[0], this); }); } get(key) { const founded = this.arr.find(item => key === item[0]); if (founded) return founded[1]; return null; } has(key) { return this.arr.findIndex(item => key === item[0]) > -1; } set(key, value) { if (this.has(key)) { const founded = this.arr.find(item => key === item[0]); if (founded) { founded[0] = key; founded[1] = value; this.arr = this.arr.sort(this.comparator); } } else { this.arr.push([key, value]); this.arr = this.arr.sort(this.comparator); } return this; } get size() { return this.arr.length; } linq() { return new index_1.Enumerable(this); } plinq() { return new index_1.ParallelEnumerable(this); } clone() { return new SortedMap(this.comparator, this); } } exports.SortedMap = SortedMap;