| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <cctype> |
| 8 |
#include <cstdint> |
| 9 |
#include <optional> |
| 10 |
#include <string_view> |
| 11 |
#include <utility> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
namespace Common::GekkoAssembler::detail |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
template <typename V, char... ExtraMatches> |
| 20 |
class CaseInsensitiveDict |
| 21 |
{ |
| 22 |
public: |
| 23 |
CaseInsensitiveDict(const std::initializer_list<std::pair<std::string_view, V>>& il) |
| 24 |
{ |
| 25 |
for (auto&& [k, v] : il) |
| 26 |
{ |
| 27 |
Add(k, v); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
template <typename T> |
| 32 |
V const* Find(const T& key) const |
| 33 |
{ |
| 34 |
auto&& [last_e, it] = TryFind(key); |
| 35 |
if (it == key.cend() && last_e->_val) |
| 36 |
{ |
| 37 |
return &*last_e->_val; |
| 38 |
} |
| 39 |
return nullptr; |
| 40 |
} |
| 41 |
static constexpr size_t NUM_CONNS = 36 + sizeof...(ExtraMatches); |
| 42 |
static constexpr uint32_t INVALID_CONN = static_cast<uint32_t>(-1); |
| 43 |
|
| 44 |
private: |
| 45 |
struct TrieEntry |
| 46 |
{ |
| 47 |
std::array<uint32_t, 36 + sizeof...(ExtraMatches)> _conns; |
| 48 |
std::optional<V> _val; |
| 49 |
|
| 50 |
TrieEntry() { _conns.fill(INVALID_CONN); } |
| 51 |
}; |
| 52 |
|
| 53 |
constexpr size_t IndexOf(char c) const |
| 54 |
{ |
| 55 |
size_t idx; |
| 56 |
if (std::isalpha(c)) |
| 57 |
{ |
| 58 |
idx = std::tolower(c) - 'a'; |
| 59 |
} |
| 60 |
else if (std::isdigit(c)) |
| 61 |
{ |
| 62 |
idx = c - '0' + 26; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
idx = 36; |
| 67 |
|
| 68 |
if constexpr (sizeof...(ExtraMatches) > 0) |
| 69 |
{ |
| 70 |
(void)((c != ExtraMatches ? ++idx, true : false) && ...); |
| 71 |
} |
| 72 |
} |
| 73 |
return idx; |
| 74 |
} |
| 75 |
|
| 76 |
template <typename T> |
| 77 |
auto TryFind(const T& key) const -> std::pair<TrieEntry const*, decltype(key.cbegin())> |
| 78 |
{ |
| 79 |
std::pair<TrieEntry const*, decltype(key.cbegin())> ret(&m_root_entry, key.cbegin()); |
| 80 |
const auto k_end = key.cend(); |
| 81 |
|
| 82 |
for (; ret.second != k_end; ret.second++) |
| 83 |
{ |
| 84 |
const size_t idx = IndexOf(*ret.second); |
| 85 |
if (idx >= NUM_CONNS || ret.first->_conns[idx] == INVALID_CONN) |
| 86 |
{ |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
ret.first = &m_entry_pool[ret.first->_conns[idx]]; |
| 91 |
} |
| 92 |
|
| 93 |
return ret; |
| 94 |
} |
| 95 |
|
| 96 |
template <typename T> |
| 97 |
auto TryFind(const T& key) -> std::pair<TrieEntry*, decltype(key.cbegin())> |
| 98 |
{ |
| 99 |
auto&& [e_const, it] = |
| 100 |
const_cast<CaseInsensitiveDict<V, ExtraMatches...> const*>(this)->TryFind(key); |
| 101 |
return {const_cast<TrieEntry*>(e_const), it}; |
| 102 |
} |
| 103 |
|
| 104 |
void Add(std::string_view key, const V& val) |
| 105 |
{ |
| 106 |
auto&& [last_e, it] = TryFind(key); |
| 107 |
if (it != key.cend()) |
| 108 |
{ |
| 109 |
for (; it != key.cend(); it++) |
| 110 |
{ |
| 111 |
const size_t idx = IndexOf(*it); |
| 112 |
if (idx >= NUM_CONNS) |
| 113 |
{ |
| 114 |
break; |
| 115 |
} |
| 116 |
last_e->_conns[idx] = static_cast<uint32_t>(m_entry_pool.size()); |
| 117 |
last_e = &m_entry_pool.emplace_back(); |
| 118 |
} |
| 119 |
} |
| 120 |
last_e->_val = val; |
| 121 |
} |
| 122 |
|
| 123 |
TrieEntry m_root_entry; |
| 124 |
std::vector<TrieEntry> m_entry_pool; |
| 125 |
}; |
| 126 |
} |