qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
unordered_map.h
Go to the documentation of this file.
1
25
26#ifndef QB_UNORDERED_MAP_H
27#define QB_UNORDERED_MAP_H
28#include <string>
29#include <map>
30#include <ska_hash/unordered_map.hpp>
31#include <unordered_map>
32
33namespace qb {
34
48template <typename K, typename V, typename H = std::hash<K>,
49 typename E = std::equal_to<K>,
50 typename A = std::allocator<std::pair<const K, V>>>
51using unordered_flat_map = ska::flat_hash_map<K, V, H, E, A>;
52
53#ifdef NDEBUG
68template <typename K, typename V, typename H = std::hash<K>,
69 typename E = std::equal_to<K>,
70 typename A = std::allocator<std::pair<const K, V>>>
71using unordered_map = ska::unordered_map<K, V, H, E, A>;
72#else
87template <typename K, typename V, typename H = std::hash<K>,
88 typename E = std::equal_to<K>,
89 typename A = std::allocator<std::pair<const K, V>>>
90using unordered_map = std::unordered_map<K, V, H, E, A>;
91#endif
92
106 constexpr static char
107 charToLower(const char c) {
108 return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
109 }
110
116 template <std::size_t N>
117 class const_str {
118 private:
119 const char s[N + 1]; // One extra byte to fill with a 0 value
120
121 public:
130 template <typename T, T... Nums>
131 constexpr const_str(const char (&str)[N], std::integer_sequence<T, Nums...>)
132 : s{charToLower(str[Nums])..., 0} {}
133
140 constexpr char
141 operator[](std::size_t i) const {
142 return s[i];
143 }
144
150 operator const char *() const {
151 return s;
152 }
153 };
154
162 template <std::size_t N>
163 constexpr static const_str<N>
164 toLower(const char (&str)[N]) {
165 return {str, std::make_integer_sequence<unsigned, N>()};
166 }
167
168public:
176 template <std::size_t N>
177 static std::string
178 convert(const char (&str)[N]) {
179 return std::string{toLower(str), N - 1};
180 }
181
188 static std::string
189 convert(std::string const &str) {
190 std::string copy;
191 copy.reserve(str.size());
192 std::for_each(str.cbegin(), str.cend(),
193 [&copy](const char &c) { copy += charToLower(c); });
194 return copy;
195 }
196
203 static std::string
204 convert(std::string_view const &str) {
205 std::string copy;
206 copy.reserve(str.size());
207 std::for_each(str.cbegin(), str.cend(),
208 [&copy](const char &c) { copy += charToLower(c); });
209 return copy;
210 }
211};
212
222template <typename _Map, typename _Trait = string_to_lower>
223class icase_basic_map : _Map {
224 using base_t = _Map;
225
226public:
228 icase_basic_map() = default;
229
232
234 icase_basic_map(icase_basic_map &&) noexcept = default;
235
241 icase_basic_map(std::initializer_list<typename _Map::value_type> il) {
242 for (auto &it : il)
243 emplace(it.first, std::move(it.second));
244 }
245
248
251
261 template <typename T, typename... _Args>
262 auto
263 emplace(T &&key, _Args &&...args) {
264 return static_cast<base_t &>(*this).emplace(
265 _Trait::convert(std::forward<T>(key)), std::forward<_Args>(args)...);
266 }
267
277 template <typename T, typename... _Args>
278 auto
279 try_emplace(T &&key, _Args &&...args) {
280 return static_cast<base_t &>(*this).try_emplace(
281 _Trait::convert(std::forward<T>(key)), std::forward<_Args>(args)...);
282 }
283
292 template <typename T>
293 auto &
294 at(T &&key) {
295 return static_cast<base_t &>(*this).at(_Trait::convert(std::forward<T>(key)));
296 }
297
306 template <typename T>
307 const auto &
308 at(T &&key) const {
309 return static_cast<base_t const &>(*this).at(
310 _Trait::convert(std::forward<T>(key)));
311 }
312
320 template <typename T>
321 auto &
322 operator[](T &&key) {
323 return static_cast<base_t &>(*this)[_Trait::convert(std::forward<T>(key))];
324 }
325
333 template <typename T>
334 auto
335 find(T &&key) {
336 return static_cast<base_t &>(*this).find(_Trait::convert(std::forward<T>(key)));
337 }
338
347 template <typename T>
348 auto
349 find(T &&key) const {
350 return static_cast<base_t const &>(*this).find(
351 _Trait::convert(std::forward<T>(key)));
352 }
353
361 template <typename T>
362 bool
363 has(T &&key) const {
364 return find(std::forward<T>(key)) != this->cend();
365 }
366
374 template <typename T>
375 auto
376 erase(T &&key) {
377 return static_cast<base_t &>(*this).erase(_Trait::convert(std::forward<T>(key)));
378 }
379
380 // Import methods from the base map
381 using base_t::begin;
382 using base_t::cbegin;
383 using base_t::cend;
384 using base_t::clear;
385 using base_t::end;
386 using base_t::erase;
387 using base_t::size;
388 using base_t::empty;
389
399 template <typename T>
400 static std::string
401 convert_key(T &&key) noexcept {
402 return _Trait::convert(std::forward<T>(key));
403 }
404};
405
412template <typename Value, typename _Trait = string_to_lower>
414
421template <typename Value, typename _Trait = string_to_lower>
424
425} // namespace qb
426
427#endif // QB_UNORDERED_MAP_H
A case-insensitive map implementation.
Definition unordered_map.h:223
const auto & at(T &&key) const
Access a value by key, with the key converted to lowercase (const version)
Definition unordered_map.h:308
icase_basic_map & operator=(icase_basic_map const &)=default
Copy assignment operator.
auto find(T &&key) const
Find a key-value pair by key, with the key converted to lowercase (const version)
Definition unordered_map.h:349
auto find(T &&key)
Find a key-value pair by key, with the key converted to lowercase.
Definition unordered_map.h:335
static std::string convert_key(T &&key) noexcept
Convert a key to lowercase.
Definition unordered_map.h:401
icase_basic_map(icase_basic_map &&) noexcept=default
Move constructor.
bool has(T &&key) const
Check if a key exists in the map.
Definition unordered_map.h:363
icase_basic_map(icase_basic_map const &)=default
Copy constructor.
auto try_emplace(T &&key, _Args &&...args)
Try to emplace a new key-value pair with the key converted to lowercase.
Definition unordered_map.h:279
auto erase(T &&key)
Erase a key-value pair by key, with the key converted to lowercase.
Definition unordered_map.h:376
icase_basic_map()=default
Default constructor.
auto emplace(T &&key, _Args &&...args)
Emplace a new key-value pair with the key converted to lowercase.
Definition unordered_map.h:263
icase_basic_map & operator=(icase_basic_map &&) noexcept=default
Move assignment operator.
auto & operator[](T &&key)
Access or insert a value by key, with the key converted to lowercase.
Definition unordered_map.h:322
auto & at(T &&key)
Access a value by key, with the key converted to lowercase.
Definition unordered_map.h:294
Utility class for case-insensitive string operations.
Definition unordered_map.h:99
static std::string convert(const char(&str)[N])
Converts a character array to lowercase.
Definition unordered_map.h:178
static std::string convert(std::string const &str)
Converts a string to lowercase.
Definition unordered_map.h:189
static std::string convert(std::string_view const &str)
Converts a string_view to lowercase.
Definition unordered_map.h:204
std::unordered_map< K, V, H, E, A > unordered_map
The primary unordered map implementation.
Definition unordered_map.h:90
icase_basic_map< std::map< std::string, Value >, _Trait > icase_map
Case-insensitive ordered map using std::map.
Definition unordered_map.h:413
icase_basic_map< qb::unordered_map< std::string, Value >, _Trait > icase_unordered_map
Case-insensitive unordered map using qb::unordered_map.
Definition unordered_map.h:422
ska::flat_hash_map< K, V, H, E, A > unordered_flat_map
A high-performance flat hash map implementation.
Definition unordered_map.h:51