Add a new HashMap implementation

Adds a new, cleaned up, HashMap implementation.

* Uses Robin Hood Hashing (https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing).
* Keeps elements in a double linked list for simpler, ordered, iteration.
* Allows keeping iterators for later use in removal (Unlike Map<>, it does not do much
  for performance vs keeping the key, but helps replace old code).
* Uses a more modern C++ iterator API, deprecates the old one.
* Supports custom allocator (in case there is a wish to use a paged one).

This class aims to unify all the associative template usage and replace it by this one:
* Map<> (whereas key order does not matter, which is 99% of cases)
* HashMap<>
* OrderedHashMap<>
* OAHashMap<>
This commit is contained in:
reduz 2022-05-08 10:09:19 +02:00 committed by Rémi Verschelde
parent 9b7e16a6b8
commit 8b7c7f5a75
95 changed files with 1434 additions and 1874 deletions

View file

@ -1,5 +1,5 @@
/*************************************************************************/
/* test_ordered_hash_map.h */
/* test_hash_map.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
@ -28,56 +28,53 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef TEST_ORDERED_HASH_MAP_H
#define TEST_ORDERED_HASH_MAP_H
#ifndef TEST_HASH_MAP_H
#define TEST_HASH_MAP_H
#include "core/templates/ordered_hash_map.h"
#include "core/templates/hash_map.h"
#include "tests/test_macros.h"
namespace TestOrderedHashMap {
namespace TestHashMap {
TEST_CASE("[OrderedHashMap] Insert element") {
OrderedHashMap<int, int> map;
OrderedHashMap<int, int>::Element e = map.insert(42, 84);
TEST_CASE("[HashMap] Insert element") {
HashMap<int, int> map;
HashMap<int, int>::Iterator e = map.insert(42, 84);
CHECK(e);
CHECK(e.key() == 42);
CHECK(e.get() == 84);
CHECK(e.value() == 84);
CHECK(e->key == 42);
CHECK(e->value == 84);
CHECK(map[42] == 84);
CHECK(map.has(42));
CHECK(map.find(42));
}
TEST_CASE("[OrderedHashMap] Overwrite element") {
OrderedHashMap<int, int> map;
TEST_CASE("[HashMap] Overwrite element") {
HashMap<int, int> map;
map.insert(42, 84);
map.insert(42, 1234);
CHECK(map[42] == 1234);
}
TEST_CASE("[OrderedHashMap] Erase via element") {
OrderedHashMap<int, int> map;
OrderedHashMap<int, int>::Element e = map.insert(42, 84);
map.erase(e);
CHECK(!e);
TEST_CASE("[HashMap] Erase via element") {
HashMap<int, int> map;
HashMap<int, int>::Iterator e = map.insert(42, 84);
map.remove(e);
CHECK(!map.has(42));
CHECK(!map.find(42));
}
TEST_CASE("[OrderedHashMap] Erase via key") {
OrderedHashMap<int, int> map;
TEST_CASE("[HashMap] Erase via key") {
HashMap<int, int> map;
map.insert(42, 84);
map.erase(42);
CHECK(!map.has(42));
CHECK(!map.find(42));
}
TEST_CASE("[OrderedHashMap] Size") {
OrderedHashMap<int, int> map;
TEST_CASE("[HashMap] Size") {
HashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 84);
map.insert(123, 84);
@ -87,8 +84,8 @@ TEST_CASE("[OrderedHashMap] Size") {
CHECK(map.size() == 4);
}
TEST_CASE("[OrderedHashMap] Iteration") {
OrderedHashMap<int, int> map;
TEST_CASE("[HashMap] Iteration") {
HashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 12385);
map.insert(0, 12934);
@ -102,34 +99,35 @@ TEST_CASE("[OrderedHashMap] Iteration") {
expected.push_back(Pair<int, int>(123485, 1238888));
int idx = 0;
for (OrderedHashMap<int, int>::Element E = map.front(); E; E = E.next()) {
CHECK(expected[idx] == Pair<int, int>(E.key(), E.value()));
for (const KeyValue<int, int> &E : map) {
CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
++idx;
}
}
TEST_CASE("[OrderedHashMap] Const iteration") {
OrderedHashMap<int, int> map;
TEST_CASE("[HashMap] Const iteration") {
HashMap<int, int> map;
map.insert(42, 84);
map.insert(123, 12385);
map.insert(0, 12934);
map.insert(123485, 1238888);
map.insert(123, 111111);
const OrderedHashMap<int, int> const_map = map;
const HashMap<int, int> const_map = map;
Vector<Pair<int, int>> expected;
expected.push_back(Pair<int, int>(42, 84));
expected.push_back(Pair<int, int>(123, 111111));
expected.push_back(Pair<int, int>(0, 12934));
expected.push_back(Pair<int, int>(123485, 1238888));
expected.push_back(Pair<int, int>(123, 111111));
int idx = 0;
for (OrderedHashMap<int, int>::ConstElement E = const_map.front(); E; E = E.next()) {
CHECK(expected[idx] == Pair<int, int>(E.key(), E.value()));
for (const KeyValue<int, int> &E : const_map) {
CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
++idx;
}
}
} // namespace TestOrderedHashMap
} // namespace TestHashMap
#endif // TEST_ORDERED_HASH_MAP_H
#endif // TEST_HASH_MAP_H