isotree 0.2.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -1
  3. data/LICENSE.txt +2 -2
  4. data/README.md +32 -14
  5. data/ext/isotree/ext.cpp +144 -31
  6. data/ext/isotree/extconf.rb +7 -7
  7. data/lib/isotree/isolation_forest.rb +110 -30
  8. data/lib/isotree/version.rb +1 -1
  9. data/vendor/isotree/LICENSE +1 -1
  10. data/vendor/isotree/README.md +165 -27
  11. data/vendor/isotree/include/isotree.hpp +2116 -0
  12. data/vendor/isotree/include/isotree_oop.hpp +394 -0
  13. data/vendor/isotree/inst/COPYRIGHTS +132 -0
  14. data/vendor/isotree/src/RcppExports.cpp +594 -57
  15. data/vendor/isotree/src/Rwrapper.cpp +2452 -304
  16. data/vendor/isotree/src/c_interface.cpp +958 -0
  17. data/vendor/isotree/src/crit.hpp +4236 -0
  18. data/vendor/isotree/src/digamma.hpp +184 -0
  19. data/vendor/isotree/src/dist.hpp +1886 -0
  20. data/vendor/isotree/src/exp_depth_table.hpp +134 -0
  21. data/vendor/isotree/src/extended.hpp +1444 -0
  22. data/vendor/isotree/src/external_facing_generic.hpp +399 -0
  23. data/vendor/isotree/src/fit_model.hpp +2401 -0
  24. data/vendor/isotree/src/{dealloc.cpp → headers_joined.hpp} +38 -22
  25. data/vendor/isotree/src/helpers_iforest.hpp +814 -0
  26. data/vendor/isotree/src/{impute.cpp → impute.hpp} +382 -123
  27. data/vendor/isotree/src/indexer.cpp +515 -0
  28. data/vendor/isotree/src/instantiate_template_headers.cpp +118 -0
  29. data/vendor/isotree/src/instantiate_template_headers.hpp +240 -0
  30. data/vendor/isotree/src/isoforest.hpp +1659 -0
  31. data/vendor/isotree/src/isotree.hpp +1815 -394
  32. data/vendor/isotree/src/isotree_exportable.hpp +99 -0
  33. data/vendor/isotree/src/merge_models.cpp +159 -16
  34. data/vendor/isotree/src/mult.hpp +1321 -0
  35. data/vendor/isotree/src/oop_interface.cpp +844 -0
  36. data/vendor/isotree/src/oop_interface.hpp +278 -0
  37. data/vendor/isotree/src/other_helpers.hpp +219 -0
  38. data/vendor/isotree/src/predict.hpp +1932 -0
  39. data/vendor/isotree/src/python_helpers.hpp +114 -0
  40. data/vendor/isotree/src/ref_indexer.hpp +154 -0
  41. data/vendor/isotree/src/robinmap/LICENSE +21 -0
  42. data/vendor/isotree/src/robinmap/README.md +483 -0
  43. data/vendor/isotree/src/robinmap/include/tsl/robin_growth_policy.h +406 -0
  44. data/vendor/isotree/src/robinmap/include/tsl/robin_hash.h +1639 -0
  45. data/vendor/isotree/src/robinmap/include/tsl/robin_map.h +807 -0
  46. data/vendor/isotree/src/robinmap/include/tsl/robin_set.h +660 -0
  47. data/vendor/isotree/src/serialize.cpp +4316 -139
  48. data/vendor/isotree/src/sql.cpp +143 -61
  49. data/vendor/isotree/src/subset_models.cpp +174 -0
  50. data/vendor/isotree/src/utils.hpp +3786 -0
  51. data/vendor/isotree/src/xoshiro.hpp +463 -0
  52. data/vendor/isotree/src/ziggurat.hpp +405 -0
  53. metadata +40 -105
  54. data/vendor/cereal/LICENSE +0 -24
  55. data/vendor/cereal/README.md +0 -85
  56. data/vendor/cereal/include/cereal/access.hpp +0 -351
  57. data/vendor/cereal/include/cereal/archives/adapters.hpp +0 -163
  58. data/vendor/cereal/include/cereal/archives/binary.hpp +0 -169
  59. data/vendor/cereal/include/cereal/archives/json.hpp +0 -1019
  60. data/vendor/cereal/include/cereal/archives/portable_binary.hpp +0 -334
  61. data/vendor/cereal/include/cereal/archives/xml.hpp +0 -956
  62. data/vendor/cereal/include/cereal/cereal.hpp +0 -1089
  63. data/vendor/cereal/include/cereal/details/helpers.hpp +0 -422
  64. data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +0 -796
  65. data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +0 -65
  66. data/vendor/cereal/include/cereal/details/static_object.hpp +0 -127
  67. data/vendor/cereal/include/cereal/details/traits.hpp +0 -1411
  68. data/vendor/cereal/include/cereal/details/util.hpp +0 -84
  69. data/vendor/cereal/include/cereal/external/base64.hpp +0 -134
  70. data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +0 -284
  71. data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +0 -78
  72. data/vendor/cereal/include/cereal/external/rapidjson/document.h +0 -2652
  73. data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +0 -299
  74. data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +0 -716
  75. data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +0 -74
  76. data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +0 -161
  77. data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +0 -99
  78. data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +0 -104
  79. data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +0 -151
  80. data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +0 -290
  81. data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +0 -271
  82. data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +0 -245
  83. data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +0 -78
  84. data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +0 -308
  85. data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +0 -186
  86. data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +0 -55
  87. data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +0 -740
  88. data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +0 -232
  89. data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +0 -69
  90. data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +0 -290
  91. data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +0 -46
  92. data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +0 -128
  93. data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +0 -70
  94. data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +0 -71
  95. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +0 -316
  96. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +0 -300
  97. data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +0 -81
  98. data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +0 -1414
  99. data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +0 -277
  100. data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +0 -656
  101. data/vendor/cereal/include/cereal/external/rapidjson/reader.h +0 -2230
  102. data/vendor/cereal/include/cereal/external/rapidjson/schema.h +0 -2497
  103. data/vendor/cereal/include/cereal/external/rapidjson/stream.h +0 -223
  104. data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +0 -121
  105. data/vendor/cereal/include/cereal/external/rapidjson/writer.h +0 -709
  106. data/vendor/cereal/include/cereal/external/rapidxml/license.txt +0 -52
  107. data/vendor/cereal/include/cereal/external/rapidxml/manual.html +0 -406
  108. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +0 -2624
  109. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +0 -175
  110. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +0 -428
  111. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +0 -123
  112. data/vendor/cereal/include/cereal/macros.hpp +0 -154
  113. data/vendor/cereal/include/cereal/specialize.hpp +0 -139
  114. data/vendor/cereal/include/cereal/types/array.hpp +0 -79
  115. data/vendor/cereal/include/cereal/types/atomic.hpp +0 -55
  116. data/vendor/cereal/include/cereal/types/base_class.hpp +0 -203
  117. data/vendor/cereal/include/cereal/types/bitset.hpp +0 -176
  118. data/vendor/cereal/include/cereal/types/boost_variant.hpp +0 -164
  119. data/vendor/cereal/include/cereal/types/chrono.hpp +0 -72
  120. data/vendor/cereal/include/cereal/types/common.hpp +0 -129
  121. data/vendor/cereal/include/cereal/types/complex.hpp +0 -56
  122. data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +0 -73
  123. data/vendor/cereal/include/cereal/types/deque.hpp +0 -62
  124. data/vendor/cereal/include/cereal/types/forward_list.hpp +0 -68
  125. data/vendor/cereal/include/cereal/types/functional.hpp +0 -43
  126. data/vendor/cereal/include/cereal/types/list.hpp +0 -62
  127. data/vendor/cereal/include/cereal/types/map.hpp +0 -36
  128. data/vendor/cereal/include/cereal/types/memory.hpp +0 -425
  129. data/vendor/cereal/include/cereal/types/optional.hpp +0 -66
  130. data/vendor/cereal/include/cereal/types/polymorphic.hpp +0 -483
  131. data/vendor/cereal/include/cereal/types/queue.hpp +0 -132
  132. data/vendor/cereal/include/cereal/types/set.hpp +0 -103
  133. data/vendor/cereal/include/cereal/types/stack.hpp +0 -76
  134. data/vendor/cereal/include/cereal/types/string.hpp +0 -61
  135. data/vendor/cereal/include/cereal/types/tuple.hpp +0 -123
  136. data/vendor/cereal/include/cereal/types/unordered_map.hpp +0 -36
  137. data/vendor/cereal/include/cereal/types/unordered_set.hpp +0 -99
  138. data/vendor/cereal/include/cereal/types/utility.hpp +0 -47
  139. data/vendor/cereal/include/cereal/types/valarray.hpp +0 -89
  140. data/vendor/cereal/include/cereal/types/variant.hpp +0 -109
  141. data/vendor/cereal/include/cereal/types/vector.hpp +0 -112
  142. data/vendor/cereal/include/cereal/version.hpp +0 -52
  143. data/vendor/isotree/src/Makevars +0 -4
  144. data/vendor/isotree/src/crit.cpp +0 -912
  145. data/vendor/isotree/src/dist.cpp +0 -749
  146. data/vendor/isotree/src/extended.cpp +0 -790
  147. data/vendor/isotree/src/fit_model.cpp +0 -1090
  148. data/vendor/isotree/src/helpers_iforest.cpp +0 -324
  149. data/vendor/isotree/src/isoforest.cpp +0 -771
  150. data/vendor/isotree/src/mult.cpp +0 -607
  151. data/vendor/isotree/src/predict.cpp +0 -853
  152. data/vendor/isotree/src/utils.cpp +0 -1566
