1 // Profiling unordered_set/unordered_multiset implementation -*- C++ -*-
3 // Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
24 /** @file profile/unordered_set
25 * This file is a GNU profile extension to the Standard C++ Library.
28 #ifndef _GLIBCXX_PROFILE_UNORDERED_SET
29 #define _GLIBCXX_PROFILE_UNORDERED_SET 1
31 #if __cplusplus < 201103L
32 # include <bits/c++0x_warning.h>
34 # include <unordered_set>
36 #include <profile/base.h>
37 #include <profile/unordered_base.h>
39 #define _GLIBCXX_BASE unordered_set<_Key, _Hash, _Pred, _Alloc>
40 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
42 namespace std _GLIBCXX_VISIBILITY(default)
46 /** @brief Unordered_set wrapper with performance instrumentation. */
47 template<typename _Key,
48 typename _Hash = std::hash<_Key>,
49 typename _Pred = std::equal_to<_Key>,
50 typename _Alloc = std::allocator<_Key> >
52 : public _GLIBCXX_STD_BASE,
53 public _Unordered_profile<unordered_set<_Key, _Hash, _Pred, _Alloc>,
56 typedef _GLIBCXX_STD_BASE _Base;
59 _M_base() noexcept { return *this; }
62 _M_base() const noexcept { return *this; }
65 typedef typename _Base::size_type size_type;
66 typedef typename _Base::hasher hasher;
67 typedef typename _Base::key_equal key_equal;
68 typedef typename _Base::allocator_type allocator_type;
69 typedef typename _Base::key_type key_type;
70 typedef typename _Base::value_type value_type;
71 typedef typename _Base::difference_type difference_type;
72 typedef typename _Base::reference reference;
73 typedef typename _Base::const_reference const_reference;
75 typedef typename _Base::iterator iterator;
76 typedef typename _Base::const_iterator const_iterator;
78 unordered_set() = default;
81 unordered_set(size_type __n,
82 const hasher& __hf = hasher(),
83 const key_equal& __eql = key_equal(),
84 const allocator_type& __a = allocator_type())
85 : _Base(__n, __hf, __eql, __a)
88 template<typename _InputIterator>
89 unordered_set(_InputIterator __f, _InputIterator __l,
91 const hasher& __hf = hasher(),
92 const key_equal& __eql = key_equal(),
93 const allocator_type& __a = allocator_type())
94 : _Base(__f, __l, __n, __hf, __eql, __a)
97 unordered_set(const unordered_set&) = default;
99 unordered_set(const _Base& __x)
103 unordered_set(unordered_set&&) = default;
106 unordered_set(const allocator_type& __a)
110 unordered_set(const unordered_set& __uset,
111 const allocator_type& __a)
112 : _Base(__uset._M_base(), __a)
115 unordered_set(unordered_set&& __uset,
116 const allocator_type& __a)
117 : _Base(std::move(__uset._M_base()), __a)
120 unordered_set(initializer_list<value_type> __l,
122 const hasher& __hf = hasher(),
123 const key_equal& __eql = key_equal(),
124 const allocator_type& __a = allocator_type())
125 : _Base(__l, __n, __hf, __eql, __a)
128 unordered_set(size_type __n, const allocator_type& __a)
129 : unordered_set(__n, hasher(), key_equal(), __a)
132 unordered_set(size_type __n, const hasher& __hf,
133 const allocator_type& __a)
134 : unordered_set(__n, __hf, key_equal(), __a)
137 template<typename _InputIterator>
138 unordered_set(_InputIterator __first, _InputIterator __last,
140 const allocator_type& __a)
141 : unordered_set(__first, __last, __n, hasher(), key_equal(), __a)
144 template<typename _InputIterator>
145 unordered_set(_InputIterator __first, _InputIterator __last,
146 size_type __n, const hasher& __hf,
147 const allocator_type& __a)
148 : unordered_set(__first, __last, __n, __hf, key_equal(), __a)
151 unordered_set(initializer_list<value_type> __l,
153 const allocator_type& __a)
154 : unordered_set(__l, __n, hasher(), key_equal(), __a)
157 unordered_set(initializer_list<value_type> __l,
158 size_type __n, const hasher& __hf,
159 const allocator_type& __a)
160 : unordered_set(__l, __n, __hf, key_equal(), __a)
164 operator=(const unordered_set&) = default;
167 operator=(unordered_set&&) = default;
170 operator=(initializer_list<value_type> __l)
172 this->_M_profile_destruct();
174 this->_M_profile_construct();
179 swap(unordered_set& __x)
180 noexcept( noexcept(__x._M_base().swap(__x)) )
189 this->_M_profile_destruct();
191 this->_M_profile_construct();
194 template<typename... _Args>
195 std::pair<iterator, bool>
196 emplace(_Args&&... __args)
198 size_type __old_size = _Base::bucket_count();
199 std::pair<iterator, bool> __res
200 = _Base::emplace(std::forward<_Args>(__args)...);
201 this->_M_profile_resize(__old_size);
205 template<typename... _Args>
207 emplace_hint(const_iterator __it, _Args&&... __args)
209 size_type __old_size = _Base::bucket_count();
211 = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
212 this->_M_profile_resize(__old_size);
217 insert(std::initializer_list<value_type> __l)
219 size_type __old_size = _Base::bucket_count();
221 this->_M_profile_resize(__old_size);
224 std::pair<iterator, bool>
225 insert(const value_type& __obj)
227 size_type __old_size = _Base::bucket_count();
228 std::pair<iterator, bool> __res = _Base::insert(__obj);
229 this->_M_profile_resize(__old_size);
234 insert(const_iterator __iter, const value_type& __v)
236 size_type __old_size = _Base::bucket_count();
237 iterator __res = _Base::insert(__iter, __v);
238 this->_M_profile_resize(__old_size);
242 std::pair<iterator, bool>
243 insert(value_type&& __obj)
245 size_type __old_size = _Base::bucket_count();
246 std::pair<iterator, bool> __res = _Base::insert(std::move(__obj));
247 this->_M_profile_resize(__old_size);
252 insert(const_iterator __iter, value_type&& __v)
254 size_type __old_size = _Base::bucket_count();
255 iterator __res = _Base::insert(__iter, std::move(__v));
256 this->_M_profile_resize(__old_size);
260 template<typename _InputIter>
262 insert(_InputIter __first, _InputIter __last)
264 size_type __old_size = _Base::bucket_count();
265 _Base::insert(__first, __last);
266 this->_M_profile_resize(__old_size);
270 rehash(size_type __n)
272 size_type __old_size = _Base::bucket_count();
274 this->_M_profile_resize(__old_size);
278 template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
280 swap(unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
281 unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
284 template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
286 operator==(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
287 const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
288 { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
290 template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
292 operator!=(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
293 const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
294 { return !(__x == __y); }
297 #undef _GLIBCXX_STD_BASE
298 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
299 #define _GLIBCXX_BASE unordered_multiset<_Value, _Hash, _Pred, _Alloc>
301 /** @brief Unordered_multiset wrapper with performance instrumentation. */
302 template<typename _Value,
303 typename _Hash = std::hash<_Value>,
304 typename _Pred = std::equal_to<_Value>,
305 typename _Alloc = std::allocator<_Value> >
306 class unordered_multiset
307 : public _GLIBCXX_STD_BASE,
308 public _Unordered_profile<unordered_multiset<_Value,
309 _Hash, _Pred, _Alloc>,
312 typedef _GLIBCXX_STD_BASE _Base;
315 _M_base() noexcept { return *this; }
318 _M_base() const noexcept { return *this; }
321 typedef typename _Base::size_type size_type;
322 typedef typename _Base::hasher hasher;
323 typedef typename _Base::key_equal key_equal;
324 typedef typename _Base::allocator_type allocator_type;
325 typedef typename _Base::key_type key_type;
326 typedef typename _Base::value_type value_type;
327 typedef typename _Base::difference_type difference_type;
328 typedef typename _Base::reference reference;
329 typedef typename _Base::const_reference const_reference;
331 typedef typename _Base::iterator iterator;
332 typedef typename _Base::const_iterator const_iterator;
334 unordered_multiset() = default;
337 unordered_multiset(size_type __n,
338 const hasher& __hf = hasher(),
339 const key_equal& __eql = key_equal(),
340 const allocator_type& __a = allocator_type())
341 : _Base(__n, __hf, __eql, __a)
344 template<typename _InputIterator>
345 unordered_multiset(_InputIterator __f, _InputIterator __l,
347 const hasher& __hf = hasher(),
348 const key_equal& __eql = key_equal(),
349 const allocator_type& __a = allocator_type())
350 : _Base(__f, __l, __n, __hf, __eql, __a)
353 unordered_multiset(const unordered_multiset&) = default;
355 unordered_multiset(const _Base& __x)
359 unordered_multiset(unordered_multiset&&) = default;
362 unordered_multiset(const allocator_type& __a)
366 unordered_multiset(const unordered_multiset& __umset,
367 const allocator_type& __a)
368 : _Base(__umset._M_base(), __a)
371 unordered_multiset(unordered_multiset&& __umset,
372 const allocator_type& __a)
373 : _Base(std::move(__umset._M_base()), __a)
376 unordered_multiset(initializer_list<value_type> __l,
378 const hasher& __hf = hasher(),
379 const key_equal& __eql = key_equal(),
380 const allocator_type& __a = allocator_type())
381 : _Base(__l, __n, __hf, __eql, __a)
384 unordered_multiset(size_type __n, const allocator_type& __a)
385 : unordered_multiset(__n, hasher(), key_equal(), __a)
388 unordered_multiset(size_type __n, const hasher& __hf,
389 const allocator_type& __a)
390 : unordered_multiset(__n, __hf, key_equal(), __a)
393 template<typename _InputIterator>
394 unordered_multiset(_InputIterator __first, _InputIterator __last,
396 const allocator_type& __a)
397 : unordered_multiset(__first, __last, __n, hasher(), key_equal(), __a)
400 template<typename _InputIterator>
401 unordered_multiset(_InputIterator __first, _InputIterator __last,
402 size_type __n, const hasher& __hf,
403 const allocator_type& __a)
404 : unordered_multiset(__first, __last, __n, __hf, key_equal(), __a)
407 unordered_multiset(initializer_list<value_type> __l,
409 const allocator_type& __a)
410 : unordered_multiset(__l, __n, hasher(), key_equal(), __a)
413 unordered_multiset(initializer_list<value_type> __l,
414 size_type __n, const hasher& __hf,
415 const allocator_type& __a)
416 : unordered_multiset(__l, __n, __hf, key_equal(), __a)
420 operator=(const unordered_multiset&) = default;
423 operator=(unordered_multiset&&) = default;
426 operator=(initializer_list<value_type> __l)
428 this->_M_profile_destruct();
430 this->_M_profile_construct();
435 swap(unordered_multiset& __x)
436 noexcept( noexcept(__x._M_base().swap(__x)) )
445 this->_M_profile_destruct();
447 this->_M_profile_construct();
450 template<typename... _Args>
452 emplace(_Args&&... __args)
454 size_type __old_size = _Base::bucket_count();
455 iterator __res = _Base::emplace(std::forward<_Args>(__args)...);
456 this->_M_profile_resize(__old_size);
460 template<typename... _Args>
462 emplace_hint(const_iterator __it, _Args&&... __args)
464 size_type __old_size = _Base::bucket_count();
466 = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
467 this->_M_profile_resize(__old_size);
472 insert(std::initializer_list<value_type> __l)
474 size_type __old_size = _Base::bucket_count();
476 this->_M_profile_resize(__old_size);
480 insert(const value_type& __obj)
482 size_type __old_size = _Base::bucket_count();
483 iterator __res = _Base::insert(__obj);
484 this->_M_profile_resize(__old_size);
489 insert(const_iterator __iter, const value_type& __v)
491 size_type __old_size = _Base::bucket_count();
492 iterator __res = _Base::insert(__iter, __v);
493 this->_M_profile_resize(__old_size);
498 insert(value_type&& __obj)
500 size_type __old_size = _Base::bucket_count();
501 iterator __res = _Base::insert(std::move(__obj));
502 this->_M_profile_resize(__old_size);
507 insert(const_iterator __iter, value_type&& __v)
509 size_type __old_size = _Base::bucket_count();
510 iterator __res = _Base::insert(__iter, std::move(__v));
511 this->_M_profile_resize(__old_size);
515 template<typename _InputIter>
517 insert(_InputIter __first, _InputIter __last)
519 size_type __old_size = _Base::bucket_count();
520 _Base::insert(__first, __last);
521 this->_M_profile_resize(__old_size);
525 rehash(size_type __n)
527 size_type __old_size = _Base::bucket_count();
529 this->_M_profile_resize(__old_size);
533 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
535 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
536 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
539 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
541 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
542 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
543 { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
545 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
547 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
548 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
549 { return !(__x == __y); }
551 } // namespace __profile
555 #undef _GLIBCXX_STD_BASE