|
libstdc++
|
00001 // unordered_set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2010-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/unordered_set.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{unordered_set} 00028 */ 00029 00030 #ifndef _UNORDERED_SET_H 00031 #define _UNORDERED_SET_H 00032 00033 namespace std _GLIBCXX_VISIBILITY(default) 00034 { 00035 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00036 00037 /// Base types for unordered_set. 00038 template<bool _Cache> 00039 using __uset_traits = __detail::_Hashtable_traits<_Cache, true, true>; 00040 00041 template<typename _Value, 00042 typename _Hash = hash<_Value>, 00043 typename _Pred = std::equal_to<_Value>, 00044 typename _Alloc = std::allocator<_Value>, 00045 typename _Tr = __uset_traits<__cache_default<_Value, _Hash>::value>> 00046 using __uset_hashtable = _Hashtable<_Value, _Value, _Alloc, 00047 __detail::_Identity, _Pred, _Hash, 00048 __detail::_Mod_range_hashing, 00049 __detail::_Default_ranged_hash, 00050 __detail::_Prime_rehash_policy, _Tr>; 00051 00052 /// Base types for unordered_multiset. 00053 template<bool _Cache> 00054 using __umset_traits = __detail::_Hashtable_traits<_Cache, true, false>; 00055 00056 template<typename _Value, 00057 typename _Hash = hash<_Value>, 00058 typename _Pred = std::equal_to<_Value>, 00059 typename _Alloc = std::allocator<_Value>, 00060 typename _Tr = __umset_traits<__cache_default<_Value, _Hash>::value>> 00061 using __umset_hashtable = _Hashtable<_Value, _Value, _Alloc, 00062 __detail::_Identity, 00063 _Pred, _Hash, 00064 __detail::_Mod_range_hashing, 00065 __detail::_Default_ranged_hash, 00066 __detail::_Prime_rehash_policy, _Tr>; 00067 00068 /** 00069 * @brief A standard container composed of unique keys (containing 00070 * at most one of each key value) in which the elements' keys are 00071 * the elements themselves. 00072 * 00073 * @ingroup unordered_associative_containers 00074 * 00075 * @tparam _Value Type of key objects. 00076 * @tparam _Hash Hashing function object type, defaults to hash<_Value>. 00077 00078 * @tparam _Pred Predicate function object type, defaults to 00079 * equal_to<_Value>. 00080 * 00081 * @tparam _Alloc Allocator type, defaults to allocator<_Key>. 00082 * 00083 * Meets the requirements of a <a href="tables.html#65">container</a>, and 00084 * <a href="tables.html#xx">unordered associative container</a> 00085 * 00086 * Base is _Hashtable, dispatched at compile time via template 00087 * alias __uset_hashtable. 00088 */ 00089 template<class _Value, 00090 class _Hash = hash<_Value>, 00091 class _Pred = std::equal_to<_Value>, 00092 class _Alloc = std::allocator<_Value> > 00093 class unordered_set 00094 { 00095 typedef __uset_hashtable<_Value, _Hash, _Pred, _Alloc> _Hashtable; 00096 _Hashtable _M_h; 00097 00098 public: 00099 // typedefs: 00100 //@{ 00101 /// Public typedefs. 00102 typedef typename _Hashtable::key_type key_type; 00103 typedef typename _Hashtable::value_type value_type; 00104 typedef typename _Hashtable::hasher hasher; 00105 typedef typename _Hashtable::key_equal key_equal; 00106 typedef typename _Hashtable::allocator_type allocator_type; 00107 //@} 00108 00109 //@{ 00110 /// Iterator-related typedefs. 00111 typedef typename _Hashtable::pointer pointer; 00112 typedef typename _Hashtable::const_pointer const_pointer; 00113 typedef typename _Hashtable::reference reference; 00114 typedef typename _Hashtable::const_reference const_reference; 00115 typedef typename _Hashtable::iterator iterator; 00116 typedef typename _Hashtable::const_iterator const_iterator; 00117 typedef typename _Hashtable::local_iterator local_iterator; 00118 typedef typename _Hashtable::const_local_iterator const_local_iterator; 00119 typedef typename _Hashtable::size_type size_type; 00120 typedef typename _Hashtable::difference_type difference_type; 00121 //@} 00122 00123 // construct/destroy/copy 00124 00125 /// Default constructor. 00126 unordered_set() = default; 00127 00128 /** 00129 * @brief Default constructor creates no elements. 00130 * @param __n Minimal initial number of buckets. 00131 * @param __hf A hash functor. 00132 * @param __eql A key equality functor. 00133 * @param __a An allocator object. 00134 */ 00135 explicit 00136 unordered_set(size_type __n, 00137 const hasher& __hf = hasher(), 00138 const key_equal& __eql = key_equal(), 00139 const allocator_type& __a = allocator_type()) 00140 : _M_h(__n, __hf, __eql, __a) 00141 { } 00142 00143 /** 00144 * @brief Builds an %unordered_set from a range. 00145 * @param __first An input iterator. 00146 * @param __last An input iterator. 00147 * @param __n Minimal initial number of buckets. 00148 * @param __hf A hash functor. 00149 * @param __eql A key equality functor. 00150 * @param __a An allocator object. 00151 * 00152 * Create an %unordered_set consisting of copies of the elements from 00153 * [__first,__last). This is linear in N (where N is 00154 * distance(__first,__last)). 00155 */ 00156 template<typename _InputIterator> 00157 unordered_set(_InputIterator __first, _InputIterator __last, 00158 size_type __n = 0, 00159 const hasher& __hf = hasher(), 00160 const key_equal& __eql = key_equal(), 00161 const allocator_type& __a = allocator_type()) 00162 : _M_h(__first, __last, __n, __hf, __eql, __a) 00163 { } 00164 00165 /// Copy constructor. 00166 unordered_set(const unordered_set&) = default; 00167 00168 /// Move constructor. 00169 unordered_set(unordered_set&&) = default; 00170 00171 /** 00172 * @brief Creates an %unordered_set with no elements. 00173 * @param __a An allocator object. 00174 */ 00175 explicit 00176 unordered_set(const allocator_type& __a) 00177 : _M_h(__a) 00178 { } 00179 00180 /* 00181 * @brief Copy constructor with allocator argument. 00182 * @param __uset Input %unordered_set to copy. 00183 * @param __a An allocator object. 00184 */ 00185 unordered_set(const unordered_set& __uset, 00186 const allocator_type& __a) 00187 : _M_h(__uset._M_h, __a) 00188 { } 00189 00190 /* 00191 * @brief Move constructor with allocator argument. 00192 * @param __uset Input %unordered_set to move. 00193 * @param __a An allocator object. 00194 */ 00195 unordered_set(unordered_set&& __uset, 00196 const allocator_type& __a) 00197 : _M_h(std::move(__uset._M_h), __a) 00198 { } 00199 00200 /** 00201 * @brief Builds an %unordered_set from an initializer_list. 00202 * @param __l An initializer_list. 00203 * @param __n Minimal initial number of buckets. 00204 * @param __hf A hash functor. 00205 * @param __eql A key equality functor. 00206 * @param __a An allocator object. 00207 * 00208 * Create an %unordered_set consisting of copies of the elements in the 00209 * list. This is linear in N (where N is @a __l.size()). 00210 */ 00211 unordered_set(initializer_list<value_type> __l, 00212 size_type __n = 0, 00213 const hasher& __hf = hasher(), 00214 const key_equal& __eql = key_equal(), 00215 const allocator_type& __a = allocator_type()) 00216 : _M_h(__l, __n, __hf, __eql, __a) 00217 { } 00218 00219 /// Copy assignment operator. 00220 unordered_set& 00221 operator=(const unordered_set&) = default; 00222 00223 /// Move assignment operator. 00224 unordered_set& 00225 operator=(unordered_set&&) = default; 00226 00227 /** 00228 * @brief %Unordered_set list assignment operator. 00229 * @param __l An initializer_list. 00230 * 00231 * This function fills an %unordered_set with copies of the elements in 00232 * the initializer list @a __l. 00233 * 00234 * Note that the assignment completely changes the %unordered_set and 00235 * that the resulting %unordered_set's size is the same as the number 00236 * of elements assigned. Old data may be lost. 00237 */ 00238 unordered_set& 00239 operator=(initializer_list<value_type> __l) 00240 { 00241 _M_h = __l; 00242 return *this; 00243 } 00244 00245 /// Returns the allocator object with which the %unordered_set was 00246 /// constructed. 00247 allocator_type 00248 get_allocator() const noexcept 00249 { return _M_h.get_allocator(); } 00250 00251 // size and capacity: 00252 00253 /// Returns true if the %unordered_set is empty. 00254 bool 00255 empty() const noexcept 00256 { return _M_h.empty(); } 00257 00258 /// Returns the size of the %unordered_set. 00259 size_type 00260 size() const noexcept 00261 { return _M_h.size(); } 00262 00263 /// Returns the maximum size of the %unordered_set. 00264 size_type 00265 max_size() const noexcept 00266 { return _M_h.max_size(); } 00267 00268 // iterators. 00269 00270 //@{ 00271 /** 00272 * Returns a read-only (constant) iterator that points to the first 00273 * element in the %unordered_set. 00274 */ 00275 iterator 00276 begin() noexcept 00277 { return _M_h.begin(); } 00278 00279 const_iterator 00280 begin() const noexcept 00281 { return _M_h.begin(); } 00282 //@} 00283 00284 //@{ 00285 /** 00286 * Returns a read-only (constant) iterator that points one past the last 00287 * element in the %unordered_set. 00288 */ 00289 iterator 00290 end() noexcept 00291 { return _M_h.end(); } 00292 00293 const_iterator 00294 end() const noexcept 00295 { return _M_h.end(); } 00296 //@} 00297 00298 /** 00299 * Returns a read-only (constant) iterator that points to the first 00300 * element in the %unordered_set. 00301 */ 00302 const_iterator 00303 cbegin() const noexcept 00304 { return _M_h.begin(); } 00305 00306 /** 00307 * Returns a read-only (constant) iterator that points one past the last 00308 * element in the %unordered_set. 00309 */ 00310 const_iterator 00311 cend() const noexcept 00312 { return _M_h.end(); } 00313 00314 // modifiers. 00315 00316 /** 00317 * @brief Attempts to build and insert an element into the 00318 * %unordered_set. 00319 * @param __args Arguments used to generate an element. 00320 * @return A pair, of which the first element is an iterator that points 00321 * to the possibly inserted element, and the second is a bool 00322 * that is true if the element was actually inserted. 00323 * 00324 * This function attempts to build and insert an element into the 00325 * %unordered_set. An %unordered_set relies on unique keys and thus an 00326 * element is only inserted if it is not already present in the 00327 * %unordered_set. 00328 * 00329 * Insertion requires amortized constant time. 00330 */ 00331 template<typename... _Args> 00332 std::pair<iterator, bool> 00333 emplace(_Args&&... __args) 00334 { return _M_h.emplace(std::forward<_Args>(__args)...); } 00335 00336 /** 00337 * @brief Attempts to insert an element into the %unordered_set. 00338 * @param __pos An iterator that serves as a hint as to where the 00339 * element should be inserted. 00340 * @param __args Arguments used to generate the element to be 00341 * inserted. 00342 * @return An iterator that points to the element with key equivalent to 00343 * the one generated from @a __args (may or may not be the 00344 * element itself). 00345 * 00346 * This function is not concerned about whether the insertion took place, 00347 * and thus does not return a boolean like the single-argument emplace() 00348 * does. Note that the first parameter is only a hint and can 00349 * potentially improve the performance of the insertion process. A bad 00350 * hint would cause no gains in efficiency. 00351 * 00352 * For more on @a hinting, see: 00353 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00354 * 00355 * Insertion requires amortized constant time. 00356 */ 00357 template<typename... _Args> 00358 iterator 00359 emplace_hint(const_iterator __pos, _Args&&... __args) 00360 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); } 00361 00362 //@{ 00363 /** 00364 * @brief Attempts to insert an element into the %unordered_set. 00365 * @param __x Element to be inserted. 00366 * @return A pair, of which the first element is an iterator that points 00367 * to the possibly inserted element, and the second is a bool 00368 * that is true if the element was actually inserted. 00369 * 00370 * This function attempts to insert an element into the %unordered_set. 00371 * An %unordered_set relies on unique keys and thus an element is only 00372 * inserted if it is not already present in the %unordered_set. 00373 * 00374 * Insertion requires amortized constant time. 00375 */ 00376 std::pair<iterator, bool> 00377 insert(const value_type& __x) 00378 { return _M_h.insert(__x); } 00379 00380 std::pair<iterator, bool> 00381 insert(value_type&& __x) 00382 { return _M_h.insert(std::move(__x)); } 00383 //@} 00384 00385 //@{ 00386 /** 00387 * @brief Attempts to insert an element into the %unordered_set. 00388 * @param __hint An iterator that serves as a hint as to where the 00389 * element should be inserted. 00390 * @param __x Element to be inserted. 00391 * @return An iterator that points to the element with key of 00392 * @a __x (may or may not be the element passed in). 00393 * 00394 * This function is not concerned about whether the insertion took place, 00395 * and thus does not return a boolean like the single-argument insert() 00396 * does. Note that the first parameter is only a hint and can 00397 * potentially improve the performance of the insertion process. A bad 00398 * hint would cause no gains in efficiency. 00399 * 00400 * For more on @a hinting, see: 00401 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00402 * 00403 * Insertion requires amortized constant. 00404 */ 00405 iterator 00406 insert(const_iterator __hint, const value_type& __x) 00407 { return _M_h.insert(__hint, __x); } 00408 00409 iterator 00410 insert(const_iterator __hint, value_type&& __x) 00411 { return _M_h.insert(__hint, std::move(__x)); } 00412 //@} 00413 00414 /** 00415 * @brief A template function that attempts to insert a range of 00416 * elements. 00417 * @param __first Iterator pointing to the start of the range to be 00418 * inserted. 00419 * @param __last Iterator pointing to the end of the range. 00420 * 00421 * Complexity similar to that of the range constructor. 00422 */ 00423 template<typename _InputIterator> 00424 void 00425 insert(_InputIterator __first, _InputIterator __last) 00426 { _M_h.insert(__first, __last); } 00427 00428 /** 00429 * @brief Attempts to insert a list of elements into the %unordered_set. 00430 * @param __l A std::initializer_list<value_type> of elements 00431 * to be inserted. 00432 * 00433 * Complexity similar to that of the range constructor. 00434 */ 00435 void 00436 insert(initializer_list<value_type> __l) 00437 { _M_h.insert(__l); } 00438 00439 //@{ 00440 /** 00441 * @brief Erases an element from an %unordered_set. 00442 * @param __position An iterator pointing to the element to be erased. 00443 * @return An iterator pointing to the element immediately following 00444 * @a __position prior to the element being erased. If no such 00445 * element exists, end() is returned. 00446 * 00447 * This function erases an element, pointed to by the given iterator, 00448 * from an %unordered_set. Note that this function only erases the 00449 * element, and that if the element is itself a pointer, the pointed-to 00450 * memory is not touched in any way. Managing the pointer is the user's 00451 * responsibility. 00452 */ 00453 iterator 00454 erase(const_iterator __position) 00455 { return _M_h.erase(__position); } 00456 00457 // LWG 2059. 00458 iterator 00459 erase(iterator __position) 00460 { return _M_h.erase(__position); } 00461 //@} 00462 00463 /** 00464 * @brief Erases elements according to the provided key. 00465 * @param __x Key of element to be erased. 00466 * @return The number of elements erased. 00467 * 00468 * This function erases all the elements located by the given key from 00469 * an %unordered_set. For an %unordered_set the result of this function 00470 * can only be 0 (not present) or 1 (present). 00471 * Note that this function only erases the element, and that if 00472 * the element is itself a pointer, the pointed-to memory is not touched 00473 * in any way. Managing the pointer is the user's responsibility. 00474 */ 00475 size_type 00476 erase(const key_type& __x) 00477 { return _M_h.erase(__x); } 00478 00479 /** 00480 * @brief Erases a [__first,__last) range of elements from an 00481 * %unordered_set. 00482 * @param __first Iterator pointing to the start of the range to be 00483 * erased. 00484 * @param __last Iterator pointing to the end of the range to 00485 * be erased. 00486 * @return The iterator @a __last. 00487 * 00488 * This function erases a sequence of elements from an %unordered_set. 00489 * Note that this function only erases the element, and that if 00490 * the element is itself a pointer, the pointed-to memory is not touched 00491 * in any way. Managing the pointer is the user's responsibility. 00492 */ 00493 iterator 00494 erase(const_iterator __first, const_iterator __last) 00495 { return _M_h.erase(__first, __last); } 00496 00497 /** 00498 * Erases all elements in an %unordered_set. Note that this function only 00499 * erases the elements, and that if the elements themselves are pointers, 00500 * the pointed-to memory is not touched in any way. Managing the pointer 00501 * is the user's responsibility. 00502 */ 00503 void 00504 clear() noexcept 00505 { _M_h.clear(); } 00506 00507 /** 00508 * @brief Swaps data with another %unordered_set. 00509 * @param __x An %unordered_set of the same element and allocator 00510 * types. 00511 * 00512 * This exchanges the elements between two sets in constant time. 00513 * Note that the global std::swap() function is specialized such that 00514 * std::swap(s1,s2) will feed to this function. 00515 */ 00516 void 00517 swap(unordered_set& __x) 00518 noexcept( noexcept(_M_h.swap(__x._M_h)) ) 00519 { _M_h.swap(__x._M_h); } 00520 00521 // observers. 00522 00523 /// Returns the hash functor object with which the %unordered_set was 00524 /// constructed. 00525 hasher 00526 hash_function() const 00527 { return _M_h.hash_function(); } 00528 00529 /// Returns the key comparison object with which the %unordered_set was 00530 /// constructed. 00531 key_equal 00532 key_eq() const 00533 { return _M_h.key_eq(); } 00534 00535 // lookup. 00536 00537 //@{ 00538 /** 00539 * @brief Tries to locate an element in an %unordered_set. 00540 * @param __x Element to be located. 00541 * @return Iterator pointing to sought-after element, or end() if not 00542 * found. 00543 * 00544 * This function takes a key and tries to locate the element with which 00545 * the key matches. If successful the function returns an iterator 00546 * pointing to the sought after element. If unsuccessful it returns the 00547 * past-the-end ( @c end() ) iterator. 00548 */ 00549 iterator 00550 find(const key_type& __x) 00551 { return _M_h.find(__x); } 00552 00553 const_iterator 00554 find(const key_type& __x) const 00555 { return _M_h.find(__x); } 00556 //@} 00557 00558 /** 00559 * @brief Finds the number of elements. 00560 * @param __x Element to located. 00561 * @return Number of elements with specified key. 00562 * 00563 * This function only makes sense for unordered_multisets; for 00564 * unordered_set the result will either be 0 (not present) or 1 00565 * (present). 00566 */ 00567 size_type 00568 count(const key_type& __x) const 00569 { return _M_h.count(__x); } 00570 00571 //@{ 00572 /** 00573 * @brief Finds a subsequence matching given key. 00574 * @param __x Key to be located. 00575 * @return Pair of iterators that possibly points to the subsequence 00576 * matching given key. 00577 * 00578 * This function probably only makes sense for multisets. 00579 */ 00580 std::pair<iterator, iterator> 00581 equal_range(const key_type& __x) 00582 { return _M_h.equal_range(__x); } 00583 00584 std::pair<const_iterator, const_iterator> 00585 equal_range(const key_type& __x) const 00586 { return _M_h.equal_range(__x); } 00587 //@} 00588 00589 // bucket interface. 00590 00591 /// Returns the number of buckets of the %unordered_set. 00592 size_type 00593 bucket_count() const noexcept 00594 { return _M_h.bucket_count(); } 00595 00596 /// Returns the maximum number of buckets of the %unordered_set. 00597 size_type 00598 max_bucket_count() const noexcept 00599 { return _M_h.max_bucket_count(); } 00600 00601 /* 00602 * @brief Returns the number of elements in a given bucket. 00603 * @param __n A bucket index. 00604 * @return The number of elements in the bucket. 00605 */ 00606 size_type 00607 bucket_size(size_type __n) const 00608 { return _M_h.bucket_size(__n); } 00609 00610 /* 00611 * @brief Returns the bucket index of a given element. 00612 * @param __key A key instance. 00613 * @return The key bucket index. 00614 */ 00615 size_type 00616 bucket(const key_type& __key) const 00617 { return _M_h.bucket(__key); } 00618 00619 //@{ 00620 /** 00621 * @brief Returns a read-only (constant) iterator pointing to the first 00622 * bucket element. 00623 * @param __n The bucket index. 00624 * @return A read-only local iterator. 00625 */ 00626 local_iterator 00627 begin(size_type __n) 00628 { return _M_h.begin(__n); } 00629 00630 const_local_iterator 00631 begin(size_type __n) const 00632 { return _M_h.begin(__n); } 00633 00634 const_local_iterator 00635 cbegin(size_type __n) const 00636 { return _M_h.cbegin(__n); } 00637 //@} 00638 00639 //@{ 00640 /** 00641 * @brief Returns a read-only (constant) iterator pointing to one past 00642 * the last bucket elements. 00643 * @param __n The bucket index. 00644 * @return A read-only local iterator. 00645 */ 00646 local_iterator 00647 end(size_type __n) 00648 { return _M_h.end(__n); } 00649 00650 const_local_iterator 00651 end(size_type __n) const 00652 { return _M_h.end(__n); } 00653 00654 const_local_iterator 00655 cend(size_type __n) const 00656 { return _M_h.cend(__n); } 00657 //@} 00658 00659 // hash policy. 00660 00661 /// Returns the average number of elements per bucket. 00662 float 00663 load_factor() const noexcept 00664 { return _M_h.load_factor(); } 00665 00666 /// Returns a positive number that the %unordered_set tries to keep the 00667 /// load factor less than or equal to. 00668 float 00669 max_load_factor() const noexcept 00670 { return _M_h.max_load_factor(); } 00671 00672 /** 00673 * @brief Change the %unordered_set maximum load factor. 00674 * @param __z The new maximum load factor. 00675 */ 00676 void 00677 max_load_factor(float __z) 00678 { _M_h.max_load_factor(__z); } 00679 00680 /** 00681 * @brief May rehash the %unordered_set. 00682 * @param __n The new number of buckets. 00683 * 00684 * Rehash will occur only if the new number of buckets respect the 00685 * %unordered_set maximum load factor. 00686 */ 00687 void 00688 rehash(size_type __n) 00689 { _M_h.rehash(__n); } 00690 00691 /** 00692 * @brief Prepare the %unordered_set for a specified number of 00693 * elements. 00694 * @param __n Number of elements required. 00695 * 00696 * Same as rehash(ceil(n / max_load_factor())). 00697 */ 00698 void 00699 reserve(size_type __n) 00700 { _M_h.reserve(__n); } 00701 00702 template<typename _Value1, typename _Hash1, typename _Pred1, 00703 typename _Alloc1> 00704 friend bool 00705 operator==(const unordered_set<_Value1, _Hash1, _Pred1, _Alloc1>&, 00706 const unordered_set<_Value1, _Hash1, _Pred1, _Alloc1>&); 00707 }; 00708 00709 /** 00710 * @brief A standard container composed of equivalent keys 00711 * (possibly containing multiple of each key value) in which the 00712 * elements' keys are the elements themselves. 00713 * 00714 * @ingroup unordered_associative_containers 00715 * 00716 * @tparam _Value Type of key objects. 00717 * @tparam _Hash Hashing function object type, defaults to hash<_Value>. 00718 * @tparam _Pred Predicate function object type, defaults 00719 * to equal_to<_Value>. 00720 * @tparam _Alloc Allocator type, defaults to allocator<_Key>. 00721 * 00722 * Meets the requirements of a <a href="tables.html#65">container</a>, and 00723 * <a href="tables.html#xx">unordered associative container</a> 00724 * 00725 * Base is _Hashtable, dispatched at compile time via template 00726 * alias __umset_hashtable. 00727 */ 00728 template<class _Value, 00729 class _Hash = hash<_Value>, 00730 class _Pred = std::equal_to<_Value>, 00731 class _Alloc = std::allocator<_Value> > 00732 class unordered_multiset 00733 { 00734 typedef __umset_hashtable<_Value, _Hash, _Pred, _Alloc> _Hashtable; 00735 _Hashtable _M_h; 00736 00737 public: 00738 // typedefs: 00739 //@{ 00740 /// Public typedefs. 00741 typedef typename _Hashtable::key_type key_type; 00742 typedef typename _Hashtable::value_type value_type; 00743 typedef typename _Hashtable::hasher hasher; 00744 typedef typename _Hashtable::key_equal key_equal; 00745 typedef typename _Hashtable::allocator_type allocator_type; 00746 //@} 00747 00748 //@{ 00749 /// Iterator-related typedefs. 00750 typedef typename _Hashtable::pointer pointer; 00751 typedef typename _Hashtable::const_pointer const_pointer; 00752 typedef typename _Hashtable::reference reference; 00753 typedef typename _Hashtable::const_reference const_reference; 00754 typedef typename _Hashtable::iterator iterator; 00755 typedef typename _Hashtable::const_iterator const_iterator; 00756 typedef typename _Hashtable::local_iterator local_iterator; 00757 typedef typename _Hashtable::const_local_iterator const_local_iterator; 00758 typedef typename _Hashtable::size_type size_type; 00759 typedef typename _Hashtable::difference_type difference_type; 00760 //@} 00761 00762 // construct/destroy/copy 00763 00764 /// Default constructor. 00765 unordered_multiset() = default; 00766 00767 /** 00768 * @brief Default constructor creates no elements. 00769 * @param __n Minimal initial number of buckets. 00770 * @param __hf A hash functor. 00771 * @param __eql A key equality functor. 00772 * @param __a An allocator object. 00773 */ 00774 explicit 00775 unordered_multiset(size_type __n, 00776 const hasher& __hf = hasher(), 00777 const key_equal& __eql = key_equal(), 00778 const allocator_type& __a = allocator_type()) 00779 : _M_h(__n, __hf, __eql, __a) 00780 { } 00781 00782 /** 00783 * @brief Builds an %unordered_multiset from a range. 00784 * @param __first An input iterator. 00785 * @param __last An input iterator. 00786 * @param __n Minimal initial number of buckets. 00787 * @param __hf A hash functor. 00788 * @param __eql A key equality functor. 00789 * @param __a An allocator object. 00790 * 00791 * Create an %unordered_multiset consisting of copies of the elements 00792 * from [__first,__last). This is linear in N (where N is 00793 * distance(__first,__last)). 00794 */ 00795 template<typename _InputIterator> 00796 unordered_multiset(_InputIterator __first, _InputIterator __last, 00797 size_type __n = 0, 00798 const hasher& __hf = hasher(), 00799 const key_equal& __eql = key_equal(), 00800 const allocator_type& __a = allocator_type()) 00801 : _M_h(__first, __last, __n, __hf, __eql, __a) 00802 { } 00803 00804 /// Copy constructor. 00805 unordered_multiset(const unordered_multiset&) = default; 00806 00807 /// Move constructor. 00808 unordered_multiset(unordered_multiset&&) = default; 00809 00810 /** 00811 * @brief Builds an %unordered_multiset from an initializer_list. 00812 * @param __l An initializer_list. 00813 * @param __n Minimal initial number of buckets. 00814 * @param __hf A hash functor. 00815 * @param __eql A key equality functor. 00816 * @param __a An allocator object. 00817 * 00818 * Create an %unordered_multiset consisting of copies of the elements in 00819 * the list. This is linear in N (where N is @a __l.size()). 00820 */ 00821 unordered_multiset(initializer_list<value_type> __l, 00822 size_type __n = 0, 00823 const hasher& __hf = hasher(), 00824 const key_equal& __eql = key_equal(), 00825 const allocator_type& __a = allocator_type()) 00826 : _M_h(__l, __n, __hf, __eql, __a) 00827 { } 00828 00829 /// Copy assignment operator. 00830 unordered_multiset& 00831 operator=(const unordered_multiset&) = default; 00832 00833 /// Move assignment operator. 00834 unordered_multiset& 00835 operator=(unordered_multiset&&) = default; 00836 00837 /** 00838 * @brief Creates an %unordered_multiset with no elements. 00839 * @param __a An allocator object. 00840 */ 00841 explicit 00842 unordered_multiset(const allocator_type& __a) 00843 : _M_h(__a) 00844 { } 00845 00846 /* 00847 * @brief Copy constructor with allocator argument. 00848 * @param __uset Input %unordered_multiset to copy. 00849 * @param __a An allocator object. 00850 */ 00851 unordered_multiset(const unordered_multiset& __umset, 00852 const allocator_type& __a) 00853 : _M_h(__umset._M_h, __a) 00854 { } 00855 00856 /* 00857 * @brief Move constructor with allocator argument. 00858 * @param __umset Input %unordered_multiset to move. 00859 * @param __a An allocator object. 00860 */ 00861 unordered_multiset(unordered_multiset&& __umset, 00862 const allocator_type& __a) 00863 : _M_h(std::move(__umset._M_h), __a) 00864 { } 00865 00866 /** 00867 * @brief %Unordered_multiset list assignment operator. 00868 * @param __l An initializer_list. 00869 * 00870 * This function fills an %unordered_multiset with copies of the elements 00871 * in the initializer list @a __l. 00872 * 00873 * Note that the assignment completely changes the %unordered_multiset 00874 * and that the resulting %unordered_set's size is the same as the number 00875 * of elements assigned. Old data may be lost. 00876 */ 00877 unordered_multiset& 00878 operator=(initializer_list<value_type> __l) 00879 { 00880 _M_h = __l; 00881 return *this; 00882 } 00883 00884 /// Returns the allocator object with which the %unordered_multiset was 00885 /// constructed. 00886 allocator_type 00887 get_allocator() const noexcept 00888 { return _M_h.get_allocator(); } 00889 00890 // size and capacity: 00891 00892 /// Returns true if the %unordered_multiset is empty. 00893 bool 00894 empty() const noexcept 00895 { return _M_h.empty(); } 00896 00897 /// Returns the size of the %unordered_multiset. 00898 size_type 00899 size() const noexcept 00900 { return _M_h.size(); } 00901 00902 /// Returns the maximum size of the %unordered_multiset. 00903 size_type 00904 max_size() const noexcept 00905 { return _M_h.max_size(); } 00906 00907 // iterators. 00908 00909 //@{ 00910 /** 00911 * Returns a read-only (constant) iterator that points to the first 00912 * element in the %unordered_multiset. 00913 */ 00914 iterator 00915 begin() noexcept 00916 { return _M_h.begin(); } 00917 00918 const_iterator 00919 begin() const noexcept 00920 { return _M_h.begin(); } 00921 //@} 00922 00923 //@{ 00924 /** 00925 * Returns a read-only (constant) iterator that points one past the last 00926 * element in the %unordered_multiset. 00927 */ 00928 iterator 00929 end() noexcept 00930 { return _M_h.end(); } 00931 00932 const_iterator 00933 end() const noexcept 00934 { return _M_h.end(); } 00935 //@} 00936 00937 /** 00938 * Returns a read-only (constant) iterator that points to the first 00939 * element in the %unordered_multiset. 00940 */ 00941 const_iterator 00942 cbegin() const noexcept 00943 { return _M_h.begin(); } 00944 00945 /** 00946 * Returns a read-only (constant) iterator that points one past the last 00947 * element in the %unordered_multiset. 00948 */ 00949 const_iterator 00950 cend() const noexcept 00951 { return _M_h.end(); } 00952 00953 // modifiers. 00954 00955 /** 00956 * @brief Builds and insert an element into the %unordered_multiset. 00957 * @param __args Arguments used to generate an element. 00958 * @return An iterator that points to the inserted element. 00959 * 00960 * Insertion requires amortized constant time. 00961 */ 00962 template<typename... _Args> 00963 iterator 00964 emplace(_Args&&... __args) 00965 { return _M_h.emplace(std::forward<_Args>(__args)...); } 00966 00967 /** 00968 * @brief Inserts an element into the %unordered_multiset. 00969 * @param __pos An iterator that serves as a hint as to where the 00970 * element should be inserted. 00971 * @param __args Arguments used to generate the element to be 00972 * inserted. 00973 * @return An iterator that points to the inserted element. 00974 * 00975 * Note that the first parameter is only a hint and can potentially 00976 * improve the performance of the insertion process. A bad hint would 00977 * cause no gains in efficiency. 00978 * 00979 * For more on @a hinting, see: 00980 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00981 * 00982 * Insertion requires amortized constant time. 00983 */ 00984 template<typename... _Args> 00985 iterator 00986 emplace_hint(const_iterator __pos, _Args&&... __args) 00987 { return _M_h.emplace_hint(__pos, std::forward<_Args>(__args)...); } 00988 00989 //@{ 00990 /** 00991 * @brief Inserts an element into the %unordered_multiset. 00992 * @param __x Element to be inserted. 00993 * @return An iterator that points to the inserted element. 00994 * 00995 * Insertion requires amortized constant time. 00996 */ 00997 iterator 00998 insert(const value_type& __x) 00999 { return _M_h.insert(__x); } 01000 01001 iterator 01002 insert(value_type&& __x) 01003 { return _M_h.insert(std::move(__x)); } 01004 //@} 01005 01006 //@{ 01007 /** 01008 * @brief Inserts an element into the %unordered_multiset. 01009 * @param __hint An iterator that serves as a hint as to where the 01010 * element should be inserted. 01011 * @param __x Element to be inserted. 01012 * @return An iterator that points to the inserted element. 01013 * 01014 * Note that the first parameter is only a hint and can potentially 01015 * improve the performance of the insertion process. A bad hint would 01016 * cause no gains in efficiency. 01017 * 01018 * For more on @a hinting, see: 01019 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 01020 * 01021 * Insertion requires amortized constant. 01022 */ 01023 iterator 01024 insert(const_iterator __hint, const value_type& __x) 01025 { return _M_h.insert(__hint, __x); } 01026 01027 iterator 01028 insert(const_iterator __hint, value_type&& __x) 01029 { return _M_h.insert(__hint, std::move(__x)); } 01030 //@} 01031 01032 /** 01033 * @brief A template function that inserts a range of elements. 01034 * @param __first Iterator pointing to the start of the range to be 01035 * inserted. 01036 * @param __last Iterator pointing to the end of the range. 01037 * 01038 * Complexity similar to that of the range constructor. 01039 */ 01040 template<typename _InputIterator> 01041 void 01042 insert(_InputIterator __first, _InputIterator __last) 01043 { _M_h.insert(__first, __last); } 01044 01045 /** 01046 * @brief Inserts a list of elements into the %unordered_multiset. 01047 * @param __l A std::initializer_list<value_type> of elements to be 01048 * inserted. 01049 * 01050 * Complexity similar to that of the range constructor. 01051 */ 01052 void 01053 insert(initializer_list<value_type> __l) 01054 { _M_h.insert(__l); } 01055 01056 //@{ 01057 /** 01058 * @brief Erases an element from an %unordered_multiset. 01059 * @param __position An iterator pointing to the element to be erased. 01060 * @return An iterator pointing to the element immediately following 01061 * @a __position prior to the element being erased. If no such 01062 * element exists, end() is returned. 01063 * 01064 * This function erases an element, pointed to by the given iterator, 01065 * from an %unordered_multiset. 01066 * 01067 * Note that this function only erases the element, and that if the 01068 * element is itself a pointer, the pointed-to memory is not touched in 01069 * any way. Managing the pointer is the user's responsibility. 01070 */ 01071 iterator 01072 erase(const_iterator __position) 01073 { return _M_h.erase(__position); } 01074 01075 // LWG 2059. 01076 iterator 01077 erase(iterator __position) 01078 { return _M_h.erase(__position); } 01079 //@} 01080 01081 01082 /** 01083 * @brief Erases elements according to the provided key. 01084 * @param __x Key of element to be erased. 01085 * @return The number of elements erased. 01086 * 01087 * This function erases all the elements located by the given key from 01088 * an %unordered_multiset. 01089 * 01090 * Note that this function only erases the element, and that if the 01091 * element is itself a pointer, the pointed-to memory is not touched in 01092 * any way. Managing the pointer is the user's responsibility. 01093 */ 01094 size_type 01095 erase(const key_type& __x) 01096 { return _M_h.erase(__x); } 01097 01098 /** 01099 * @brief Erases a [__first,__last) range of elements from an 01100 * %unordered_multiset. 01101 * @param __first Iterator pointing to the start of the range to be 01102 * erased. 01103 * @param __last Iterator pointing to the end of the range to 01104 * be erased. 01105 * @return The iterator @a __last. 01106 * 01107 * This function erases a sequence of elements from an 01108 * %unordered_multiset. 01109 * 01110 * Note that this function only erases the element, and that if 01111 * the element is itself a pointer, the pointed-to memory is not touched 01112 * in any way. Managing the pointer is the user's responsibility. 01113 */ 01114 iterator 01115 erase(const_iterator __first, const_iterator __last) 01116 { return _M_h.erase(__first, __last); } 01117 01118 /** 01119 * Erases all elements in an %unordered_multiset. 01120 * 01121 * Note that this function only erases the elements, and that if the 01122 * elements themselves are pointers, the pointed-to memory is not touched 01123 * in any way. Managing the pointer is the user's responsibility. 01124 */ 01125 void 01126 clear() noexcept 01127 { _M_h.clear(); } 01128 01129 /** 01130 * @brief Swaps data with another %unordered_multiset. 01131 * @param __x An %unordered_multiset of the same element and allocator 01132 * types. 01133 * 01134 * This exchanges the elements between two sets in constant time. 01135 * Note that the global std::swap() function is specialized such that 01136 * std::swap(s1,s2) will feed to this function. 01137 */ 01138 void 01139 swap(unordered_multiset& __x) 01140 noexcept( noexcept(_M_h.swap(__x._M_h)) ) 01141 { _M_h.swap(__x._M_h); } 01142 01143 // observers. 01144 01145 /// Returns the hash functor object with which the %unordered_multiset 01146 /// was constructed. 01147 hasher 01148 hash_function() const 01149 { return _M_h.hash_function(); } 01150 01151 /// Returns the key comparison object with which the %unordered_multiset 01152 /// was constructed. 01153 key_equal 01154 key_eq() const 01155 { return _M_h.key_eq(); } 01156 01157 // lookup. 01158 01159 //@{ 01160 /** 01161 * @brief Tries to locate an element in an %unordered_multiset. 01162 * @param __x Element to be located. 01163 * @return Iterator pointing to sought-after element, or end() if not 01164 * found. 01165 * 01166 * This function takes a key and tries to locate the element with which 01167 * the key matches. If successful the function returns an iterator 01168 * pointing to the sought after element. If unsuccessful it returns the 01169 * past-the-end ( @c end() ) iterator. 01170 */ 01171 iterator 01172 find(const key_type& __x) 01173 { return _M_h.find(__x); } 01174 01175 const_iterator 01176 find(const key_type& __x) const 01177 { return _M_h.find(__x); } 01178 //@} 01179 01180 /** 01181 * @brief Finds the number of elements. 01182 * @param __x Element to located. 01183 * @return Number of elements with specified key. 01184 */ 01185 size_type 01186 count(const key_type& __x) const 01187 { return _M_h.count(__x); } 01188 01189 //@{ 01190 /** 01191 * @brief Finds a subsequence matching given key. 01192 * @param __x Key to be located. 01193 * @return Pair of iterators that possibly points to the subsequence 01194 * matching given key. 01195 */ 01196 std::pair<iterator, iterator> 01197 equal_range(const key_type& __x) 01198 { return _M_h.equal_range(__x); } 01199 01200 std::pair<const_iterator, const_iterator> 01201 equal_range(const key_type& __x) const 01202 { return _M_h.equal_range(__x); } 01203 //@} 01204 01205 // bucket interface. 01206 01207 /// Returns the number of buckets of the %unordered_multiset. 01208 size_type 01209 bucket_count() const noexcept 01210 { return _M_h.bucket_count(); } 01211 01212 /// Returns the maximum number of buckets of the %unordered_multiset. 01213 size_type 01214 max_bucket_count() const noexcept 01215 { return _M_h.max_bucket_count(); } 01216 01217 /* 01218 * @brief Returns the number of elements in a given bucket. 01219 * @param __n A bucket index. 01220 * @return The number of elements in the bucket. 01221 */ 01222 size_type 01223 bucket_size(size_type __n) const 01224 { return _M_h.bucket_size(__n); } 01225 01226 /* 01227 * @brief Returns the bucket index of a given element. 01228 * @param __key A key instance. 01229 * @return The key bucket index. 01230 */ 01231 size_type 01232 bucket(const key_type& __key) const 01233 { return _M_h.bucket(__key); } 01234 01235 //@{ 01236 /** 01237 * @brief Returns a read-only (constant) iterator pointing to the first 01238 * bucket element. 01239 * @param __n The bucket index. 01240 * @return A read-only local iterator. 01241 */ 01242 local_iterator 01243 begin(size_type __n) 01244 { return _M_h.begin(__n); } 01245 01246 const_local_iterator 01247 begin(size_type __n) const 01248 { return _M_h.begin(__n); } 01249 01250 const_local_iterator 01251 cbegin(size_type __n) const 01252 { return _M_h.cbegin(__n); } 01253 //@} 01254 01255 //@{ 01256 /** 01257 * @brief Returns a read-only (constant) iterator pointing to one past 01258 * the last bucket elements. 01259 * @param __n The bucket index. 01260 * @return A read-only local iterator. 01261 */ 01262 local_iterator 01263 end(size_type __n) 01264 { return _M_h.end(__n); } 01265 01266 const_local_iterator 01267 end(size_type __n) const 01268 { return _M_h.end(__n); } 01269 01270 const_local_iterator 01271 cend(size_type __n) const 01272 { return _M_h.cend(__n); } 01273 //@} 01274 01275 // hash policy. 01276 01277 /// Returns the average number of elements per bucket. 01278 float 01279 load_factor() const noexcept 01280 { return _M_h.load_factor(); } 01281 01282 /// Returns a positive number that the %unordered_multiset tries to keep the 01283 /// load factor less than or equal to. 01284 float 01285 max_load_factor() const noexcept 01286 { return _M_h.max_load_factor(); } 01287 01288 /** 01289 * @brief Change the %unordered_multiset maximum load factor. 01290 * @param __z The new maximum load factor. 01291 */ 01292 void 01293 max_load_factor(float __z) 01294 { _M_h.max_load_factor(__z); } 01295 01296 /** 01297 * @brief May rehash the %unordered_multiset. 01298 * @param __n The new number of buckets. 01299 * 01300 * Rehash will occur only if the new number of buckets respect the 01301 * %unordered_multiset maximum load factor. 01302 */ 01303 void 01304 rehash(size_type __n) 01305 { _M_h.rehash(__n); } 01306 01307 /** 01308 * @brief Prepare the %unordered_multiset for a specified number of 01309 * elements. 01310 * @param __n Number of elements required. 01311 * 01312 * Same as rehash(ceil(n / max_load_factor())). 01313 */ 01314 void 01315 reserve(size_type __n) 01316 { _M_h.reserve(__n); } 01317 01318 template<typename _Value1, typename _Hash1, typename _Pred1, 01319 typename _Alloc1> 01320 friend bool 01321 operator==(const unordered_multiset<_Value1, _Hash1, _Pred1, _Alloc1>&, 01322 const unordered_multiset<_Value1, _Hash1, _Pred1, _Alloc1>&); 01323 }; 01324 01325 template<class _Value, class _Hash, class _Pred, class _Alloc> 01326 inline void 01327 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x, 01328 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y) 01329 { __x.swap(__y); } 01330 01331 template<class _Value, class _Hash, class _Pred, class _Alloc> 01332 inline void 01333 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x, 01334 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y) 01335 { __x.swap(__y); } 01336 01337 template<class _Value, class _Hash, class _Pred, class _Alloc> 01338 inline bool 01339 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x, 01340 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y) 01341 { return __x._M_h._M_equal(__y._M_h); } 01342 01343 template<class _Value, class _Hash, class _Pred, class _Alloc> 01344 inline bool 01345 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x, 01346 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y) 01347 { return !(__x == __y); } 01348 01349 template<class _Value, class _Hash, class _Pred, class _Alloc> 01350 inline bool 01351 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x, 01352 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y) 01353 { return __x._M_h._M_equal(__y._M_h); } 01354 01355 template<class _Value, class _Hash, class _Pred, class _Alloc> 01356 inline bool 01357 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x, 01358 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y) 01359 { return !(__x == __y); } 01360 01361 _GLIBCXX_END_NAMESPACE_CONTAINER 01362 } // namespace std 01363 01364 #endif /* _UNORDERED_SET_H */