@@ -0,0 +1,1639 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ #ifndef TSL_ROBIN_HASH_H
25
+ #define TSL_ROBIN_HASH_H
26
+
27
+ #include <algorithm>
28
+ #include <cassert>
29
+ #include <cmath>
30
+ #include <cstddef>
31
+ #include <cstdint>
32
+ #include <exception>
33
+ #include <iterator>
34
+ #include <limits>
35
+ #include <memory>
36
+ #include <new>
37
+ #include <stdexcept>
38
+ #include <tuple>
39
+ #include <type_traits>
40
+ #include <utility>
41
+ #include <vector>
42
+
43
+ #include "robin_growth_policy.h"
44
+
45
+ namespace tsl {
46
+
47
+ namespace detail_robin_hash {
48
+
49
+ template <typename T>
50
+ struct make_void {
51
+ using type = void;
52
+ };
53
+
54
+ template <typename T, typename = void>
55
+ struct has_is_transparent : std::false_type {};
56
+
57
+ template <typename T>
58
+ struct has_is_transparent<T,
59
+ typename make_void<typename T::is_transparent>::type>
60
+ : std::true_type {};
61
+
62
+ template <typename U>
63
+ struct is_power_of_two_policy : std::false_type {};
64
+
65
+ template <std::size_t GrowthFactor>
66
+ struct is_power_of_two_policy<tsl::rh::power_of_two_growth_policy<GrowthFactor>>
67
+ : std::true_type {};
68
+
69
+ // Only available in C++17, we need to be compatible with C++11
70
+ template <class T>
71
+ const T& clamp(const T& v, const T& lo, const T& hi) {
72
+ return std::min(hi, std::max(lo, v));
73
+ }
74
+
75
+ template <typename T, typename U>
76
+ static T numeric_cast(U value,
77
+ const char* error_message = "numeric_cast() failed.") {
78
+ T ret = static_cast<T>(value);
79
+ if (static_cast<U>(ret) != value) {
80
+ TSL_RH_THROW_OR_TERMINATE(std::runtime_error, error_message);
81
+ }
82
+
83
+ const bool is_same_signedness =
84
+ (std::is_unsigned<T>::value && std::is_unsigned<U>::value) ||
85
+ (std::is_signed<T>::value && std::is_signed<U>::value);
86
+ if (!is_same_signedness && (ret < T{}) != (value < U{})) {
87
+ TSL_RH_THROW_OR_TERMINATE(std::runtime_error, error_message);
88
+ }
89
+
90
+ return ret;
91
+ }
92
+
93
+ template <class T, class Deserializer>
94
+ static T deserialize_value(Deserializer& deserializer) {
95
+ // MSVC < 2017 is not conformant, circumvent the problem by removing the
96
+ // template keyword
97
+ #if defined(_MSC_VER) && _MSC_VER < 1910
98
+ return deserializer.Deserializer::operator()<T>();
99
+ #else
100
+ return deserializer.Deserializer::template operator()<T>();
101
+ #endif
102
+ }
103
+
104
+ /**
105
+ * Fixed size type used to represent size_type values on serialization. Need to
106
+ * be big enough to represent a std::size_t on 32 and 64 bits platforms, and
107
+ * must be the same size on both platforms.
108
+ */
109
+ using slz_size_type = std::uint64_t;
110
+ static_assert(std::numeric_limits<slz_size_type>::max() >=
111
+ std::numeric_limits<std::size_t>::max(),
112
+ "slz_size_type must be >= std::size_t");
113
+
114
+ using truncated_hash_type = std::uint32_t;
115
+
116
+ /**
117
+ * Helper class that stores a truncated hash if StoreHash is true and nothing
118
+ * otherwise.
119
+ */
120
+ template <bool StoreHash>
121
+ class bucket_entry_hash {
122
+ public:
123
+ bool bucket_hash_equal(std::size_t /*hash*/) const noexcept { return true; }
124
+
125
+ truncated_hash_type truncated_hash() const noexcept { return 0; }
126
+
127
+ protected:
128
+ void set_hash(truncated_hash_type /*hash*/) noexcept {}
129
+ };
130
+
131
+ template <>
132
+ class bucket_entry_hash<true> {
133
+ public:
134
+ bool bucket_hash_equal(std::size_t hash) const noexcept {
135
+ return m_hash == truncated_hash_type(hash);
136
+ }
137
+
138
+ truncated_hash_type truncated_hash() const noexcept { return m_hash; }
139
+
140
+ protected:
141
+ void set_hash(truncated_hash_type hash) noexcept {
142
+ m_hash = truncated_hash_type(hash);
143
+ }
144
+
145
+ private:
146
+ truncated_hash_type m_hash;
147
+ };
148
+
149
+ /**
150
+ * Each bucket entry has:
151
+ * - A value of type `ValueType`.
152
+ * - An integer to store how far the value of the bucket, if any, is from its
153
+ * ideal bucket (ex: if the current bucket 5 has the value 'foo' and
154
+ * `hash('foo') % nb_buckets` == 3, `dist_from_ideal_bucket()` will return 2 as
155
+ * the current value of the bucket is two buckets away from its ideal bucket) If
156
+ * there is no value in the bucket (i.e. `empty()` is true)
157
+ * `dist_from_ideal_bucket()` will be < 0.
158
+ * - A marker which tells us if the bucket is the last bucket of the bucket
159
+ * array (useful for the iterator of the hash table).
160
+ * - If `StoreHash` is true, 32 bits of the hash of the value, if any, are also
161
+ * stored in the bucket. If the size of the hash is more than 32 bits, it is
162
+ * truncated. We don't store the full hash as storing the hash is a potential
163
+ * opportunity to use the unused space due to the alignment of the bucket_entry
164
+ * structure. We can thus potentially store the hash without any extra space
165
+ * (which would not be possible with 64 bits of the hash).
166
+ */
167
+ template <typename ValueType, bool StoreHash>
168
+ class bucket_entry : public bucket_entry_hash<StoreHash> {
169
+ using bucket_hash = bucket_entry_hash<StoreHash>;
170
+
171
+ public:
172
+ using value_type = ValueType;
173
+ using distance_type = std::int16_t;
174
+
175
+ bucket_entry() noexcept
176
+ : bucket_hash(),
177
+ m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),
178
+ m_last_bucket(false) {
179
+ tsl_rh_assert(empty());
180
+ }
181
+
182
+ bucket_entry(bool last_bucket) noexcept
183
+ : bucket_hash(),
184
+ m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),
185
+ m_last_bucket(last_bucket) {
186
+ tsl_rh_assert(empty());
187
+ }
188
+
189
+ bucket_entry(const bucket_entry& other) noexcept(
190
+ std::is_nothrow_copy_constructible<value_type>::value)
191
+ : bucket_hash(other),
192
+ m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),
193
+ m_last_bucket(other.m_last_bucket) {
194
+ if (!other.empty()) {
195
+ ::new (static_cast<void*>(std::addressof(m_value)))
196
+ value_type(other.value());
197
+ m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;
198
+ }
199
+ tsl_rh_assert(empty() == other.empty());
200
+ }
201
+
202
+ /**
203
+ * Never really used, but still necessary as we must call resize on an empty
204
+ * `std::vector<bucket_entry>`. and we need to support move-only types. See
205
+ * robin_hash constructor for details.
206
+ */
207
+ bucket_entry(bucket_entry&& other) noexcept(
208
+ std::is_nothrow_move_constructible<value_type>::value)
209
+ : bucket_hash(std::move(other)),
210
+ m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),
211
+ m_last_bucket(other.m_last_bucket) {
212
+ if (!other.empty()) {
213
+ ::new (static_cast<void*>(std::addressof(m_value)))
214
+ value_type(std::move(other.value()));
215
+ m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;
216
+ }
217
+ tsl_rh_assert(empty() == other.empty());
218
+ }
219
+
220
+ bucket_entry& operator=(const bucket_entry& other) noexcept(
221
+ std::is_nothrow_copy_constructible<value_type>::value) {
222
+ if (this != &other) {
223
+ clear();
224
+
225
+ bucket_hash::operator=(other);
226
+ if (!other.empty()) {
227
+ ::new (static_cast<void*>(std::addressof(m_value)))
228
+ value_type(other.value());
229
+ }
230
+
231
+ m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;
232
+ m_last_bucket = other.m_last_bucket;
233
+ }
234
+
235
+ return *this;
236
+ }
237
+
238
+ bucket_entry& operator=(bucket_entry&&) = delete;
239
+
240
+ ~bucket_entry() noexcept { clear(); }
241
+
242
+ void clear() noexcept {
243
+ if (!empty()) {
244
+ destroy_value();
245
+ m_dist_from_ideal_bucket = EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;
246
+ }
247
+ }
248
+
249
+ bool empty() const noexcept {
250
+ return m_dist_from_ideal_bucket == EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;
251
+ }
252
+
253
+ value_type& value() noexcept {
254
+ tsl_rh_assert(!empty());
255
+ #if defined(__cplusplus) && __cplusplus >= 201703L
256
+ return *std::launder(
257
+ reinterpret_cast<value_type*>(std::addressof(m_value)));
258
+ #else
259
+ return *reinterpret_cast<value_type*>(std::addressof(m_value));
260
+ #endif
261
+ }
262
+
263
+ const value_type& value() const noexcept {
264
+ tsl_rh_assert(!empty());
265
+ #if defined(__cplusplus) && __cplusplus >= 201703L
266
+ return *std::launder(
267
+ reinterpret_cast<const value_type*>(std::addressof(m_value)));
268
+ #else
269
+ return *reinterpret_cast<const value_type*>(std::addressof(m_value));
270
+ #endif
271
+ }
272
+
273
+ distance_type dist_from_ideal_bucket() const noexcept {
274
+ return m_dist_from_ideal_bucket;
275
+ }
276
+
277
+ bool last_bucket() const noexcept { return m_last_bucket; }
278
+
279
+ void set_as_last_bucket() noexcept { m_last_bucket = true; }
280
+
281
+ template <typename... Args>
282
+ void set_value_of_empty_bucket(distance_type dist_from_ideal_bucket,
283
+ truncated_hash_type hash,
284
+ Args&&... value_type_args) {
285
+ tsl_rh_assert(dist_from_ideal_bucket >= 0);
286
+ tsl_rh_assert(empty());
287
+
288
+ ::new (static_cast<void*>(std::addressof(m_value)))
289
+ value_type(std::forward<Args>(value_type_args)...);
290
+ this->set_hash(hash);
291
+ m_dist_from_ideal_bucket = dist_from_ideal_bucket;
292
+
293
+ tsl_rh_assert(!empty());
294
+ }
295
+
296
+ void swap_with_value_in_bucket(distance_type& dist_from_ideal_bucket,
297
+ truncated_hash_type& hash, value_type& value) {
298
+ tsl_rh_assert(!empty());
299
+ tsl_rh_assert(dist_from_ideal_bucket > m_dist_from_ideal_bucket);
300
+
301
+ using std::swap;
302
+ swap(value, this->value());
303
+ swap(dist_from_ideal_bucket, m_dist_from_ideal_bucket);
304
+
305
+ if (StoreHash) {
306
+ const truncated_hash_type tmp_hash = this->truncated_hash();
307
+ this->set_hash(hash);
308
+ hash = tmp_hash;
309
+ } else {
310
+ // Avoid warning of unused variable if StoreHash is false
311
+ TSL_RH_UNUSED(hash);
312
+ }
313
+ }
314
+
315
+ static truncated_hash_type truncate_hash(std::size_t hash) noexcept {
316
+ return truncated_hash_type(hash);
317
+ }
318
+
319
+ private:
320
+ void destroy_value() noexcept {
321
+ tsl_rh_assert(!empty());
322
+ value().~value_type();
323
+ }
324
+
325
+ public:
326
+ static const distance_type EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET = -1;
327
+ static const distance_type DIST_FROM_IDEAL_BUCKET_LIMIT = 8192;
328
+ static_assert(DIST_FROM_IDEAL_BUCKET_LIMIT <=
329
+ std::numeric_limits<distance_type>::max() - 1,
330
+ "DIST_FROM_IDEAL_BUCKET_LIMIT must be <= "
331
+ "std::numeric_limits<distance_type>::max() - 1.");
332
+
333
+ private:
334
+ distance_type m_dist_from_ideal_bucket;
335
+ bool m_last_bucket;
336
+ alignas(value_type) unsigned char m_value[sizeof(value_type)];
337
+ };
338
+
339
+ /**
340
+ * Internal common class used by `robin_map` and `robin_set`.
341
+ *
342
+ * ValueType is what will be stored by `robin_hash` (usually `std::pair<Key, T>`
343
+ * for map and `Key` for set).
344
+ *
345
+ * `KeySelect` should be a `FunctionObject` which takes a `ValueType` in
346
+ * parameter and returns a reference to the key.
347
+ *
348
+ * `ValueSelect` should be a `FunctionObject` which takes a `ValueType` in
349
+ * parameter and returns a reference to the value. `ValueSelect` should be void
350
+ * if there is no value (in a set for example).
351
+ *
352
+ * The strong exception guarantee only holds if the expression
353
+ * `std::is_nothrow_swappable<ValueType>::value &&
354
+ * std::is_nothrow_move_constructible<ValueType>::value` is true.
355
+ *
356
+ * Behaviour is undefined if the destructor of `ValueType` throws.
357
+ */
358
+ template <class ValueType, class KeySelect, class ValueSelect, class Hash,
359
+ class KeyEqual, class Allocator, bool StoreHash, class GrowthPolicy>
360
+ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
361
+ private:
362
+ template <typename U>
363
+ using has_mapped_type =
364
+ typename std::integral_constant<bool, !std::is_same<U, void>::value>;
365
+
366
+ static_assert(
367
+ noexcept(std::declval<GrowthPolicy>().bucket_for_hash(std::size_t(0))),
368
+ "GrowthPolicy::bucket_for_hash must be noexcept.");
369
+ static_assert(noexcept(std::declval<GrowthPolicy>().clear()),
370
+ "GrowthPolicy::clear must be noexcept.");
371
+
372
+ public:
373
+ template <bool IsConst>
374
+ class robin_iterator;
375
+
376
+ using key_type = typename KeySelect::key_type;
377
+ using value_type = ValueType;
378
+ using size_type = std::size_t;
379
+ using difference_type = std::ptrdiff_t;
380
+ using hasher = Hash;
381
+ using key_equal = KeyEqual;
382
+ using allocator_type = Allocator;
383
+ using reference = value_type&;
384
+ using const_reference = const value_type&;
385
+ using pointer = value_type*;
386
+ using const_pointer = const value_type*;
387
+ using iterator = robin_iterator<false>;
388
+ using const_iterator = robin_iterator<true>;
389
+
390
+ private:
391
+ /**
392
+ * Either store the hash because we are asked by the `StoreHash` template
393
+ * parameter or store the hash because it doesn't cost us anything in size and
394
+ * can be used to speed up rehash.
395
+ */
396
+ static constexpr bool STORE_HASH =
397
+ StoreHash ||
398
+ ((sizeof(tsl::detail_robin_hash::bucket_entry<value_type, true>) ==
399
+ sizeof(tsl::detail_robin_hash::bucket_entry<value_type, false>)) &&
400
+ (sizeof(std::size_t) == sizeof(truncated_hash_type) ||
401
+ is_power_of_two_policy<GrowthPolicy>::value) &&
402
+ // Don't store the hash for primitive types with default hash.
403
+ (!std::is_arithmetic<key_type>::value ||
404
+ !std::is_same<Hash, std::hash<key_type>>::value));
405
+
406
+ /**
407
+ * Only use the stored hash on lookup if we are explicitly asked. We are not
408
+ * sure how slow the KeyEqual operation is. An extra comparison may slow
409
+ * things down with a fast KeyEqual.
410
+ */
411
+ static constexpr bool USE_STORED_HASH_ON_LOOKUP = StoreHash;
412
+
413
+ /**
414
+ * We can only use the hash on rehash if the size of the hash type is the same
415
+ * as the stored one or if we use a power of two modulo. In the case of the
416
+ * power of two modulo, we just mask the least significant bytes, we just have
417
+ * to check that the truncated_hash_type didn't truncated more bytes.
418
+ */
419
+ static bool USE_STORED_HASH_ON_REHASH(size_type bucket_count) {
420
+ if (STORE_HASH && sizeof(std::size_t) == sizeof(truncated_hash_type)) {
421
+ TSL_RH_UNUSED(bucket_count);
422
+ return true;
423
+ } else if (STORE_HASH && is_power_of_two_policy<GrowthPolicy>::value) {
424
+ return bucket_count == 0 ||
425
+ (bucket_count - 1) <=
426
+ std::numeric_limits<truncated_hash_type>::max();
427
+ } else {
428
+ TSL_RH_UNUSED(bucket_count);
429
+ return false;
430
+ }
431
+ }
432
+
433
+ using bucket_entry =
434
+ tsl::detail_robin_hash::bucket_entry<value_type, STORE_HASH>;
435
+ using distance_type = typename bucket_entry::distance_type;
436
+
437
+ using buckets_allocator = typename std::allocator_traits<
438
+ allocator_type>::template rebind_alloc<bucket_entry>;
439
+ using buckets_container_type = std::vector<bucket_entry, buckets_allocator>;
440
+
441
+ public:
442
+ /**
443
+ * The 'operator*()' and 'operator->()' methods return a const reference and
444
+ * const pointer respectively to the stored value type.
445
+ *
446
+ * In case of a map, to get a mutable reference to the value associated to a
447
+ * key (the '.second' in the stored pair), you have to call 'value()'.
448
+ *
449
+ * The main reason for this is that if we returned a `std::pair<Key, T>&`
450
+ * instead of a `const std::pair<Key, T>&`, the user may modify the key which
451
+ * will put the map in a undefined state.
452
+ */
453
+ template <bool IsConst>
454
+ class robin_iterator {
455
+ friend class robin_hash;
456
+
457
+ private:
458
+ using bucket_entry_ptr =
459
+ typename std::conditional<IsConst, const bucket_entry*,
460
+ bucket_entry*>::type;
461
+
462
+ robin_iterator(bucket_entry_ptr bucket) noexcept : m_bucket(bucket) {}
463
+
464
+ public:
465
+ using iterator_category = std::forward_iterator_tag;
466
+ using value_type = const typename robin_hash::value_type;
467
+ using difference_type = std::ptrdiff_t;
468
+ using reference = value_type&;
469
+ using pointer = value_type*;
470
+
471
+ robin_iterator() noexcept {}
472
+
473
+ // Copy constructor from iterator to const_iterator.
474
+ template <bool TIsConst = IsConst,
475
+ typename std::enable_if<TIsConst>::type* = nullptr>
476
+ robin_iterator(const robin_iterator<!TIsConst>& other) noexcept
477
+ : m_bucket(other.m_bucket) {}
478
+
479
+ robin_iterator(const robin_iterator& other) = default;
480
+ robin_iterator(robin_iterator&& other) = default;
481
+ robin_iterator& operator=(const robin_iterator& other) = default;
482
+ robin_iterator& operator=(robin_iterator&& other) = default;
483
+
484
+ const typename robin_hash::key_type& key() const {
485
+ return KeySelect()(m_bucket->value());
486
+ }
487
+
488
+ template <class U = ValueSelect,
489
+ typename std::enable_if<has_mapped_type<U>::value &&
490
+ IsConst>::type* = nullptr>
491
+ const typename U::value_type& value() const {
492
+ return U()(m_bucket->value());
493
+ }
494
+
495
+ template <class U = ValueSelect,
496
+ typename std::enable_if<has_mapped_type<U>::value &&
497
+ !IsConst>::type* = nullptr>
498
+ typename U::value_type& value() const {
499
+ return U()(m_bucket->value());
500
+ }
501
+
502
+ reference operator*() const { return m_bucket->value(); }
503
+
504
+ pointer operator->() const { return std::addressof(m_bucket->value()); }
505
+
506
+ robin_iterator& operator++() {
507
+ while (true) {
508
+ if (m_bucket->last_bucket()) {
509
+ ++m_bucket;
510
+ return *this;
511
+ }
512
+
513
+ ++m_bucket;
514
+ if (!m_bucket->empty()) {
515
+ return *this;
516
+ }
517
+ }
518
+ }
519
+
520
+ robin_iterator operator++(int) {
521
+ robin_iterator tmp(*this);
522
+ ++*this;
523
+
524
+ return tmp;
525
+ }
526
+
527
+ friend bool operator==(const robin_iterator& lhs,
528
+ const robin_iterator& rhs) {
529
+ return lhs.m_bucket == rhs.m_bucket;
530
+ }
531
+
532
+ friend bool operator!=(const robin_iterator& lhs,
533
+ const robin_iterator& rhs) {
534
+ return !(lhs == rhs);
535
+ }
536
+
537
+ private:
538
+ bucket_entry_ptr m_bucket;
539
+ };
540
+
541
+ public:
542
+ #if defined(__cplusplus) && __cplusplus >= 201402L
543
+ robin_hash(size_type bucket_count, const Hash& hash, const KeyEqual& equal,
544
+ const Allocator& alloc,
545
+ float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,
546
+ float max_load_factor = DEFAULT_MAX_LOAD_FACTOR)
547
+ : Hash(hash),
548
+ KeyEqual(equal),
549
+ GrowthPolicy(bucket_count),
550
+ m_buckets_data(bucket_count, alloc),
551
+ m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr()
552
+ : m_buckets_data.data()),
553
+ m_bucket_count(bucket_count),
554
+ m_nb_elements(0),
555
+ m_grow_on_next_insert(false),
556
+ m_try_shrink_on_next_insert(false) {
557
+ if (bucket_count > max_bucket_count()) {
558
+ TSL_RH_THROW_OR_TERMINATE(std::length_error,
559
+ "The map exceeds its maximum bucket count.");
560
+ }
561
+
562
+ if (m_bucket_count > 0) {
563
+ tsl_rh_assert(!m_buckets_data.empty());
564
+ m_buckets_data.back().set_as_last_bucket();
565
+ }
566
+
567
+ this->min_load_factor(min_load_factor);
568
+ this->max_load_factor(max_load_factor);
569
+ }
570
+ #else
571
+ /**
572
+ * C++11 doesn't support the creation of a std::vector with a custom allocator
573
+ * and 'count' default-inserted elements. The needed contructor `explicit
574
+ * vector(size_type count, const Allocator& alloc = Allocator());` is only
575
+ * available in C++14 and later. We thus must resize after using the
576
+ * `vector(const Allocator& alloc)` constructor.
577
+ *
578
+ * We can't use `vector(size_type count, const T& value, const Allocator&
579
+ * alloc)` as it requires the value T to be copyable.
580
+ */
581
+ robin_hash(size_type bucket_count, const Hash& hash, const KeyEqual& equal,
582
+ const Allocator& alloc,
583
+ float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,
584
+ float max_load_factor = DEFAULT_MAX_LOAD_FACTOR)
585
+ : Hash(hash),
586
+ KeyEqual(equal),
587
+ GrowthPolicy(bucket_count),
588
+ m_buckets_data(alloc),
589
+ m_buckets(static_empty_bucket_ptr()),
590
+ m_bucket_count(bucket_count),
591
+ m_nb_elements(0),
592
+ m_grow_on_next_insert(false),
593
+ m_try_shrink_on_next_insert(false) {
594
+ if (bucket_count > max_bucket_count()) {
595
+ TSL_RH_THROW_OR_TERMINATE(std::length_error,
596
+ "The map exceeds its maximum bucket count.");
597
+ }
598
+
599
+ if (m_bucket_count > 0) {
600
+ m_buckets_data.resize(m_bucket_count);
601
+ m_buckets = m_buckets_data.data();
602
+
603
+ tsl_rh_assert(!m_buckets_data.empty());
604
+ m_buckets_data.back().set_as_last_bucket();
605
+ }
606
+
607
+ this->min_load_factor(min_load_factor);
608
+ this->max_load_factor(max_load_factor);
609
+ }
610
+ #endif
611
+
612
+ robin_hash(const robin_hash& other)
613
+ : Hash(other),
614
+ KeyEqual(other),
615
+ GrowthPolicy(other),
616
+ m_buckets_data(other.m_buckets_data),
617
+ m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr()
618
+ : m_buckets_data.data()),
619
+ m_bucket_count(other.m_bucket_count),
620
+ m_nb_elements(other.m_nb_elements),
621
+ m_load_threshold(other.m_load_threshold),
622
+ m_min_load_factor(other.m_min_load_factor),
623
+ m_max_load_factor(other.m_max_load_factor),
624
+ m_grow_on_next_insert(other.m_grow_on_next_insert),
625
+ m_try_shrink_on_next_insert(other.m_try_shrink_on_next_insert) {}
626
+
627
+ robin_hash(robin_hash&& other) noexcept(
628
+ std::is_nothrow_move_constructible<
629
+ Hash>::value&& std::is_nothrow_move_constructible<KeyEqual>::value&&
630
+ std::is_nothrow_move_constructible<GrowthPolicy>::value&&
631
+ std::is_nothrow_move_constructible<buckets_container_type>::value)
632
+ : Hash(std::move(static_cast<Hash&>(other))),
633
+ KeyEqual(std::move(static_cast<KeyEqual&>(other))),
634
+ GrowthPolicy(std::move(static_cast<GrowthPolicy&>(other))),
635
+ m_buckets_data(std::move(other.m_buckets_data)),
636
+ m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr()
637
+ : m_buckets_data.data()),
638
+ m_bucket_count(other.m_bucket_count),
639
+ m_nb_elements(other.m_nb_elements),
640
+ m_load_threshold(other.m_load_threshold),
641
+ m_min_load_factor(other.m_min_load_factor),
642
+ m_max_load_factor(other.m_max_load_factor),
643
+ m_grow_on_next_insert(other.m_grow_on_next_insert),
644
+ m_try_shrink_on_next_insert(other.m_try_shrink_on_next_insert) {
645
+ other.clear_and_shrink();
646
+ }
647
+
648
+ robin_hash& operator=(const robin_hash& other) {
649
+ if (&other != this) {
650
+ Hash::operator=(other);
651
+ KeyEqual::operator=(other);
652
+ GrowthPolicy::operator=(other);
653
+
654
+ m_buckets_data = other.m_buckets_data;
655
+ m_buckets = m_buckets_data.empty() ? static_empty_bucket_ptr()
656
+ : m_buckets_data.data();
657
+ m_bucket_count = other.m_bucket_count;
658
+ m_nb_elements = other.m_nb_elements;
659
+
660
+ m_load_threshold = other.m_load_threshold;
661
+ m_min_load_factor = other.m_min_load_factor;
662
+ m_max_load_factor = other.m_max_load_factor;
663
+
664
+ m_grow_on_next_insert = other.m_grow_on_next_insert;
665
+ m_try_shrink_on_next_insert = other.m_try_shrink_on_next_insert;
666
+ }
667
+
668
+ return *this;
669
+ }
670
+
671
+ robin_hash& operator=(robin_hash&& other) {
672
+ other.swap(*this);
673
+ other.clear_and_shrink();
674
+
675
+ return *this;
676
+ }
677
+
678
+ allocator_type get_allocator() const {
679
+ return m_buckets_data.get_allocator();
680
+ }
681
+
682
+ /*
683
+ * Iterators
684
+ */
685
+ iterator begin() noexcept {
686
+ std::size_t i = 0;
687
+ while (i < m_bucket_count && m_buckets[i].empty()) {
688
+ i++;
689
+ }
690
+
691
+ return iterator(m_buckets + i);
692
+ }
693
+
694
+ const_iterator begin() const noexcept { return cbegin(); }
695
+
696
+ const_iterator cbegin() const noexcept {
697
+ std::size_t i = 0;
698
+ while (i < m_bucket_count && m_buckets[i].empty()) {
699
+ i++;
700
+ }
701
+
702
+ return const_iterator(m_buckets + i);
703
+ }
704
+
705
+ iterator end() noexcept { return iterator(m_buckets + m_bucket_count); }
706
+
707
+ const_iterator end() const noexcept { return cend(); }
708
+
709
+ const_iterator cend() const noexcept {
710
+ return const_iterator(m_buckets + m_bucket_count);
711
+ }
712
+
713
+ /*
714
+ * Capacity
715
+ */
716
+ bool empty() const noexcept { return m_nb_elements == 0; }
717
+
718
+ size_type size() const noexcept { return m_nb_elements; }
719
+
720
+ size_type max_size() const noexcept { return m_buckets_data.max_size(); }
721
+
722
+ /*
723
+ * Modifiers
724
+ */
725
+ void clear() noexcept {
726
+ if (m_min_load_factor > 0.0f) {
727
+ clear_and_shrink();
728
+ } else {
729
+ for (auto& bucket : m_buckets_data) {
730
+ bucket.clear();
731
+ }
732
+
733
+ m_nb_elements = 0;
734
+ m_grow_on_next_insert = false;
735
+ }
736
+ }
737
+
738
+ template <typename P>
739
+ std::pair<iterator, bool> insert(P&& value) {
740
+ return insert_impl(KeySelect()(value), std::forward<P>(value));
741
+ }
742
+
743
+ template <typename P>
744
+ iterator insert_hint(const_iterator hint, P&& value) {
745
+ if (hint != cend() &&
746
+ compare_keys(KeySelect()(*hint), KeySelect()(value))) {
747
+ return mutable_iterator(hint);
748
+ }
749
+
750
+ return insert(std::forward<P>(value)).first;
751
+ }
752
+
753
+ template <class InputIt>
754
+ void insert(InputIt first, InputIt last) {
755
+ if (std::is_base_of<
756
+ std::forward_iterator_tag,
757
+ typename std::iterator_traits<InputIt>::iterator_category>::value) {
758
+ const auto nb_elements_insert = std::distance(first, last);
759
+ const size_type nb_free_buckets = m_load_threshold - size();
760
+ tsl_rh_assert(m_load_threshold >= size());
761
+
762
+ if (nb_elements_insert > 0 &&
763
+ nb_free_buckets < size_type(nb_elements_insert)) {
764
+ reserve(size() + size_type(nb_elements_insert));
765
+ }
766
+ }
767
+
768
+ for (; first != last; ++first) {
769
+ insert(*first);
770
+ }
771
+ }
772
+
773
+ template <class K, class M>
774
+ std::pair<iterator, bool> insert_or_assign(K&& key, M&& obj) {
775
+ auto it = try_emplace(std::forward<K>(key), std::forward<M>(obj));
776
+ if (!it.second) {
777
+ it.first.value() = std::forward<M>(obj);
778
+ }
779
+
780
+ return it;
781
+ }
782
+
783
+ template <class K, class M>
784
+ iterator insert_or_assign(const_iterator hint, K&& key, M&& obj) {
785
+ if (hint != cend() && compare_keys(KeySelect()(*hint), key)) {
786
+ auto it = mutable_iterator(hint);
787
+ it.value() = std::forward<M>(obj);
788
+
789
+ return it;
790
+ }
791
+
792
+ return insert_or_assign(std::forward<K>(key), std::forward<M>(obj)).first;
793
+ }
794
+
795
+ template <class... Args>
796
+ std::pair<iterator, bool> emplace(Args&&... args) {
797
+ return insert(value_type(std::forward<Args>(args)...));
798
+ }
799
+
800
+ template <class... Args>
801
+ iterator emplace_hint(const_iterator hint, Args&&... args) {
802
+ return insert_hint(hint, value_type(std::forward<Args>(args)...));
803
+ }
804
+
805
+ template <class K, class... Args>
806
+ std::pair<iterator, bool> try_emplace(K&& key, Args&&... args) {
807
+ return insert_impl(key, std::piecewise_construct,
808
+ std::forward_as_tuple(std::forward<K>(key)),
809
+ std::forward_as_tuple(std::forward<Args>(args)...));
810
+ }
811
+
812
+ template <class K, class... Args>
813
+ iterator try_emplace_hint(const_iterator hint, K&& key, Args&&... args) {
814
+ if (hint != cend() && compare_keys(KeySelect()(*hint), key)) {
815
+ return mutable_iterator(hint);
816
+ }
817
+
818
+ return try_emplace(std::forward<K>(key), std::forward<Args>(args)...).first;
819
+ }
820
+
821
+ /**
822
+ * Here to avoid `template<class K> size_type erase(const K& key)` being used
823
+ * when we use an `iterator` instead of a `const_iterator`.
824
+ */
825
+ iterator erase(iterator pos) {
826
+ erase_from_bucket(pos);
827
+
828
+ /**
829
+ * Erase bucket used a backward shift after clearing the bucket.
830
+ * Check if there is a new value in the bucket, if not get the next
831
+ * non-empty.
832
+ */
833
+ if (pos.m_bucket->empty()) {
834
+ ++pos;
835
+ }
836
+
837
+ m_try_shrink_on_next_insert = true;
838
+
839
+ return pos;
840
+ }
841
+
842
+ iterator erase(const_iterator pos) { return erase(mutable_iterator(pos)); }
843
+
844
+ iterator erase(const_iterator first, const_iterator last) {
845
+ if (first == last) {
846
+ return mutable_iterator(first);
847
+ }
848
+
849
+ auto first_mutable = mutable_iterator(first);
850
+ auto last_mutable = mutable_iterator(last);
851
+ for (auto it = first_mutable.m_bucket; it != last_mutable.m_bucket; ++it) {
852
+ if (!it->empty()) {
853
+ it->clear();
854
+ m_nb_elements--;
855
+ }
856
+ }
857
+
858
+ if (last_mutable == end()) {
859
+ m_try_shrink_on_next_insert = true;
860
+ return end();
861
+ }
862
+
863
+ /*
864
+ * Backward shift on the values which come after the deleted values.
865
+ * We try to move the values closer to their ideal bucket.
866
+ */
867
+ std::size_t icloser_bucket =
868
+ static_cast<std::size_t>(first_mutable.m_bucket - m_buckets);
869
+ std::size_t ito_move_closer_value =
870
+ static_cast<std::size_t>(last_mutable.m_bucket - m_buckets);
871
+ tsl_rh_assert(ito_move_closer_value > icloser_bucket);
872
+
873
+ const std::size_t ireturn_bucket =
874
+ ito_move_closer_value -
875
+ std::min(
876
+ ito_move_closer_value - icloser_bucket,
877
+ std::size_t(
878
+ m_buckets[ito_move_closer_value].dist_from_ideal_bucket()));
879
+
880
+ while (ito_move_closer_value < m_bucket_count &&
881
+ m_buckets[ito_move_closer_value].dist_from_ideal_bucket() > 0) {
882
+ icloser_bucket =
883
+ ito_move_closer_value -
884
+ std::min(
885
+ ito_move_closer_value - icloser_bucket,
886
+ std::size_t(
887
+ m_buckets[ito_move_closer_value].dist_from_ideal_bucket()));
888
+
889
+ tsl_rh_assert(m_buckets[icloser_bucket].empty());
890
+ const distance_type new_distance = distance_type(
891
+ m_buckets[ito_move_closer_value].dist_from_ideal_bucket() -
892
+ (ito_move_closer_value - icloser_bucket));
893
+ m_buckets[icloser_bucket].set_value_of_empty_bucket(
894
+ new_distance, m_buckets[ito_move_closer_value].truncated_hash(),
895
+ std::move(m_buckets[ito_move_closer_value].value()));
896
+ m_buckets[ito_move_closer_value].clear();
897
+
898
+ ++icloser_bucket;
899
+ ++ito_move_closer_value;
900
+ }
901
+
902
+ m_try_shrink_on_next_insert = true;
903
+
904
+ return iterator(m_buckets + ireturn_bucket);
905
+ }
906
+
907
+ template <class K>
908
+ size_type erase(const K& key) {
909
+ return erase(key, hash_key(key));
910
+ }
911
+
912
+ template <class K>
913
+ size_type erase(const K& key, std::size_t hash) {
914
+ auto it = find(key, hash);
915
+ if (it != end()) {
916
+ erase_from_bucket(it);
917
+ m_try_shrink_on_next_insert = true;
918
+
919
+ return 1;
920
+ } else {
921
+ return 0;
922
+ }
923
+ }
924
+
925
+ void swap(robin_hash& other) {
926
+ using std::swap;
927
+
928
+ swap(static_cast<Hash&>(*this), static_cast<Hash&>(other));
929
+ swap(static_cast<KeyEqual&>(*this), static_cast<KeyEqual&>(other));
930
+ swap(static_cast<GrowthPolicy&>(*this), static_cast<GrowthPolicy&>(other));
931
+ swap(m_buckets_data, other.m_buckets_data);
932
+ swap(m_buckets, other.m_buckets);
933
+ swap(m_bucket_count, other.m_bucket_count);
934
+ swap(m_nb_elements, other.m_nb_elements);
935
+ swap(m_load_threshold, other.m_load_threshold);
936
+ swap(m_min_load_factor, other.m_min_load_factor);
937
+ swap(m_max_load_factor, other.m_max_load_factor);
938
+ swap(m_grow_on_next_insert, other.m_grow_on_next_insert);
939
+ swap(m_try_shrink_on_next_insert, other.m_try_shrink_on_next_insert);
940
+ }
941
+
942
+ /*
943
+ * Lookup
944
+ */
945
+ template <class K, class U = ValueSelect,
946
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
947
+ typename U::value_type& at(const K& key) {
948
+ return at(key, hash_key(key));
949
+ }
950
+
951
+ template <class K, class U = ValueSelect,
952
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
953
+ typename U::value_type& at(const K& key, std::size_t hash) {
954
+ return const_cast<typename U::value_type&>(
955
+ static_cast<const robin_hash*>(this)->at(key, hash));
956
+ }
957
+
958
+ template <class K, class U = ValueSelect,
959
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
960
+ const typename U::value_type& at(const K& key) const {
961
+ return at(key, hash_key(key));
962
+ }
963
+
964
+ template <class K, class U = ValueSelect,
965
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
966
+ const typename U::value_type& at(const K& key, std::size_t hash) const {
967
+ auto it = find(key, hash);
968
+ if (it != cend()) {
969
+ return it.value();
970
+ } else {
971
+ TSL_RH_THROW_OR_TERMINATE(std::out_of_range, "Couldn't find key.");
972
+ }
973
+ }
974
+
975
+ template <class K, class U = ValueSelect,
976
+ typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
977
+ typename U::value_type& operator[](K&& key) {
978
+ return try_emplace(std::forward<K>(key)).first.value();
979
+ }
980
+
981
+ template <class K>
982
+ size_type count(const K& key) const {
983
+ return count(key, hash_key(key));
984
+ }
985
+
986
+ template <class K>
987
+ size_type count(const K& key, std::size_t hash) const {
988
+ if (find(key, hash) != cend()) {
989
+ return 1;
990
+ } else {
991
+ return 0;
992
+ }
993
+ }
994
+
995
+ template <class K>
996
+ iterator find(const K& key) {
997
+ return find_impl(key, hash_key(key));
998
+ }
999
+
1000
+ template <class K>
1001
+ iterator find(const K& key, std::size_t hash) {
1002
+ return find_impl(key, hash);
1003
+ }
1004
+
1005
+ template <class K>
1006
+ const_iterator find(const K& key) const {
1007
+ return find_impl(key, hash_key(key));
1008
+ }
1009
+
1010
+ template <class K>
1011
+ const_iterator find(const K& key, std::size_t hash) const {
1012
+ return find_impl(key, hash);
1013
+ }
1014
+
1015
+ template <class K>
1016
+ bool contains(const K& key) const {
1017
+ return contains(key, hash_key(key));
1018
+ }
1019
+
1020
+ template <class K>
1021
+ bool contains(const K& key, std::size_t hash) const {
1022
+ return count(key, hash) != 0;
1023
+ }
1024
+
1025
+ template <class K>
1026
+ std::pair<iterator, iterator> equal_range(const K& key) {
1027
+ return equal_range(key, hash_key(key));
1028
+ }
1029
+
1030
+ template <class K>
1031
+ std::pair<iterator, iterator> equal_range(const K& key, std::size_t hash) {
1032
+ iterator it = find(key, hash);
1033
+ return std::make_pair(it, (it == end()) ? it : std::next(it));
1034
+ }
1035
+
1036
+ template <class K>
1037
+ std::pair<const_iterator, const_iterator> equal_range(const K& key) const {
1038
+ return equal_range(key, hash_key(key));
1039
+ }
1040
+
1041
+ template <class K>
1042
+ std::pair<const_iterator, const_iterator> equal_range(
1043
+ const K& key, std::size_t hash) const {
1044
+ const_iterator it = find(key, hash);
1045
+ return std::make_pair(it, (it == cend()) ? it : std::next(it));
1046
+ }
1047
+
1048
+ /*
1049
+ * Bucket interface
1050
+ */
1051
+ size_type bucket_count() const { return m_bucket_count; }
1052
+
1053
+ size_type max_bucket_count() const {
1054
+ return std::min(GrowthPolicy::max_bucket_count(),
1055
+ m_buckets_data.max_size());
1056
+ }
1057
+
1058
+ /*
1059
+ * Hash policy
1060
+ */
1061
+ float load_factor() const {
1062
+ if (bucket_count() == 0) {
1063
+ return 0;
1064
+ }
1065
+
1066
+ return float(m_nb_elements) / float(bucket_count());
1067
+ }
1068
+
1069
+ float min_load_factor() const { return m_min_load_factor; }
1070
+
1071
+ float max_load_factor() const { return m_max_load_factor; }
1072
+
1073
+ void min_load_factor(float ml) {
1074
+ m_min_load_factor = clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR),
1075
+ float(MAXIMUM_MIN_LOAD_FACTOR));
1076
+ }
1077
+
1078
+ void max_load_factor(float ml) {
1079
+ m_max_load_factor = clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR),
1080
+ float(MAXIMUM_MAX_LOAD_FACTOR));
1081
+ m_load_threshold = size_type(float(bucket_count()) * m_max_load_factor);
1082
+ tsl_rh_assert(bucket_count() == 0 || m_load_threshold < bucket_count());
1083
+ }
1084
+
1085
+ void rehash(size_type count_) {
1086
+ count_ = std::max(count_,
1087
+ size_type(std::ceil(float(size()) / max_load_factor())));
1088
+ rehash_impl(count_);
1089
+ }
1090
+
1091
+ void reserve(size_type count_) {
1092
+ rehash(size_type(std::ceil(float(count_) / max_load_factor())));
1093
+ }
1094
+
1095
+ /*
1096
+ * Observers
1097
+ */
1098
+ hasher hash_function() const { return static_cast<const Hash&>(*this); }
1099
+
1100
+ key_equal key_eq() const { return static_cast<const KeyEqual&>(*this); }
1101
+
1102
+ /*
1103
+ * Other
1104
+ */
1105
+ iterator mutable_iterator(const_iterator pos) {
1106
+ return iterator(const_cast<bucket_entry*>(pos.m_bucket));
1107
+ }
1108
+
1109
+ template <class Serializer>
1110
+ void serialize(Serializer& serializer) const {
1111
+ serialize_impl(serializer);
1112
+ }
1113
+
1114
+ template <class Deserializer>
1115
+ void deserialize(Deserializer& deserializer, bool hash_compatible) {
1116
+ deserialize_impl(deserializer, hash_compatible);
1117
+ }
1118
+
1119
+ private:
1120
+ template <class K>
1121
+ std::size_t hash_key(const K& key) const {
1122
+ return Hash::operator()(key);
1123
+ }
1124
+
1125
+ template <class K1, class K2>
1126
+ bool compare_keys(const K1& key1, const K2& key2) const {
1127
+ return KeyEqual::operator()(key1, key2);
1128
+ }
1129
+
1130
+ std::size_t bucket_for_hash(std::size_t hash) const {
1131
+ const std::size_t bucket = GrowthPolicy::bucket_for_hash(hash);
1132
+ tsl_rh_assert(bucket < m_bucket_count ||
1133
+ (bucket == 0 && m_bucket_count == 0));
1134
+
1135
+ return bucket;
1136
+ }
1137
+
1138
+ template <class U = GrowthPolicy,
1139
+ typename std::enable_if<is_power_of_two_policy<U>::value>::type* =
1140
+ nullptr>
1141
+ std::size_t next_bucket(std::size_t index) const noexcept {
1142
+ tsl_rh_assert(index < bucket_count());
1143
+
1144
+ return (index + 1) & this->m_mask;
1145
+ }
1146
+
1147
+ template <class U = GrowthPolicy,
1148
+ typename std::enable_if<!is_power_of_two_policy<U>::value>::type* =
1149
+ nullptr>
1150
+ std::size_t next_bucket(std::size_t index) const noexcept {
1151
+ tsl_rh_assert(index < bucket_count());
1152
+
1153
+ index++;
1154
+ return (index != bucket_count()) ? index : 0;
1155
+ }
1156
+
1157
+ template <class K>
1158
+ iterator find_impl(const K& key, std::size_t hash) {
1159
+ return mutable_iterator(
1160
+ static_cast<const robin_hash*>(this)->find(key, hash));
1161
+ }
1162
+
1163
+ template <class K>
1164
+ const_iterator find_impl(const K& key, std::size_t hash) const {
1165
+ std::size_t ibucket = bucket_for_hash(hash);
1166
+ distance_type dist_from_ideal_bucket = 0;
1167
+
1168
+ while (dist_from_ideal_bucket <=
1169
+ m_buckets[ibucket].dist_from_ideal_bucket()) {
1170
+ if (TSL_RH_LIKELY(
1171
+ (!USE_STORED_HASH_ON_LOOKUP ||
1172
+ m_buckets[ibucket].bucket_hash_equal(hash)) &&
1173
+ compare_keys(KeySelect()(m_buckets[ibucket].value()), key))) {
1174
+ return const_iterator(m_buckets + ibucket);
1175
+ }
1176
+
1177
+ ibucket = next_bucket(ibucket);
1178
+ dist_from_ideal_bucket++;
1179
+ }
1180
+
1181
+ return cend();
1182
+ }
1183
+
1184
+ void erase_from_bucket(iterator pos) {
1185
+ pos.m_bucket->clear();
1186
+ m_nb_elements--;
1187
+
1188
+ /**
1189
+ * Backward shift, swap the empty bucket, previous_ibucket, with the values
1190
+ * on its right, ibucket, until we cross another empty bucket or if the
1191
+ * other bucket has a distance_from_ideal_bucket == 0.
1192
+ *
1193
+ * We try to move the values closer to their ideal bucket.
1194
+ */
1195
+ std::size_t previous_ibucket =
1196
+ static_cast<std::size_t>(pos.m_bucket - m_buckets);
1197
+ std::size_t ibucket = next_bucket(previous_ibucket);
1198
+
1199
+ while (m_buckets[ibucket].dist_from_ideal_bucket() > 0) {
1200
+ tsl_rh_assert(m_buckets[previous_ibucket].empty());
1201
+
1202
+ const distance_type new_distance =
1203
+ distance_type(m_buckets[ibucket].dist_from_ideal_bucket() - 1);
1204
+ m_buckets[previous_ibucket].set_value_of_empty_bucket(
1205
+ new_distance, m_buckets[ibucket].truncated_hash(),
1206
+ std::move(m_buckets[ibucket].value()));
1207
+ m_buckets[ibucket].clear();
1208
+
1209
+ previous_ibucket = ibucket;
1210
+ ibucket = next_bucket(ibucket);
1211
+ }
1212
+ }
1213
+
1214
+ template <class K, class... Args>
1215
+ std::pair<iterator, bool> insert_impl(const K& key,
1216
+ Args&&... value_type_args) {
1217
+ const std::size_t hash = hash_key(key);
1218
+
1219
+ std::size_t ibucket = bucket_for_hash(hash);
1220
+ distance_type dist_from_ideal_bucket = 0;
1221
+
1222
+ while (dist_from_ideal_bucket <=
1223
+ m_buckets[ibucket].dist_from_ideal_bucket()) {
1224
+ if ((!USE_STORED_HASH_ON_LOOKUP ||
1225
+ m_buckets[ibucket].bucket_hash_equal(hash)) &&
1226
+ compare_keys(KeySelect()(m_buckets[ibucket].value()), key)) {
1227
+ return std::make_pair(iterator(m_buckets + ibucket), false);
1228
+ }
1229
+
1230
+ ibucket = next_bucket(ibucket);
1231
+ dist_from_ideal_bucket++;
1232
+ }
1233
+
1234
+ while (rehash_on_extreme_load(dist_from_ideal_bucket)) {
1235
+ ibucket = bucket_for_hash(hash);
1236
+ dist_from_ideal_bucket = 0;
1237
+
1238
+ while (dist_from_ideal_bucket <=
1239
+ m_buckets[ibucket].dist_from_ideal_bucket()) {
1240
+ ibucket = next_bucket(ibucket);
1241
+ dist_from_ideal_bucket++;
1242
+ }
1243
+ }
1244
+
1245
+ if (m_buckets[ibucket].empty()) {
1246
+ m_buckets[ibucket].set_value_of_empty_bucket(
1247
+ dist_from_ideal_bucket, bucket_entry::truncate_hash(hash),
1248
+ std::forward<Args>(value_type_args)...);
1249
+ } else {
1250
+ insert_value(ibucket, dist_from_ideal_bucket,
1251
+ bucket_entry::truncate_hash(hash),
1252
+ std::forward<Args>(value_type_args)...);
1253
+ }
1254
+
1255
+ m_nb_elements++;
1256
+ /*
1257
+ * The value will be inserted in ibucket in any case, either because it was
1258
+ * empty or by stealing the bucket (robin hood).
1259
+ */
1260
+ return std::make_pair(iterator(m_buckets + ibucket), true);
1261
+ }
1262
+
1263
+ template <class... Args>
1264
+ void insert_value(std::size_t ibucket, distance_type dist_from_ideal_bucket,
1265
+ truncated_hash_type hash, Args&&... value_type_args) {
1266
+ value_type value(std::forward<Args>(value_type_args)...);
1267
+ insert_value_impl(ibucket, dist_from_ideal_bucket, hash, value);
1268
+ }
1269
+
1270
+ void insert_value(std::size_t ibucket, distance_type dist_from_ideal_bucket,
1271
+ truncated_hash_type hash, value_type&& value) {
1272
+ insert_value_impl(ibucket, dist_from_ideal_bucket, hash, value);
1273
+ }
1274
+
1275
+ /*
1276
+ * We don't use `value_type&& value` as last argument due to a bug in MSVC
1277
+ * when `value_type` is a pointer, The compiler is not able to see the
1278
+ * difference between `std::string*` and `std::string*&&` resulting in a
1279
+ * compilation error.
1280
+ *
1281
+ * The `value` will be in a moved state at the end of the function.
1282
+ */
1283
+ void insert_value_impl(std::size_t ibucket,
1284
+ distance_type dist_from_ideal_bucket,
1285
+ truncated_hash_type hash, value_type& value) {
1286
+ tsl_rh_assert(dist_from_ideal_bucket >
1287
+ m_buckets[ibucket].dist_from_ideal_bucket());
1288
+ m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash,
1289
+ value);
1290
+ ibucket = next_bucket(ibucket);
1291
+ dist_from_ideal_bucket++;
1292
+
1293
+ while (!m_buckets[ibucket].empty()) {
1294
+ if (dist_from_ideal_bucket >
1295
+ m_buckets[ibucket].dist_from_ideal_bucket()) {
1296
+ if (dist_from_ideal_bucket >
1297
+ bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT) {
1298
+ /**
1299
+ * The number of probes is really high, rehash the map on the next
1300
+ * insert. Difficult to do now as rehash may throw an exception.
1301
+ */
1302
+ m_grow_on_next_insert = true;
1303
+ }
1304
+
1305
+ m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket,
1306
+ hash, value);
1307
+ }
1308
+
1309
+ ibucket = next_bucket(ibucket);
1310
+ dist_from_ideal_bucket++;
1311
+ }
1312
+
1313
+ m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, hash,
1314
+ std::move(value));
1315
+ }
1316
+
1317
+ void rehash_impl(size_type count_) {
1318
+ robin_hash new_table(count_, static_cast<Hash&>(*this),
1319
+ static_cast<KeyEqual&>(*this), get_allocator(),
1320
+ m_min_load_factor, m_max_load_factor);
1321
+ tsl_rh_assert(size() <= new_table.m_load_threshold);
1322
+
1323
+ const bool use_stored_hash =
1324
+ USE_STORED_HASH_ON_REHASH(new_table.bucket_count());
1325
+ for (auto& bucket : m_buckets_data) {
1326
+ if (bucket.empty()) {
1327
+ continue;
1328
+ }
1329
+
1330
+ const std::size_t hash =
1331
+ use_stored_hash ? bucket.truncated_hash()
1332
+ : new_table.hash_key(KeySelect()(bucket.value()));
1333
+
1334
+ new_table.insert_value_on_rehash(new_table.bucket_for_hash(hash), 0,
1335
+ bucket_entry::truncate_hash(hash),
1336
+ std::move(bucket.value()));
1337
+ }
1338
+
1339
+ new_table.m_nb_elements = m_nb_elements;
1340
+ new_table.swap(*this);
1341
+ }
1342
+
1343
+ void clear_and_shrink() noexcept {
1344
+ GrowthPolicy::clear();
1345
+ m_buckets_data.clear();
1346
+ m_buckets = static_empty_bucket_ptr();
1347
+ m_bucket_count = 0;
1348
+ m_nb_elements = 0;
1349
+ m_load_threshold = 0;
1350
+ m_grow_on_next_insert = false;
1351
+ m_try_shrink_on_next_insert = false;
1352
+ }
1353
+
1354
+ void insert_value_on_rehash(std::size_t ibucket,
1355
+ distance_type dist_from_ideal_bucket,
1356
+ truncated_hash_type hash, value_type&& value) {
1357
+ while (true) {
1358
+ if (dist_from_ideal_bucket >
1359
+ m_buckets[ibucket].dist_from_ideal_bucket()) {
1360
+ if (m_buckets[ibucket].empty()) {
1361
+ m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket,
1362
+ hash, std::move(value));
1363
+ return;
1364
+ } else {
1365
+ m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket,
1366
+ hash, value);
1367
+ }
1368
+ }
1369
+
1370
+ dist_from_ideal_bucket++;
1371
+ ibucket = next_bucket(ibucket);
1372
+ }
1373
+ }
1374
+
1375
+ /**
1376
+ * Grow the table if m_grow_on_next_insert is true or we reached the
1377
+ * max_load_factor. Shrink the table if m_try_shrink_on_next_insert is true
1378
+ * (an erase occurred) and we're below the min_load_factor.
1379
+ *
1380
+ * Return true if the table has been rehashed.
1381
+ */
1382
+ bool rehash_on_extreme_load(distance_type curr_dist_from_ideal_bucket) {
1383
+ if (m_grow_on_next_insert ||
1384
+ curr_dist_from_ideal_bucket >
1385
+ bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT ||
1386
+ size() >= m_load_threshold) {
1387
+ rehash_impl(GrowthPolicy::next_bucket_count());
1388
+ m_grow_on_next_insert = false;
1389
+
1390
+ return true;
1391
+ }
1392
+
1393
+ if (m_try_shrink_on_next_insert) {
1394
+ m_try_shrink_on_next_insert = false;
1395
+ if (m_min_load_factor != 0.0f && load_factor() < m_min_load_factor) {
1396
+ reserve(size() + 1);
1397
+
1398
+ return true;
1399
+ }
1400
+ }
1401
+
1402
+ return false;
1403
+ }
1404
+
1405
+ template <class Serializer>
1406
+ void serialize_impl(Serializer& serializer) const {
1407
+ const slz_size_type version = SERIALIZATION_PROTOCOL_VERSION;
1408
+ serializer(version);
1409
+
1410
+ // Indicate if the truncated hash of each bucket is stored. Use a
1411
+ // std::int16_t instead of a bool to avoid the need for the serializer to
1412
+ // support an extra 'bool' type.
1413
+ const std::int16_t hash_stored_for_bucket =
1414
+ static_cast<std::int16_t>(STORE_HASH);
1415
+ serializer(hash_stored_for_bucket);
1416
+
1417
+ const slz_size_type nb_elements = m_nb_elements;
1418
+ serializer(nb_elements);
1419
+
1420
+ const slz_size_type bucket_count = m_buckets_data.size();
1421
+ serializer(bucket_count);
1422
+
1423
+ const float min_load_factor = m_min_load_factor;
1424
+ serializer(min_load_factor);
1425
+
1426
+ const float max_load_factor = m_max_load_factor;
1427
+ serializer(max_load_factor);
1428
+
1429
+ for (const bucket_entry& bucket : m_buckets_data) {
1430
+ if (bucket.empty()) {
1431
+ const std::int16_t empty_bucket =
1432
+ bucket_entry::EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;
1433
+ serializer(empty_bucket);
1434
+ } else {
1435
+ const std::int16_t dist_from_ideal_bucket =
1436
+ bucket.dist_from_ideal_bucket();
1437
+ serializer(dist_from_ideal_bucket);
1438
+ if (STORE_HASH) {
1439
+ const std::uint32_t truncated_hash = bucket.truncated_hash();
1440
+ serializer(truncated_hash);
1441
+ }
1442
+ serializer(bucket.value());
1443
+ }
1444
+ }
1445
+ }
1446
+
1447
+ template <class Deserializer>
1448
+ void deserialize_impl(Deserializer& deserializer, bool hash_compatible) {
1449
+ tsl_rh_assert(m_buckets_data.empty()); // Current hash table must be empty
1450
+
1451
+ const slz_size_type version =
1452
+ deserialize_value<slz_size_type>(deserializer);
1453
+ // For now we only have one version of the serialization protocol.
1454
+ // If it doesn't match there is a problem with the file.
1455
+ if (version != SERIALIZATION_PROTOCOL_VERSION) {
1456
+ TSL_RH_THROW_OR_TERMINATE(std::runtime_error,
1457
+ "Can't deserialize the ordered_map/set. "
1458
+ "The protocol version header is invalid.");
1459
+ }
1460
+
1461
+ const bool hash_stored_for_bucket =
1462
+ deserialize_value<std::int16_t>(deserializer) ? true : false;
1463
+ if (hash_compatible && STORE_HASH != hash_stored_for_bucket) {
1464
+ TSL_RH_THROW_OR_TERMINATE(
1465
+ std::runtime_error,
1466
+ "Can't deserialize a map with a different StoreHash "
1467
+ "than the one used during the serialization when "
1468
+ "hash compatibility is used");
1469
+ }
1470
+
1471
+ const slz_size_type nb_elements =
1472
+ deserialize_value<slz_size_type>(deserializer);
1473
+ const slz_size_type bucket_count_ds =
1474
+ deserialize_value<slz_size_type>(deserializer);
1475
+ const float min_load_factor = deserialize_value<float>(deserializer);
1476
+ const float max_load_factor = deserialize_value<float>(deserializer);
1477
+
1478
+ if (min_load_factor < MINIMUM_MIN_LOAD_FACTOR ||
1479
+ min_load_factor > MAXIMUM_MIN_LOAD_FACTOR) {
1480
+ TSL_RH_THROW_OR_TERMINATE(
1481
+ std::runtime_error,
1482
+ "Invalid min_load_factor. Check that the serializer "
1483
+ "and deserializer support floats correctly as they "
1484
+ "can be converted implicitly to ints.");
1485
+ }
1486
+
1487
+ if (max_load_factor < MINIMUM_MAX_LOAD_FACTOR ||
1488
+ max_load_factor > MAXIMUM_MAX_LOAD_FACTOR) {
1489
+ TSL_RH_THROW_OR_TERMINATE(
1490
+ std::runtime_error,
1491
+ "Invalid max_load_factor. Check that the serializer "
1492
+ "and deserializer support floats correctly as they "
1493
+ "can be converted implicitly to ints.");
1494
+ }
1495
+
1496
+ this->min_load_factor(min_load_factor);
1497
+ this->max_load_factor(max_load_factor);
1498
+
1499
+ if (bucket_count_ds == 0) {
1500
+ tsl_rh_assert(nb_elements == 0);
1501
+ return;
1502
+ }
1503
+
1504
+ if (!hash_compatible) {
1505
+ reserve(numeric_cast<size_type>(nb_elements,
1506
+ "Deserialized nb_elements is too big."));
1507
+ for (slz_size_type ibucket = 0; ibucket < bucket_count_ds; ibucket++) {
1508
+ const distance_type dist_from_ideal_bucket =
1509
+ deserialize_value<std::int16_t>(deserializer);
1510
+ if (dist_from_ideal_bucket !=
1511
+ bucket_entry::EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET) {
1512
+ if (hash_stored_for_bucket) {
1513
+ TSL_RH_UNUSED(deserialize_value<std::uint32_t>(deserializer));
1514
+ }
1515
+
1516
+ insert(deserialize_value<value_type>(deserializer));
1517
+ }
1518
+ }
1519
+
1520
+ tsl_rh_assert(nb_elements == size());
1521
+ } else {
1522
+ m_bucket_count = numeric_cast<size_type>(
1523
+ bucket_count_ds, "Deserialized bucket_count is too big.");
1524
+
1525
+ GrowthPolicy::operator=(GrowthPolicy(m_bucket_count));
1526
+ // GrowthPolicy should not modify the bucket count we got from
1527
+ // deserialization
1528
+ if (m_bucket_count != bucket_count_ds) {
1529
+ TSL_RH_THROW_OR_TERMINATE(std::runtime_error,
1530
+ "The GrowthPolicy is not the same even "
1531
+ "though hash_compatible is true.");
1532
+ }
1533
+
1534
+ m_nb_elements = numeric_cast<size_type>(
1535
+ nb_elements, "Deserialized nb_elements is too big.");
1536
+ m_buckets_data.resize(m_bucket_count);
1537
+ m_buckets = m_buckets_data.data();
1538
+
1539
+ for (bucket_entry& bucket : m_buckets_data) {
1540
+ const distance_type dist_from_ideal_bucket =
1541
+ deserialize_value<std::int16_t>(deserializer);
1542
+ if (dist_from_ideal_bucket !=
1543
+ bucket_entry::EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET) {
1544
+ truncated_hash_type truncated_hash = 0;
1545
+ if (hash_stored_for_bucket) {
1546
+ tsl_rh_assert(hash_stored_for_bucket);
1547
+ truncated_hash = deserialize_value<std::uint32_t>(deserializer);
1548
+ }
1549
+
1550
+ bucket.set_value_of_empty_bucket(
1551
+ dist_from_ideal_bucket, truncated_hash,
1552
+ deserialize_value<value_type>(deserializer));
1553
+ }
1554
+ }
1555
+
1556
+ if (!m_buckets_data.empty()) {
1557
+ m_buckets_data.back().set_as_last_bucket();
1558
+ }
1559
+ }
1560
+ }
1561
+
1562
+ public:
1563
+ static const size_type DEFAULT_INIT_BUCKETS_SIZE = 0;
1564
+
1565
+ static constexpr float DEFAULT_MAX_LOAD_FACTOR = 0.5f;
1566
+ static constexpr float MINIMUM_MAX_LOAD_FACTOR = 0.2f;
1567
+ static constexpr float MAXIMUM_MAX_LOAD_FACTOR = 0.95f;
1568
+
1569
+ static constexpr float DEFAULT_MIN_LOAD_FACTOR = 0.0f;
1570
+ static constexpr float MINIMUM_MIN_LOAD_FACTOR = 0.0f;
1571
+ static constexpr float MAXIMUM_MIN_LOAD_FACTOR = 0.15f;
1572
+
1573
+ static_assert(MINIMUM_MAX_LOAD_FACTOR < MAXIMUM_MAX_LOAD_FACTOR,
1574
+ "MINIMUM_MAX_LOAD_FACTOR should be < MAXIMUM_MAX_LOAD_FACTOR");
1575
+ static_assert(MINIMUM_MIN_LOAD_FACTOR < MAXIMUM_MIN_LOAD_FACTOR,
1576
+ "MINIMUM_MIN_LOAD_FACTOR should be < MAXIMUM_MIN_LOAD_FACTOR");
1577
+ static_assert(MAXIMUM_MIN_LOAD_FACTOR < MINIMUM_MAX_LOAD_FACTOR,
1578
+ "MAXIMUM_MIN_LOAD_FACTOR should be < MINIMUM_MAX_LOAD_FACTOR");
1579
+
1580
+ private:
1581
+ /**
1582
+ * Protocol version currenlty used for serialization.
1583
+ */
1584
+ static const slz_size_type SERIALIZATION_PROTOCOL_VERSION = 1;
1585
+
1586
+ /**
1587
+ * Return an always valid pointer to an static empty bucket_entry with
1588
+ * last_bucket() == true.
1589
+ */
1590
+ bucket_entry* static_empty_bucket_ptr() noexcept {
1591
+ static bucket_entry empty_bucket(true);
1592
+ tsl_rh_assert(empty_bucket.empty());
1593
+ return &empty_bucket;
1594
+ }
1595
+
1596
+ private:
1597
+ buckets_container_type m_buckets_data;
1598
+
1599
+ /**
1600
+ * Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points
1601
+ * to static_empty_bucket_ptr. This variable is useful to avoid the cost of
1602
+ * checking if m_buckets_data is empty when trying to find an element.
1603
+ *
1604
+ * TODO Remove m_buckets_data and only use a pointer instead of a
1605
+ * pointer+vector to save some space in the robin_hash object. Manage the
1606
+ * Allocator manually.
1607
+ */
1608
+ bucket_entry* m_buckets;
1609
+
1610
+ /**
1611
+ * Used a lot in find, avoid the call to m_buckets_data.size() which is a bit
1612
+ * slower.
1613
+ */
1614
+ size_type m_bucket_count;
1615
+
1616
+ size_type m_nb_elements;
1617
+
1618
+ size_type m_load_threshold;
1619
+
1620
+ float m_min_load_factor;
1621
+ float m_max_load_factor;
1622
+
1623
+ bool m_grow_on_next_insert;
1624
+
1625
+ /**
1626
+ * We can't shrink down the map on erase operations as the erase methods need
1627
+ * to return the next iterator. Shrinking the map would invalidate all the
1628
+ * iterators and we could not return the next iterator in a meaningful way, On
1629
+ * erase, we thus just indicate on erase that we should try to shrink the hash
1630
+ * table on the next insert if we go below the min_load_factor.
1631
+ */
1632
+ bool m_try_shrink_on_next_insert;
1633
+ };
1634
+
1635
+ } // namespace detail_robin_hash
1636
+
1637
+ } // namespace tsl
1638
+
1639
+ #endif