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


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