1 // Profiling unordered_set/unordered_multiset implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file profile/unordered_set
31 * This file is a GNU profile extension to the Standard C++ Library.
34 #ifndef _GLIBCXX_PROFILE_UNORDERED_SET
35 #define _GLIBCXX_PROFILE_UNORDERED_SET 1
37 #ifndef __GXX_EXPERIMENTAL_CXX0X__
38 # include <bits/c++0x_warning.h>
40 # include <unordered_set>
42 #include <profile/base.h>
44 #define _GLIBCXX_BASE unordered_set<_Key, _Hash, _Pred, _Alloc>
45 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
47 namespace std _GLIBCXX_VISIBILITY(default)
51 /** @brief Unordered_set wrapper with performance instrumentation. */
52 template<typename _Key,
53 typename _Hash = std::hash<_Key>,
54 typename _Pred = std::equal_to<_Key>,
55 typename _Alloc = std::allocator<_Key> >
57 : public _GLIBCXX_STD_BASE
59 typedef typename _GLIBCXX_STD_BASE _Base;
62 typedef typename _Base::size_type size_type;
63 typedef typename _Base::hasher hasher;
64 typedef typename _Base::key_equal key_equal;
65 typedef typename _Base::allocator_type allocator_type;
66 typedef typename _Base::key_type key_type;
67 typedef typename _Base::value_type value_type;
68 typedef typename _Base::difference_type difference_type;
69 typedef typename _Base::reference reference;
70 typedef typename _Base::const_reference const_reference;
72 typedef typename _Base::iterator iterator;
73 typedef typename _Base::const_iterator const_iterator;
76 unordered_set(size_type __n = 10,
77 const hasher& __hf = hasher(),
78 const key_equal& __eql = key_equal(),
79 const allocator_type& __a = allocator_type())
80 : _Base(__n, __hf, __eql, __a)
82 __profcxx_hashtable_construct(this, _Base::bucket_count());
83 __profcxx_hashtable_construct2(this);
86 template<typename _InputIterator>
87 unordered_set(_InputIterator __f, _InputIterator __l,
89 const hasher& __hf = hasher(),
90 const key_equal& __eql = key_equal(),
91 const allocator_type& __a = allocator_type())
92 : _Base(__f, __l, __n, __hf, __eql, __a)
94 __profcxx_hashtable_construct(this, _Base::bucket_count());
95 __profcxx_hashtable_construct2(this);
98 unordered_set(const unordered_set& __x)
101 __profcxx_hashtable_construct(this, _Base::bucket_count());
102 __profcxx_hashtable_construct2(this);
105 unordered_set(const _Base& __x)
108 __profcxx_hashtable_construct(this, _Base::bucket_count());
109 __profcxx_hashtable_construct2(this);
112 unordered_set(unordered_set&& __x)
113 : _Base(std::move(__x))
115 __profcxx_hashtable_construct(this, _Base::bucket_count());
116 __profcxx_hashtable_construct2(this);
119 unordered_set(initializer_list<value_type> __l,
121 const hasher& __hf = hasher(),
122 const key_equal& __eql = key_equal(),
123 const allocator_type& __a = allocator_type())
124 : _Base(__l, __n, __hf, __eql, __a) { }
127 operator=(const unordered_set& __x)
129 *static_cast<_Base*>(this) = __x;
134 operator=(unordered_set&& __x)
144 operator=(initializer_list<value_type> __l)
151 ~unordered_set() noexcept
153 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
155 _M_profile_destruct();
159 swap(unordered_set& __x)
167 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
169 _M_profile_destruct();
174 insert(std::initializer_list<value_type> __l)
176 size_type __old_size = _Base::bucket_count();
178 _M_profile_resize(__old_size, _Base::bucket_count());
181 std::pair<iterator, bool>
182 insert(const value_type& __obj)
184 size_type __old_size = _Base::bucket_count();
185 std::pair<iterator, bool> __res = _Base::insert(__obj);
186 _M_profile_resize(__old_size, _Base::bucket_count());
191 insert(const_iterator __iter, const value_type& __v)
193 size_type __old_size = _Base::bucket_count();
194 iterator __res = _Base::insert(__iter, __v);
195 _M_profile_resize(__old_size, _Base::bucket_count());
199 std::pair<iterator, bool>
200 insert(value_type&& __obj)
202 size_type __old_size = _Base::bucket_count();
203 std::pair<iterator, bool> __res = _Base::insert(std::move(__obj));
204 _M_profile_resize(__old_size, _Base::bucket_count());
209 insert(const_iterator __iter, value_type&& __v)
211 size_type __old_size = _Base::bucket_count();
212 iterator __res = _Base::insert(__iter, std::move(__v));
213 _M_profile_resize(__old_size, _Base::bucket_count());
217 template<typename _InputIter>
219 insert(_InputIter __first, _InputIter __last)
221 size_type __old_size = _Base::bucket_count();
222 _Base::insert(__first, __last);
223 _M_profile_resize(__old_size, _Base::bucket_count());
227 insert(const value_type* __first, const value_type* __last)
229 size_type __old_size = _Base::bucket_count();
230 _Base::insert(__first, __last);
231 _M_profile_resize(__old_size, _Base::bucket_count());
234 void rehash(size_type __n)
236 size_type __old_size = _Base::bucket_count();
238 _M_profile_resize(__old_size, _Base::bucket_count());
243 _M_base() noexcept { return *this; }
246 _M_base() const noexcept { return *this; }
249 _M_profile_resize(size_type __old_size, size_type __new_size)
251 if (__old_size != __new_size)
252 __profcxx_hashtable_resize(this, __old_size, __new_size);
256 _M_profile_destruct()
258 size_type __hops = 0, __lc = 0, __chain = 0;
259 for (iterator __it = _M_base().begin(); __it != _M_base().end();
262 while (__it._M_cur_node->_M_next)
271 __lc = __lc > __chain ? __lc : __chain;
272 __hops += __chain * (__chain - 1) / 2;
276 __profcxx_hashtable_destruct2(this, __lc, _Base::size(), __hops);
281 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
283 swap(unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
284 unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
287 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
289 operator==(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
290 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
291 { return __x._M_equal(__y); }
293 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
295 operator!=(const unordered_set<_Value, _Hash, _Pred, _Alloc>& __x,
296 const unordered_set<_Value, _Hash, _Pred, _Alloc>& __y)
297 { return !(__x == __y); }
300 #undef _GLIBCXX_STD_BASE
301 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
302 #define _GLIBCXX_BASE unordered_multiset<_Value, _Hash, _Pred, _Alloc>
304 /** @brief Unordered_multiset wrapper with performance instrumentation. */
305 template<typename _Value,
306 typename _Hash = std::hash<_Value>,
307 typename _Pred = std::equal_to<_Value>,
308 typename _Alloc = std::allocator<_Value> >
309 class unordered_multiset
310 : public _GLIBCXX_STD_BASE
312 typedef typename _GLIBCXX_STD_BASE _Base;
315 typedef typename _Base::size_type size_type;
316 typedef typename _Base::hasher hasher;
317 typedef typename _Base::key_equal key_equal;
318 typedef typename _Base::allocator_type allocator_type;
319 typedef typename _Base::key_type key_type;
320 typedef typename _Base::value_type value_type;
321 typedef typename _Base::difference_type difference_type;
322 typedef typename _Base::reference reference;
323 typedef typename _Base::const_reference const_reference;
325 typedef typename _Base::iterator iterator;
326 typedef typename _Base::const_iterator const_iterator;
329 unordered_multiset(size_type __n = 10,
330 const hasher& __hf = hasher(),
331 const key_equal& __eql = key_equal(),
332 const allocator_type& __a = allocator_type())
333 : _Base(__n, __hf, __eql, __a)
335 __profcxx_hashtable_construct(this, _Base::bucket_count());
338 template<typename _InputIterator>
339 unordered_multiset(_InputIterator __f, _InputIterator __l,
341 const hasher& __hf = hasher(),
342 const key_equal& __eql = key_equal(),
343 const allocator_type& __a = allocator_type())
344 : _Base(__f, __l, __n, __hf, __eql, __a)
346 __profcxx_hashtable_construct(this, _Base::bucket_count());
349 unordered_multiset(const unordered_multiset& __x)
352 __profcxx_hashtable_construct(this, _Base::bucket_count());
355 unordered_multiset(const _Base& __x)
358 __profcxx_hashtable_construct(this, _Base::bucket_count());
361 unordered_multiset(unordered_multiset&& __x)
362 : _Base(std::move(__x))
364 __profcxx_hashtable_construct(this, _Base::bucket_count());
367 unordered_multiset(initializer_list<value_type> __l,
369 const hasher& __hf = hasher(),
370 const key_equal& __eql = key_equal(),
371 const allocator_type& __a = allocator_type())
372 : _Base(__l, __n, __hf, __eql, __a) { }
375 operator=(const unordered_multiset& __x)
377 *static_cast<_Base*>(this) = __x;
382 operator=(unordered_multiset&& __x)
392 operator=(initializer_list<value_type> __l)
399 ~unordered_multiset() noexcept
401 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
403 _M_profile_destruct();
407 swap(unordered_multiset& __x)
415 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
417 _M_profile_destruct();
422 insert(std::initializer_list<value_type> __l)
424 size_type __old_size = _Base::bucket_count();
426 _M_profile_resize(__old_size, _Base::bucket_count());
430 insert(const value_type& __obj)
432 size_type __old_size = _Base::bucket_count();
433 iterator __res = _Base::insert(__obj);
434 _M_profile_resize(__old_size, _Base::bucket_count());
439 insert(const_iterator __iter, const value_type& __v)
441 size_type __old_size = _Base::bucket_count();
442 iterator __res = _Base::insert(__iter, __v);
443 _M_profile_resize(__old_size, _Base::bucket_count());
448 insert(value_type&& __obj)
450 size_type __old_size = _Base::bucket_count();
451 iterator __res = _Base::insert(std::move(__obj));
452 _M_profile_resize(__old_size, _Base::bucket_count());
457 insert(const_iterator __iter, value_type&& __v)
459 size_type __old_size = _Base::bucket_count();
460 iterator __res = _Base::insert(__iter, std::move(__v));
461 _M_profile_resize(__old_size, _Base::bucket_count());
465 template<typename _InputIter>
467 insert(_InputIter __first, _InputIter __last)
469 size_type __old_size = _Base::bucket_count();
470 _Base::insert(__first, __last);
471 _M_profile_resize(__old_size, _Base::bucket_count());
475 insert(const value_type* __first, const value_type* __last)
477 size_type __old_size = _Base::bucket_count();
478 _Base::insert(__first, __last);
479 _M_profile_resize(__old_size, _Base::bucket_count());
482 void rehash(size_type __n)
484 size_type __old_size = _Base::bucket_count();
486 _M_profile_resize(__old_size, _Base::bucket_count());
491 _M_base() noexcept { return *this; }
494 _M_base() const noexcept { return *this; }
497 _M_profile_resize(size_type __old_size, size_type __new_size)
499 if (__old_size != __new_size)
500 __profcxx_hashtable_resize(this, __old_size, __new_size);
504 _M_profile_destruct()
506 size_type __hops = 0, __lc = 0, __chain = 0;
507 for (iterator __it = _M_base().begin(); __it != _M_base().end();
510 while (__it._M_cur_node->_M_next)
519 __lc = __lc > __chain ? __lc : __chain;
520 __hops += __chain * (__chain - 1) / 2;
524 __profcxx_hashtable_destruct2(this, __lc, _Base::size(), __hops);
529 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
531 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
532 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
535 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
537 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
538 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
539 { return __x._M_equal(__y); }
541 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
543 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
544 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
545 { return !(__x == __y); }
547 } // namespace __profile
551 #undef _GLIBCXX_STD_BASE
553 #endif // __GXX_EXPERIMENTAL_CXX0X__