1 // Profiling unordered_map/unordered_multimap 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_map
25 * This file is a GNU profile extension to the Standard C++ Library.
28 #ifndef _GLIBCXX_PROFILE_UNORDERED_MAP
29 #define _GLIBCXX_PROFILE_UNORDERED_MAP 1
31 #if __cplusplus < 201103L
32 # include <bits/c++0x_warning.h>
34 # include <unordered_map>
36 #include <profile/base.h>
37 #include <profile/unordered_base.h>
39 #define _GLIBCXX_BASE unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
40 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
42 namespace std _GLIBCXX_VISIBILITY(default)
46 /// Class std::unordered_map wrapper with performance instrumentation.
47 template<typename _Key, typename _Tp,
48 typename _Hash = std::hash<_Key>,
49 typename _Pred = std::equal_to<_Key>,
50 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
52 : public _GLIBCXX_STD_BASE,
53 public _Unordered_profile<unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>,
56 typedef typename _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;
74 typedef typename _Base::mapped_type mapped_type;
76 typedef typename _Base::iterator iterator;
77 typedef typename _Base::const_iterator const_iterator;
80 unordered_map(size_type __n = 10,
81 const hasher& __hf = hasher(),
82 const key_equal& __eql = key_equal(),
83 const allocator_type& __a = allocator_type())
84 : _Base(__n, __hf, __eql, __a)
87 template<typename _InputIterator>
88 unordered_map(_InputIterator __f, _InputIterator __l,
90 const hasher& __hf = hasher(),
91 const key_equal& __eql = key_equal(),
92 const allocator_type& __a = allocator_type())
93 : _Base(__f, __l, __n, __hf, __eql, __a)
96 unordered_map(const unordered_map&) = default;
98 unordered_map(const _Base& __x)
102 unordered_map(unordered_map&&) = default;
104 unordered_map(initializer_list<value_type> __l,
106 const hasher& __hf = hasher(),
107 const key_equal& __eql = key_equal(),
108 const allocator_type& __a = allocator_type())
109 : _Base(__l, __n, __hf, __eql, __a)
113 operator=(const unordered_map&) = default;
116 operator=(unordered_map&&) = default;
119 operator=(initializer_list<value_type> __l)
128 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
130 this->_M_profile_destruct();
134 template<typename... _Args>
135 std::pair<iterator, bool>
136 emplace(_Args&&... __args)
138 size_type __old_size = _Base::bucket_count();
139 std::pair<iterator, bool> __res
140 = _Base::emplace(std::forward<_Args>(__args)...);
141 _M_profile_resize(__old_size);
145 template<typename... _Args>
147 emplace_hint(const_iterator __it, _Args&&... __args)
149 size_type __old_size = _Base::bucket_count();
151 = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
152 _M_profile_resize(__old_size);
157 insert(std::initializer_list<value_type> __l)
159 size_type __old_size = _Base::bucket_count();
161 _M_profile_resize(__old_size);
164 std::pair<iterator, bool>
165 insert(const value_type& __obj)
167 size_type __old_size = _Base::bucket_count();
168 std::pair<iterator, bool> __res = _Base::insert(__obj);
169 _M_profile_resize(__old_size);
174 insert(const_iterator __iter, const value_type& __v)
176 size_type __old_size = _Base::bucket_count();
177 iterator __res = _Base::insert(__iter, __v);
178 _M_profile_resize(__old_size);
182 template<typename _Pair, typename = typename
183 std::enable_if<std::is_constructible<value_type,
184 _Pair&&>::value>::type>
185 std::pair<iterator, bool>
186 insert(_Pair&& __obj)
188 size_type __old_size = _Base::bucket_count();
189 std::pair<iterator, bool> __res
190 = _Base::insert(std::forward<_Pair>(__obj));
191 _M_profile_resize(__old_size);
195 template<typename _Pair, typename = typename
196 std::enable_if<std::is_constructible<value_type,
197 _Pair&&>::value>::type>
199 insert(const_iterator __iter, _Pair&& __v)
201 size_type __old_size = _Base::bucket_count();
202 iterator __res = _Base::insert(__iter, std::forward<_Pair>(__v));
203 _M_profile_resize(__old_size);
207 template<typename _InputIter>
209 insert(_InputIter __first, _InputIter __last)
211 size_type __old_size = _Base::bucket_count();
212 _Base::insert(__first, __last);
213 _M_profile_resize(__old_size);
218 operator[](const _Key& __k)
220 size_type __old_size = _Base::bucket_count();
221 mapped_type& __res = _M_base()[__k];
222 _M_profile_resize(__old_size);
227 operator[](_Key&& __k)
229 size_type __old_size = _Base::bucket_count();
230 mapped_type& __res = _M_base()[std::move(__k)];
231 _M_profile_resize(__old_size);
236 swap(unordered_map& __x)
237 { _Base::swap(__x._M_base()); }
239 void rehash(size_type __n)
241 size_type __old_size = _Base::bucket_count();
243 _M_profile_resize(__old_size);
248 _M_profile_resize(size_type __old_size)
250 size_type __new_size = _Base::bucket_count();
251 if (__old_size != __new_size)
252 __profcxx_hashtable_resize(this, __old_size, __new_size);
256 template<typename _Key, typename _Tp, typename _Hash,
257 typename _Pred, typename _Alloc>
259 swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
260 unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
263 template<typename _Key, typename _Tp, typename _Hash,
264 typename _Pred, typename _Alloc>
266 operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
267 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
268 { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
270 template<typename _Key, typename _Tp, typename _Hash,
271 typename _Pred, typename _Alloc>
273 operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
274 const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
275 { return !(__x == __y); }
278 #undef _GLIBCXX_STD_BASE
279 #define _GLIBCXX_BASE unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>
280 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
282 /// Class std::unordered_multimap wrapper with performance instrumentation.
283 template<typename _Key, typename _Tp,
284 typename _Hash = std::hash<_Key>,
285 typename _Pred = std::equal_to<_Key>,
286 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
287 class unordered_multimap
288 : public _GLIBCXX_STD_BASE,
289 public _Unordered_profile<unordered_multimap<_Key, _Tp,
290 _Hash, _Pred, _Alloc>,
293 typedef typename _GLIBCXX_STD_BASE _Base;
296 _M_base() noexcept { return *this; }
299 _M_base() const noexcept { return *this; }
302 typedef typename _Base::size_type size_type;
303 typedef typename _Base::hasher hasher;
304 typedef typename _Base::key_equal key_equal;
305 typedef typename _Base::allocator_type allocator_type;
306 typedef typename _Base::key_type key_type;
307 typedef typename _Base::value_type value_type;
308 typedef typename _Base::difference_type difference_type;
309 typedef typename _Base::reference reference;
310 typedef typename _Base::const_reference const_reference;
312 typedef typename _Base::iterator iterator;
313 typedef typename _Base::const_iterator const_iterator;
316 unordered_multimap(size_type __n = 10,
317 const hasher& __hf = hasher(),
318 const key_equal& __eql = key_equal(),
319 const allocator_type& __a = allocator_type())
320 : _Base(__n, __hf, __eql, __a)
323 template<typename _InputIterator>
324 unordered_multimap(_InputIterator __f, _InputIterator __l,
326 const hasher& __hf = hasher(),
327 const key_equal& __eql = key_equal(),
328 const allocator_type& __a = allocator_type())
329 : _Base(__f, __l, __n, __hf, __eql, __a)
332 unordered_multimap(const unordered_multimap&) = default;
334 unordered_multimap(const _Base& __x)
338 unordered_multimap(unordered_multimap&&) = default;
340 unordered_multimap(initializer_list<value_type> __l,
342 const hasher& __hf = hasher(),
343 const key_equal& __eql = key_equal(),
344 const allocator_type& __a = allocator_type())
345 : _Base(__l, __n, __hf, __eql, __a)
349 operator=(const unordered_multimap&) = default;
352 operator=(unordered_multimap&&) = default;
355 operator=(initializer_list<value_type> __l)
364 __profcxx_hashtable_destruct(this, _Base::bucket_count(),
366 this->_M_profile_destruct();
370 template<typename... _Args>
372 emplace(_Args&&... __args)
374 size_type __old_size = _Base::bucket_count();
376 = _Base::emplace(std::forward<_Args>(__args)...);
377 _M_profile_resize(__old_size);
381 template<typename... _Args>
383 emplace_hint(const_iterator __it, _Args&&... __args)
385 size_type __old_size = _Base::bucket_count();
387 = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
388 _M_profile_resize(__old_size);
393 insert(std::initializer_list<value_type> __l)
395 size_type __old_size = _Base::bucket_count();
397 _M_profile_resize(__old_size);
401 insert(const value_type& __obj)
403 size_type __old_size = _Base::bucket_count();
404 iterator __res = _Base::insert(__obj);
405 _M_profile_resize(__old_size);
410 insert(const_iterator __iter, const value_type& __v)
412 size_type __old_size = _Base::bucket_count();
413 iterator __res = _Base::insert(__iter, __v);
414 _M_profile_resize(__old_size);
418 template<typename _Pair, typename = typename
419 std::enable_if<std::is_constructible<value_type,
420 _Pair&&>::value>::type>
422 insert(_Pair&& __obj)
424 size_type __old_size = _Base::bucket_count();
425 iterator __res = _Base::insert(std::forward<_Pair>(__obj));
426 _M_profile_resize(__old_size);
430 template<typename _Pair, typename = typename
431 std::enable_if<std::is_constructible<value_type,
432 _Pair&&>::value>::type>
434 insert(const_iterator __iter, _Pair&& __v)
436 size_type __old_size = _Base::bucket_count();
437 iterator __res = _Base::insert(__iter, std::forward<_Pair>(__v));
438 _M_profile_resize(__old_size);
442 template<typename _InputIter>
444 insert(_InputIter __first, _InputIter __last)
446 size_type __old_size = _Base::bucket_count();
447 _Base::insert(__first, __last);
448 _M_profile_resize(__old_size);
452 swap(unordered_multimap& __x)
453 { _Base::swap(__x._M_base()); }
456 rehash(size_type __n)
458 size_type __old_size = _Base::bucket_count();
460 _M_profile_resize(__old_size);
465 _M_profile_resize(size_type __old_size)
467 size_type __new_size = _Base::bucket_count();
468 if (__old_size != __new_size)
469 __profcxx_hashtable_resize(this, __old_size, __new_size);
473 template<typename _Key, typename _Tp, typename _Hash,
474 typename _Pred, typename _Alloc>
476 swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
477 unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
480 template<typename _Key, typename _Tp, typename _Hash,
481 typename _Pred, typename _Alloc>
483 operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
484 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
485 { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
487 template<typename _Key, typename _Tp, typename _Hash,
488 typename _Pred, typename _Alloc>
490 operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
491 const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
492 { return !(__x == __y); }
494 } // namespace __profile
498 #undef _GLIBCXX_STD_BASE