← 中文资源

简体中文版本

泛型哈希表双哈希

该资源提供 ts-hash-table-open-addr-p16 的中文技术参考,涵盖核心方法、参数说明与使用示例

阅读英文原文

功能说明

泛型哈希表双哈希是一个面向data-integrity场景的typescript代码示例。用双哈希开放寻址解决冲突的泛型哈希表。该示例展示了如何在实际项目中运用这一技术,代码结构清晰,便于理解和复用。

关键实现

该示例使用typescript语言实现,核心逻辑封装在函数中。代码遵循语言惯用风格,处理了边界情况,并考虑了性能与可读性的平衡。tags 标签包括:hash-table、open-addressing、double-hash,方便在平台上检索同类示例。

使用注意

使用本示例时需要注意输入约束和边界条件。该示例为代码片段(isSnippet),可直接嵌入到项目中使用。建议在实际应用前,根据业务需求调整参数和错误处理* 建议在实际应用前,根据业务需求调整参数和错误处理策略。relatedExamples 中列出了相关的其他示例,可供参考学习。

验证方式

可通过编写单元测试验证该示例的正确性。测试应覆盖正常输入、边界值和异常情况。确保在目标运行环境中测试代码的兼容性和性能表现。updatedDate 为 2026-10-25,已通过审核并发布。

代码示例

class HashTable<K, V> {
  private keys: (K | null)[]; private vals: (V | null)[]; private size: number;
  constructor(size = 101) { this.size = size; this.keys = new Array(size).fill(null); this.vals = new Array(size).fill(null); }
  private h1(key: K): number { const s = String(key); let h = 0; for (const c of s) h = (h * 31 + c.charCodeAt(0)) % this.size; return h; }
  private h2(key: K): number { const s = String(key); let h = 0; for (const c of s) h = (h * 17 + c.charCodeAt(0)) % (this.size - 1); return h + 1; }
  set(key: K, val: V): void {
    const step = this.h2(key); let idx = this.h1(key);
    for (let i = 0; i < this.size; i++) { if (this.keys[idx] === null || this.keys[idx] === key) { this.keys[idx] = key; this.vals[idx] = val; return; } idx = (idx + step) % this.size; }
    throw new Error("table full");
  }
  get(key: K): V | undefined { const step = this.h2(key); let idx = this.h1(key); for (let i = 0; i < this.size; i++) { if (this.keys[idx] === null) return undefined; if (this.keys[idx] === key) return this.vals[idx]!; idx = (idx + step) % this.size; } return undefined; }
}