稀疏哈希表背后的主要实现思想是什么?

2024-02-13

为什么Google稀疏哈希开源库有两种实现:密集哈希表和稀疏哈希表?


密集哈希表是普通教科书哈希表的实现。

稀疏哈希表仅存储实际已设置的元素,并划分为多个数组。引用自comments http://code.google.com/p/google-sparsehash/source/browse/trunk/src/google/sparsetable#605在稀疏表的实现中:

// 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 位的开销(在限制范围内)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

稀疏哈希表背后的主要实现思想是什么? 的相关文章

随机推荐