[core] Switch to ankerl::unordered_dense

Signed-off-by: x7z4w <x7z4w@noreply.codeberg.org>
This commit is contained in:
x7z4w
2025-11-24 17:34:56 +00:00
parent 03132c6877
commit ef6522ed28
282 changed files with 4386 additions and 1456 deletions

View File

@@ -5,7 +5,8 @@
#include <map>
#include <string>
#include <unordered_map>
#include "3party/ankerl/unordered_dense.h"
using namespace base;
using namespace std;

View File

@@ -4,7 +4,8 @@
#include <list>
#include <set>
#include <unordered_map>
#include "3party/ankerl/unordered_dense.h"
#ifdef __clang__
#pragma clang diagnostic push
@@ -24,7 +25,7 @@ public:
FifoCacheTest(size_t capacity, typename FifoCache<Key, Value>::Loader const & loader) : m_cache(capacity, loader) {}
Value const & GetValue(Key const & key) { return m_cache.GetValue(key); }
unordered_map<Key, Value> const & GetMap() const { return m_cache.m_map; }
ankerl::unordered_dense::map<Key, Value> const & GetMap() const { return m_cache.m_map; }
boost::circular_buffer<Key> const & GetFifo() const { return m_cache.m_fifo; }
bool IsValid() const
@@ -67,7 +68,7 @@ UNIT_TEST(FifoCache)
TEST_EQUAL(cache.GetValue(2), 2, ());
TEST(cache.IsValid(), ());
{
unordered_map<Key, Value> expectedMap({{1 /* key */, 1 /* value */}, {2, 2}, {3, 3}});
ankerl::unordered_dense::map<Key, Value> expectedMap({{1 /* key */, 1 /* value */}, {2, 2}, {3, 3}});
TEST_EQUAL(cache.GetMap(), expectedMap, ());
list<Key> expectedList({2, 3, 1});
boost::circular_buffer<Key> expectedCB(expectedList.cbegin(), expectedList.cend());
@@ -77,7 +78,7 @@ UNIT_TEST(FifoCache)
TEST_EQUAL(cache.GetValue(7), 7, ());
TEST(cache.IsValid(), ());
{
unordered_map<Key, Value> expectedMap({{7 /* key */, 7 /* value */}, {2, 2}, {3, 3}});
ankerl::unordered_dense::map<Key, Value> expectedMap({{7 /* key */, 7 /* value */}, {2, 2}, {3, 3}});
TEST_EQUAL(cache.GetMap(), expectedMap, ());
list<Key> expectedList({7, 2, 3});
boost::circular_buffer<Key> expectedCB(expectedList.cbegin(), expectedList.cend());

View File

@@ -41,7 +41,7 @@ public:
size_t GetAge() const { return m_keyAge.m_age; }
std::map<size_t, Key> const & GetAgeToKey() const { return m_keyAge.m_ageToKey; }
std::unordered_map<Key, size_t> const & GetKeyToAge() const { return m_keyAge.m_keyToAge; }
ankerl::unordered_dense::map<Key, size_t> const & GetKeyToAge() const { return m_keyAge.m_keyToAge; }
private:
typename LruCache<Key, Value>::KeyAge m_keyAge;
@@ -49,7 +49,8 @@ private:
template <typename Key, typename Value>
void TestAge(LruCacheKeyAgeTest<Key, Value> const & keyAge, size_t expectedAge,
std::map<size_t, Key> const & expectedAgeToKey, std::unordered_map<Key, size_t> const & expectedKeyToAge)
std::map<size_t, Key> const & expectedAgeToKey,
ankerl::unordered_dense::map<Key, size_t> const & expectedKeyToAge)
{
TEST(keyAge.IsValid(), ());
TEST_EQUAL(keyAge.GetAge(), expectedAge, ());
@@ -70,49 +71,49 @@ UNIT_TEST(LruCacheAgeTest)
age.InsertKey(10);
{
std::map<size_t, Key> const expectedAgeToKey({{1 /* age */, 10 /* key */}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{10 /* key */, 1 /* age */}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{10 /* key */, 1 /* age */}});
TestAge(age, 1 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.InsertKey(9);
{
std::map<size_t, Key> const expectedAgeToKey({{1, 10}, {2, 9}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{10, 1}, {9, 2}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{10, 1}, {9, 2}});
TestAge(age, 2 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.RemoveLru();
{
std::map<size_t, Key> const expectedAgeToKey({{2, 9}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 2}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{9, 2}});
TestAge(age, 2 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.InsertKey(11);
{
std::map<size_t, Key> const expectedAgeToKey({{2, 9}, {3, 11}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 2}, {11, 3}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{9, 2}, {11, 3}});
TestAge(age, 3 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.UpdateAge(9);
{
std::map<size_t, Key> const expectedAgeToKey({{4, 9}, {3, 11}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 4}, {11, 3}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{9, 4}, {11, 3}});
TestAge(age, 4 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.RemoveLru();
{
std::map<size_t, Key> const expectedAgeToKey({{4, 9}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 4}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{9, 4}});
TestAge(age, 4 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
age.InsertKey(12);
{
std::map<size_t, Key> const expectedAgeToKey({{4, 9}, {5, 12}});
std::unordered_map<Key, size_t> const expectedKeyToAge({{9, 4}, {12, 5}});
ankerl::unordered_dense::map<Key, size_t> const expectedKeyToAge({{9, 4}, {12, 5}});
TestAge(age, 5 /* cache age */, expectedAgeToKey, expectedKeyToAge);
}
}

View File

@@ -7,9 +7,10 @@
#include <algorithm>
#include <iterator>
#include <random>
#include <unordered_map>
#include <vector>
#include "3party/ankerl/unordered_dense.h"
namespace small_set_test
{
using namespace base;
@@ -96,7 +97,7 @@ UNIT_TEST(SmallMap_Benchmark1)
{
// 1. Init maps.
// Dataset is similar to routing::VehicleModel.
std::unordered_map<uint32_t, bool> uMap = {
ankerl::unordered_dense::map<uint32_t, bool> uMap = {
{1, true}, {2, false}, {4, false}, {6, true}, {7, true}, {8, true}, {12, false},
{15, false}, {26, true}, {30, false}, {36, false}, {43, false}, {54, false}, {57, true},
{58, true}, {65, true}, {69, true}, {90, true}, {95, false}, {119, false}, {167, true},
@@ -138,7 +139,7 @@ UNIT_TEST(SmallMap_Benchmark2)
uint32_t i = 0;
// Dataset is similar to routing::VehicleModelFactory.
unordered_map<string, shared_ptr<int>> uMap = {
ankerl::unordered_dense::map<string, shared_ptr<int>> uMap = {
{"", make_shared<int>(i++)},
{"Australia", make_shared<int>(i++)},
{"Austria", make_shared<int>(i++)},
@@ -217,7 +218,7 @@ UNIT_TEST(SmallMap_Benchmark2)
UNIT_TEST(SmallMap_Benchmark3)
{
// Dataset is similar to routing::VehicleModel.m_surfaceFactors.
std::unordered_map<int, int> uMap = {
ankerl::unordered_dense::map<int, int> uMap = {
{1, 0}, {10, 1}, {100, 2}, {1000, 3},
};

View File

@@ -7,11 +7,11 @@
#include <cmath>
#include <fstream>
#include <limits>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <sstream>
#include "3party/ankerl/unordered_dense.h"
/// internal function in base
namespace strings
@@ -28,7 +28,7 @@ UNIT_TEST(LowerUniChar)
TEST(file.is_open(), (kFile));
size_t fCount = 0, cCount = 0;
std::unordered_map<strings::UniChar, strings::UniString> m;
ankerl::unordered_dense::map<strings::UniChar, strings::UniString> m;
std::string line;
while (file.good())
{

View File

@@ -1,14 +1,16 @@
#pragma once
#include <cstddef>
#include <unordered_map>
#include "3party/ankerl/unordered_dense.h"
namespace base
{
// A bidirectional map to store a one-to-one correspondence between
// keys and values.
template <typename K, typename V, template <typename...> typename KToVMap = std::unordered_map,
typename KToVHashOrComparator = std::hash<K>, template <typename...> typename VToKMap = std::unordered_map,
template <typename K, typename V, template <typename...> typename KToVMap = ankerl::unordered_dense::map,
typename KToVHashOrComparator = std::hash<K>,
template <typename...> typename VToKMap = ankerl::unordered_dense::map,
typename VToKHashOrComparator = std::hash<V>>
class BidirectionalMap
{

View File

@@ -4,10 +4,11 @@
#include <cstddef>
#include <iterator>
#include <unordered_map>
#include <utility>
#include <vector>
#include "3party/ankerl/unordered_dense.h"
namespace base
{
// Maps keys to lists of values, but allows to combine keys into
@@ -155,6 +156,6 @@ private:
cv.shrink_to_fit();
}
std::unordered_map<Key, Entry, Hash> m_table;
ankerl::unordered_dense::map<Key, Entry, Hash> m_table;
};
} // namespace base

View File

@@ -4,7 +4,8 @@
#include <cstddef>
#include <functional>
#include <unordered_map>
#include "3party/ankerl/unordered_dense.h"
#ifdef __clang__
#pragma clang diagnostic push
@@ -15,7 +16,7 @@
#pragma clang diagnostic pop
#endif
template <class Key, class Value, class HashContainer = std::unordered_map<Key, Value>>
template <class Key, class Value, class HashContainer = ankerl::unordered_dense::map<Key, Value>>
class FifoCache
{
template <typename K, typename V>

View File

@@ -15,11 +15,11 @@
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "3party/ankerl/unordered_dense.h"
/// @name Declarations.
//@{
template <typename T>
@@ -65,9 +65,9 @@ template <typename T>
inline std::string DebugPrint(std::unique_ptr<T> const & v);
template <class Key, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>>
inline std::string DebugPrint(std::unordered_set<Key, Hash, Pred> const & v);
inline std::string DebugPrint(ankerl::unordered_dense::set<Key, Hash, Pred> const & v);
template <class Key, class T, class Hash = std::hash<Key>, class Pred = std::equal_to<Key>>
inline std::string DebugPrint(std::unordered_map<Key, T, Hash, Pred> const & v);
inline std::string DebugPrint(ankerl::unordered_dense::map<Key, T, Hash, Pred> const & v);
//@}
template <typename T>
@@ -234,13 +234,13 @@ inline std::string DebugPrint(std::initializer_list<T> const & v)
}
template <class Key, class Hash, class Pred>
inline std::string DebugPrint(std::unordered_set<Key, Hash, Pred> const & v)
inline std::string DebugPrint(ankerl::unordered_dense::set<Key, Hash, Pred> const & v)
{
return DebugPrintSequence(v.begin(), v.end());
}
template <class Key, class T, class Hash, class Pred>
inline std::string DebugPrint(std::unordered_map<Key, T, Hash, Pred> const & v)
inline std::string DebugPrint(ankerl::unordered_dense::map<Key, T, Hash, Pred> const & v)
{
return DebugPrintSequence(v.begin(), v.end());
}

View File

@@ -190,8 +190,8 @@ void LevenshteinDFA::State::Normalize()
// LevenshteinDFA ----------------------------------------------------------------------------------
// static
LevenshteinDFA::LevenshteinDFA(UniString const & s, size_t prefixSize, std::array<UniString, 11> const & prefixMisprints,
size_t maxErrors)
LevenshteinDFA::LevenshteinDFA(UniString const & s, size_t prefixSize,
std::array<UniString, 11> const & prefixMisprints, size_t maxErrors)
: m_size(s.size())
, m_maxErrors(maxErrors)
{

View File

@@ -3,11 +3,12 @@
#include "base/assert.hpp"
#include <list>
#include <unordered_map>
#include "3party/ankerl/unordered_dense.h"
namespace base
{
template <typename Key, typename Value, template <typename...> typename Map = std::unordered_map>
template <typename Key, typename Value, template <typename...> typename Map = ankerl::unordered_dense::map>
class LinkedMap
{
public:

View File

@@ -5,7 +5,8 @@
#include <cstddef>
#include <functional>
#include <map>
#include <unordered_map>
#include "3party/ankerl/unordered_dense.h"
/// \brief Implementation of cache with least recently used replacement policy.
template <typename Key, typename Value>
@@ -164,10 +165,10 @@ private:
private:
size_t m_age = 0;
std::map<size_t, Key> m_ageToKey;
std::unordered_map<Key, size_t> m_keyToAge;
ankerl::unordered_dense::map<Key, size_t> m_keyToAge;
};
size_t const m_maxCacheSize;
std::unordered_map<Key, Value> m_cache;
ankerl::unordered_dense::map<Key, Value> m_cache;
KeyAge m_keyAge;
};

View File

@@ -5,9 +5,10 @@
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "3party/ankerl/unordered_dense.h"
namespace base
{
@@ -79,7 +80,7 @@ public:
template <class Key>
class TopStatsCounter
{
std::unordered_map<Key, size_t> m_data;
ankerl::unordered_dense::map<Key, size_t> m_data;
public:
void Add(Key const & key) { ++m_data[key]; }
@@ -88,7 +89,7 @@ public:
{
ASSERT(count > 0, ());
using PtrT = std::pair<Key const, size_t> const *;
using PtrT = typename ankerl::unordered_dense::map<Key, size_t>::value_type const *;
struct GreaterNumber
{
bool operator()(PtrT l, PtrT r) const { return l->second > r->second; }

View File

@@ -2,9 +2,10 @@
#include <array>
#include <map>
#include <unordered_map>
#include <vector>
#include "3party/ankerl/unordered_dense.h"
/// @name std containers serialization
/// TArchive should be an archive class in global namespace.
//@{
@@ -130,14 +131,14 @@ TArchive & operator>>(TArchive & ar, std::multimap<T1, T2> & rMap)
}
template <class TArchive, class T1, class T2>
TArchive & operator<<(TArchive & ar, std::unordered_map<T1, T2> const & rMap)
TArchive & operator<<(TArchive & ar, ankerl::unordered_dense::map<T1, T2> const & rMap)
{
save_like_map(ar, rMap);
return ar;
}
template <class TArchive, class T1, class T2>
TArchive & operator>>(TArchive & ar, std::unordered_map<T1, T2> & rMap)
TArchive & operator>>(TArchive & ar, ankerl::unordered_dense::map<T1, T2> & rMap)
{
load_like_map(ar, rMap);
return ar;

View File

@@ -118,10 +118,8 @@ size_t CountChar(Utf8 const & utf8)
size_t codePoints = 0;
for (auto const c : utf8)
{
if ((c & 0xC0) != 0x80)
++codePoints;
}
return codePoints;
}
@@ -175,7 +173,7 @@ bool IsASCIILatin(UniChar c);
/// Escape characters not allowed in XML
template <typename T>
std::string EscapeForXML(const T & in)
std::string EscapeForXML(T const & in)
{
std::string result;
result.reserve(in.size());
@@ -184,12 +182,12 @@ std::string EscapeForXML(const T & in)
{
switch (c)
{
case '&': result.append("&amp;"); break;
case '<': result.append("&lt;"); break;
case '>': result.append("&gt;"); break;
case '"': result.append("&quot;"); break;
case '\'': result.append("&apos;"); break;
default: result.append(1, c); break;
case '&': result.append("&amp;"); break;
case '<': result.append("&lt;"); break;
case '>': result.append("&gt;"); break;
case '"': result.append("&quot;"); break;
case '\'': result.append("&apos;"); break;
default: result.append(1, c); break;
}
}

View File

@@ -10,9 +10,10 @@
#include <condition_variable>
#include <map>
#include <memory>
#include <unordered_map>
#include <vector>
#include "3party/ankerl/unordered_dense.h"
namespace base
{
// This class represents a simple thread pool with a queue of tasks.
@@ -119,7 +120,7 @@ private:
using DelayedValue = std::shared_ptr<DelayedTask>;
class DelayedQueue
: public BidirectionalMap<TaskId, DelayedValue, std::unordered_map, std::hash<TaskId>, std::multimap,
: public BidirectionalMap<TaskId, DelayedValue, ankerl::unordered_dense::map, std::hash<TaskId>, std::multimap,
DeRef<DelayedValue>>
{
public: