/** * Memory-Efficient Data Structures (P-003) * * Provides optimized data structures for managing large collections: * 1. LRU Cache with O(1) operations * 2. Quantized embedding storage (8x memory reduction) * 3. Domain-indexed collections for fast filtering */ /** * LRU Cache with O(1) get, set, and eviction. * * Uses a doubly-linked list for ordering and a Map for O(1) lookups. * The most recently used items are at the head, least recently used at tail. * * @typeParam K - Key type * @typeParam V - Value type * * @example * ```ts * const cache = new LRUCache(1000); * cache.set('skill-1', skillData); * const skill = cache.get('skill-1'); // Moves to head * ``` */ export declare class LRUCache { private cache; private head; private tail; private readonly maxSize; constructor(maxSize: number); /** * Get a value from the cache, moving it to the head (most recently used). */ get(key: K): V | undefined; /** * Check if a key exists in the cache. */ has(key: K): boolean; /** * Set a value in the cache. If at capacity, evicts the least recently used item. * * @returns The evicted key-value pair if eviction occurred, undefined otherwise. */ set(key: K, value: V): { key: K; value: V; } | undefined; /** * Delete a key from the cache. */ delete(key: K): boolean; /** * Clear the entire cache. */ clear(): void; /** * Get the current size of the cache. */ get size(): number; /** * Get all keys in LRU order (most recent first). * Uses a generator for memory efficiency. */ keys(): IterableIterator; /** * Get all values in LRU order (most recent first). * Uses a generator for memory efficiency. */ values(): IterableIterator; /** * Get all entries as [key, value] pairs in LRU order. * Uses a generator for memory efficiency. */ entries(): IterableIterator<[K, V]>; /** * Iterate over entries (supports for...of). */ [Symbol.iterator](): IterableIterator<[K, V]>; /** * Get access statistics for a key. */ getStats(key: K): { accessCount: number; lastAccessedAt: number; } | undefined; /** * Get the least recently used key without evicting. */ peekLRU(): K | undefined; /** * Get the most recently used key without modifying order. */ peekMRU(): K | undefined; /** * Evict multiple least recently used entries. * * @param count - Number of entries to evict * @returns Array of evicted key-value pairs */ evictMultiple(count: number): Array<{ key: K; value: V; }>; /** * Move a node to the head of the list. */ private moveToHead; /** * Remove a node from the list (but not the cache). */ private removeNode; /** * Evict the least recently used entry. */ private evictLRU; } /** * A collection indexed by domain for fast filtering. * * Maintains both a primary Map and a secondary domain index. * Supports multi-domain entries (items can belong to multiple domains). * * @typeParam K - Key type * @typeParam V - Value type */ export declare class DomainIndexedMap { private items; private domainIndex; private keyToDomains; /** * Remove a key from the domain index. */ private removeFromIndex; /** * Set an item with its associated domains. */ set(key: K, value: V, domains: string | string[]): void; /** * Get an item by key. */ get(key: K): V | undefined; /** * Check if key exists. */ has(key: K): boolean; /** * Delete an item and remove from all domain indices. */ delete(key: K): boolean; /** * Get all items for a specific domain. */ getByDomain(domain: string): V[]; /** * Get all keys for a specific domain. */ getKeysByDomain(domain: string): K[]; /** * Get all domains that have at least one item. */ getDomains(): string[]; /** * Get the number of items for a domain. */ countByDomain(domain: string): number; /** * Clear all items. */ clear(): void; /** * Get total item count. */ get size(): number; /** * Iterate over all items. */ [Symbol.iterator](): IterableIterator<[K, V]>; /** * Get all keys. */ keys(): IterableIterator; /** * Get all values. */ values(): IterableIterator; /** * Get all entries. */ entries(): IterableIterator<[K, V]>; } /** * Quantized embedding storage using Uint8Array. * * Reduces memory from 8 bytes/float to 1 byte/value (8x reduction). * Values are scaled from [-1, 1] or [0, 1] range to [0, 255]. * * For 64-dim embeddings: 512 bytes -> 64 bytes (8x reduction). */ export declare class QuantizedEmbedding { private data; private readonly minVal; private readonly maxVal; /** * Create a quantized embedding from float array. * * @param embedding - Original float embedding * @param minVal - Minimum expected value (default: -1) * @param maxVal - Maximum expected value (default: 1) */ constructor(embedding?: number[], minVal?: number, maxVal?: number); /** * Quantize a float array into the internal Uint8Array. */ private quantize; /** * Dequantize back to float array. */ toFloatArray(): number[]; /** * Get the raw quantized data. */ getRawData(): Uint8Array; /** * Set from raw quantized data. */ setRawData(data: Uint8Array): void; /** * Get the dimension of the embedding. */ get dimension(): number; /** * Get memory size in bytes. */ get byteLength(): number; /** * Compute cosine similarity with another quantized embedding. * Uses integer arithmetic for efficiency. */ cosineSimilarity(other: QuantizedEmbedding): number; /** * Create from serialized format. */ static fromSerialized(data: { raw: number[]; minVal: number; maxVal: number; }): QuantizedEmbedding; /** * Serialize for storage. */ toSerialized(): { raw: number[]; minVal: number; maxVal: number; }; } /** * Batch quantize multiple embeddings. */ export declare function quantizeEmbeddings(embeddings: number[][], minVal?: number, maxVal?: number): QuantizedEmbedding[]; /** * Batch dequantize multiple embeddings. */ export declare function dequantizeEmbeddings(quantized: QuantizedEmbedding[]): number[][]; /** * Memory usage statistics for a collection. */ export interface MemoryStats { /** Number of items in the collection */ itemCount: number; /** Estimated memory usage in bytes */ estimatedBytes: number; /** Estimated memory usage in human-readable format */ estimatedSize: string; /** Additional breakdown by type */ breakdown?: Record; } /** * Format bytes to human-readable string. */ export declare function formatBytes(bytes: number): string; /** * Estimate memory size of a JavaScript value. * * This is a rough estimate as actual memory depends on V8 implementation. * Handles circular references by tracking visited objects. * * @param value - The value to estimate size for * @param seen - Internal set of visited objects to handle circular references */ export declare function estimateSize(value: unknown, seen?: Set): number; //# sourceMappingURL=memory-efficient-structures.d.ts.map