数据结构 – 稀疏哈希表背后的主要实现思想是什么?

为什么Google sparsehash的开源库有两个实现:一个密集的哈希表和一个稀疏的
最佳答案
密集哈希表是您普通教科书的哈希表实现.

稀疏哈希表仅存储实际设置的元素,并分割数组.引用从comments执行稀疏表:

// The idea is that a table with (logically) t buckets is divided
// into t/M *groups* of M buckets each.  (M is a constant set in
// GROUP_SIZE for efficiency.)  Each group is stored sparsely.
// Thus, inserting into the table causes some array to grow, which is
// slow but still constant time.  Lookup involves doing a
// logical-position-to-sparse-position lookup, which is also slow but
// constant time.  The larger M is, the slower these operations are
// but the less overhead (slightly).

要知道数组的哪些元素被设置,一个稀疏表包括一个位图:

// To store the sparse array, we store a bitmap B, where B[i] = 1 iff
// bucket i is non-empty.  Then to look up bucket i we really look up
// array[# of 1s before i in B].  This is constant time for fixed M.

因此每个元素仅产生1位的开销(限制).

转载注明原文:数据结构 – 稀疏哈希表背后的主要实现思想是什么? - 代码日志