isotree 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -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 +2111 -0
  12. data/vendor/isotree/include/isotree_oop.hpp +394 -0
  13. data/vendor/isotree/inst/COPYRIGHTS +62 -0
  14. data/vendor/isotree/src/RcppExports.cpp +525 -52
  15. data/vendor/isotree/src/Rwrapper.cpp +1931 -268
  16. data/vendor/isotree/src/c_interface.cpp +953 -0
  17. data/vendor/isotree/src/crit.hpp +4232 -0
  18. data/vendor/isotree/src/dist.hpp +1886 -0
  19. data/vendor/isotree/src/exp_depth_table.hpp +134 -0
  20. data/vendor/isotree/src/extended.hpp +1444 -0
  21. data/vendor/isotree/src/external_facing_generic.hpp +399 -0
  22. data/vendor/isotree/src/fit_model.hpp +2401 -0
  23. data/vendor/isotree/src/{dealloc.cpp → headers_joined.hpp} +38 -22
  24. data/vendor/isotree/src/helpers_iforest.hpp +813 -0
  25. data/vendor/isotree/src/{impute.cpp → impute.hpp} +353 -122
  26. data/vendor/isotree/src/indexer.cpp +515 -0
  27. data/vendor/isotree/src/instantiate_template_headers.cpp +118 -0
  28. data/vendor/isotree/src/instantiate_template_headers.hpp +240 -0
  29. data/vendor/isotree/src/isoforest.hpp +1659 -0
  30. data/vendor/isotree/src/isotree.hpp +1804 -392
  31. data/vendor/isotree/src/isotree_exportable.hpp +99 -0
  32. data/vendor/isotree/src/merge_models.cpp +159 -16
  33. data/vendor/isotree/src/mult.hpp +1321 -0
  34. data/vendor/isotree/src/oop_interface.cpp +842 -0
  35. data/vendor/isotree/src/oop_interface.hpp +278 -0
  36. data/vendor/isotree/src/other_helpers.hpp +219 -0
  37. data/vendor/isotree/src/predict.hpp +1932 -0
  38. data/vendor/isotree/src/python_helpers.hpp +134 -0
  39. data/vendor/isotree/src/ref_indexer.hpp +154 -0
  40. data/vendor/isotree/src/robinmap/LICENSE +21 -0
  41. data/vendor/isotree/src/robinmap/README.md +483 -0
  42. data/vendor/isotree/src/robinmap/include/tsl/robin_growth_policy.h +406 -0
  43. data/vendor/isotree/src/robinmap/include/tsl/robin_hash.h +1620 -0
  44. data/vendor/isotree/src/robinmap/include/tsl/robin_map.h +807 -0
  45. data/vendor/isotree/src/robinmap/include/tsl/robin_set.h +660 -0
  46. data/vendor/isotree/src/serialize.cpp +4300 -139
  47. data/vendor/isotree/src/sql.cpp +141 -59
  48. data/vendor/isotree/src/subset_models.cpp +174 -0
  49. data/vendor/isotree/src/utils.hpp +3808 -0
  50. data/vendor/isotree/src/xoshiro.hpp +467 -0
  51. data/vendor/isotree/src/ziggurat.hpp +405 -0
  52. metadata +38 -104
  53. data/vendor/cereal/LICENSE +0 -24
  54. data/vendor/cereal/README.md +0 -85
  55. data/vendor/cereal/include/cereal/access.hpp +0 -351
  56. data/vendor/cereal/include/cereal/archives/adapters.hpp +0 -163
  57. data/vendor/cereal/include/cereal/archives/binary.hpp +0 -169
  58. data/vendor/cereal/include/cereal/archives/json.hpp +0 -1019
  59. data/vendor/cereal/include/cereal/archives/portable_binary.hpp +0 -334
  60. data/vendor/cereal/include/cereal/archives/xml.hpp +0 -956
  61. data/vendor/cereal/include/cereal/cereal.hpp +0 -1089
  62. data/vendor/cereal/include/cereal/details/helpers.hpp +0 -422
  63. data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +0 -796
  64. data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +0 -65
  65. data/vendor/cereal/include/cereal/details/static_object.hpp +0 -127
  66. data/vendor/cereal/include/cereal/details/traits.hpp +0 -1411
  67. data/vendor/cereal/include/cereal/details/util.hpp +0 -84
  68. data/vendor/cereal/include/cereal/external/base64.hpp +0 -134
  69. data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +0 -284
  70. data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +0 -78
  71. data/vendor/cereal/include/cereal/external/rapidjson/document.h +0 -2652
  72. data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +0 -299
  73. data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +0 -716
  74. data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +0 -74
  75. data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +0 -161
  76. data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +0 -99
  77. data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +0 -104
  78. data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +0 -151
  79. data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +0 -290
  80. data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +0 -271
  81. data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +0 -245
  82. data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +0 -78
  83. data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +0 -308
  84. data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +0 -186
  85. data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +0 -55
  86. data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +0 -740
  87. data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +0 -232
  88. data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +0 -69
  89. data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +0 -290
  90. data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +0 -46
  91. data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +0 -128
  92. data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +0 -70
  93. data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +0 -71
  94. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +0 -316
  95. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +0 -300
  96. data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +0 -81
  97. data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +0 -1414
  98. data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +0 -277
  99. data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +0 -656
  100. data/vendor/cereal/include/cereal/external/rapidjson/reader.h +0 -2230
  101. data/vendor/cereal/include/cereal/external/rapidjson/schema.h +0 -2497
  102. data/vendor/cereal/include/cereal/external/rapidjson/stream.h +0 -223
  103. data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +0 -121
  104. data/vendor/cereal/include/cereal/external/rapidjson/writer.h +0 -709
  105. data/vendor/cereal/include/cereal/external/rapidxml/license.txt +0 -52
  106. data/vendor/cereal/include/cereal/external/rapidxml/manual.html +0 -406
  107. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +0 -2624
  108. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +0 -175
  109. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +0 -428
  110. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +0 -123
  111. data/vendor/cereal/include/cereal/macros.hpp +0 -154
  112. data/vendor/cereal/include/cereal/specialize.hpp +0 -139
  113. data/vendor/cereal/include/cereal/types/array.hpp +0 -79
  114. data/vendor/cereal/include/cereal/types/atomic.hpp +0 -55
  115. data/vendor/cereal/include/cereal/types/base_class.hpp +0 -203
  116. data/vendor/cereal/include/cereal/types/bitset.hpp +0 -176
  117. data/vendor/cereal/include/cereal/types/boost_variant.hpp +0 -164
  118. data/vendor/cereal/include/cereal/types/chrono.hpp +0 -72
  119. data/vendor/cereal/include/cereal/types/common.hpp +0 -129
  120. data/vendor/cereal/include/cereal/types/complex.hpp +0 -56
  121. data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +0 -73
  122. data/vendor/cereal/include/cereal/types/deque.hpp +0 -62
  123. data/vendor/cereal/include/cereal/types/forward_list.hpp +0 -68
  124. data/vendor/cereal/include/cereal/types/functional.hpp +0 -43
  125. data/vendor/cereal/include/cereal/types/list.hpp +0 -62
  126. data/vendor/cereal/include/cereal/types/map.hpp +0 -36
  127. data/vendor/cereal/include/cereal/types/memory.hpp +0 -425
  128. data/vendor/cereal/include/cereal/types/optional.hpp +0 -66
  129. data/vendor/cereal/include/cereal/types/polymorphic.hpp +0 -483
  130. data/vendor/cereal/include/cereal/types/queue.hpp +0 -132
  131. data/vendor/cereal/include/cereal/types/set.hpp +0 -103
  132. data/vendor/cereal/include/cereal/types/stack.hpp +0 -76
  133. data/vendor/cereal/include/cereal/types/string.hpp +0 -61
  134. data/vendor/cereal/include/cereal/types/tuple.hpp +0 -123
  135. data/vendor/cereal/include/cereal/types/unordered_map.hpp +0 -36
  136. data/vendor/cereal/include/cereal/types/unordered_set.hpp +0 -99
  137. data/vendor/cereal/include/cereal/types/utility.hpp +0 -47
  138. data/vendor/cereal/include/cereal/types/valarray.hpp +0 -89
  139. data/vendor/cereal/include/cereal/types/variant.hpp +0 -109
  140. data/vendor/cereal/include/cereal/types/vector.hpp +0 -112
  141. data/vendor/cereal/include/cereal/version.hpp +0 -52
  142. data/vendor/isotree/src/Makevars +0 -4
  143. data/vendor/isotree/src/crit.cpp +0 -912
  144. data/vendor/isotree/src/dist.cpp +0 -749
  145. data/vendor/isotree/src/extended.cpp +0 -790
  146. data/vendor/isotree/src/fit_model.cpp +0 -1090
  147. data/vendor/isotree/src/helpers_iforest.cpp +0 -324
  148. data/vendor/isotree/src/isoforest.cpp +0 -771
  149. data/vendor/isotree/src/mult.cpp +0 -607
  150. data/vendor/isotree/src/predict.cpp +0 -853
  151. data/vendor/isotree/src/utils.cpp +0 -1566
