UNPKG

@thi.ng/sorted-map

Version:

Skiplist-based sorted map & set implementation

141 lines (140 loc) 4.17 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __typeError = (msg) => { throw TypeError(msg); }; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); var _map; import { dissoc } from "@thi.ng/associative/dissoc"; import { __disposableValues } from "@thi.ng/associative/internal/dispose"; import { __equivSet } from "@thi.ng/associative/internal/equiv"; import { __tostringMixin } from "@thi.ng/associative/internal/tostring"; import { into } from "@thi.ng/associative/into"; import { compare } from "@thi.ng/compare/compare"; import { map } from "@thi.ng/transducers/map"; import { SortedMap } from "./sorted-map.js"; let SortedSet = class extends Set { /** * Creates new instance with optional given values and/or * implementation options. The options are the same as used by * {@link SortedMap}. * * @param values - input values * @param opts - config options */ constructor(values, opts) { super(); __privateAdd(this, _map); __privateSet(this, _map, new SortedMap( values ? map((x) => [x, x], values) : null, opts )); } [Symbol.iterator]() { return this.keys(); } // mixin [Symbol.dispose]() { } get [Symbol.species]() { return SortedSet; } get [Symbol.toStringTag]() { return "SortedSet"; } get size() { return __privateGet(this, _map).size; } copy() { return new SortedSet(this.keys(), this.opts()); } empty() { return new SortedSet(null, this.opts()); } compare(o) { const n = this.size; const m = o.size; if (n < m) return -1; if (n > m) return 1; const i = this.entries(); const j = o.entries(); let x, y; let c; while (x = i.next(), y = j.next(), !x.done && !y.done) { if ((c = compare(x.value[0], y.value[0])) !== 0) { return c; } } return 0; } equiv(o) { return __equivSet(this, o); } $reduce(rfn, acc) { return __privateGet(this, _map).$reduce((_acc, x) => rfn(_acc, x[0]), acc); } entries(key, max = false) { return __privateGet(this, _map).entries(key, max); } keys(key, max = false) { return __privateGet(this, _map).keys(key, max); } values(key, max = false) { return __privateGet(this, _map).values(key, max); } add(key) { __privateGet(this, _map).set(key, key); return this; } into(keys) { return into(this, keys); } clear() { __privateGet(this, _map).clear(); } first() { const first = __privateGet(this, _map).first(); return first ? first[0] : void 0; } delete(key) { return __privateGet(this, _map).delete(key); } disj(keys) { return dissoc(this, keys); } forEach(fn, thisArg) { for (let p of this) { fn.call(thisArg, p, p, this); } } has(key) { return __privateGet(this, _map).has(key); } get(key, notFound) { return __privateGet(this, _map).get(key, notFound); } opts() { return __privateGet(this, _map).opts(); } }; _map = new WeakMap(); SortedSet = __decorateClass([ __disposableValues, __tostringMixin ], SortedSet); const defSortedSet = (vals, opts) => new SortedSet(vals, opts); export { SortedSet, defSortedSet };