roo_collections
API Documentation for roo_collections
Loading...
Searching...
No Matches
flat_small_hash_map.h
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Flat, memory-conscious hash map built on FlatSmallHashtable.
5/// @ingroup roo_collections
6
7#include <functional>
8
10
11namespace roo_collections {
12
13template <typename Key, typename Value>
14struct MapKeyFn {
15 const Key& operator()(const std::pair<Key, Value>& entry) const {
16 return entry.first;
17 }
18};
19
20template <typename Key, typename T>
21struct KeyCovert {
22 Key operator()(const T& val) const { return Key(val); }
23};
24
25template <>
26struct KeyCovert<std::string, ::roo::string_view> {
27 std::string operator()(const ::roo::string_view& val) const {
28 return std::string(val.data(), val.size());
29 }
30};
31
32/// @brief Flat, memory-conscious hash map optimized for small collections.
33///
34/// Uses `FlatSmallHashtable` as the underlying storage and provides a map-like
35/// interface over key-value pairs.
36///
37/// @tparam Key Key type.
38/// @tparam Value Mapped value type.
39/// @tparam HashFn Hash function type.
40/// @tparam KeyCmpFn Key equality predicate type.
41template <typename Key, typename Value, typename HashFn = DefaultHashFn<Key>,
42 typename KeyCmpFn = std::equal_to<Key>>
44 : public FlatSmallHashtable<std::pair<Key, Value>, Key, HashFn,
45 MapKeyFn<Key, Value>, KeyCmpFn> {
46 public:
47 using mapped_type = Value;
48
50 MapKeyFn<Key, Value>, KeyCmpFn>;
51
52 using key_type = typename Base::key_type;
53 using value_type = typename Base::value_type;
54 using hasher = typename Base::hasher;
55 using key_equal = typename Base::key_equal;
56 using iterator = typename Base::iterator;
58
59 /// @brief Creates an empty hash map.
60 FlatSmallHashMap(HashFn hash_fn = HashFn(), KeyCmpFn key_cmp_fn = KeyCmpFn())
61 : Base(hash_fn, MapKeyFn<Key, Value>(), key_cmp_fn) {}
62
63 /// @brief Creates a hash map with capacity for approximately `size_hint`
64 /// elements without rehashing.
65 /// @param size_hint Expected number of inserted items.
66 FlatSmallHashMap(uint16_t size_hint, HashFn hash_fn = HashFn(),
67 KeyCmpFn key_cmp_fn = KeyCmpFn())
68 : Base(size_hint, hash_fn, MapKeyFn<Key, Value>(), key_cmp_fn) {}
69
70 /// @brief Builds a map from an iterator range.
71 template <typename InputIt>
72 FlatSmallHashMap(InputIt first, InputIt last, HashFn hash_fn = HashFn(),
73 KeyCmpFn key_cmp_fn = KeyCmpFn())
74 : Base(first, last, hash_fn, MapKeyFn<Key, Value>(), key_cmp_fn) {}
75
76 /// @brief Builds a map from an initializer list.
77 FlatSmallHashMap(std::initializer_list<value_type> init,
78 HashFn hash_fn = HashFn(), KeyCmpFn key_cmp_fn = KeyCmpFn())
79 : Base(init, hash_fn, MapKeyFn<Key, Value>(), key_cmp_fn) {}
80
81 /// @brief Copy constructor.
82 FlatSmallHashMap(const FlatSmallHashMap& other) : Base(other) {}
83
84 /// @brief Returns a const reference to the mapped value for `key`.
85 ///
86 /// Asserts in debug builds if `key` is not present.
87 const Value& at(const Key& key) const {
88 auto it = this->find(key);
89 assert(it != this->end());
90 return (*it).second;
91 }
92
93 /// @brief Returns a const reference to the mapped value for `key`.
94 ///
95 /// Heterogeneous overload enabled for transparent hash/equality functions.
96 template <typename K, typename = has_is_transparent_t<HashFn, K>,
97 typename = has_is_transparent_t<KeyCmpFn, K>>
98 const Value& at(const K& key) const {
99 auto it = this->find(key);
100 assert(it != this->end());
101 return (*it).second;
102 }
103
104 /// @brief Returns a mutable reference to the mapped value for `key`.
105 ///
106 /// Asserts in debug builds if `key` is not present.
107 Value& at(const Key& key) {
108 auto it = this->lookup(key);
109 assert(it != this->end());
110 return (*it).second;
111 }
112
113 /// @brief Returns a mutable reference to the mapped value for `key`.
114 ///
115 /// Heterogeneous overload enabled for transparent hash/equality functions.
116 template <typename K, typename = has_is_transparent_t<HashFn, K>,
117 typename = has_is_transparent_t<KeyCmpFn, K>>
118 Value& at(const K& key) {
119 auto it = this->lookup(key);
120 assert(it != this->end());
121 return (*it).second;
122 }
123
124 /// @brief Finds `key` and returns an iterator to the entry, or `end()`.
125 typename Base::Iterator find(const Key& key) { return Base::lookup(key); }
126
127 /// @brief Heterogeneous lookup overload of `find`.
128 template <typename K, typename = has_is_transparent_t<HashFn, K>,
129 typename = has_is_transparent_t<KeyCmpFn, K>>
130 typename Base::Iterator find(const K& key) {
131 return Base::lookup(key);
132 }
133
134 /// @brief Finds `key` and returns a const iterator to the entry, or
135 /// `end()`.
136 typename Base::ConstIterator find(const Key& key) const {
137 return Base::find(key);
138 }
139
140 /// @brief Heterogeneous const lookup overload of `find`.
141 template <typename K, typename = has_is_transparent_t<HashFn, K>,
142 typename = has_is_transparent_t<KeyCmpFn, K>>
143 typename Base::ConstIterator find(const K& key) const {
144 return Base::find(key);
145 }
146
147 /// @brief Returns a const reference to the value for `key`.
148 const Value& operator[](const Key& key) const { return at(key); }
149
150 /// @brief Heterogeneous const lookup overload of `operator[]`.
151 template <typename K, typename = has_is_transparent_t<HashFn, K>,
152 typename = has_is_transparent_t<KeyCmpFn, K>>
153 const Value& operator[](const K& key) const {
154 return at(key);
155 }
156
157 /// @brief Returns a mutable reference to the value for `key`.
158 ///
159 /// Inserts a default-constructed value when `key` is not present.
160 Value& operator[](const Key& key) {
161 auto it = this->lookup(key);
162 if (it == this->end()) {
163 return (*this->insert(std::make_pair(key, Value())).first).second;
164 } else {
165 return (*it).second;
166 }
167 }
168
169 /// @brief Heterogeneous mutable overload of `operator[]`.
170 ///
171 /// Inserts a default-constructed value when `key` is not present.
172 template <typename K, typename = has_is_transparent_t<HashFn, K>,
173 typename = has_is_transparent_t<KeyCmpFn, K>>
174 Value& operator[](const K& key) {
175 auto it = this->lookup(key);
176 if (it == this->end()) {
177 return (*this->insert(std::make_pair(KeyCovert<Key, K>()(key), Value()))
178 .first)
179 .second;
180 } else {
181 return (*it).second;
182 }
183 }
184};
185
186/// @brief String-specialized flat hash map with heterogeneous lookup support.
187///
188/// Accepts `std::string`, `const char*`, `roo::string_view`, and Arduino
189/// `String` (when available) for lookup operations.
190template <typename Value>
192 FlatSmallHashMap<std::string, Value, TransparentStringHashFn,
194
195} // namespace roo_collections
Flat, memory-conscious hash map optimized for small collections.
FlatSmallHashMap(const FlatSmallHashMap &other)
Copy constructor.
typename Base::value_type value_type
Value & at(const K &key)
Returns a mutable reference to the mapped value for key.
FlatSmallHashMap(HashFn hash_fn=HashFn(), KeyCmpFn key_cmp_fn=KeyCmpFn())
Creates an empty hash map.
FlatSmallHashMap(InputIt first, InputIt last, HashFn hash_fn=HashFn(), KeyCmpFn key_cmp_fn=KeyCmpFn())
Builds a map from an iterator range.
Value & operator[](const K &key)
Heterogeneous mutable overload of operator[].
const Value & operator[](const Key &key) const
Returns a const reference to the value for key.
const Value & at(const Key &key) const
Returns a const reference to the mapped value for key.
Base::ConstIterator find(const Key &key) const
Finds key and returns a const iterator to the entry, or end().
typename Base::const_iterator const_iterator
const Value & operator[](const K &key) const
Heterogeneous const lookup overload of operator[].
Base::ConstIterator find(const K &key) const
Heterogeneous const lookup overload of find.
Base::Iterator find(const Key &key)
Finds key and returns an iterator to the entry, or end().
Base::Iterator find(const K &key)
Heterogeneous lookup overload of find.
Value & at(const Key &key)
Returns a mutable reference to the mapped value for key.
FlatSmallHashMap(std::initializer_list< value_type > init, HashFn hash_fn=HashFn(), KeyCmpFn key_cmp_fn=KeyCmpFn())
Builds a map from an initializer list.
const Value & at(const K &key) const
Returns a const reference to the mapped value for key.
Value & operator[](const Key &key)
Returns a mutable reference to the value for key.
FlatSmallHashMap(uint16_t size_hint, HashFn hash_fn=HashFn(), KeyCmpFn key_cmp_fn=KeyCmpFn())
Creates a hash map with capacity for approximately size_hint elements without rehashing.
Flat, memory-conscious hash table optimized for small collections.
std::pair< Iterator, bool > insert(Entry val)
Inserts val if key is not present.
Iterator end()
Returns iterator past the end.
ConstIterator find(const Key &key) const
Finds key and returns a const iterator to the matching entry.
Flat, memory-conscious hash table implementation.
std::string operator()(const ::roo::string_view &val) const
Key operator()(const T &val) const
const Key & operator()(const std::pair< Key, Value > &entry) const