UNPKG

trie-typed

Version:
416 lines (415 loc) 13.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinearLinkedBase = exports.LinearBase = exports.LinkedListNode = void 0; const iterable_element_base_1 = require("./iterable-element-base"); /** * Singly-linked list node. * @template E - Element type. * @remarks Time O(1), Space O(1) */ class LinkedListNode { /** * Initialize a node. * @param value - Element value. * @remarks Time O(1), Space O(1) */ constructor(value) { this._value = value; this._next = undefined; } /** * Element payload getter. * @returns Element value. * @remarks Time O(1), Space O(1) */ get value() { return this._value; } /** * Element payload setter. * @param value - New value. * @remarks Time O(1), Space O(1) */ set value(value) { this._value = value; } /** * Next node getter. * @returns Next node or `undefined`. * @remarks Time O(1), Space O(1) */ get next() { return this._next; } /** * Next node setter. * @param value - Next node or `undefined`. * @remarks Time O(1), Space O(1) */ set next(value) { this._next = value; } } exports.LinkedListNode = LinkedListNode; /** * Abstract linear container with array-like utilities. * @template E - Element type. * @template R - Return type for mapped/derived views. * @template NODE - Linked node type used by some implementations. * @remarks Time O(1), Space O(1) */ class LinearBase extends iterable_element_base_1.IterableElementBase { /** * Construct a linear container with runtime options. * @param options - `{ maxLen?, ... }` bounds/behavior options. * @remarks Time O(1), Space O(1) */ constructor(options) { super(options); this._maxLen = -1; if (options) { const { maxLen } = options; if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen; } } /** * Upper bound for length (if positive), or `-1` when unbounded. * @returns Maximum allowed length. * @remarks Time O(1), Space O(1) */ get maxLen() { return this._maxLen; } /** * First index of a value from the left. * @param searchElement - Value to match. * @param fromIndex - Start position (supports negative index). * @returns Index or `-1` if not found. * @remarks Time O(n), Space O(1) */ indexOf(searchElement, fromIndex = 0) { if (this.length === 0) return -1; if (fromIndex < 0) fromIndex = this.length + fromIndex; if (fromIndex < 0) fromIndex = 0; for (let i = fromIndex; i < this.length; i++) { const element = this.at(i); if (element === searchElement) return i; } return -1; } /** * Last index of a value from the right. * @param searchElement - Value to match. * @param fromIndex - Start position (supports negative index). * @returns Index or `-1` if not found. * @remarks Time O(n), Space O(1) */ lastIndexOf(searchElement, fromIndex = this.length - 1) { if (this.length === 0) return -1; if (fromIndex >= this.length) fromIndex = this.length - 1; if (fromIndex < 0) fromIndex = this.length + fromIndex; for (let i = fromIndex; i >= 0; i--) { const element = this.at(i); if (element === searchElement) return i; } return -1; } /** * Find the first index matching a predicate. * @param predicate - `(element, index, self) => boolean`. * @param thisArg - Optional `this` for callback. * @returns Index or `-1`. * @remarks Time O(n), Space O(1) */ findIndex(predicate, thisArg) { for (let i = 0; i < this.length; i++) { const item = this.at(i); if (item !== undefined && predicate.call(thisArg, item, i, this)) return i; } return -1; } /** * Concatenate elements and/or containers. * @param items - Elements or other containers. * @returns New container with combined elements (`this` type). * @remarks Time O(sum(length)), Space O(sum(length)) */ concat(...items) { const newList = this.clone(); for (const item of items) { if (item instanceof LinearBase) { newList.pushMany(item); } else { newList.push(item); } } return newList; } /** * In-place stable order via array sort semantics. * @param compareFn - Comparator `(a, b) => number`. * @returns This container. * @remarks Time O(n log n), Space O(n) (materializes to array temporarily) */ sort(compareFn) { const arr = this.toArray(); arr.sort(compareFn); this.clear(); for (const item of arr) this.push(item); return this; } /** * Remove and/or insert elements at a position (array-compatible). * @param start - Start index (supports negative index). * @param deleteCount - How many to remove. * @param items - Elements to insert. * @returns Removed elements as a new list (`this` type). * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length` */ splice(start, deleteCount = 0, ...items) { const removedList = this._createInstance(); start = start < 0 ? this.length + start : start; start = Math.max(0, Math.min(start, this.length)); deleteCount = Math.max(0, Math.min(deleteCount, this.length - start)); for (let i = 0; i < deleteCount; i++) { const removed = this.deleteAt(start); if (removed !== undefined) { removedList.push(removed); } } for (let i = 0; i < items.length; i++) { this.addAt(start + i, items[i]); } return removedList; } /** * Join all elements into a string. * @param separator - Separator string. * @returns Concatenated string. * @remarks Time O(n), Space O(n) */ join(separator = ',') { return this.toArray().join(separator); } /** * Snapshot elements into a reversed array. * @returns New reversed array. * @remarks Time O(n), Space O(n) */ toReversedArray() { const array = []; for (let i = this.length - 1; i >= 0; i--) { array.push(this.at(i)); } return array; } reduceRight(callbackfn, initialValue) { let accumulator = initialValue !== null && initialValue !== void 0 ? initialValue : 0; for (let i = this.length - 1; i >= 0; i--) { accumulator = callbackfn(accumulator, this.at(i), i, this); } return accumulator; } /** * Create a shallow copy of a subrange. * @param start - Inclusive start (supports negative index). * @param end - Exclusive end (supports negative index). * @returns New list with the range (`this` type). * @remarks Time O(n), Space O(n) */ slice(start = 0, end = this.length) { start = start < 0 ? this.length + start : start; end = end < 0 ? this.length + end : end; const newList = this._createInstance(); for (let i = start; i < end; i++) { newList.push(this.at(i)); } return newList; } /** * Fill a range with a value. * @param value - Value to set. * @param start - Inclusive start. * @param end - Exclusive end. * @returns This list. * @remarks Time O(n), Space O(1) */ fill(value, start = 0, end = this.length) { start = start < 0 ? this.length + start : start; end = end < 0 ? this.length + end : end; if (start < 0) start = 0; if (end > this.length) end = this.length; if (start >= end) return this; for (let i = start; i < end; i++) { this.setAt(i, value); } return this; } } exports.LinearBase = LinearBase; /** * Linked-list specialized linear container. * @template E - Element type. * @template R - Return type for mapped/derived views. * @template NODE - Linked node type. * @remarks Time O(1), Space O(1) */ class LinearLinkedBase extends LinearBase { constructor(options) { super(options); if (options) { const { maxLen } = options; if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen; } } /** * Linked-list optimized `indexOf` (forwards scan). * @param searchElement - Value to match. * @param fromIndex - Start position. * @returns Index or `-1`. * @remarks Time O(n), Space O(1) */ indexOf(searchElement, fromIndex = 0) { const iterator = this._getIterator(); let current = iterator.next(); let index = 0; while (index < fromIndex) { current = iterator.next(); index++; } while (!current.done) { if (current.value === searchElement) return index; current = iterator.next(); index++; } return -1; } /** * Linked-list optimized `lastIndexOf` (reverse scan). * @param searchElement - Value to match. * @param fromIndex - Start position. * @returns Index or `-1`. * @remarks Time O(n), Space O(1) */ lastIndexOf(searchElement, fromIndex = this.length - 1) { const iterator = this._getReverseIterator(); let current = iterator.next(); let index = this.length - 1; while (index > fromIndex) { current = iterator.next(); index--; } while (!current.done) { if (current.value === searchElement) return index; current = iterator.next(); index--; } return -1; } concat(...items) { const newList = this.clone(); for (const item of items) { if (item instanceof LinearBase) { newList.pushMany(item); } else { newList.push(item); } } return newList; } /** * Slice via forward iteration (no random access required). * @param start - Inclusive start (supports negative index). * @param end - Exclusive end (supports negative index). * @returns New list (`this` type). * @remarks Time O(n), Space O(n) */ slice(start = 0, end = this.length) { start = start < 0 ? this.length + start : start; end = end < 0 ? this.length + end : end; const newList = this._createInstance(); const iterator = this._getIterator(); let current = iterator.next(); let c = 0; while (c < start) { current = iterator.next(); c++; } for (let i = start; i < end; i++) { newList.push(current.value); current = iterator.next(); } return newList; } /** * Splice by walking node iterators from the start index. * @param start - Start index. * @param deleteCount - How many elements to remove. * @param items - Elements to insert after the splice point. * @returns Removed elements as a new list (`this` type). * @remarks Time O(n + m), Space O(min(n, m)) where `m = items.length` */ splice(start, deleteCount = 0, ...items) { const removedList = this._createInstance(); start = start < 0 ? this.length + start : start; start = Math.max(0, Math.min(start, this.length)); deleteCount = Math.max(0, deleteCount); let currentIndex = 0; let currentNode = undefined; let previousNode = undefined; const iterator = this._getNodeIterator(); for (const node of iterator) { if (currentIndex === start) { currentNode = node; break; } previousNode = node; currentIndex++; } for (let i = 0; i < deleteCount && currentNode; i++) { removedList.push(currentNode.value); const nextNode = currentNode.next; this.delete(currentNode); currentNode = nextNode; } for (let i = 0; i < items.length; i++) { if (previousNode) { this.addAfter(previousNode, items[i]); previousNode = previousNode.next; } else { this.addAt(0, items[i]); previousNode = this._getNodeIterator().next().value; } } return removedList; } reduceRight(callbackfn, initialValue) { let accumulator = initialValue !== null && initialValue !== void 0 ? initialValue : 0; let index = this.length - 1; for (const item of this._getReverseIterator()) { accumulator = callbackfn(accumulator, item, index--, this); } return accumulator; } } exports.LinearLinkedBase = LinearLinkedBase;