node_hash_set.h
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // -----------------------------------------------------------------------------
00016 // File: node_hash_set.h
00017 // -----------------------------------------------------------------------------
00018 //
00019 // An `absl::node_hash_set<T>` is an unordered associative container designed to
00020 // be a more efficient replacement for `std::unordered_set`. Like
00021 // `unordered_set`, search, insertion, and deletion of map elements can be done
00022 // as an `O(1)` operation. However, `node_hash_set` (and other unordered
00023 // associative containers known as the collection of Abseil "Swiss tables")
00024 // contain other optimizations that result in both memory and computation
00025 // advantages.
00026 //
00027 // In most cases, your default choice for a hash table should be a map of type
00028 // `flat_hash_map` or a set of type `flat_hash_set`. However, if you need
00029 // pointer stability, a `node_hash_set` should be your preferred choice. As
00030 // well, if you are migrating your code from using `std::unordered_set`, a
00031 // `node_hash_set` should be an easy migration. Consider migrating to
00032 // `node_hash_set` and perhaps converting to a more efficient `flat_hash_set`
00033 // upon further review.
00034 
00035 #ifndef ABSL_CONTAINER_NODE_HASH_SET_H_
00036 #define ABSL_CONTAINER_NODE_HASH_SET_H_
00037 
00038 #include <type_traits>
00039 
00040 #include "absl/algorithm/container.h"
00041 #include "absl/container/internal/hash_function_defaults.h"  // IWYU pragma: export
00042 #include "absl/container/internal/node_hash_policy.h"
00043 #include "absl/container/internal/raw_hash_set.h"  // IWYU pragma: export
00044 #include "absl/memory/memory.h"
00045 
00046 namespace absl {
00047 namespace container_internal {
00048 template <typename T>
00049 struct NodeHashSetPolicy;
00050 }  // namespace container_internal
00051 
00052 // -----------------------------------------------------------------------------
00053 // absl::node_hash_set
00054 // -----------------------------------------------------------------------------
00055 //
00056 // An `absl::node_hash_set<T>` is an unordered associative container which
00057 // has been optimized for both speed and memory footprint in most common use
00058 // cases. Its interface is similar to that of `std::unordered_set<T>` with the
00059 // following notable differences:
00060 //
00061 // * Supports heterogeneous lookup, through `find()`, `operator[]()` and
00062 //   `insert()`, provided that the map is provided a compatible heterogeneous
00063 //   hashing function and equality operator.
00064 // * Contains a `capacity()` member function indicating the number of element
00065 //   slots (open, deleted, and empty) within the hash set.
00066 // * Returns `void` from the `erase(iterator)` overload.
00067 //
00068 // By default, `node_hash_set` uses the `absl::Hash` hashing framework.
00069 // All fundamental and Abseil types that support the `absl::Hash` framework have
00070 // a compatible equality operator for comparing insertions into `node_hash_set`.
00071 // If your type is not yet supported by the `absl::Hash` framework, see
00072 // absl/hash/hash.h for information on extending Abseil hashing to user-defined
00073 // types.
00074 //
00075 // Example:
00076 //
00077 //   // Create a node hash set of three strings
00078 //   absl::node_hash_map<std::string, std::string> ducks =
00079 //     {"huey", "dewey"}, "louie"};
00080 //
00081 //  // Insert a new element into the node hash map
00082 //  ducks.insert("donald"};
00083 //
00084 //  // Force a rehash of the node hash map
00085 //  ducks.rehash(0);
00086 //
00087 //  // See if "dewey" is present
00088 //  if (ducks.contains("dewey")) {
00089 //    std::cout << "We found dewey!" << std::endl;
00090 //  }
00091 template <class T, class Hash = absl::container_internal::hash_default_hash<T>,
00092           class Eq = absl::container_internal::hash_default_eq<T>,
00093           class Alloc = std::allocator<T>>
00094 class node_hash_set
00095     : public absl::container_internal::raw_hash_set<
00096           absl::container_internal::NodeHashSetPolicy<T>, Hash, Eq, Alloc> {
00097   using Base = typename node_hash_set::raw_hash_set;
00098 
00099  public:
00100   // Constructors and Assignment Operators
00101   //
00102   // A node_hash_set supports the same overload set as `std::unordered_map`
00103   // for construction and assignment:
00104   //
00105   // *  Default constructor
00106   //
00107   //    // No allocation for the table's elements is made.
00108   //    absl::node_hash_set<std::string> set1;
00109   //
00110   // * Initializer List constructor
00111   //
00112   //   absl::node_hash_set<std::string> set2 =
00113   //       {{"huey"}, {"dewey"}, {"louie"},};
00114   //
00115   // * Copy constructor
00116   //
00117   //   absl::node_hash_set<std::string> set3(set2);
00118   //
00119   // * Copy assignment operator
00120   //
00121   //  // Hash functor and Comparator are copied as well
00122   //  absl::node_hash_set<std::string> set4;
00123   //  set4 = set3;
00124   //
00125   // * Move constructor
00126   //
00127   //   // Move is guaranteed efficient
00128   //   absl::node_hash_set<std::string> set5(std::move(set4));
00129   //
00130   // * Move assignment operator
00131   //
00132   //   // May be efficient if allocators are compatible
00133   //   absl::node_hash_set<std::string> set6;
00134   //   set6 = std::move(set5);
00135   //
00136   // * Range constructor
00137   //
00138   //   std::vector<std::string> v = {"a", "b"};
00139   //   absl::node_hash_set<std::string> set7(v.begin(), v.end());
00140   node_hash_set() {}
00141   using Base::Base;
00142 
00143   // node_hash_set::begin()
00144   //
00145   // Returns an iterator to the beginning of the `node_hash_set`.
00146   using Base::begin;
00147 
00148   // node_hash_set::cbegin()
00149   //
00150   // Returns a const iterator to the beginning of the `node_hash_set`.
00151   using Base::cbegin;
00152 
00153   // node_hash_set::cend()
00154   //
00155   // Returns a const iterator to the end of the `node_hash_set`.
00156   using Base::cend;
00157 
00158   // node_hash_set::end()
00159   //
00160   // Returns an iterator to the end of the `node_hash_set`.
00161   using Base::end;
00162 
00163   // node_hash_set::capacity()
00164   //
00165   // Returns the number of element slots (assigned, deleted, and empty)
00166   // available within the `node_hash_set`.
00167   //
00168   // NOTE: this member function is particular to `absl::node_hash_set` and is
00169   // not provided in the `std::unordered_map` API.
00170   using Base::capacity;
00171 
00172   // node_hash_set::empty()
00173   //
00174   // Returns whether or not the `node_hash_set` is empty.
00175   using Base::empty;
00176 
00177   // node_hash_set::max_size()
00178   //
00179   // Returns the largest theoretical possible number of elements within a
00180   // `node_hash_set` under current memory constraints. This value can be thought
00181   // of the largest value of `std::distance(begin(), end())` for a
00182   // `node_hash_set<T>`.
00183   using Base::max_size;
00184 
00185   // node_hash_set::size()
00186   //
00187   // Returns the number of elements currently within the `node_hash_set`.
00188   using Base::size;
00189 
00190   // node_hash_set::clear()
00191   //
00192   // Removes all elements from the `node_hash_set`. Invalidates any references,
00193   // pointers, or iterators referring to contained elements.
00194   //
00195   // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
00196   // the underlying buffer call `erase(begin(), end())`.
00197   using Base::clear;
00198 
00199   // node_hash_set::erase()
00200   //
00201   // Erases elements within the `node_hash_set`. Erasing does not trigger a
00202   // rehash. Overloads are listed below.
00203   //
00204   // void erase(const_iterator pos):
00205   //
00206   //   Erases the element at `position` of the `node_hash_set`, returning
00207   //   `void`.
00208   //
00209   //   NOTE: this return behavior is different than that of STL containers in
00210   //   general and `std::unordered_map` in particular.
00211   //
00212   // iterator erase(const_iterator first, const_iterator last):
00213   //
00214   //   Erases the elements in the open interval [`first`, `last`), returning an
00215   //   iterator pointing to `last`.
00216   //
00217   // size_type erase(const key_type& key):
00218   //
00219   //   Erases the element with the matching key, if it exists.
00220   using Base::erase;
00221 
00222   // node_hash_set::insert()
00223   //
00224   // Inserts an element of the specified value into the `node_hash_set`,
00225   // returning an iterator pointing to the newly inserted element, provided that
00226   // an element with the given key does not already exist. If rehashing occurs
00227   // due to the insertion, all iterators are invalidated. Overloads are listed
00228   // below.
00229   //
00230   // std::pair<iterator,bool> insert(const T& value):
00231   //
00232   //   Inserts a value into the `node_hash_set`. Returns a pair consisting of an
00233   //   iterator to the inserted element (or to the element that prevented the
00234   //   insertion) and a bool denoting whether the insertion took place.
00235   //
00236   // std::pair<iterator,bool> insert(T&& value):
00237   //
00238   //   Inserts a moveable value into the `node_hash_set`. Returns a pair
00239   //   consisting of an iterator to the inserted element (or to the element that
00240   //   prevented the insertion) and a bool denoting whether the insertion took
00241   //   place.
00242   //
00243   // iterator insert(const_iterator hint, const T& value):
00244   // iterator insert(const_iterator hint, T&& value):
00245   //
00246   //   Inserts a value, using the position of `hint` as a non-binding suggestion
00247   //   for where to begin the insertion search. Returns an iterator to the
00248   //   inserted element, or to the existing element that prevented the
00249   //   insertion.
00250   //
00251   // void insert(InputIterator first, InputIterator last):
00252   //
00253   //   Inserts a range of values [`first`, `last`).
00254   //
00255   //   NOTE: Although the STL does not specify which element may be inserted if
00256   //   multiple keys compare equivalently, for `node_hash_set` we guarantee the
00257   //   first match is inserted.
00258   //
00259   // void insert(std::initializer_list<T> ilist):
00260   //
00261   //   Inserts the elements within the initializer list `ilist`.
00262   //
00263   //   NOTE: Although the STL does not specify which element may be inserted if
00264   //   multiple keys compare equivalently within the initializer list, for
00265   //   `node_hash_set` we guarantee the first match is inserted.
00266   using Base::insert;
00267 
00268   // node_hash_set::emplace()
00269   //
00270   // Inserts an element of the specified value by constructing it in-place
00271   // within the `node_hash_set`, provided that no element with the given key
00272   // already exists.
00273   //
00274   // The element may be constructed even if there already is an element with the
00275   // key in the container, in which case the newly constructed element will be
00276   // destroyed immediately.
00277   //
00278   // If rehashing occurs due to the insertion, all iterators are invalidated.
00279   using Base::emplace;
00280 
00281   // node_hash_set::emplace_hint()
00282   //
00283   // Inserts an element of the specified value by constructing it in-place
00284   // within the `node_hash_set`, using the position of `hint` as a non-binding
00285   // suggestion for where to begin the insertion search, and only inserts
00286   // provided that no element with the given key already exists.
00287   //
00288   // The element may be constructed even if there already is an element with the
00289   // key in the container, in which case the newly constructed element will be
00290   // destroyed immediately.
00291   //
00292   // If rehashing occurs due to the insertion, all iterators are invalidated.
00293   using Base::emplace_hint;
00294 
00295   // node_hash_set::extract()
00296   //
00297   // Extracts the indicated element, erasing it in the process, and returns it
00298   // as a C++17-compatible node handle. Overloads are listed below.
00299   //
00300   // node_type extract(const_iterator position):
00301   //
00302   //   Extracts the element at the indicated position and returns a node handle
00303   //   owning that extracted data.
00304   //
00305   // node_type extract(const key_type& x):
00306   //
00307   //   Extracts the element with the key matching the passed key value and
00308   //   returns a node handle owning that extracted data. If the `node_hash_set`
00309   //   does not contain an element with a matching key, this function returns an
00310   // empty node handle.
00311   using Base::extract;
00312 
00313   // node_hash_set::merge()
00314   //
00315   // Extracts elements from a given `source` flat hash map into this
00316   // `node_hash_set`. If the destination `node_hash_set` already contains an
00317   // element with an equivalent key, that element is not extracted.
00318   using Base::merge;
00319 
00320   // node_hash_set::swap(node_hash_set& other)
00321   //
00322   // Exchanges the contents of this `node_hash_set` with those of the `other`
00323   // flat hash map, avoiding invocation of any move, copy, or swap operations on
00324   // individual elements.
00325   //
00326   // All iterators and references on the `node_hash_set` remain valid, excepting
00327   // for the past-the-end iterator, which is invalidated.
00328   //
00329   // `swap()` requires that the flat hash set's hashing and key equivalence
00330   // functions be Swappable, and are exchaged using unqualified calls to
00331   // non-member `swap()`. If the map's allocator has
00332   // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
00333   // set to `true`, the allocators are also exchanged using an unqualified call
00334   // to non-member `swap()`; otherwise, the allocators are not swapped.
00335   using Base::swap;
00336 
00337   // node_hash_set::rehash(count)
00338   //
00339   // Rehashes the `node_hash_set`, setting the number of slots to be at least
00340   // the passed value. If the new number of slots increases the load factor more
00341   // than the current maximum load factor
00342   // (`count` < `size()` / `max_load_factor()`), then the new number of slots
00343   // will be at least `size()` / `max_load_factor()`.
00344   //
00345   // To force a rehash, pass rehash(0).
00346   //
00347   // NOTE: unlike behavior in `std::unordered_set`, references are also
00348   // invalidated upon a `rehash()`.
00349   using Base::rehash;
00350 
00351   // node_hash_set::reserve(count)
00352   //
00353   // Sets the number of slots in the `node_hash_set` to the number needed to
00354   // accommodate at least `count` total elements without exceeding the current
00355   // maximum load factor, and may rehash the container if needed.
00356   using Base::reserve;
00357 
00358   // node_hash_set::contains()
00359   //
00360   // Determines whether an element comparing equal to the given `key` exists
00361   // within the `node_hash_set`, returning `true` if so or `false` otherwise.
00362   using Base::contains;
00363 
00364   // node_hash_set::count(const Key& key) const
00365   //
00366   // Returns the number of elements comparing equal to the given `key` within
00367   // the `node_hash_set`. note that this function will return either `1` or `0`
00368   // since duplicate elements are not allowed within a `node_hash_set`.
00369   using Base::count;
00370 
00371   // node_hash_set::equal_range()
00372   //
00373   // Returns a closed range [first, last], defined by a `std::pair` of two
00374   // iterators, containing all elements with the passed key in the
00375   // `node_hash_set`.
00376   using Base::equal_range;
00377 
00378   // node_hash_set::find()
00379   //
00380   // Finds an element with the passed `key` within the `node_hash_set`.
00381   using Base::find;
00382 
00383   // node_hash_set::bucket_count()
00384   //
00385   // Returns the number of "buckets" within the `node_hash_set`. Note that
00386   // because a flat hash map contains all elements within its internal storage,
00387   // this value simply equals the current capacity of the `node_hash_set`.
00388   using Base::bucket_count;
00389 
00390   // node_hash_set::load_factor()
00391   //
00392   // Returns the current load factor of the `node_hash_set` (the average number
00393   // of slots occupied with a value within the hash map).
00394   using Base::load_factor;
00395 
00396   // node_hash_set::max_load_factor()
00397   //
00398   // Manages the maximum load factor of the `node_hash_set`. Overloads are
00399   // listed below.
00400   //
00401   // float node_hash_set::max_load_factor()
00402   //
00403   //   Returns the current maximum load factor of the `node_hash_set`.
00404   //
00405   // void node_hash_set::max_load_factor(float ml)
00406   //
00407   //   Sets the maximum load factor of the `node_hash_set` to the passed value.
00408   //
00409   //   NOTE: This overload is provided only for API compatibility with the STL;
00410   //   `node_hash_set` will ignore any set load factor and manage its rehashing
00411   //   internally as an implementation detail.
00412   using Base::max_load_factor;
00413 
00414   // node_hash_set::get_allocator()
00415   //
00416   // Returns the allocator function associated with this `node_hash_set`.
00417   using Base::get_allocator;
00418 
00419   // node_hash_set::hash_function()
00420   //
00421   // Returns the hashing function used to hash the keys within this
00422   // `node_hash_set`.
00423   using Base::hash_function;
00424 
00425   // node_hash_set::key_eq()
00426   //
00427   // Returns the function used for comparing keys equality.
00428   using Base::key_eq;
00429 
00430   ABSL_DEPRECATED("Call `hash_function()` instead.")
00431   typename Base::hasher hash_funct() { return this->hash_function(); }
00432 
00433   ABSL_DEPRECATED("Call `rehash()` instead.")
00434   void resize(typename Base::size_type hint) { this->rehash(hint); }
00435 };
00436 
00437 namespace container_internal {
00438 
00439 template <class T>
00440 struct NodeHashSetPolicy
00441     : absl::container_internal::node_hash_policy<T&, NodeHashSetPolicy<T>> {
00442   using key_type = T;
00443   using init_type = T;
00444   using constant_iterators = std::true_type;
00445 
00446   template <class Allocator, class... Args>
00447   static T* new_element(Allocator* alloc, Args&&... args) {
00448     using ValueAlloc =
00449         typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
00450     ValueAlloc value_alloc(*alloc);
00451     T* res = absl::allocator_traits<ValueAlloc>::allocate(value_alloc, 1);
00452     absl::allocator_traits<ValueAlloc>::construct(value_alloc, res,
00453                                                   std::forward<Args>(args)...);
00454     return res;
00455   }
00456 
00457   template <class Allocator>
00458   static void delete_element(Allocator* alloc, T* elem) {
00459     using ValueAlloc =
00460         typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
00461     ValueAlloc value_alloc(*alloc);
00462     absl::allocator_traits<ValueAlloc>::destroy(value_alloc, elem);
00463     absl::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1);
00464   }
00465 
00466   template <class F, class... Args>
00467   static decltype(absl::container_internal::DecomposeValue(
00468       std::declval<F>(), std::declval<Args>()...))
00469   apply(F&& f, Args&&... args) {
00470     return absl::container_internal::DecomposeValue(
00471         std::forward<F>(f), std::forward<Args>(args)...);
00472   }
00473 
00474   static size_t element_space_used(const T*) { return sizeof(T); }
00475 };
00476 }  // namespace container_internal
00477 
00478 namespace container_algorithm_internal {
00479 
00480 // Specialization of trait in absl/algorithm/container.h
00481 template <class Key, class Hash, class KeyEqual, class Allocator>
00482 struct IsUnorderedContainer<absl::node_hash_set<Key, Hash, KeyEqual, Allocator>>
00483     : std::true_type {};
00484 
00485 }  // namespace container_algorithm_internal
00486 }  // namespace absl
00487 
00488 #endif  // ABSL_CONTAINER_NODE_HASH_SET_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15