turbo hash 容器
Turbo 提供了许多容器作为 STL 容器的替代品。这些容器通常遵循 STL 容器的属性,尽管有
通常是一些相关的 API 差异和/或实现细节与标准库不同。
Turbo 容器的设计在一般情况下效率更高;在然而,在某些情况下,STL 容器可能更高效。与其他一些不同
Turbo 提供的抽象,不应考虑这些容器 STL 对应项的直接替代品,因为有 API 和/或两组集装箱之间的合同差异。例如,绳索下降
容器通常不保证指针稳定性1插入或删除。
Turbo 容器的设计在一般情况下效率更高;然而,在某些情况下,STL 容器可能更高效。
与 Abseil 提供的其他一些抽象不同,这些容器不应被视为 STL 对应项的直接替代品,因为两组容器之间存在 API 和/或契约差异。例如,Abseil 容器通常不保证插入或删除后指针的稳定性1。
Abseil container 库定义了以下容器集:
- Swiss table 无序容器
有关每种容器类型的更多信息,请参阅下文。
Hash Tables
Turbo container 库通常包含许多有用的哈希表遵守STL容器API契约:
turbo::flat_hash_mapturbo::flat_hash_setturbo::node_hash_mapturbo::node_hash_set
这些哈希表统称为“瑞士表”,旨在
成为替代品std::unordered_map 和 std::unordered_set
与std::unordered_*容器相比,它们具有多个优点:
Collectively, these hash tables are known as "Swiss tables" and are designed to
be replacements for
std::unordered_map
and std::unordered_set
They provide several advantages over the std::unordered_* containers:
- 支持异构查找。
- 允许优化
emplace({key, value})以避免分配一对在大多数常见情况下。 - 支持异构
std::initializer_list以避免额外的副本构建和插入。 - 通过返回 void 而不是迭代器来保证“O(1)”擦除方法。
构建
Swiss table容器组支持与std::unordered_map 用于构造和赋值:
// Examples using node_hash_set and node_hash_map are equivalent
// Default constructor
// No allocation for the table's elements is made.
turbo::flat_hash_set<std::string> set1;
turbo::flat_hash_map<int, std::string> map1;
// Initializer List constructor
turbo::flat_hash_set<std::string> set2 = {{"huey"}, {"dewey"}, {"louie"},};
turbo::flat_hash_map<int, std::string> map2 =
{{1, "huey"}, {2, "dewey"}, {3, "louie"},};
// Copy constructor
turbo::flat_hash_set<std::string> set3(set2);
turbo::flat_hash_map<int, std::string> map3(map2);
// Copy assignment operator
// Hash functor and Comparator are copied as well
turbo::flat_hash_set<std::string> set4;
set4 = set3;
turbo::flat_hash_map<int, std::string> map4;
map4 = map3;
// Move constructor
// Move is guaranteed efficient
turbo::flat_hash_set<std::string> set5(std::move(set4));
turbo::flat_hash_map<int, std::string> map5(std::move(map4));
// Move assignment operator
// May be efficient if allocators are compatible
turbo::flat_hash_set<std::string> set6;
set6 = std::move(set5);
turbo::flat_hash_map<int, std::string> map6;
map6 = std::move(map5);
// Range constructor
std::vector<std::string> v = {"a", "b"};
turbo::flat_hash_set<std::string> set7(v.begin(), v.end());
std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
turbo::flat_hash_map<int, std::string> map7(v.begin(), v.end());
turbo::flat_hash_map and turbo::flat_hash_set
turbo::flat_hash_map 和 turbo::flat_hash_set 是推荐的无序形式一般用途的容器。这些是平面数据结构,存储它们的value_type 直接位于槽数组中。