/** * Deduplicated UTF-8 string storage. * * Strings are stored in a flat buffer and referenced by index. * Supports fast deduplication (string→index) and lazy reconstruction (index→string). * * @module */ import type { OsmPbfStringTable } from "@osmix/pbf"; import type { ContentHasher } from "@osmix/shared/content-hasher"; import { type BufferType, ResizeableTypedArray as RTA } from "./typed-arrays.ts"; /** * Serializable state for worker transfer. */ export interface StringTableTransferables { /** Concatenated UTF-8 bytes. */ bytes: T; /** Maps string index → byte offset. */ start: T; /** Maps string index → byte length. */ count: T; } /** * Append-only deduplicated string table. * * Limits: Max string length 65,535 bytes. * Rebuilds reverse index lazily after transfer. */ export default class StringTable { /** UTF-8 encoder for string→bytes conversion */ private enc; /** UTF-8 decoder for bytes→string conversion */ private dec; /** Concatenated UTF-8 bytes of all strings */ bytes: RTA; /** Maps string index → byte offset in bytes array */ start: RTA; /** Maps string index → byte length */ count: RTA; /** Forward lookup: string → index (populated during add) */ private stringToIndex; /** Whether the reverse index has been built (lazy after transfer) */ private reverseIndexBuilt; /** Cache of decoded strings to avoid repeated UTF-8 decoding */ private indexToString; /** * Create a new StringTable. */ constructor(opts?: StringTableTransferables); /** * Get transferable objects for passing to another thread. */ transferables(): StringTableTransferables; /** * Add a string to the table and return its index. */ add(str: string): number; /** * Decode all the strings in a primitive block and add them to the string table. * Return a mapping of block index -> string table index */ createBlockIndexMap(blockStringtable: OsmPbfStringTable): Uint32Array; /** * Get a string by its index. * Caches results to avoid repeated UTF-8 decoding. */ get(index: number): string; /** * Get the raw UTF-8 bytes of a string. * Returns a subarray view (not a copy). */ getBytes(index: number): Uint8Array; /** Number of strings in the table. */ get length(): number; /** * Finalize the string table by compacting internal arrays. * * This releases unused buffer capacity and marks the reverse index as built * (since all strings were added before this call). */ buildIndex(): void; /** * Convert the string table to an OSM PBF string table. */ toOsmPbfStringTable(): OsmPbfStringTable; /** * Lazily build the reverse index (string → index). * Decodes all strings once to populate the lookup map. */ private ensureReverseIndex; /** * Find the index of a string. */ find(str: string): number; /** * Update a ContentHasher with the string table's data. * Hashes the bytes, start offsets, and counts. */ updateHash(hasher: ContentHasher): ContentHasher; } //# sourceMappingURL=stringtable.d.ts.map