1 // Profiling unordered_set/unordered_multiset implementation -*- C++ -*-
3 // Copyright (C) 2009-2013 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;
79 unordered_set(size_type __n = 10,
80 const hasher& __hf = hasher(),
81 const key_equal& __eql = key_equal(),
82 const allocator_type& __a = allocator_type())
83 : _Base(__n, __hf, __eql, __a)
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)
95 unordered_set(const unordered_set&) = default;
97 unordered_set(const _Base& __x)
101 unordered_set(unordered_set&&) = default;
103 unordered_set(initializer_list<value_type> __l,
105 const hasher& __hf = hasher(),
106 const key_equal& __eql = key_equal(),
107 const allocator_type& __a = allocator_type())
108 : _Base(__l, __n, __hf, __eql, __a)
112 operator=(const unordered_set&) = default;
115 operator=(unordered_set&&) = default;
118 operator=(initializer_list<value_type> __l)
125 swap(unordered_set& __x)
126 { _Base::swap(__x); }
131 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
133 this->_M_profile_destruct();
137 template<typename... _Args>
138 std::pair<iterator, bool>
139 emplace(_Args&&... __args)
141 size_type __old_size = _Base::bucket_count();
142 std::pair<iterator, bool> __res
143 = _Base::emplace(std::forward<_Args>(__args)...);
144 _M_profile_resize(__old_size);
148 template<typename... _Args>
150 emplace_hint(const_iterator __it, _Args&&... __args)
152 size_type __old_size = _Base::bucket_count();
154 = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
155 _M_profile_resize(__old_size);
160 insert(std::initializer_list<value_type> __l)
162 size_type __old_size = _Base::bucket_count();
164 _M_profile_resize(__old_size);
167 std::pair<iterator, bool>
168 insert(const value_type& __obj)
170 size_type __old_size = _Base::bucket_count();
171 std::pair<iterator, bool> __res = _Base::insert(__obj);
172 _M_profile_resize(__old_size);
177 insert(const_iterator __iter, const value_type& __v)
179 size_type __old_size = _Base::bucket_count();
180 iterator __res = _Base::insert(__iter, __v);
181 _M_profile_resize(__old_size);
185 std::pair<iterator, bool>
186 insert(value_type&& __obj)
188 size_type __old_size = _Base::bucket_count();
189 std::pair<iterator, bool> __res = _Base::insert(std::move(__obj));
190 _M_profile_resize(__old_size);
195 insert(const_iterator __iter, value_type&& __v)
197 size_type __old_size = _Base::bucket_count();
198 iterator __res = _Base::insert(__iter, std::move(__v));
199 _M_profile_resize(__old_size);
203 template<typename _InputIter>
205 insert(_InputIter __first, _InputIter __last)
207 size_type __old_size = _Base::bucket_count();
208 _Base::insert(__first, __last);
209 _M_profile_resize(__old_size);
213 rehash(size_type __n)
215 size_type __old_size = _Base::bucket_count();
217 _M_profile_resize(__old_size);
222 _M_profile_resize(size_type __old_size)
224 size_type __new_size = _Base::bucket_count();
225 if (__old_size != __new_size)
226 __profcxx_hashtable_resize(this, __old_size, __new_size);
230 template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
232 swap(unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
233 unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
236 template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
238 operator==(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
239 const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
240 { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
242 template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
244 operator!=(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
245 const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
246 { return !(__x == __y); }
249 #undef _GLIBCXX_STD_BASE
250 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
251 #define _GLIBCXX_BASE unordered_multiset<_Value, _Hash, _Pred, _Alloc>
253 /** @brief Unordered_multiset wrapper with performance instrumentation. */
254 template<typename _Value,
255 typename _Hash = std::hash<_Value>,
256 typename _Pred = std::equal_to<_Value>,
257 typename _Alloc = std::allocator<_Value> >
258 class unordered_multiset
259 : public _GLIBCXX_STD_BASE,
260 public _Unordered_profile<unordered_multiset<_Value,
261 _Hash, _Pred, _Alloc>,
264 typedef _GLIBCXX_STD_BASE _Base;
267 _M_base() noexcept { return *this; }
270 _M_base() const noexcept { return *this; }
273 typedef typename _Base::size_type size_type;
274 typedef typename _Base::hasher hasher;
275 typedef typename _Base::key_equal key_equal;
276 typedef typename _Base::allocator_type allocator_type;
277 typedef typename _Base::key_type key_type;
278 typedef typename _Base::value_type value_type;
279 typedef typename _Base::difference_type difference_type;
280 typedef typename _Base::reference reference;
281 typedef typename _Base::const_reference const_reference;
283 typedef typename _Base::iterator iterator;
284 typedef typename _Base::const_iterator const_iterator;
287 unordered_multiset(size_type __n = 10,
288 const hasher& __hf = hasher(),
289 const key_equal& __eql = key_equal(),
290 const allocator_type& __a = allocator_type())
291 : _Base(__n, __hf, __eql, __a)
294 template<typename _InputIterator>
295 unordered_multiset(_InputIterator __f, _InputIterator __l,
297 const hasher& __hf = hasher(),
298 const key_equal& __eql = key_equal(),
299 const allocator_type& __a = allocator_type())
300 : _Base(__f, __l, __n, __hf, __eql, __a)
303 unordered_multiset(const unordered_multiset&) = default;
305 unordered_multiset(const _Base& __x)
309 unordered_multiset(unordered_multiset&&) = default;
311 unordered_multiset(initializer_list<value_type> __l,
313 const hasher& __hf = hasher(),
314 const key_equal& __eql = key_equal(),
315 const allocator_type& __a = allocator_type())
316 : _Base(__l, __n, __hf, __eql, __a)
320 operator=(const unordered_multiset&) = default;
323 operator=(unordered_multiset&&) = default;
326 operator=(initializer_list<value_type> __l)
333 swap(unordered_multiset& __x)
334 { _Base::swap(__x); }
339 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
341 this->_M_profile_destruct();
345 template<typename... _Args>
347 emplace(_Args&&... __args)
349 size_type __old_size = _Base::bucket_count();
350 iterator __res = _Base::emplace(std::forward<_Args>(__args)...);
351 _M_profile_resize(__old_size);
355 template<typename... _Args>
357 emplace_hint(const_iterator __it, _Args&&... __args)
359 size_type __old_size = _Base::bucket_count();
361 = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
362 _M_profile_resize(__old_size);
367 insert(std::initializer_list<value_type> __l)
369 size_type __old_size = _Base::bucket_count();
371 _M_profile_resize(__old_size);
375 insert(const value_type& __obj)
377 size_type __old_size = _Base::bucket_count();
378 iterator __res = _Base::insert(__obj);
379 _M_profile_resize(__old_size);
384 insert(const_iterator __iter, const value_type& __v)
386 size_type __old_size = _Base::bucket_count();
387 iterator __res = _Base::insert(__iter, __v);
388 _M_profile_resize(__old_size);
393 insert(value_type&& __obj)
395 size_type __old_size = _Base::bucket_count();
396 iterator __res = _Base::insert(std::move(__obj));
397 _M_profile_resize(__old_size);
402 insert(const_iterator __iter, value_type&& __v)
404 size_type __old_size = _Base::bucket_count();
405 iterator __res = _Base::insert(__iter, std::move(__v));
406 _M_profile_resize(__old_size);
410 template<typename _InputIter>
412 insert(_InputIter __first, _InputIter __last)
414 size_type __old_size = _Base::bucket_count();
415 _Base::insert(__first, __last);
416 _M_profile_resize(__old_size);
420 rehash(size_type __n)
422 size_type __old_size = _Base::bucket_count();
424 _M_profile_resize(__old_size);
429 _M_profile_resize(size_type __old_size)
431 size_type __new_size = _Base::bucket_count();
432 if (__old_size != __new_size)
433 __profcxx_hashtable_resize(this, __old_size, __new_size);
437 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
439 swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
440 unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
443 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
445 operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
446 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
447 { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
449 template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
451 operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
452 const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
453 { return !(__x == __y); }
455 } // namespace __profile
459 #undef _GLIBCXX_STD_BASE