简体   繁体   English

实现unordered_map,unordered_set,map,set using vectors

[英]Implement unordered_map, unordered_set, map, set using vectors

This is more of an intellectual exercise, but are there battle-tested C++ libraries implementing hash map/set ( std::unordered_map , std::unordered_set ), red-back trees ( std::map , std::set ) using std::vector s?这更像是一种智力练习,但是是否有经过实战检验的 C++ 库实现 hash 映射/集合( std::unordered_mapstd::unordered_set )、红背树( std::mapstd::set )使用std::vector

Take std::unordered_set for instance.std::unordered_set为例。 My idea is to use 3 vectors, X (stores heads of chains), Y (stores set elements), Z (temporary container).我的想法是使用 3 个向量, X (存储链头), Y (存储集合元素), Z (临时容器)。

Roughly speaking, given a set element 12345 ,粗略地说,给定一个集合元素12345

  1. Let i = X[hash(12345) % X.size()] .i = X[hash(12345) % X.size()] Then Y[i] is the head of the chain that 12345 lives on.那么Y[i]就是12345生存的链的头部。

  2. Y[i] is a pair (val, j) . Y[i]是一对(val, j) val is some element value. val是一些元素值。 Y[j] is the next item on chain. Y[j]是链上的下一个项目。

  3. Once 12345 is found, deleting it can leave a "hole" in Y .一旦找到12345 ,删除它可能会在Y中留下一个“洞”。

  4. A new element will be pushed back to Y and X will be adjusted accordingly.一个新元素将被推回YX将相应调整。

  5. If the number of "holes" in Y exceeds, eg 50% of Y.size() , adjust X and Y globally to erase all the "holes", during which Z might be needed as a temporary storage.如果Y中的“空洞”数量超过,例如Y.size()的 50%,则全局调整XY以清除所有“空洞”,在此期间可能需要Z作为临时存储。

The idea applies to trees in std::set and std::map .这个想法适用于std::setstd::map中的树。 Of course many other details need to be carefully taken care of.当然,还有许多其他细节需要小心处理。

Has anybody tried something like this?有人尝试过这样的事情吗? The motivation is to keep the data structure as compact as possible, and to avoid memory allocations as much as possible.动机是尽可能保持数据结构紧凑,并尽可能避免 memory 分配。 I imagine this will yield some good speedup for small and medium size applications -- or maybe I am wrong?我想这会为中小型应用程序带来一些不错的加速——也许我错了?

Thanks!谢谢!

Yes, there are.是的,有。 Google dense_hash_map is one of such example. Google dense_hash_map就是这样的例子之一。

There is an immense variety of hash maps and tables built with purpose-specific requirements like cache locality, size, read speed, write speed.有各种各样的 hash 地图和表格,这些地图和表格是根据缓存位置、大小、读取速度、写入速度等特定目的要求构建的。 As speed is highly dependent on cache locality, it is very common for these implementations to use vectors as backend storage.由于速度高度依赖于缓存位置,因此这些实现使用向量作为后端存储是很常见的。

Have a look at this shootout between hashmaps and browse through every one of them.看看哈希图之间的枪战并浏览每一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM