node_hash_set.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: node_hash_set.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // An `y_absl::node_hash_set<T>` is an unordered associative container designed to
  20. // be a more efficient replacement for `std::unordered_set`. Like
  21. // `unordered_set`, search, insertion, and deletion of set elements can be done
  22. // as an `O(1)` operation. However, `node_hash_set` (and other unordered
  23. // associative containers known as the collection of Abseil "Swiss tables")
  24. // contain other optimizations that result in both memory and computation
  25. // advantages.
  26. //
  27. // In most cases, your default choice for a hash table should be a map of type
  28. // `flat_hash_map` or a set of type `flat_hash_set`. However, if you need
  29. // pointer stability, a `node_hash_set` should be your preferred choice. As
  30. // well, if you are migrating your code from using `std::unordered_set`, a
  31. // `node_hash_set` should be an easy migration. Consider migrating to
  32. // `node_hash_set` and perhaps converting to a more efficient `flat_hash_set`
  33. // upon further review.
  34. //
  35. // `node_hash_set` is not exception-safe.
  36. #ifndef Y_ABSL_CONTAINER_NODE_HASH_SET_H_
  37. #define Y_ABSL_CONTAINER_NODE_HASH_SET_H_
  38. #include <cstddef>
  39. #include <memory>
  40. #include <type_traits>
  41. #include "y_absl/algorithm/container.h"
  42. #include "y_absl/base/attributes.h"
  43. #include "y_absl/container/hash_container_defaults.h"
  44. #include "y_absl/container/internal/container_memory.h"
  45. #include "y_absl/container/internal/node_slot_policy.h"
  46. #include "y_absl/container/internal/raw_hash_set.h" // IWYU pragma: export
  47. #include "y_absl/memory/memory.h"
  48. #include "y_absl/meta/type_traits.h"
  49. namespace y_absl {
  50. Y_ABSL_NAMESPACE_BEGIN
  51. namespace container_internal {
  52. template <typename T>
  53. struct NodeHashSetPolicy;
  54. } // namespace container_internal
  55. // -----------------------------------------------------------------------------
  56. // y_absl::node_hash_set
  57. // -----------------------------------------------------------------------------
  58. //
  59. // An `y_absl::node_hash_set<T>` is an unordered associative container which
  60. // has been optimized for both speed and memory footprint in most common use
  61. // cases. Its interface is similar to that of `std::unordered_set<T>` with the
  62. // following notable differences:
  63. //
  64. // * Supports heterogeneous lookup, through `find()`, `operator[]()` and
  65. // `insert()`, provided that the set is provided a compatible heterogeneous
  66. // hashing function and equality operator. See below for details.
  67. // * Contains a `capacity()` member function indicating the number of element
  68. // slots (open, deleted, and empty) within the hash set.
  69. // * Returns `void` from the `erase(iterator)` overload.
  70. //
  71. // By default, `node_hash_set` uses the `y_absl::Hash` hashing framework.
  72. // All fundamental and Abseil types that support the `y_absl::Hash` framework have
  73. // a compatible equality operator for comparing insertions into `node_hash_set`.
  74. // If your type is not yet supported by the `y_absl::Hash` framework, see
  75. // y_absl/hash/hash.h for information on extending Abseil hashing to user-defined
  76. // types.
  77. //
  78. // Using `y_absl::node_hash_set` at interface boundaries in dynamically loaded
  79. // libraries (e.g. .dll, .so) is unsupported due to way `y_absl::Hash` values may
  80. // be randomized across dynamically loaded libraries.
  81. //
  82. // To achieve heterogeneous lookup for custom types either `Hash` and `Eq` type
  83. // parameters can be used or `T` should have public inner types
  84. // `absl_container_hash` and (optionally) `absl_container_eq`. In either case,
  85. // `typename Hash::is_transparent` and `typename Eq::is_transparent` should be
  86. // well-formed. Both types are basically functors:
  87. // * `Hash` should support `size_t operator()(U val) const` that returns a hash
  88. // for the given `val`.
  89. // * `Eq` should support `bool operator()(U lhs, V rhs) const` that returns true
  90. // if `lhs` is equal to `rhs`.
  91. //
  92. // In most cases `T` needs only to provide the `absl_container_hash`. In this
  93. // case `std::equal_to<void>` will be used instead of `eq` part.
  94. //
  95. // Example:
  96. //
  97. // // Create a node hash set of three strings
  98. // y_absl::node_hash_set<TString> ducks =
  99. // {"huey", "dewey", "louie"};
  100. //
  101. // // Insert a new element into the node hash set
  102. // ducks.insert("donald");
  103. //
  104. // // Force a rehash of the node hash set
  105. // ducks.rehash(0);
  106. //
  107. // // See if "dewey" is present
  108. // if (ducks.contains("dewey")) {
  109. // std::cout << "We found dewey!" << std::endl;
  110. // }
  111. template <class T, class Hash = DefaultHashContainerHash<T>,
  112. class Eq = DefaultHashContainerEq<T>, class Alloc = std::allocator<T>>
  113. class Y_ABSL_INTERNAL_ATTRIBUTE_OWNER node_hash_set
  114. : public y_absl::container_internal::raw_hash_set<
  115. y_absl::container_internal::NodeHashSetPolicy<T>, Hash, Eq, Alloc> {
  116. using Base = typename node_hash_set::raw_hash_set;
  117. public:
  118. // Constructors and Assignment Operators
  119. //
  120. // A node_hash_set supports the same overload set as `std::unordered_set`
  121. // for construction and assignment:
  122. //
  123. // * Default constructor
  124. //
  125. // // No allocation for the table's elements is made.
  126. // y_absl::node_hash_set<TString> set1;
  127. //
  128. // * Initializer List constructor
  129. //
  130. // y_absl::node_hash_set<TString> set2 =
  131. // {{"huey"}, {"dewey"}, {"louie"}};
  132. //
  133. // * Copy constructor
  134. //
  135. // y_absl::node_hash_set<TString> set3(set2);
  136. //
  137. // * Copy assignment operator
  138. //
  139. // // Hash functor and Comparator are copied as well
  140. // y_absl::node_hash_set<TString> set4;
  141. // set4 = set3;
  142. //
  143. // * Move constructor
  144. //
  145. // // Move is guaranteed efficient
  146. // y_absl::node_hash_set<TString> set5(std::move(set4));
  147. //
  148. // * Move assignment operator
  149. //
  150. // // May be efficient if allocators are compatible
  151. // y_absl::node_hash_set<TString> set6;
  152. // set6 = std::move(set5);
  153. //
  154. // * Range constructor
  155. //
  156. // std::vector<TString> v = {"a", "b"};
  157. // y_absl::node_hash_set<TString> set7(v.begin(), v.end());
  158. node_hash_set() {}
  159. using Base::Base;
  160. // node_hash_set::begin()
  161. //
  162. // Returns an iterator to the beginning of the `node_hash_set`.
  163. using Base::begin;
  164. // node_hash_set::cbegin()
  165. //
  166. // Returns a const iterator to the beginning of the `node_hash_set`.
  167. using Base::cbegin;
  168. // node_hash_set::cend()
  169. //
  170. // Returns a const iterator to the end of the `node_hash_set`.
  171. using Base::cend;
  172. // node_hash_set::end()
  173. //
  174. // Returns an iterator to the end of the `node_hash_set`.
  175. using Base::end;
  176. // node_hash_set::capacity()
  177. //
  178. // Returns the number of element slots (assigned, deleted, and empty)
  179. // available within the `node_hash_set`.
  180. //
  181. // NOTE: this member function is particular to `y_absl::node_hash_set` and is
  182. // not provided in the `std::unordered_set` API.
  183. using Base::capacity;
  184. // node_hash_set::empty()
  185. //
  186. // Returns whether or not the `node_hash_set` is empty.
  187. using Base::empty;
  188. // node_hash_set::max_size()
  189. //
  190. // Returns the largest theoretical possible number of elements within a
  191. // `node_hash_set` under current memory constraints. This value can be thought
  192. // of the largest value of `std::distance(begin(), end())` for a
  193. // `node_hash_set<T>`.
  194. using Base::max_size;
  195. // node_hash_set::size()
  196. //
  197. // Returns the number of elements currently within the `node_hash_set`.
  198. using Base::size;
  199. // node_hash_set::clear()
  200. //
  201. // Removes all elements from the `node_hash_set`. Invalidates any references,
  202. // pointers, or iterators referring to contained elements.
  203. //
  204. // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
  205. // the underlying buffer call `erase(begin(), end())`.
  206. using Base::clear;
  207. // node_hash_set::erase()
  208. //
  209. // Erases elements within the `node_hash_set`. Erasing does not trigger a
  210. // rehash. Overloads are listed below.
  211. //
  212. // void erase(const_iterator pos):
  213. //
  214. // Erases the element at `position` of the `node_hash_set`, returning
  215. // `void`.
  216. //
  217. // NOTE: this return behavior is different than that of STL containers in
  218. // general and `std::unordered_set` in particular.
  219. //
  220. // iterator erase(const_iterator first, const_iterator last):
  221. //
  222. // Erases the elements in the open interval [`first`, `last`), returning an
  223. // iterator pointing to `last`. The special case of calling
  224. // `erase(begin(), end())` resets the reserved growth such that if
  225. // `reserve(N)` has previously been called and there has been no intervening
  226. // call to `clear()`, then after calling `erase(begin(), end())`, it is safe
  227. // to assume that inserting N elements will not cause a rehash.
  228. //
  229. // size_type erase(const key_type& key):
  230. //
  231. // Erases the element with the matching key, if it exists, returning the
  232. // number of elements erased (0 or 1).
  233. using Base::erase;
  234. // node_hash_set::insert()
  235. //
  236. // Inserts an element of the specified value into the `node_hash_set`,
  237. // returning an iterator pointing to the newly inserted element, provided that
  238. // an element with the given key does not already exist. If rehashing occurs
  239. // due to the insertion, all iterators are invalidated. Overloads are listed
  240. // below.
  241. //
  242. // std::pair<iterator,bool> insert(const T& value):
  243. //
  244. // Inserts a value into the `node_hash_set`. Returns a pair consisting of an
  245. // iterator to the inserted element (or to the element that prevented the
  246. // insertion) and a bool denoting whether the insertion took place.
  247. //
  248. // std::pair<iterator,bool> insert(T&& value):
  249. //
  250. // Inserts a moveable value into the `node_hash_set`. Returns a pair
  251. // consisting of an iterator to the inserted element (or to the element that
  252. // prevented the insertion) and a bool denoting whether the insertion took
  253. // place.
  254. //
  255. // iterator insert(const_iterator hint, const T& value):
  256. // iterator insert(const_iterator hint, T&& value):
  257. //
  258. // Inserts a value, using the position of `hint` as a non-binding suggestion
  259. // for where to begin the insertion search. Returns an iterator to the
  260. // inserted element, or to the existing element that prevented the
  261. // insertion.
  262. //
  263. // void insert(InputIterator first, InputIterator last):
  264. //
  265. // Inserts a range of values [`first`, `last`).
  266. //
  267. // NOTE: Although the STL does not specify which element may be inserted if
  268. // multiple keys compare equivalently, for `node_hash_set` we guarantee the
  269. // first match is inserted.
  270. //
  271. // void insert(std::initializer_list<T> ilist):
  272. //
  273. // Inserts the elements within the initializer list `ilist`.
  274. //
  275. // NOTE: Although the STL does not specify which element may be inserted if
  276. // multiple keys compare equivalently within the initializer list, for
  277. // `node_hash_set` we guarantee the first match is inserted.
  278. using Base::insert;
  279. // node_hash_set::emplace()
  280. //
  281. // Inserts an element of the specified value by constructing it in-place
  282. // within the `node_hash_set`, provided that no element with the given key
  283. // already exists.
  284. //
  285. // The element may be constructed even if there already is an element with the
  286. // key in the container, in which case the newly constructed element will be
  287. // destroyed immediately.
  288. //
  289. // If rehashing occurs due to the insertion, all iterators are invalidated.
  290. using Base::emplace;
  291. // node_hash_set::emplace_hint()
  292. //
  293. // Inserts an element of the specified value by constructing it in-place
  294. // within the `node_hash_set`, using the position of `hint` as a non-binding
  295. // suggestion for where to begin the insertion search, and only inserts
  296. // provided that no element with the given key already exists.
  297. //
  298. // The element may be constructed even if there already is an element with the
  299. // key in the container, in which case the newly constructed element will be
  300. // destroyed immediately.
  301. //
  302. // If rehashing occurs due to the insertion, all iterators are invalidated.
  303. using Base::emplace_hint;
  304. // node_hash_set::extract()
  305. //
  306. // Extracts the indicated element, erasing it in the process, and returns it
  307. // as a C++17-compatible node handle. Overloads are listed below.
  308. //
  309. // node_type extract(const_iterator position):
  310. //
  311. // Extracts the element at the indicated position and returns a node handle
  312. // owning that extracted data.
  313. //
  314. // node_type extract(const key_type& x):
  315. //
  316. // Extracts the element with the key matching the passed key value and
  317. // returns a node handle owning that extracted data. If the `node_hash_set`
  318. // does not contain an element with a matching key, this function returns an
  319. // empty node handle.
  320. using Base::extract;
  321. // node_hash_set::merge()
  322. //
  323. // Extracts elements from a given `source` node hash set into this
  324. // `node_hash_set`. If the destination `node_hash_set` already contains an
  325. // element with an equivalent key, that element is not extracted.
  326. using Base::merge;
  327. // node_hash_set::swap(node_hash_set& other)
  328. //
  329. // Exchanges the contents of this `node_hash_set` with those of the `other`
  330. // node hash set, avoiding invocation of any move, copy, or swap operations on
  331. // individual elements.
  332. //
  333. // All iterators and references on the `node_hash_set` remain valid, excepting
  334. // for the past-the-end iterator, which is invalidated.
  335. //
  336. // `swap()` requires that the node hash set's hashing and key equivalence
  337. // functions be Swappable, and are exchanged using unqualified calls to
  338. // non-member `swap()`. If the set's allocator has
  339. // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
  340. // set to `true`, the allocators are also exchanged using an unqualified call
  341. // to non-member `swap()`; otherwise, the allocators are not swapped.
  342. using Base::swap;
  343. // node_hash_set::rehash(count)
  344. //
  345. // Rehashes the `node_hash_set`, setting the number of slots to be at least
  346. // the passed value. If the new number of slots increases the load factor more
  347. // than the current maximum load factor
  348. // (`count` < `size()` / `max_load_factor()`), then the new number of slots
  349. // will be at least `size()` / `max_load_factor()`.
  350. //
  351. // To force a rehash, pass rehash(0).
  352. //
  353. // NOTE: unlike behavior in `std::unordered_set`, references are also
  354. // invalidated upon a `rehash()`.
  355. using Base::rehash;
  356. // node_hash_set::reserve(count)
  357. //
  358. // Sets the number of slots in the `node_hash_set` to the number needed to
  359. // accommodate at least `count` total elements without exceeding the current
  360. // maximum load factor, and may rehash the container if needed.
  361. using Base::reserve;
  362. // node_hash_set::contains()
  363. //
  364. // Determines whether an element comparing equal to the given `key` exists
  365. // within the `node_hash_set`, returning `true` if so or `false` otherwise.
  366. using Base::contains;
  367. // node_hash_set::count(const Key& key) const
  368. //
  369. // Returns the number of elements comparing equal to the given `key` within
  370. // the `node_hash_set`. note that this function will return either `1` or `0`
  371. // since duplicate elements are not allowed within a `node_hash_set`.
  372. using Base::count;
  373. // node_hash_set::equal_range()
  374. //
  375. // Returns a closed range [first, last], defined by a `std::pair` of two
  376. // iterators, containing all elements with the passed key in the
  377. // `node_hash_set`.
  378. using Base::equal_range;
  379. // node_hash_set::find()
  380. //
  381. // Finds an element with the passed `key` within the `node_hash_set`.
  382. using Base::find;
  383. // node_hash_set::bucket_count()
  384. //
  385. // Returns the number of "buckets" within the `node_hash_set`. Note that
  386. // because a node hash set contains all elements within its internal storage,
  387. // this value simply equals the current capacity of the `node_hash_set`.
  388. using Base::bucket_count;
  389. // node_hash_set::load_factor()
  390. //
  391. // Returns the current load factor of the `node_hash_set` (the average number
  392. // of slots occupied with a value within the hash set).
  393. using Base::load_factor;
  394. // node_hash_set::max_load_factor()
  395. //
  396. // Manages the maximum load factor of the `node_hash_set`. Overloads are
  397. // listed below.
  398. //
  399. // float node_hash_set::max_load_factor()
  400. //
  401. // Returns the current maximum load factor of the `node_hash_set`.
  402. //
  403. // void node_hash_set::max_load_factor(float ml)
  404. //
  405. // Sets the maximum load factor of the `node_hash_set` to the passed value.
  406. //
  407. // NOTE: This overload is provided only for API compatibility with the STL;
  408. // `node_hash_set` will ignore any set load factor and manage its rehashing
  409. // internally as an implementation detail.
  410. using Base::max_load_factor;
  411. // node_hash_set::get_allocator()
  412. //
  413. // Returns the allocator function associated with this `node_hash_set`.
  414. using Base::get_allocator;
  415. // node_hash_set::hash_function()
  416. //
  417. // Returns the hashing function used to hash the keys within this
  418. // `node_hash_set`.
  419. using Base::hash_function;
  420. // node_hash_set::key_eq()
  421. //
  422. // Returns the function used for comparing keys equality.
  423. using Base::key_eq;
  424. };
  425. // erase_if(node_hash_set<>, Pred)
  426. //
  427. // Erases all elements that satisfy the predicate `pred` from the container `c`.
  428. // Returns the number of erased elements.
  429. template <typename T, typename H, typename E, typename A, typename Predicate>
  430. typename node_hash_set<T, H, E, A>::size_type erase_if(
  431. node_hash_set<T, H, E, A>& c, Predicate pred) {
  432. return container_internal::EraseIf(pred, &c);
  433. }
  434. namespace container_internal {
  435. // c_for_each_fast(node_hash_set<>, Function)
  436. //
  437. // Container-based version of the <algorithm> `std::for_each()` function to
  438. // apply a function to a container's elements.
  439. // There is no guarantees on the order of the function calls.
  440. // Erasure and/or insertion of elements in the function is not allowed.
  441. template <typename T, typename H, typename E, typename A, typename Function>
  442. decay_t<Function> c_for_each_fast(const node_hash_set<T, H, E, A>& c,
  443. Function&& f) {
  444. container_internal::ForEach(f, &c);
  445. return f;
  446. }
  447. template <typename T, typename H, typename E, typename A, typename Function>
  448. decay_t<Function> c_for_each_fast(node_hash_set<T, H, E, A>& c, Function&& f) {
  449. container_internal::ForEach(f, &c);
  450. return f;
  451. }
  452. template <typename T, typename H, typename E, typename A, typename Function>
  453. decay_t<Function> c_for_each_fast(node_hash_set<T, H, E, A>&& c, Function&& f) {
  454. container_internal::ForEach(f, &c);
  455. return f;
  456. }
  457. } // namespace container_internal
  458. namespace container_internal {
  459. template <class T>
  460. struct NodeHashSetPolicy
  461. : y_absl::container_internal::node_slot_policy<T&, NodeHashSetPolicy<T>> {
  462. using key_type = T;
  463. using init_type = T;
  464. using constant_iterators = std::true_type;
  465. template <class Allocator, class... Args>
  466. static T* new_element(Allocator* alloc, Args&&... args) {
  467. using ValueAlloc =
  468. typename y_absl::allocator_traits<Allocator>::template rebind_alloc<T>;
  469. ValueAlloc value_alloc(*alloc);
  470. T* res = y_absl::allocator_traits<ValueAlloc>::allocate(value_alloc, 1);
  471. y_absl::allocator_traits<ValueAlloc>::construct(value_alloc, res,
  472. std::forward<Args>(args)...);
  473. return res;
  474. }
  475. template <class Allocator>
  476. static void delete_element(Allocator* alloc, T* elem) {
  477. using ValueAlloc =
  478. typename y_absl::allocator_traits<Allocator>::template rebind_alloc<T>;
  479. ValueAlloc value_alloc(*alloc);
  480. y_absl::allocator_traits<ValueAlloc>::destroy(value_alloc, elem);
  481. y_absl::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1);
  482. }
  483. template <class F, class... Args>
  484. static decltype(y_absl::container_internal::DecomposeValue(
  485. std::declval<F>(), std::declval<Args>()...))
  486. apply(F&& f, Args&&... args) {
  487. return y_absl::container_internal::DecomposeValue(
  488. std::forward<F>(f), std::forward<Args>(args)...);
  489. }
  490. static size_t element_space_used(const T*) { return sizeof(T); }
  491. template <class Hash>
  492. static constexpr HashSlotFn get_hash_slot_fn() {
  493. return &TypeErasedDerefAndApplyToSlotFn<Hash, T>;
  494. }
  495. };
  496. } // namespace container_internal
  497. namespace container_algorithm_internal {
  498. // Specialization of trait in y_absl/algorithm/container.h
  499. template <class Key, class Hash, class KeyEqual, class Allocator>
  500. struct IsUnorderedContainer<y_absl::node_hash_set<Key, Hash, KeyEqual, Allocator>>
  501. : std::true_type {};
  502. } // namespace container_algorithm_internal
  503. Y_ABSL_NAMESPACE_END
  504. } // namespace y_absl
  505. #endif // Y_ABSL_CONTAINER_NODE_HASH_SET_H_