flat_hash_set.h 21 KB

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