@pujansrt/dsx-ts
Version:
Type-safe data-structure implementations in TypeScript.
131 lines (122 loc) • 3.22 kB
text/typescript
interface IQueue<T> {
enqueue(item: T): void;
dequeue(): T | undefined;
size(): number;
}
/**
* A basic Queue implementation in TypeScript.
*
* @author Pujan Srivastava
*/
declare class Queue<T> implements IQueue<T> {
private capacity;
private storage;
constructor(capacity?: number);
enqueue(item: T): void;
dequeue(): T | undefined;
size(): number;
}
interface IStack<T> {
push(item: T): void;
pop(): T | undefined;
peek(): T | undefined;
size(): number;
}
/**
* A basic Stack implementation in TypeScript.
*
* @author Pujan Srivastava
*/
declare class Stack<T> implements IStack<T> {
private capacity;
private storage;
constructor(capacity?: number);
push(item: T): void;
pop(): T | undefined;
peek(): T | undefined;
size(): number;
}
declare class LRUCache<K, V> {
private readonly capacity;
private map;
private head;
private tail;
constructor(capacity: number);
get(key: K): V | undefined;
put(key: K, value: V): void;
private moveToFront;
private addToFront;
private removeLRU;
}
/**
* Priority Queue implementation using a binary heap.
* This implementation allows for custom comparison functions.
*
* @author Pujan Srivastava
*/
declare class PriorityQueue<T> {
private heap;
private comparator;
constructor(comparator?: (a: T, b: T) => number);
size(): number;
isEmpty(): boolean;
peek(): T | undefined;
add(item: T): void;
poll(): T | undefined;
private bubbleUp;
private bubbleDown;
}
/**
* A simple Bloom Filter implementation in TypeScript. Probabilistic Membership Checker
* Does not store actual data — only hashed bits.
*
* Ideal for:
* * Checking if a value might exist
* * Early-out filters to avoid DB/cache/network calls
* * Scenarios where memory is tight (IoT, web proxies, edge caches)
*
* @author Pujan Srivastava
*/
declare class BloomFilter {
private readonly size;
private bitArray;
private hashFns;
constructor(size: number, hashFns: ((val: string) => number)[]);
add(item: string): void;
has(item: string): boolean;
}
declare function hashFnv1a(str: string): number;
declare function hashDjb2(str: string): number;
type DistanceFunction<T> = (a: T, b: T) => number;
declare class BKTree<T> {
private root;
private distanceFn;
constructor(distanceFn: DistanceFunction<T>);
add(value: T): void;
search(query: T, threshold: number): T[];
}
declare function levenshtein(a: string, b: string): number;
declare class TTLCache<K, V> {
private ttl;
private store;
constructor(ttl: number);
set(key: K, value: V): void;
get(key: K): V | undefined;
has(key: K): boolean;
delete(key: K): void;
clear(): void;
size(): number;
purgeExpired(): void;
}
declare class AhoCorasick {
private keywords;
private root;
constructor(keywords: string[]);
private buildTrie;
private buildFailures;
search(text: string): {
match: string;
index: number;
}[];
}
export { AhoCorasick, BKTree, BloomFilter, LRUCache, PriorityQueue, Queue, Stack, TTLCache, hashDjb2, hashFnv1a, levenshtein };