node_hash_set.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 `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 ABSL_CONTAINER_NODE_HASH_SET_H_
  37. #define ABSL_CONTAINER_NODE_HASH_SET_H_
  38. #include <cstddef>
  39. #include <memory>
  40. #include <type_traits>
  41. #include "absl/algorithm/container.h"
  42. #include "absl/base/attributes.h"
  43. #include "absl/container/hash_container_defaults.h"
  44. #include "absl/container/internal/container_memory.h"
  45. #include "absl/container/internal/node_slot_policy.h"
  46. #include "absl/container/internal/raw_hash_set.h" // IWYU pragma: export
  47. #include "absl/memory/memory.h"
  48. #include "absl/meta/type_traits.h"
  49. namespace absl {
  50. ABSL_NAMESPACE_BEGIN
  51. namespace container_internal {
  52. template <typename T>
  53. struct NodeHashSetPolicy;
  54. } // namespace container_internal
  55. // -----------------------------------------------------------------------------
  56. // absl::node_hash_set
  57. // -----------------------------------------------------------------------------
  58. //
  59. // An `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 `absl::Hash` hashing framework.
  72. // All fundamental and Abseil types that support the `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 `absl::Hash` framework, see
  75. // absl/hash/hash.h for information on extending Abseil hashing to user-defined
  76. // types.
  77. //
  78. // Using `absl::node_hash_set` at interface boundaries in dynamically loaded
  79. // libraries (e.g. .dll, .so) is unsupported due to way `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. // absl::node_hash_set<std::string> 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 ABSL_ATTRIBUTE_OWNER node_hash_set
  114. : public absl::container_internal::raw_hash_set<
  115. 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. // absl::node_hash_set<std::string> set1;
  127. //
  128. // * Initializer List constructor
  129. //
  130. // absl::node_hash_set<std::string> set2 =
  131. // {{"huey"}, {"dewey"}, {"louie"}};
  132. //
  133. // * Copy constructor
  134. //
  135. // absl::node_hash_set<std::string> set3(set2);
  136. //
  137. // * Copy assignment operator
  138. //
  139. // // Hash functor and Comparator are copied as well
  140. // absl::node_hash_set<std::string> set4;
  141. // set4 = set3;
  142. //
  143. // * Move constructor
  144. //
  145. // // Move is guaranteed efficient
  146. // absl::node_hash_set<std::string> set5(std::move(set4));
  147. //
  148. // * Move assignment operator
  149. //
  150. // // May be efficient if allocators are compatible
  151. // absl::node_hash_set<std::string> set6;
  152. // set6 = std::move(set5);
  153. //
  154. // * Range constructor
  155. //
  156. // std::vector<std::string> v = {"a", "b"};
  157. // absl::node_hash_set<std::string> 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 `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: Returning `void` in this case is different than that of STL
  218. // containers in general and `std::unordered_map` in particular (which
  219. // return an iterator to the element following the erased element). If that
  220. // iterator is needed, simply post increment the iterator:
  221. //
  222. // map.erase(it++);
  223. //
  224. //
  225. // iterator erase(const_iterator first, const_iterator last):
  226. //
  227. // Erases the elements in the open interval [`first`, `last`), returning an
  228. // iterator pointing to `last`. The special case of calling
  229. // `erase(begin(), end())` resets the reserved growth such that if
  230. // `reserve(N)` has previously been called and there has been no intervening
  231. // call to `clear()`, then after calling `erase(begin(), end())`, it is safe
  232. // to assume that inserting N elements will not cause a rehash.
  233. //
  234. // size_type erase(const key_type& key):
  235. //
  236. // Erases the element with the matching key, if it exists, returning the
  237. // number of elements erased (0 or 1).
  238. using Base::erase;
  239. // node_hash_set::insert()
  240. //
  241. // Inserts an element of the specified value into the `node_hash_set`,
  242. // returning an iterator pointing to the newly inserted element, provided that
  243. // an element with the given key does not already exist. If rehashing occurs
  244. // due to the insertion, all iterators are invalidated. Overloads are listed
  245. // below.
  246. //
  247. // std::pair<iterator,bool> insert(const T& value):
  248. //
  249. // Inserts a value into the `node_hash_set`. Returns a pair consisting of an
  250. // iterator to the inserted element (or to the element that prevented the
  251. // insertion) and a bool denoting whether the insertion took place.
  252. //
  253. // std::pair<iterator,bool> insert(T&& value):
  254. //
  255. // Inserts a moveable value into the `node_hash_set`. Returns a pair
  256. // consisting of an iterator to the inserted element (or to the element that
  257. // prevented the insertion) and a bool denoting whether the insertion took
  258. // place.
  259. //
  260. // iterator insert(const_iterator hint, const T& value):
  261. // iterator insert(const_iterator hint, T&& value):
  262. //
  263. // Inserts a value, using the position of `hint` as a non-binding suggestion
  264. // for where to begin the insertion search. Returns an iterator to the
  265. // inserted element, or to the existing element that prevented the
  266. // insertion.
  267. //
  268. // void insert(InputIterator first, InputIterator last):
  269. //
  270. // Inserts a range of values [`first`, `last`).
  271. //
  272. // NOTE: Although the STL does not specify which element may be inserted if
  273. // multiple keys compare equivalently, for `node_hash_set` we guarantee the
  274. // first match is inserted.
  275. //
  276. // void insert(std::initializer_list<T> ilist):
  277. //
  278. // Inserts the elements within the initializer list `ilist`.
  279. //
  280. // NOTE: Although the STL does not specify which element may be inserted if
  281. // multiple keys compare equivalently within the initializer list, for
  282. // `node_hash_set` we guarantee the first match is inserted.
  283. using Base::insert;
  284. // node_hash_set::emplace()
  285. //
  286. // Inserts an element of the specified value by constructing it in-place
  287. // within the `node_hash_set`, provided that no element with the given key
  288. // already exists.
  289. //
  290. // The element may be constructed even if there already is an element with the
  291. // key in the container, in which case the newly constructed element will be
  292. // destroyed immediately.
  293. //
  294. // If rehashing occurs due to the insertion, all iterators are invalidated.
  295. using Base::emplace;
  296. // node_hash_set::emplace_hint()
  297. //
  298. // Inserts an element of the specified value by constructing it in-place
  299. // within the `node_hash_set`, using the position of `hint` as a non-binding
  300. // suggestion for where to begin the insertion search, and only inserts
  301. // provided that no element with the given key already exists.
  302. //
  303. // The element may be constructed even if there already is an element with the
  304. // key in the container, in which case the newly constructed element will be
  305. // destroyed immediately.
  306. //
  307. // If rehashing occurs due to the insertion, all iterators are invalidated.
  308. using Base::emplace_hint;
  309. // node_hash_set::extract()
  310. //
  311. // Extracts the indicated element, erasing it in the process, and returns it
  312. // as a C++17-compatible node handle. Overloads are listed below.
  313. //
  314. // node_type extract(const_iterator position):
  315. //
  316. // Extracts the element at the indicated position and returns a node handle
  317. // owning that extracted data.
  318. //
  319. // node_type extract(const key_type& x):
  320. //
  321. // Extracts the element with the key matching the passed key value and
  322. // returns a node handle owning that extracted data. If the `node_hash_set`
  323. // does not contain an element with a matching key, this function returns an
  324. // empty node handle.
  325. using Base::extract;
  326. // node_hash_set::merge()
  327. //
  328. // Extracts elements from a given `source` node hash set into this
  329. // `node_hash_set`. If the destination `node_hash_set` already contains an
  330. // element with an equivalent key, that element is not extracted.
  331. using Base::merge;
  332. // node_hash_set::swap(node_hash_set& other)
  333. //
  334. // Exchanges the contents of this `node_hash_set` with those of the `other`
  335. // node hash set.
  336. //
  337. // All iterators and references on the `node_hash_set` remain valid, excepting
  338. // for the past-the-end iterator, which is invalidated.
  339. //
  340. // `swap()` requires that the node hash set's hashing and key equivalence
  341. // functions be Swappable, and are exchanged using unqualified calls to
  342. // non-member `swap()`. If the set's allocator has
  343. // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
  344. // set to `true`, the allocators are also exchanged using an unqualified call
  345. // to non-member `swap()`; otherwise, the allocators are not swapped.
  346. using Base::swap;
  347. // node_hash_set::rehash(count)
  348. //
  349. // Rehashes the `node_hash_set`, setting the number of slots to be at least
  350. // the passed value. If the new number of slots increases the load factor more
  351. // than the current maximum load factor
  352. // (`count` < `size()` / `max_load_factor()`), then the new number of slots
  353. // will be at least `size()` / `max_load_factor()`.
  354. //
  355. // To force a rehash, pass rehash(0).
  356. //
  357. // NOTE: unlike behavior in `std::unordered_set`, references are also
  358. // invalidated upon a `rehash()`.
  359. using Base::rehash;
  360. // node_hash_set::reserve(count)
  361. //
  362. // Sets the number of slots in the `node_hash_set` to the number needed to
  363. // accommodate at least `count` total elements without exceeding the current
  364. // maximum load factor, and may rehash the container if needed.
  365. using Base::reserve;
  366. // node_hash_set::contains()
  367. //
  368. // Determines whether an element comparing equal to the given `key` exists
  369. // within the `node_hash_set`, returning `true` if so or `false` otherwise.
  370. using Base::contains;
  371. // node_hash_set::count(const Key& key) const
  372. //
  373. // Returns the number of elements comparing equal to the given `key` within
  374. // the `node_hash_set`. note that this function will return either `1` or `0`
  375. // since duplicate elements are not allowed within a `node_hash_set`.
  376. using Base::count;
  377. // node_hash_set::equal_range()
  378. //
  379. // Returns a closed range [first, last], defined by a `std::pair` of two
  380. // iterators, containing all elements with the passed key in the
  381. // `node_hash_set`.
  382. using Base::equal_range;
  383. // node_hash_set::find()
  384. //
  385. // Finds an element with the passed `key` within the `node_hash_set`.
  386. using Base::find;
  387. // node_hash_set::bucket_count()
  388. //
  389. // Returns the number of "buckets" within the `node_hash_set`. Note that
  390. // because a node hash set contains all elements within its internal storage,
  391. // this value simply equals the current capacity of the `node_hash_set`.
  392. using Base::bucket_count;
  393. // node_hash_set::load_factor()
  394. //
  395. // Returns the current load factor of the `node_hash_set` (the average number
  396. // of slots occupied with a value within the hash set).
  397. using Base::load_factor;
  398. // node_hash_set::max_load_factor()
  399. //
  400. // Manages the maximum load factor of the `node_hash_set`. Overloads are
  401. // listed below.
  402. //
  403. // float node_hash_set::max_load_factor()
  404. //
  405. // Returns the current maximum load factor of the `node_hash_set`.
  406. //
  407. // void node_hash_set::max_load_factor(float ml)
  408. //
  409. // Sets the maximum load factor of the `node_hash_set` to the passed value.
  410. //
  411. // NOTE: This overload is provided only for API compatibility with the STL;
  412. // `node_hash_set` will ignore any set load factor and manage its rehashing
  413. // internally as an implementation detail.
  414. using Base::max_load_factor;
  415. // node_hash_set::get_allocator()
  416. //
  417. // Returns the allocator function associated with this `node_hash_set`.
  418. using Base::get_allocator;
  419. // node_hash_set::hash_function()
  420. //
  421. // Returns the hashing function used to hash the keys within this
  422. // `node_hash_set`.
  423. using Base::hash_function;
  424. // node_hash_set::key_eq()
  425. //
  426. // Returns the function used for comparing keys equality.
  427. using Base::key_eq;
  428. };
  429. // erase_if(node_hash_set<>, Pred)
  430. //
  431. // Erases all elements that satisfy the predicate `pred` from the container `c`.
  432. // Returns the number of erased elements.
  433. template <typename T, typename H, typename E, typename A, typename Predicate>
  434. typename node_hash_set<T, H, E, A>::size_type erase_if(
  435. node_hash_set<T, H, E, A>& c, Predicate pred) {
  436. return container_internal::EraseIf(pred, &c);
  437. }
  438. // swap(node_hash_set<>, node_hash_set<>)
  439. //
  440. // Swaps the contents of two `node_hash_set` containers.
  441. //
  442. // NOTE: we need to define this function template in order for
  443. // `flat_hash_set::swap` to be called instead of `std::swap`. Even though we
  444. // have `swap(raw_hash_set&, raw_hash_set&)` defined, that function requires a
  445. // derived-to-base conversion, whereas `std::swap` is a function template so
  446. // `std::swap` will be preferred by compiler.
  447. template <typename T, typename H, typename E, typename A>
  448. void swap(node_hash_set<T, H, E, A>& x,
  449. node_hash_set<T, H, E, A>& y) noexcept(noexcept(x.swap(y))) {
  450. return x.swap(y);
  451. }
  452. namespace container_internal {
  453. // c_for_each_fast(node_hash_set<>, Function)
  454. //
  455. // Container-based version of the <algorithm> `std::for_each()` function to
  456. // apply a function to a container's elements.
  457. // There is no guarantees on the order of the function calls.
  458. // Erasure and/or insertion of elements in the function is not allowed.
  459. template <typename T, typename H, typename E, typename A, typename Function>
  460. decay_t<Function> c_for_each_fast(const node_hash_set<T, H, E, A>& c,
  461. Function&& f) {
  462. container_internal::ForEach(f, &c);
  463. return f;
  464. }
  465. template <typename T, typename H, typename E, typename A, typename Function>
  466. decay_t<Function> c_for_each_fast(node_hash_set<T, H, E, A>& c, Function&& f) {
  467. container_internal::ForEach(f, &c);
  468. return f;
  469. }
  470. template <typename T, typename H, typename E, typename A, typename Function>
  471. decay_t<Function> c_for_each_fast(node_hash_set<T, H, E, A>&& c, Function&& f) {
  472. container_internal::ForEach(f, &c);
  473. return f;
  474. }
  475. } // namespace container_internal
  476. namespace container_internal {
  477. template <class T>
  478. struct NodeHashSetPolicy
  479. : absl::container_internal::node_slot_policy<T&, NodeHashSetPolicy<T>> {
  480. using key_type = T;
  481. using init_type = T;
  482. using constant_iterators = std::true_type;
  483. template <class Allocator, class... Args>
  484. static T* new_element(Allocator* alloc, Args&&... args) {
  485. using ValueAlloc =
  486. typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
  487. ValueAlloc value_alloc(*alloc);
  488. T* res = absl::allocator_traits<ValueAlloc>::allocate(value_alloc, 1);
  489. absl::allocator_traits<ValueAlloc>::construct(value_alloc, res,
  490. std::forward<Args>(args)...);
  491. return res;
  492. }
  493. template <class Allocator>
  494. static void delete_element(Allocator* alloc, T* elem) {
  495. using ValueAlloc =
  496. typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
  497. ValueAlloc value_alloc(*alloc);
  498. absl::allocator_traits<ValueAlloc>::destroy(value_alloc, elem);
  499. absl::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1);
  500. }
  501. template <class F, class... Args>
  502. static decltype(absl::container_internal::DecomposeValue(
  503. std::declval<F>(), std::declval<Args>()...))
  504. apply(F&& f, Args&&... args) {
  505. return absl::container_internal::DecomposeValue(
  506. std::forward<F>(f), std::forward<Args>(args)...);
  507. }
  508. static size_t element_space_used(const T*) { return sizeof(T); }
  509. template <class Hash>
  510. static constexpr HashSlotFn get_hash_slot_fn() {
  511. return &TypeErasedDerefAndApplyToSlotFn<Hash, T>;
  512. }
  513. };
  514. } // namespace container_internal
  515. namespace container_algorithm_internal {
  516. // Specialization of trait in absl/algorithm/container.h
  517. template <class Key, class Hash, class KeyEqual, class Allocator>
  518. struct IsUnorderedContainer<absl::node_hash_set<Key, Hash, KeyEqual, Allocator>>
  519. : std::true_type {};
  520. } // namespace container_algorithm_internal
  521. ABSL_NAMESPACE_END
  522. } // namespace absl
  523. #endif // ABSL_CONTAINER_NODE_HASH_SET_H_