UNPKG

@thi.ng/sorted-map

Version:

Skiplist-based sorted map & set implementation

130 lines (129 loc) 3.32 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; 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; }; import { dissoc } from "@thi.ng/associative/dissoc"; import { __equivSet } from "@thi.ng/associative/internal/equiv"; import { __inspectable } from "@thi.ng/associative/internal/inspect"; 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"; const __private = /* @__PURE__ */ new WeakMap(); 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(); __private.set( this, new SortedMap( values ? map((x) => [x, x], values) : null, opts ) ); } [Symbol.iterator]() { return this.keys(); } get [Symbol.species]() { return SortedSet; } get [Symbol.toStringTag]() { return "SortedSet"; } get size() { return __private.get(this).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 __private.get(this).$reduce((_acc, x) => rfn(_acc, x[0]), acc); } entries(key, max = false) { return __private.get(this).entries(key, max); } keys(key, max = false) { return __private.get(this).keys(key, max); } values(key, max = false) { return __private.get(this).values(key, max); } add(key) { __private.get(this).set(key, key); return this; } into(keys) { return into(this, keys); } clear() { __private.get(this).clear(); } first() { const first = __private.get(this).first(); return first ? first[0] : void 0; } delete(key) { return __private.get(this).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 __private.get(this).has(key); } get(key, notFound) { return __private.get(this).get(key, notFound); } opts() { return __private.get(this).opts(); } }; SortedSet = __decorateClass([ __inspectable ], SortedSet); const defSortedSet = (vals, opts) => new SortedSet(vals, opts); export { SortedSet, defSortedSet };