@@ -0,0 +1,483 @@
1
+ [![CI](https://github.com/Tessil/robin-map/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Tessil/robin-map/actions/workflows/ci.yml)
2
+
3
+ ## A C++ implementation of a fast hash map and hash set using robin hood hashing
4
+
5
+ The robin-map library is a C++ implementation of a fast hash map and hash set using open-addressing and linear robin hood hashing with backward shift deletion to resolve collisions.
6
+
7
+ Four classes are provided: `tsl::robin_map`, `tsl::robin_set`, `tsl::robin_pg_map` and `tsl::robin_pg_set`. The first two are faster and use a power of two growth policy, the last two use a prime growth policy instead and are able to cope better with a poor hash function. Use the prime version if there is a chance of repeating patterns in the lower bits of your hash (e.g. you are storing pointers with an identity hash function). See [GrowthPolicy](#growth-policy) for details.
8
+
9
+ A **benchmark** of `tsl::robin_map` against other hash maps may be found [here](https://tessil.github.io/2016/08/29/benchmark-hopscotch-map.html). This page also gives some advices on which hash table structure you should try for your use case (useful if you are a bit lost with the multiple hash tables implementations in the `tsl` namespace).
10
+
11
+ ### Key features
12
+
13
+ - Header-only library, just add the [include](include/) directory to your include path and you are ready to go. If you use CMake, you can also use the `tsl::robin_map` exported target from the [CMakeLists.txt](CMakeLists.txt).
14
+ - Fast hash table, check the [benchmark](https://tessil.github.io/2016/08/29/benchmark-hopscotch-map.html) for some numbers.
15
+ - Support for move-only and non-default constructible key/value.
16
+ - Support for heterogeneous lookups allowing the usage of `find` with a type different than `Key` (e.g. if you have a map that uses `std::unique_ptr<foo>` as key, you can use a `foo*` or a `std::uintptr_t` as key parameter to `find` without constructing a `std::unique_ptr<foo>`, see [example](#heterogeneous-lookups)).
17
+ - No need to reserve any sentinel value from the keys.
18
+ - Possibility to store the hash value alongside the stored key-value for faster rehash and lookup if the hash or the key equal functions are expensive to compute. Note that hash may be stored even if not asked explicitly when the library can detect that it will have no impact on the size of the structure in memory due to alignment. See the [StoreHash](https://tessil.github.io/robin-map/classtsl_1_1robin__map.html#details) template parameter for details.
19
+ - If the hash is known before a lookup, it is possible to pass it as parameter to speed-up the lookup (see `precalculated_hash` parameter in [API](https://tessil.github.io/robin-map/classtsl_1_1robin__map.html#a35021b11aabb61820236692a54b3a0f8)).
20
+ - Support for efficient serialization and deserialization (see [example](#serialization) and the `serialize/deserialize` methods in the [API](https://tessil.github.io/robin-map/classtsl_1_1robin__map.html) for details).
21
+ - The library can be used with exceptions disabled (through `-fno-exceptions` option on Clang and GCC, without an `/EH` option on MSVC or simply by defining `TSL_NO_EXCEPTIONS`). `std::terminate` is used in replacement of the `throw` instruction when exceptions are disabled.
22
+ - API closely similar to `std::unordered_map` and `std::unordered_set`.
23
+
24
+ ### Differences compared to `std::unordered_map`
25
+
26
+ `tsl::robin_map` tries to have an interface similar to `std::unordered_map`, but some differences exist.
27
+ - The **strong exception guarantee only holds** if the following statement is true `std::is_nothrow_swappable<value_type>::value && std::is_nothrow_move_constructible<value_type>::value` (where `value_type` is `Key` for `tsl::robin_set` and `std::pair<Key, T>` for `tsl::robin_map`). Otherwise if an exception is thrown during the swap or the move, the structure may end up in a undefined state. Note that per the standard, a `value_type` with a noexcept copy constructor and no move constructor also satisfies this condition and will thus guarantee the strong exception guarantee for the structure (see [API](https://tessil.github.io/robin-map/classtsl_1_1robin__map.html#details) for details).
28
+ - The type `Key`, and also `T` in case of map, must be swappable. They must also be copy and/or move constructible.
29
+ - Iterator invalidation doesn't behave in the same way, any operation modifying the hash table invalidate them (see [API](https://tessil.github.io/robin-map/classtsl_1_1robin__map.html#details) for details).
30
+ - References and pointers to keys or values in the map are invalidated in the same way as iterators to these keys-values.
31
+ - For iterators of `tsl::robin_map`, `operator*()` and `operator->()` return a reference and a pointer to `const std::pair<Key, T>` instead of `std::pair<const Key, T>` making the value `T` not modifiable. To modify the value you have to call the `value()` method of the iterator to get a mutable reference. Example:
32
+ ```c++
33
+ tsl::robin_map<int, int> map = {{1, 1}, {2, 1}, {3, 1}};
34
+ for(auto it = map.begin(); it != map.end(); ++it) {
35
+ //it->second = 2; // Illegal
36
+ it.value() = 2; // Ok
37
+ }
38
+ ```
39
+ - No support for some buckets related methods (like `bucket_size`, `bucket`, ...).
40
+
41
+ These differences also apply between `std::unordered_set` and `tsl::robin_set`.
42
+
43
+ Thread-safety guarantees are the same as `std::unordered_map/set` (i.e. possible to have multiple readers with no writer).
44
+
45
+ ### Growth policy
46
+
47
+ The library supports multiple growth policies through the `GrowthPolicy` template parameter. Three policies are provided by the library but you can easily implement your own if needed.
48
+
49
+ * **[tsl::rh::power_of_two_growth_policy.](https://tessil.github.io/robin-map/classtsl_1_1rh_1_1power__of__two__growth__policy.html)** Default policy used by `tsl::robin_map/set`. This policy keeps the size of the bucket array of the hash table to a power of two. This constraint allows the policy to avoid the usage of the slow modulo operation to map a hash to a bucket, instead of <code>hash % 2<sup>n</sup></code>, it uses <code>hash & (2<sup>n</sup> - 1)</code> (see [fast modulo](https://en.wikipedia.org/wiki/Modulo_operation#Performance_issues)). Fast but this may cause a lot of collisions with a poor hash function as the modulo with a power of two only masks the most significant bits in the end.
50
+ * **[tsl::rh::prime_growth_policy.](https://tessil.github.io/robin-map/classtsl_1_1rh_1_1prime__growth__policy.html)** Default policy used by `tsl::robin_pg_map/set`. The policy keeps the size of the bucket array of the hash table to a prime number. When mapping a hash to a bucket, using a prime number as modulo will result in a better distribution of the hash across the buckets even with a poor hash function. To allow the compiler to optimize the modulo operation, the policy use a lookup table with constant primes modulos (see [API](https://tessil.github.io/robin-map/classtsl_1_1rh_1_1prime__growth__policy.html#details) for details). Slower than `tsl::rh::power_of_two_growth_policy` but more secure.
51
+ * **[tsl::rh::mod_growth_policy.](https://tessil.github.io/robin-map/classtsl_1_1rh_1_1mod__growth__policy.html)** The policy grows the map by a customizable growth factor passed in parameter. It then just use the modulo operator to map a hash to a bucket. Slower but more flexible.
52
+
53
+
54
+ To implement your own policy, you have to implement the following interface.
55
+
56
+ ```c++
57
+ struct custom_policy {
58
+ // Called on hash table construction and rehash, min_bucket_count_in_out is the minimum buckets
59
+ // that the hash table needs. The policy can change it to a higher number of buckets if needed
60
+ // and the hash table will use this value as bucket count. If 0 bucket is asked, then the value
61
+ // must stay at 0.
62
+ explicit custom_policy(std::size_t& min_bucket_count_in_out);
63
+
64
+ // Return the bucket [0, bucket_count()) to which the hash belongs.
65
+ // If bucket_count() is 0, it must always return 0.
66
+ std::size_t bucket_for_hash(std::size_t hash) const noexcept;
67
+
68
+ // Return the number of buckets that should be used on next growth
69
+ std::size_t next_bucket_count() const;
70
+
71
+ // Maximum number of buckets supported by the policy
72
+ std::size_t max_bucket_count() const;
73
+
74
+ // Reset the growth policy as if the policy was created with a bucket count of 0.
75
+ // After a clear, the policy must always return 0 when bucket_for_hash() is called.
76
+ void clear() noexcept;
77
+ }
78
+ ```
79
+
80
+ ### Installation
81
+
82
+ To use robin-map, just add the [include](include/) directory to your include path. It is a **header-only** library.
83
+
84
+ If you use CMake, you can also use the `tsl::robin_map` exported target from the [CMakeLists.txt](CMakeLists.txt) with `target_link_libraries`.
85
+ ```cmake
86
+ # Example where the robin-map project is stored in a third-party directory
87
+ add_subdirectory(third-party/robin-map)
88
+ target_link_libraries(your_target PRIVATE tsl::robin_map)
89
+ ```
90
+
91
+ If the project has been installed through `make install`, you can also use `find_package(tsl-robin-map REQUIRED)` instead of `add_subdirectory`.
92
+
93
+ The library is available in [vcpkg](https://github.com/Microsoft/vcpkg/tree/master/ports/robin-map) and [conan](https://bintray.com/tessil/tsl/tsl-robin-map%3Atessil). It's also present in [Debian](https://packages.debian.org/buster/robin-map-dev), [Ubuntu](https://packages.ubuntu.com/disco/robin-map-dev) and [Fedora](https://apps.fedoraproject.org/packages/robin-map-devel) package repositories.
94
+
95
+ The code should work with any C++11 standard-compliant compiler and has been tested with GCC 4.8.4, Clang 3.5.0 and Visual Studio 2015.
96
+
97
+ To run the tests you will need the Boost Test library and CMake.
98
+
99
+ ```bash
100
+ git clone https://github.com/Tessil/robin-map.git
101
+ cd robin-map/tests
102
+ mkdir build
103
+ cd build
104
+ cmake ..
105
+ cmake --build .
106
+ ./tsl_robin_map_tests
107
+ ```
108
+
109
+ ### Usage
110
+
111
+ The API can be found [here](https://tessil.github.io/robin-map/).
112
+
113
+ All methods are not documented yet, but they replicate the behavior of the ones in `std::unordered_map` and `std::unordered_set`, except if specified otherwise.
114
+
115
+
116
+ ### Example
117
+
118
+ ```c++
119
+ #include <cstdint>
120
+ #include <iostream>
121
+ #include <string>
122
+ #include <tsl/robin_map.h>
123
+ #include <tsl/robin_set.h>
124
+
125
+ int main() {
126
+ tsl::robin_map<std::string, int> map = {{"a", 1}, {"b", 2}};
127
+ map["c"] = 3;
128
+ map["d"] = 4;
129
+
130
+ map.insert({"e", 5});
131
+ map.erase("b");
132
+
133
+ for(auto it = map.begin(); it != map.end(); ++it) {
134
+ //it->second += 2; // Not valid.
135
+ it.value() += 2;
136
+ }
137
+
138
+ // {d, 6} {a, 3} {e, 7} {c, 5}
139
+ for(const auto& key_value : map) {
140
+ std::cout << "{" << key_value.first << ", " << key_value.second << "}" << std::endl;
141
+ }
142
+
143
+
144
+ if(map.find("a") != map.end()) {
145
+ std::cout << "Found \"a\"." << std::endl;
146
+ }
147
+
148
+ const std::size_t precalculated_hash = std::hash<std::string>()("a");
149
+ // If we already know the hash beforehand, we can pass it in parameter to speed-up lookups.
150
+ if(map.find("a", precalculated_hash) != map.end()) {
151
+ std::cout << "Found \"a\" with hash " << precalculated_hash << "." << std::endl;
152
+ }
153
+
154
+
155
+ /*
156
+ * Calculating the hash and comparing two std::string may be slow.
157
+ * We can store the hash of each std::string in the hash map to make
158
+ * the inserts and lookups faster by setting StoreHash to true.
159
+ */
160
+ tsl::robin_map<std::string, int, std::hash<std::string>,
161
+ std::equal_to<std::string>,
162
+ std::allocator<std::pair<std::string, int>>,
163
+ true> map2;
164
+
165
+ map2["a"] = 1;
166
+ map2["b"] = 2;
167
+
168
+ // {a, 1} {b, 2}
169
+ for(const auto& key_value : map2) {
170
+ std::cout << "{" << key_value.first << ", " << key_value.second << "}" << std::endl;
171
+ }
172
+
173
+
174
+
175
+
176
+ tsl::robin_set<int> set;
177
+ set.insert({1, 9, 0});
178
+ set.insert({2, -1, 9});
179
+
180
+ // {0} {1} {2} {9} {-1}
181
+ for(const auto& key : set) {
182
+ std::cout << "{" << key << "}" << std::endl;
183
+ }
184
+ }
185
+ ```
186
+
187
+ #### Heterogeneous lookups
188
+
189
+ Heterogeneous overloads allow the usage of other types than `Key` for lookup and erase operations as long as the used types are hashable and comparable to `Key`.
190
+
191
+ To activate the heterogeneous overloads in `tsl::robin_map/set`, the qualified-id `KeyEqual::is_transparent` must be valid. It works the same way as for [`std::map::find`](http://en.cppreference.com/w/cpp/container/map/find). You can either use [`std::equal_to<>`](http://en.cppreference.com/w/cpp/utility/functional/equal_to_void) or define your own function object.
192
+
193
+ Both `KeyEqual` and `Hash` will need to be able to deal with the different types.
194
+
195
+ ```c++
196
+ #include <functional>
197
+ #include <iostream>
198
+ #include <string>
199
+ #include <tsl/robin_map.h>
200
+
201
+
202
+ struct employee {
203
+ employee(int id, std::string name) : m_id(id), m_name(std::move(name)) {
204
+ }
205
+
206
+ // Either we include the comparators in the class and we use `std::equal_to<>`...
207
+ friend bool operator==(const employee& empl, int empl_id) {
208
+ return empl.m_id == empl_id;
209
+ }
210
+
211
+ friend bool operator==(int empl_id, const employee& empl) {
212
+ return empl_id == empl.m_id;
213
+ }
214
+
215
+ friend bool operator==(const employee& empl1, const employee& empl2) {
216
+ return empl1.m_id == empl2.m_id;
217
+ }
218
+
219
+
220
+ int m_id;
221
+ std::string m_name;
222
+ };
223
+
224
+ // ... or we implement a separate class to compare employees.
225
+ struct equal_employee {
226
+ using is_transparent = void;
227
+
228
+ bool operator()(const employee& empl, int empl_id) const {
229
+ return empl.m_id == empl_id;
230
+ }
231
+
232
+ bool operator()(int empl_id, const employee& empl) const {
233
+ return empl_id == empl.m_id;
234
+ }
235
+
236
+ bool operator()(const employee& empl1, const employee& empl2) const {
237
+ return empl1.m_id == empl2.m_id;
238
+ }
239
+ };
240
+
241
+ struct hash_employee {
242
+ std::size_t operator()(const employee& empl) const {
243
+ return std::hash<int>()(empl.m_id);
244
+ }
245
+
246
+ std::size_t operator()(int id) const {
247
+ return std::hash<int>()(id);
248
+ }
249
+ };
250
+
251
+
252
+ int main() {
253
+ // Use std::equal_to<> which will automatically deduce and forward the parameters
254
+ tsl::robin_map<employee, int, hash_employee, std::equal_to<>> map;
255
+ map.insert({employee(1, "John Doe"), 2001});
256
+ map.insert({employee(2, "Jane Doe"), 2002});
257
+ map.insert({employee(3, "John Smith"), 2003});
258
+
259
+ // John Smith 2003
260
+ auto it = map.find(3);
261
+ if(it != map.end()) {
262
+ std::cout << it->first.m_name << " " << it->second << std::endl;
263
+ }
264
+
265
+ map.erase(1);
266
+
267
+
268
+
269
+ // Use a custom KeyEqual which has an is_transparent member type
270
+ tsl::robin_map<employee, int, hash_employee, equal_employee> map2;
271
+ map2.insert({employee(4, "Johnny Doe"), 2004});
272
+
273
+ // 2004
274
+ std::cout << map2.at(4) << std::endl;
275
+ }
276
+ ```
277
+
278
+
279
+ #### Serialization
280
+
281
+ The library provides an efficient way to serialize and deserialize a map or a set so that it can be saved to a file or send through the network.
282
+ To do so, it requires the user to provide a function object for both serialization and deserialization.
283
+
284
+ ```c++
285
+ struct serializer {
286
+ // Must support the following types for U: std::int16_t, std::uint32_t,
287
+ // std::uint64_t, float and std::pair<Key, T> if a map is used or Key for
288
+ // a set.
289
+ template<typename U>
290
+ void operator()(const U& value);
291
+ };
292
+ ```
293
+
294
+ ```c++
295
+ struct deserializer {
296
+ // Must support the following types for U: std::int16_t, std::uint32_t,
297
+ // std::uint64_t, float and std::pair<Key, T> if a map is used or Key for
298
+ // a set.
299
+ template<typename U>
300
+ U operator()();
301
+ };
302
+ ```
303
+
304
+ Note that the implementation leaves binary compatibility (endianness, float binary representation, size of int, ...) of the types it serializes/deserializes in the hands of the provided function objects if compatibility is required.
305
+
306
+ More details regarding the `serialize` and `deserialize` methods can be found in the [API](https://tessil.github.io/robin-map/classtsl_1_1robin__map.html).
307
+
308
+ ```c++
309
+ #include <cassert>
310
+ #include <cstdint>
311
+ #include <fstream>
312
+ #include <type_traits>
313
+ #include <tsl/robin_map.h>
314
+
315
+
316
+ class serializer {
317
+ public:
318
+ serializer(const char* file_name) {
319
+ m_ostream.exceptions(m_ostream.badbit | m_ostream.failbit);
320
+ m_ostream.open(file_name, std::ios::binary);
321
+ }
322
+
323
+ template<class T,
324
+ typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
325
+ void operator()(const T& value) {
326
+ m_ostream.write(reinterpret_cast<const char*>(&value), sizeof(T));
327
+ }
328
+
329
+ void operator()(const std::pair<std::int64_t, std::int64_t>& value) {
330
+ (*this)(value.first);
331
+ (*this)(value.second);
332
+ }
333
+
334
+ private:
335
+ std::ofstream m_ostream;
336
+ };
337
+
338
+ class deserializer {
339
+ public:
340
+ deserializer(const char* file_name) {
341
+ m_istream.exceptions(m_istream.badbit | m_istream.failbit | m_istream.eofbit);
342
+ m_istream.open(file_name, std::ios::binary);
343
+ }
344
+
345
+ template<class T>
346
+ T operator()() {
347
+ T value;
348
+ deserialize(value);
349
+
350
+ return value;
351
+ }
352
+
353
+ private:
354
+ template<class T,
355
+ typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
356
+ void deserialize(T& value) {
357
+ m_istream.read(reinterpret_cast<char*>(&value), sizeof(T));
358
+ }
359
+
360
+ void deserialize(std::pair<std::int64_t, std::int64_t>& value) {
361
+ deserialize(value.first);
362
+ deserialize(value.second);
363
+ }
364
+
365
+ private:
366
+ std::ifstream m_istream;
367
+ };
368
+
369
+
370
+ int main() {
371
+ const tsl::robin_map<std::int64_t, std::int64_t> map = {{1, -1}, {2, -2}, {3, -3}, {4, -4}};
372
+
373
+
374
+ const char* file_name = "robin_map.data";
375
+ {
376
+ serializer serial(file_name);
377
+ map.serialize(serial);
378
+ }
379
+
380
+ {
381
+ deserializer dserial(file_name);
382
+ auto map_deserialized = tsl::robin_map<std::int64_t, std::int64_t>::deserialize(dserial);
383
+
384
+ assert(map == map_deserialized);
385
+ }
386
+
387
+ {
388
+ deserializer dserial(file_name);
389
+
390
+ /**
391
+ * If the serialized and deserialized map are hash compatibles (see conditions in API),
392
+ * setting the argument to true speed-up the deserialization process as we don't have
393
+ * to recalculate the hash of each key. We also know how much space each bucket needs.
394
+ */
395
+ const bool hash_compatible = true;
396
+ auto map_deserialized =
397
+ tsl::robin_map<std::int64_t, std::int64_t>::deserialize(dserial, hash_compatible);
398
+
399
+ assert(map == map_deserialized);
400
+ }
401
+ }
402
+ ```
403
+
404
+ ##### Serialization with Boost Serialization and compression with zlib
405
+
406
+ It is possible to use a serialization library to avoid the boilerplate.
407
+
408
+ The following example uses Boost Serialization with the Boost zlib compression stream to reduce the size of the resulting serialized file. The example requires C++20 due to the usage of the template parameter list syntax in lambdas, but it can be adapted to less recent versions.
409
+
410
+ ```c++
411
+ #include <boost/archive/binary_iarchive.hpp>
412
+ #include <boost/archive/binary_oarchive.hpp>
413
+ #include <boost/iostreams/filter/zlib.hpp>
414
+ #include <boost/iostreams/filtering_stream.hpp>
415
+ #include <boost/serialization/split_free.hpp>
416
+ #include <boost/serialization/utility.hpp>
417
+ #include <cassert>
418
+ #include <cstdint>
419
+ #include <fstream>
420
+ #include <tsl/robin_map.h>
421
+
422
+
423
+ namespace boost { namespace serialization {
424
+ template<class Archive, class Key, class T>
425
+ void serialize(Archive & ar, tsl::robin_map<Key, T>& map, const unsigned int version) {
426
+ split_free(ar, map, version);
427
+ }
428
+
429
+ template<class Archive, class Key, class T>
430
+ void save(Archive & ar, const tsl::robin_map<Key, T>& map, const unsigned int /*version*/) {
431
+ auto serializer = [&ar](const auto& v) { ar & v; };
432
+ map.serialize(serializer);
433
+ }
434
+
435
+ template<class Archive, class Key, class T>
436
+ void load(Archive & ar, tsl::robin_map<Key, T>& map, const unsigned int /*version*/) {
437
+ auto deserializer = [&ar]<typename U>() { U u; ar & u; return u; };
438
+ map = tsl::robin_map<Key, T>::deserialize(deserializer);
439
+ }
440
+ }}
441
+
442
+
443
+ int main() {
444
+ tsl::robin_map<std::int64_t, std::int64_t> map = {{1, -1}, {2, -2}, {3, -3}, {4, -4}};
445
+
446
+
447
+ const char* file_name = "robin_map.data";
448
+ {
449
+ std::ofstream ofs;
450
+ ofs.exceptions(ofs.badbit | ofs.failbit);
451
+ ofs.open(file_name, std::ios::binary);
452
+
453
+ boost::iostreams::filtering_ostream fo;
454
+ fo.push(boost::iostreams::zlib_compressor());
455
+ fo.push(ofs);
456
+
457
+ boost::archive::binary_oarchive oa(fo);
458
+
459
+ oa << map;
460
+ }
461
+
462
+ {
463
+ std::ifstream ifs;
464
+ ifs.exceptions(ifs.badbit | ifs.failbit | ifs.eofbit);
465
+ ifs.open(file_name, std::ios::binary);
466
+
467
+ boost::iostreams::filtering_istream fi;
468
+ fi.push(boost::iostreams::zlib_decompressor());
469
+ fi.push(ifs);
470
+
471
+ boost::archive::binary_iarchive ia(fi);
472
+
473
+ tsl::robin_map<std::int64_t, std::int64_t> map_deserialized;
474
+ ia >> map_deserialized;
475
+
476
+ assert(map == map_deserialized);
477
+ }
478
+ }
479
+ ```
480
+
481
+ ### License
482
+
483
+ The code is licensed under the MIT license, see the [LICENSE file](LICENSE) for details.