@webkrafters/trie
Version:
Trie - trie data structure accepting any data type
145 lines (144 loc) • 6.01 kB
TypeScript
export type EqualityFn<T = unknown> = (nodeData: T, comparehend: unknown) => boolean;
export interface Options<T = unknown> {
equalityMatcher?: EqualityFn<T>;
lessThanMatcher?: EqualityFn<T>;
sorted?: boolean;
}
export declare const enum Compared {
EQ = 0,
GT = 1,
LT = -1
}
export interface ClosestKeyDesc {
desc: Compared;
index: number;
}
export declare const enum OpStatus {
FAILED = "FAILED",
SUCCESSFUL = "SUCCESSFUL"
}
export declare const enum Status {
NOOP = "NOOP",
REMOVED = "REMOVED",
UPDATED = "UPDATED"
}
export interface TrieableNode<T = unknown> {
data: T | null;
children?: Array<TrieableNode<T>>;
isBoundary?: boolean;
parent?: TrieableNode<T> | null;
}
interface ChildNodeEntry<T = unknown> {
bucketIndex: number;
code: number;
value: null | [Node<T>, number];
}
export type KeyType = string | number | symbol;
export interface TrieableNodeKeyMapping {
data: KeyType;
children: KeyType | Array<KeyType>;
isBoundary: KeyType;
}
export declare const LT_TYPES_MSG = "Default LessThan matcher can only be used with `null` values and `string`, `number`, `bigint`, `boolean` and `undefined` value types. For any other type, please provide a custom `isLessThanValue` function option.";
export declare const LT_ARG_TYPES_MISMATCH_MSG = "Default LessThan matcher requires both matched values to be of the same type and also accepts `undefined` and `null` values.";
export declare const getDescriptor: ((obj: any) => string);
export declare const lessThanValue: EqualityFn;
export declare const sameValueZero: EqualityFn;
export default class Trie<T = unknown> {
private isLessThanValue;
private isSameValue;
private sorted;
private root;
static makeTrieable<T = unknown, NESTED_OBJECT extends {} = {}>(node: NESTED_OBJECT, keyMap: TrieableNodeKeyMapping, parentNode?: TrieableNode<T>): TrieableNode<T>;
constructor(data?: Trie<T>, opts?: Options<T>);
constructor(data?: TrieableNode<T>, opts?: Options<T>);
constructor(data?: Array<TrieableNode<T>>, opts?: Options<T>);
constructor(data?: Array<Iterable<T>>, opts?: Options<T>);
constructor(data?: Array<Iterable<T> | TrieableNode<T>>, opts?: Options<T>);
get isEmpty(): boolean;
get size(): number;
add(data: Iterable<T>): void;
addMany(data: Array<Iterable<T>>): void;
addMany(data: Array<TrieableNode<T>>): void;
addMany(data: Array<Iterable<T> | TrieableNode<T>>): void;
asArray(completeSequencesOnly?: boolean): Array<Iterable<T>>;
asTrieableNode(): TrieableNode<T>;
clear(): void;
clone(): Trie<T>;
getAllStartingWith(prefix?: Iterable<T>, completeSequencesOnly?: boolean): Iterable<T>[];
getFarthestIn(sequence?: Iterable<T>): Iterable<T>;
has(sequence: Iterable<T>): boolean;
isSame(trie: Trie<T>): boolean;
matches(graph: Array<Iterable<T>>): boolean;
matches(graph: Array<TrieableNode<T>>): boolean;
matches(graph: TrieableNode<T>): boolean;
matches(graph: Trie<T>): boolean;
merge(data: Trie<T>): void;
merge(data: TrieableNode<T>): void;
remove(data: Iterable<T>): boolean;
removeAllStartingWith(prefix?: Iterable<T>): void;
removeMany(data: Array<Iterable<T>>): OpStatus[];
protected _getNodeAtPrefixEnd(prefix: Iterable<T>): Node<T>;
private _getAllStartingWith;
}
export declare class Node<T = unknown> {
private _cNodes;
private _data;
private _isEqualValue;
private _isLessThanValue;
private _isSequenceBoundary;
private _pNode;
constructor(data: T | null, isEqualValue?: EqualityFn<T>, isLessThanValue?: EqualityFn<T>, successorData?: Array<Iterable<T> | TrieableNode<T>>, pNode?: Node<T>, isSequenceBoundary?: boolean);
get childNodes(): ChildNodes<T>;
get data(): T;
get isEmpty(): boolean;
get isRoot(): boolean;
get isSequenceBoundary(): boolean;
set isSequenceBoundary(flag: boolean);
get parentNode(): Node<T>;
get size(): number;
addChild(childData: Array<T>): void;
asArray(completeSequencesOnly?: boolean, depth?: number): T[][];
asTrieableNode(parentTrieableNode?: TrieableNode<T>): TrieableNode<T>;
empty(): void;
getChildPrefixEnd(prefix?: Array<T>): Node<T>;
getDeepestNodeIn({ length: sLen, ...sequence }?: Array<T>): {
index: number;
node: Node<T>;
};
isEqual(graph: Array<Array<T>>): boolean;
isEqual(graph: Node<T>): boolean;
isEqual(graph: TrieableNode<T>): boolean;
merge(node: Node<T>): void;
mergeTrieableNode(trieableNode: TrieableNode<T>): void;
removeChild(childData: Array<T>): boolean;
private _countSequences;
private _removeChild;
}
export declare abstract class ChildNodes<T = unknown> {
protected codes: Array<number>;
protected keys: Array<T>;
protected buckets: Array<Array<[Node<T>, number]>>;
protected isEqualValue: EqualityFn<T>;
constructor(isEqualityValue: EqualityFn<T>);
get size(): number;
clear(): void;
get(key: T): Node<T>;
list(): Node<T>[];
abstract indexOf(data: T): number;
remove(node: Node<T>): void;
set(node: Node<T>): void;
protected _get(key: T, code?: number): Node<T>;
protected _getEntry(key: T, code?: number): ChildNodeEntry<T>;
protected _getEntryByKeyIndex(i: number): ChildNodeEntry<T>;
protected _set(node: Node<T>, code?: number): void;
protected abstract _optForKeyLocator(key: T): boolean;
protected _splice(code: number, bucketIndex: number): void;
protected _splice(code: number, newNode: Node<T>, insertionIndex?: number): void;
protected _syncBuckets(startKeyIndex?: number): void;
}
export declare function robustHash<T>(key: T): number;
export declare function bSearch<T = unknown>(needle: T, haystack: Array<T>, compare?: (a: any, b: any) => Compared): ClosestKeyDesc;
export declare function isIterable(sequence: any): boolean;
export declare function toArray<T>(sequence: Iterable<T>): Array<T>;
export {};