UNPKG

cmpstr

Version:

CmpStr is a lightweight, fast and well performing package for calculating string similarity

65 lines (63 loc) 1.92 kB
// CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License class Hasher { static FNV_PRIME = 0x01000193; static HASH_OFFSET = 0x811c9dc5; static fastFNV1a(str) { const len = str.length; let hash = this.HASH_OFFSET; const chunks = Math.floor(len / 4); for (let i = 0; i < chunks; i++) { const pos = i * 4; const chunk = str.charCodeAt(pos) | (str.charCodeAt(pos + 1) << 8) | (str.charCodeAt(pos + 2) << 16) | (str.charCodeAt(pos + 3) << 24); hash ^= chunk; hash = Math.imul(hash, this.FNV_PRIME); } const remaining = len % 4; if (remaining > 0) { const pos = chunks * 4; for (let i = 0; i < remaining; i++) { hash ^= str.charCodeAt(pos + i); hash = Math.imul(hash, this.FNV_PRIME); } } hash ^= hash >>> 16; hash *= 0x85ebca6b; hash ^= hash >>> 13; hash *= 0xc2b2ae35; hash ^= hash >>> 16; return hash >>> 0; } } class HashTable { LRU; static MAX_LEN = 2048; static TABLE_SIZE = 10_000; table = new Map(); constructor(LRU = true) { this.LRU = LRU; } key(label, strs, sorted = false) { for (const str of strs) if (str.length > HashTable.MAX_LEN) return false; const hashes = strs.map((s) => Hasher.fastFNV1a(s)); return [label, ...(sorted ? hashes.sort() : hashes)].join('-'); } has = (key) => this.table.has(key); get = (key) => this.table.get(key); set(key, entry, update = true) { if (!update && this.table.has(key)) return false; while (!this.table.has(key) && this.table.size >= HashTable.TABLE_SIZE) { if (!this.LRU) return false; this.table.delete(this.table.keys().next().value); } this.table.set(key, entry); return true; } delete = (key) => this.table.delete(key); clear = () => this.table.clear(); size = () => this.table.size; } export { HashTable, Hasher };