"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7;// src/utils/graph.ts function dfs(graph, node, cb, visited = /* @__PURE__ */ new Set()) { if (!graph.has(node) || visited.has(node)) return; cb(node, graph); visited.add(node); graph.getAdjacentNodes(node).forEach((neighbor) => { if (!visited.has(neighbor)) { dfs(graph, neighbor, cb, visited); } }); } function dfsbt(graph, node, cb, visited = /* @__PURE__ */ new Set()) { if (!graph.has(node) || visited.has(node)) return; visited.add(node); graph.getAdjacentNodes(node).forEach((neighbor) => { if (!visited.has(neighbor)) { dfsbt(graph, neighbor, cb, visited); } }); cb(node, graph); } function isCyclic(graph, source, target, visited = /* @__PURE__ */ new Set()) { if (source === target) return true; if (!graph.has(source) || !graph.has(target)) return false; if (graph.isAdjacent(target, source)) return true; visited.add(target); const neighbors = graph.getAdjacentNodes(target); if (!_optionalChain([neighbors, 'optionalAccess', _ => _.length])) return false; for (const neighbor of neighbors) { if (!visited.has(neighbor) && isCyclic(graph, source, neighbor, visited)) { return true; } } return false; } var CycleError = class extends Error { constructor(message) { super(message); this.name = "CycleError"; } }; // src/utils/linkedList.ts var LinkedListIterator = class { constructor(head) { this.current = head; } [Symbol.iterator]() { return this; } next() { if (this.current) { const result = { value: this.current.value, done: false }; this.current = this.current.next; return result; } return { value: void 0, done: true }; } }; // src/LinkedNode.ts var LinkedNode = (_class = class { __init() {this.next = null} constructor(value, next) {;_class.prototype.__init.call(this); this.value = value; this.next = _nullishCoalesce(next, () => ( null)); } setNext(node) { this.next = node; } }, _class); // src/LinkedList.ts var LinkedList = (_class2 = class _LinkedList { __init2() {this.head = null} __init3() {this.tail = null} constructor(value) {;_class2.prototype.__init2.call(this);_class2.prototype.__init3.call(this); if (arguments.length) { this.head = new LinkedNode(value); this.tail = this.head; } } static from(iterable) { const list = new _LinkedList(); for (const value of iterable) { list.addTail(value); } return list; } [Symbol.iterator]() { return new LinkedListIterator(this.head); } addHead(value) { const next = this.head; this.head = new LinkedNode(value, next); if (!this.tail) { this.tail = this.head; } return this; } addTail(value) { const tail = new LinkedNode(value); if (this.isEmpty()) { this.head = tail; this.tail = tail; } else { this.tail.next = tail; this.tail = tail; } return this; } clear() { this.head = null; this.tail = null; return true; } contains(value) { let node = this.head; if (!node) return false; if (!node.next || node.next === this.head) { return Object.is(node.value, value); } while (node.next) { if (Object.is(node.next.value, value)) { return true; } if (node.next === this.tail) { return false; } node = node.next; } return false; } getHead() { return _optionalChain([this, 'access', _2 => _2.head, 'optionalAccess', _3 => _3.value]); } getTail() { return _optionalChain([this, 'access', _4 => _4.tail, 'optionalAccess', _5 => _5.value]); } isCircular() { if (this.isEmpty()) return false; let slow = this.head; let fast = this.head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) return true; } return false; } isEmpty() { return this.head === null; } remove(value) { let node = this.head; if (!node) return false; if (!node.next || node.next === this.head) { if (Object.is(node.value, value)) { this.clear(); return true; } return false; } while (node.next) { if (Object.is(node.next.value, value)) { const removed = node.next; node.setNext(removed.next); if (this.tail === removed) { this.tail = node; } return true; } if (node.next === this.tail) { return false; } node = node.next; } return false; } removeHead() { const removed = this.head; if (!_optionalChain([removed, 'optionalAccess', _6 => _6.next])) { this.clear(); } else { this.head = removed.next; } return _optionalChain([removed, 'optionalAccess', _7 => _7.value]); } removeTail() { let node = this.head; if (!node) return void 0; if (!node.next || node.next === this.head) { this.clear(); return node.value; } while (node.next) { if (node.next === this.tail) { const removed = node.next; node.setNext(removed.next); this.tail = node; return removed.value; } node = node.next; } return void 0; } size() { let count = 0; let node = this.head; while (node) { count++; if (node === this.tail) break; node = node.next; } return count; } }, _class2); // src/Queue.ts var Queue = class _Queue extends LinkedList { constructor(iterable) { super(); if (iterable) { for (const value of iterable) { this.enqueue(value); } } } static from(iterable) { return new _Queue(iterable); } enqueue(value) { return this.addTail(value); } dequeue() { return this.removeHead(); } peek() { return this.getHead(); } }; // src/Graph.ts var Graph = (_class3 = class {constructor() { _class3.prototype.__init4.call(this); } __init4() {this.nodes = /* @__PURE__ */ new Map()} addNode(node) { if (!this.has(node)) { this.nodes.set(node, /* @__PURE__ */ new Set()); } return this; } removeNode(node) { if (!this.has(node)) return false; for (const [, adjacentNodes] of this.nodes.entries()) { adjacentNodes.delete(node); } return this.nodes.delete(node); } addEdge(sourceNode, targerNode) { if (!this.has(sourceNode)) { this.addNode(sourceNode); } if (!this.has(targerNode)) { this.addNode(targerNode); } const adjacentNodes = this.nodes.get(sourceNode); if (adjacentNodes.has(targerNode)) return false; adjacentNodes.add(targerNode); return true; } removeEdge(sourceNode, targerNode) { const adjacentNodes = this.nodes.get(sourceNode); if (!adjacentNodes) return false; return adjacentNodes.delete(targerNode); } has(node) { return this.nodes.has(node); } getAdjacentNodes(node) { const adjacentNodes = this.nodes.get(node); if (!adjacentNodes) return void 0; return Array.from(adjacentNodes); } isAdjacent(sourceNode, targerNode) { const adjacentNodes = this.nodes.get(sourceNode); if (!adjacentNodes) return false; return adjacentNodes.has(targerNode); } getInDegree(node) { let inDegree = 0; for (const [, adjacentNodes] of this.nodes.entries()) { if (adjacentNodes.has(node)) { inDegree++; } } return inDegree; } getOutDegree(node) { return _nullishCoalesce(_optionalChain([this, 'access', _8 => _8.nodes, 'access', _9 => _9.get, 'call', _10 => _10(node), 'optionalAccess', _11 => _11.size]), () => ( 0)); } forEachDFS(node, cb) { dfs(this, node, cb); } forEachBFS(node, cb) { const queue = new Queue([node]); const visited = /* @__PURE__ */ new Set([node]); while (!queue.isEmpty()) { const currentNode = queue.dequeue(); cb(currentNode, this); _optionalChain([this, 'access', _12 => _12.getAdjacentNodes, 'call', _13 => _13(currentNode), 'optionalAccess', _14 => _14.forEach, 'call', _15 => _15((neighbor) => { if (!visited.has(neighbor)) { visited.add(neighbor); queue.enqueue(neighbor); } })]); } } entries() { return this.nodes.entries(); } }, _class3); // src/Stack.ts var Stack = class _Stack extends Array { constructor(iterable) { super(); if (iterable) { for (const value of iterable) { this.push(value); } } } static from(arrayLike) { return new _Stack(Array.from(arrayLike)); } clear() { this.length = 0; } isEmpty() { return !this.length; } peek() { return this[this.length - 1]; } size() { return this.length; } }; // src/AcyclicGraph.ts var AcyclicGraph = class extends Graph { addEdge(sourceNode, targerNode) { if (isCyclic(this, sourceNode, targerNode)) { throw new CycleError("Adding edge creates a cycle"); } return super.addEdge(sourceNode, targerNode); } toSorted(sourceNode) { const stack = new Stack(); const visited = /* @__PURE__ */ new Set(); const cb = (node) => stack.push(node); if (arguments.length) { dfsbt(this, sourceNode, cb, visited); } else { Array.from(this.entries()).forEach(([node]) => { dfsbt(this, node, cb, visited); }); } return Array.from(stack).reverse(); } }; // src/utils/stringify.ts var jsonPrimitiveTypes = ["string", "number", "boolean"]; function stringify(value) { if (!arguments.length) return ""; if (value === null || jsonPrimitiveTypes.includes(typeof value)) { return JSON.stringify(value); } if (value === void 0) return "undefined"; if (typeof value === "bigint") { return `BigInt(${value.toString()})`; } if (typeof value === "symbol") { return value.toString(); } if (typeof value === "function") { return `Function(${value.toString().replace(/\s+/g, "")})`; } if (Array.isArray(value)) { return `[${value.map((item) => stringify(item)).join(",")}]`; } if (value instanceof Date) { return `Date(${value.toISOString()})`; } if (value instanceof Set) { return `Set(${stringify(Array.from(value))})`; } if (value instanceof Map) { return `Map(${stringify(Array.from(value.entries()))})`; } const sortedKeys = Object.keys(value).sort(); const string = value.toString(); if (!sortedKeys.length && !string.startsWith("[object")) { return `${value.constructor.name}(${string})`; } return `{${sortedKeys.map( (key) => `${stringify(key)}:${stringify(value[key])}` ).join(",")}}`; } // src/BinaryNode.ts var BinaryNode = (_class4 = class { __init5() {this.left = null} __init6() {this.right = null} constructor(value) {;_class4.prototype.__init5.call(this);_class4.prototype.__init6.call(this); this.key = stringify(value); this.value = value; } isNumeric() { return (typeof this.value === "number" || typeof this.value === "bigint") && !Number.isNaN(this.value); } compare(node) { if (this.isNumeric() && node.isNumeric()) { if (this.value > node.value) { return 1; } if (this.value < node.value) { return -1; } return 0; } return this.key.localeCompare(node.key); } equals(node) { return this.key === node.key; } strictEquals(node) { return Object.is(this.value, node.value); } getValue() { return this.value; } getKey() { return this.key; } setValue(value) { this.key = stringify(value); this.value = value; } }, _class4); // src/utils/binaryTree.ts function insertNode(root, node) { if (node.compare(root) < 0) { if (!root.left) { root.left = node; return true; } return insertNode(root.left, node); } if (!root.right) { root.right = node; return true; } return insertNode(root.right, node); } function searchNode(root, node) { if (!root) return false; if (node.compare(root) < 0) { return searchNode(root.left, node); } if (node.compare(root) > 0 || !node.strictEquals(root)) { return searchNode(root.right, node); } return true; } function findMax(root) { let node = root; while (node.right) { node = node.right; } return node; } function deleteNode(root, node) { if (!root) return null; if (node.compare(root) < 0) { root.left = deleteNode(root.left, node); return root; } if (node.compare(root) > 0 || !node.compare(root) && !node.strictEquals(root)) { root.right = deleteNode(root.right, node); return root; } if (!root.left) return root.right; if (!root.right) return root.left; const deepestRight = findMax(root.left); root.setValue(deepestRight.value); root.left = deleteNode(root.left, deepestRight); return root; } function traverseInOrder(node, tree, cb) { if (node) { traverseInOrder(node.left, tree, cb); cb(node.value, node, tree); traverseInOrder(node.right, tree, cb); } } function traversePreOrder(node, tree, cb) { if (node) { cb(node.value, node, tree); traverseInOrder(node.left, tree, cb); traverseInOrder(node.right, tree, cb); } } function traversePostOrder(node, tree, cb) { if (node) { traverseInOrder(node.left, tree, cb); traverseInOrder(node.right, tree, cb); cb(node.value, node, tree); } } // src/BinaryTree.ts var BinaryTree = (_class5 = class _BinaryTree { __init7() {this.root = null} constructor(rootValue) {;_class5.prototype.__init7.call(this); if (arguments.length) { this.root = new BinaryNode(rootValue); } } static from(values) { const tree = new _BinaryTree(); for (const value of values) { tree.add(value); } return tree; } add(value) { const node = new BinaryNode(value); if (this.isEmpty()) { this.root = node; return this; } insertNode(this.root, node); return this; } isEmpty() { return !this.root; } contains(value) { const node = new BinaryNode(value); return searchNode(this.root, node); } delete(value) { const node = new BinaryNode(value); this.root = deleteNode(this.root, node); } height(node = this.root) { if (!node) return -1; const leftHeight = this.height(node.left); const rightHeight = this.height(node.right); return Math.max(leftHeight, rightHeight) + 1; } size(node = this.root) { if (!node) return 0; return this.size(node.left) + this.size(node.right) + 1; } forEachInOrder(cb) { traverseInOrder(this.root, this, cb); } forEachPreOrder(cb) { traversePreOrder(this.root, this, cb); } forEachPostOrder(cb) { traversePostOrder(this.root, this, cb); } toSortedArray() { const result = []; this.forEachInOrder((value) => { result.push(value); }); return result; } }, _class5); // src/BloomFilter.ts var _fnvplus = require('fnv-plus'); var _fnvplus2 = _interopRequireDefault(_fnvplus); var _imurmurhash = require('imurmurhash'); var _imurmurhash2 = _interopRequireDefault(_imurmurhash); // src/utils/bloomFilter.ts function clamp(value, min, max) { if (value < min) return min; if (value > max) return max; return value; } // src/BloomFilter.ts var MIN_SIZE = 64; var DEFAULT_SIZE = 256; var MAX_SIZE = 4294967295; var BITS = 32; var K = 2; var BloomFilter = class _BloomFilter { constructor(size) { const M = size && Number.isFinite(size) ? clamp(size, MIN_SIZE, MAX_SIZE) : DEFAULT_SIZE; const bitSize = Math.trunc(K * M / Math.LN2); this.bitArray = new Uint32Array(Math.ceil(bitSize / BITS)); this.bitSize = bitSize; } static from(values) { const array = Array.from(values); return new _BloomFilter(array.length).addAll(array); } add(value) { const stringValue = stringify(value); const fnv = _fnvplus2.default.fast1a32(stringValue) % this.bitSize; const fnvIndex = Math.floor(fnv / BITS); const fnvPosition = fnv % BITS; const mur = _imurmurhash2.default.call(void 0, stringValue).result() % this.bitSize; const murIndex = Math.floor(mur / BITS); const murPosition = mur % BITS; this.bitArray[fnvIndex] |= 1 << fnvPosition; this.bitArray[murIndex] |= 1 << murPosition; return this; } addAll(values) { for (const value of values) { this.add(value); } return this; } has(value) { const stringValue = stringify(value); const fnv = _fnvplus2.default.fast1a32(stringValue) % this.bitSize; const fnvIndex = Math.floor(fnv / BITS); const fnvPosition = fnv % BITS; if ((this.bitArray[fnvIndex] & 1 << fnvPosition) === 0) { return false; } const mur = _imurmurhash2.default.call(void 0, stringValue).result() % this.bitSize; const murIndex = Math.floor(mur / BITS); const murPosition = mur % BITS; return (this.bitArray[murIndex] & 1 << murPosition) !== 0; } }; // src/utils/heap.ts function isNumeric(value) { return typeof value === "number" || typeof value === "bigint"; } function defaultCompareFn(a, b) { if (isNumeric(a) && isNumeric(b)) { return Number(a) - Number(b); } return stringify(a).localeCompare(stringify(b)); } function defaultMinPredicate(a, b) { return defaultCompareFn(a, b) < 0; } function getParentIndex(index) { return Math.floor((index - 1) / 2); } function getLeftChildIndex(parentIndex) { return 2 * parentIndex + 1; } function getRightChildIndex(parentIndex) { return 2 * parentIndex + 2; } function swap(heap, i1, i2) { if (i1 < heap.length && i2 < heap.length) { const v1 = heap[i1]; heap[i1] = heap[i2]; heap[i2] = v1; } } function heapifyUp(heap, compareFn) { let index = heap.length - 1; let parentIndex = getParentIndex(index); while (parentIndex >= 0 && compareFn(heap[index], heap[parentIndex]) < 0) { swap(heap, index, parentIndex); index = parentIndex; parentIndex = getParentIndex(index); } } function heapifyDown(heap, compareFn) { let index = 0; let nextIndex = getLeftChildIndex(index); while (nextIndex < heap.length) { const rightChildIndex = getRightChildIndex(index); if (rightChildIndex < heap.length && compareFn(heap[rightChildIndex], heap[nextIndex]) < 0) { nextIndex = rightChildIndex; } if (compareFn(heap[index], heap[nextIndex]) < 0) { return; } swap(heap, index, nextIndex); index = nextIndex; nextIndex = getLeftChildIndex(index); } } // src/Heap.ts var Heap = class _Heap extends Array { constructor(compareFn = defaultCompareFn) { super(); this.compareFn = compareFn; } static from(arrayLike) { const heap = new _Heap(); const items = Array.from(arrayLike); heap.push(...items); return heap; } parent(index) { return this[getParentIndex(index)]; } leftChild(index) { return this[getLeftChildIndex(index)]; } rightChild(index) { return this[getRightChildIndex(index)]; } push(...items) { items.forEach((item) => { super.push(item); heapifyUp(this, this.compareFn); }); return this.length; } pop() { if (this.length < 2) return super.pop(); const top = this[0]; this[0] = super.pop(); heapifyDown(this, this.compareFn); return top; } peek() { return this[0]; } size() { return this.length; } clear() { this.length = 0; } isEmpty() { return !this.length; } updateComparator(compareFn) { this.compareFn = compareFn; } }; // src/PriorityQueue.ts var PriorityQueue = class _PriorityQueue extends Heap { constructor(priorityPredicate = defaultMinPredicate) { super((a, b) => priorityPredicate(a, b) ? -1 : 1); } static from(arrayLike) { const heap = new _PriorityQueue(); const items = Array.from(arrayLike); heap.push(...items); return heap; } enqueue(...items) { this.push(...items); return this; } dequeue() { return this.pop(); } updatePriority(priorityPredicate) { super.updateComparator((a, b) => priorityPredicate(a, b) ? -1 : 1); } }; // src/TrieNode.ts var TrieNode = (_class6 = class _TrieNode { __init8() {this.children = /* @__PURE__ */ new Map()} __init9() {this.complete = /* @__PURE__ */ new Set()} __init10() {this.partial = /* @__PURE__ */ new Set()} constructor(parent, key, value, complete = false) {;_class6.prototype.__init8.call(this);_class6.prototype.__init9.call(this);_class6.prototype.__init10.call(this); if (key.length !== 1) { throw new TypeError("Key of TrieNode must be a single character"); } this.parent = parent; this.key = key; this.addValue(value, complete); } isLeaf() { return !this.children.size; } isEmpty() { !this.complete.size && !this.partial.size; } size() { return this.children.size; } clear() { this.children.clear(); } addChild(key, value, isLastIndex = false) { let childnode = this.getChild(key); if (!childnode) { childnode = new _TrieNode(this, key, value, isLastIndex); this.children.set(key, childnode); } else { childnode.addValue(value, isLastIndex); } return childnode; } getChild(key) { return this.children.get(key); } hasChild(key) { return this.children.has(key); } isComplete() { return !!this.complete.size; } addValue(value, complete = false) { if (complete) { this.complete.add(value); } else { this.partial.add(value); } return this; } getCompleteValues() { return Array.from(this.complete); } keys() { return Array.from(this.children.keys()); } values() { return { complete: this.getCompleteValues(), partial: Array.from(this.partial) }; } deleteNode(key) { return this.children.delete(key); } deleteValue() { this.complete.clear(); } deletePartialValue(value) { return this.partial.delete(value); } }, _class6); // src/Trie.ts var ROOT_KEY = "*"; var Trie = (_class7 = class {constructor() { _class7.prototype.__init11.call(this); } __init11() {this.root = new TrieNode(null, ROOT_KEY, null, true)} add(string, value) { let node = this.root; const _value = arguments.length > 1 ? value : string; for (let i = 0; i < string.length; i++) { const key = string[i]; const isLastIndex = i === string.length - 1; node = node.addChild(key, _value, isLastIndex); } return this; } addMany(strings, value) { strings.forEach((string) => { this.add(string, value); }); return this; } getLastNode(substring) { let node = this.root; for (const key of substring) { if (!node.hasChild(key)) return null; node = node.getChild(key); } return node; } delete(string) { let node = this.getLastNode(string); if (!node || !node.isComplete()) return false; node.deleteValue(); while (node.parent) { if (!node.isLeaf()) return true; node.parent.deleteNode(node.key); node = node.parent; } return true; } clear() { this.root.clear(); } next(substring) { const node = this.getLastNode(substring); if (!node) return void 0; return node.keys(); } includes(substring) { return !!this.getLastNode(substring); } has(string) { return !!_optionalChain([this, 'access', _16 => _16.getLastNode, 'call', _17 => _17(string), 'optionalAccess', _18 => _18.isComplete, 'call', _19 => _19()]); } getValues(substring) { return _nullishCoalesce(_optionalChain([this, 'access', _20 => _20.getLastNode, 'call', _21 => _21(substring), 'optionalAccess', _22 => _22.values, 'call', _23 => _23()]), () => ( null)); } }, _class7); exports.AcyclicGraph = AcyclicGraph; exports.BinaryNode = BinaryNode; exports.BinaryTree = BinaryTree; exports.BloomFilter = BloomFilter; exports.Graph = Graph; exports.Heap = Heap; exports.LinkedList = LinkedList; exports.LinkedNode = LinkedNode; exports.PriorityQueue = PriorityQueue; exports.Queue = Queue; exports.Stack = Stack; exports.Trie = Trie; exports.TrieNode = TrieNode; //# sourceMappingURL=index.cjs.map