22
33#include < algorithm>
44#include < cstdint>
5+ #include < cstdlib>
56#include < cstring>
67#include < functional>
78#include < limits>
@@ -36,7 +37,7 @@ namespace bee {
3637 key_type key;
3738 mapped_type obj;
3839 uint8_t dib;
39- flatmap_bucket_kv (const key_type & key, mapped_type && obj, uint8_t dib) noexcept
40+ flatmap_bucket_kv (const key_type& key, mapped_type&& obj, uint8_t dib) noexcept
4041 : key(key)
4142 , obj(std::forward<mapped_type>(obj))
4243 , dib(dib) {
@@ -47,7 +48,7 @@ namespace bee {
4748 struct flatmap_bucket_kv <key_type, void > {
4849 key_type key;
4950 uint8_t dib;
50- flatmap_bucket_kv (const key_type & key, uint8_t dib) noexcept
51+ flatmap_bucket_kv (const key_type& key, uint8_t dib) noexcept
5152 : key(key)
5253 , dib(dib) {
5354 }
@@ -58,7 +59,7 @@ namespace bee {
5859 mapped_type obj;
5960 key_type key;
6061 uint8_t dib;
61- flatmap_bucket_vk (const key_type & key, mapped_type && obj, uint8_t dib) noexcept
62+ flatmap_bucket_vk (const key_type& key, mapped_type&& obj, uint8_t dib) noexcept
6263 : obj(std::forward<mapped_type>(obj))
6364 , key(key)
6465 , dib(dib) {
@@ -69,7 +70,7 @@ namespace bee {
6970 struct flatmap_bucket_vk <key_type, void > {
7071 key_type key;
7172 uint8_t dib;
72- flatmap_bucket_vk (const key_type & key, uint8_t dib) noexcept
73+ flatmap_bucket_vk (const key_type& key, uint8_t dib) noexcept
7374 : key(key)
7475 , dib(dib) {
7576 }
@@ -94,14 +95,14 @@ namespace bee {
9495 static_assert (sizeof (bucket) <= 3 * sizeof (size_t ));
9596
9697 flatmap () noexcept = default ;
97- flatmap (flatmap && rhs) noexcept {
98+ flatmap (flatmap&& rhs) noexcept {
9899 std::swap (m_mask, rhs.m_mask );
99100 std::swap (m_maxsize, rhs.m_maxsize );
100101 std::swap (m_size, rhs.m_size );
101102 std::swap (m_buckets, rhs.m_buckets );
102103 }
103104
104- flatmap & operator =(flatmap && rhs) noexcept {
105+ flatmap& operator =(flatmap&& rhs) noexcept {
105106 if (this != &rhs) {
106107 std::swap (m_mask, rhs.m_mask );
107108 std::swap (m_maxsize, rhs.m_maxsize );
@@ -111,15 +112,15 @@ namespace bee {
111112 return *this ;
112113 }
113114
114- flatmap (const flatmap &) = delete ;
115- flatmap & operator =(const flatmap &) = delete ;
115+ flatmap (const flatmap&) = delete ;
116+ flatmap& operator =(const flatmap&) = delete ;
116117
117118 ~flatmap () noexcept {
118119 clear ();
119120 }
120121
121- std::conditional_t <std::is_same_v<mapped_type, void >, bool , std::tuple<bool , mapped_type *>>
122- find_or_insert (const key_type & key) {
122+ std::conditional_t <std::is_same_v<mapped_type, void >, bool , std::tuple<bool , mapped_type*>>
123+ find_or_insert (const key_type& key) {
123124 if (m_size >= m_maxsize) {
124125 increase_size ();
125126 }
@@ -161,7 +162,7 @@ namespace bee {
161162 }
162163
163164 template <typename MappedType>
164- bool insert (const key_type & key, MappedType obj) {
165+ bool insert (const key_type& key, MappedType obj) {
165166 if constexpr (std::is_same_v<mapped_type, void >) {
166167 return !find_or_insert (key);
167168 } else {
@@ -174,7 +175,7 @@ namespace bee {
174175 }
175176
176177 template <typename MappedType>
177- void insert_or_assign (const key_type & key, MappedType obj) {
178+ void insert_or_assign (const key_type& key, MappedType obj) {
178179 auto [found, value] = find_or_insert (key);
179180 if constexpr (!std::is_trivially_destructible_v<mapped_type>) {
180181 if (found) {
@@ -184,7 +185,7 @@ namespace bee {
184185 new (value) mapped_type { std::forward<mapped_type>(obj) };
185186 }
186187
187- [[nodiscard]] bool contains (const key_type & key) const noexcept {
188+ [[nodiscard]] bool contains (const key_type& key) const noexcept {
188189 auto slot = find_key (key);
189190 return slot != kInvalidSlot ;
190191 }
@@ -197,19 +198,19 @@ namespace bee {
197198 return size () == 0 ;
198199 }
199200
200- [[nodiscard]] mapped_type * find (const key_type & key) noexcept {
201+ [[nodiscard]] mapped_type* find (const key_type& key) noexcept {
201202 auto slot = find_key (key);
202203 if (slot == kInvalidSlot ) {
203204 return nullptr ;
204205 }
205206 return &m_buckets[slot].obj ;
206207 }
207208
208- [[nodiscard]] const mapped_type * find (const key_type & key) const noexcept {
209- return const_cast <flatmap *>(this )->find (key);
209+ [[nodiscard]] const mapped_type* find (const key_type& key) const noexcept {
210+ return const_cast <flatmap*>(this )->find (key);
210211 }
211212
212- void erase (const key_type & key) noexcept {
213+ void erase (const key_type& key) noexcept {
213214 auto slot = find_key (key);
214215 if (slot == kInvalidSlot ) {
215216 return ;
@@ -254,7 +255,7 @@ namespace bee {
254255 m_mask = 0 ;
255256 m_maxsize = 0 ;
256257 m_size = 0 ;
257- m_buckets = reinterpret_cast <bucket *>(&m_mask);
258+ m_buckets = reinterpret_cast <bucket*>(&m_mask);
258259 }
259260
260261 void rehash (size_t c) {
@@ -266,9 +267,9 @@ namespace bee {
266267 }
267268
268269 struct iterator {
269- const flatmap & m;
270+ const flatmap& m;
270271 size_t n;
271- iterator (const flatmap & m, size_t n) noexcept
272+ iterator (const flatmap& m, size_t n) noexcept
272273 : m(m)
273274 , n(n) {
274275 if (m.m_size == 0 ) {
@@ -277,15 +278,15 @@ namespace bee {
277278 }
278279 next_valid ();
279280 }
280- bool operator !=(const iterator & rhs) const noexcept {
281+ bool operator !=(const iterator& rhs) const noexcept {
281282 return &m != &rhs.m || n != rhs.n ;
282283 }
283284 void operator ++() noexcept {
284285 n++;
285286 next_valid ();
286287 }
287288 auto operator *() noexcept {
288- auto & bucket = m.m_buckets [n];
289+ auto & bucket = m.m_buckets [n];
289290 if constexpr (std::is_same_v<mapped_type, void >) {
290291 return bucket.key ;
291292 } else {
@@ -320,13 +321,13 @@ namespace bee {
320321 size_t maxsize;
321322 size_t size;
322323 } h;
323- bucket * buckets;
324+ bucket* buckets;
324325 };
325- [[nodiscard]] const rawdata & toraw () const noexcept {
326- return *reinterpret_cast <const rawdata *>(&m_mask);
326+ [[nodiscard]] const rawdata& toraw () const noexcept {
327+ return *reinterpret_cast <const rawdata*>(&m_mask);
327328 }
328- [[nodiscard]] rawdata & toraw () noexcept {
329- return *reinterpret_cast <rawdata *>(&m_mask);
329+ [[nodiscard]] rawdata& toraw () noexcept {
330+ return *reinterpret_cast <rawdata*>(&m_mask);
330331 }
331332
332333#if 0
@@ -340,7 +341,7 @@ namespace bee {
340341#endif
341342
342343 private:
343- [[nodiscard]] size_t find_key (const key_type & key) const noexcept {
344+ [[nodiscard]] size_t find_key (const key_type& key) const noexcept {
344345 size_t slot = KeyHash::operator ()(key) & m_mask;
345346 for (uint32_t dib = 1 ;; ++dib) {
346347 if (m_buckets[slot].dib != 0 && KeyEqual::operator ()(key, m_buckets[slot].key )) {
@@ -361,7 +362,7 @@ namespace bee {
361362 }
362363
363364 template <size_t REHASH>
364- void internal_insert (size_t slot, bucket && tmp) {
365+ void internal_insert (size_t slot, bucket&& tmp) {
365366 for (;;) {
366367 if (m_buckets[slot].dib == 0 ) {
367368 new (&m_buckets[slot]) bucket (std::forward<bucket>(tmp));
@@ -384,7 +385,7 @@ namespace bee {
384385 }
385386
386387 template <size_t REHASH>
387- void internal_insert (bucket && b) {
388+ void internal_insert (bucket&& b) {
388389 size_t slot = KeyHash::operator ()(b.key ) & m_mask;
389390 b.dib = 1 ;
390391 return internal_insert<REHASH>(slot, std::forward<bucket>(b));
@@ -407,7 +408,7 @@ namespace bee {
407408 return ;
408409 }
409410
410- bucket * oldbuckets = m_buckets;
411+ bucket* oldbuckets = m_buckets;
411412 size_t oldmaxsize = m_mask + 1 ;
412413
413414 m_buckets = alloc_bucket (newmaxsize);
@@ -428,16 +429,16 @@ namespace bee {
428429
429430 struct free_bucket {
430431 ~free_bucket () noexcept { std::free (b); }
431- bucket * b;
432+ bucket* b;
432433 };
433434
434- static bucket * alloc_bucket (size_t n) {
435- void * t = std::malloc (n * sizeof (bucket));
435+ static bucket* alloc_bucket (size_t n) {
436+ void * t = std::malloc (n * sizeof (bucket));
436437 if (!t) {
437438 throw std::bad_alloc {};
438439 }
439440 std::memset (t, 0 , n * sizeof (bucket));
440- return reinterpret_cast <bucket *>(t);
441+ return reinterpret_cast <bucket*>(t);
441442 }
442443
443444 void throw_overflow () const {
@@ -448,7 +449,7 @@ namespace bee {
448449 size_t m_mask = 0 ;
449450 size_t m_maxsize = 0 ;
450451 size_t m_size = 0 ;
451- bucket * m_buckets = reinterpret_cast <bucket *>(&m_mask);
452+ bucket* m_buckets = reinterpret_cast <bucket*>(&m_mask);
452453 };
453454
454455 template <typename Key, typename KeyHash = flatmap_hash<Key>, typename KeyEqual = std::equal_to<Key>>
@@ -458,7 +459,7 @@ namespace bee {
458459 using mybase = flatmap<Key, void , KeyHash, KeyEqual>;
459460
460461 public:
461- bool insert (const key_type & key) {
462+ bool insert (const key_type& key) {
462463 return mybase::insert (key, 0 );
463464 }
464465 };
0 commit comments