2014-11-10 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / profile / unordered_set
blobd6e3d478e0e7cb713260689c5ddaee2abb25cfc5
1 // Profiling unordered_set/unordered_multiset implementation -*- C++ -*-
3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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.
26  */
28 #ifndef _GLIBCXX_PROFILE_UNORDERED_SET
29 #define _GLIBCXX_PROFILE_UNORDERED_SET 1
31 #if __cplusplus < 201103L
32 # include <bits/c++0x_warning.h>
33 #else
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)
44 namespace __profile
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> >
51     class unordered_set
52     : public _GLIBCXX_STD_BASE,
53       public _Unordered_profile<unordered_set<_Key, _Hash, _Pred, _Alloc>,
54                                 true>
55     {
56       typedef _GLIBCXX_STD_BASE _Base;
58       _Base&
59       _M_base() noexcept       { return *this; }
61       const _Base&
62       _M_base() const noexcept { return *this; }
64     public:
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;
80       explicit
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)
86       { }
88       template<typename _InputIterator>
89         unordered_set(_InputIterator __f, _InputIterator __l,
90                       size_type __n = 0,
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)
95       { }
97       unordered_set(const unordered_set&) = default;
99       unordered_set(const _Base& __x)
100         : _Base(__x)
101       { }
103       unordered_set(unordered_set&&) = default;
105       explicit
106       unordered_set(const allocator_type& __a)
107         : _Base(__a)
108       { }
110       unordered_set(const unordered_set& __uset,
111                     const allocator_type& __a)
112         : _Base(__uset._M_base(), __a)
113       { }
115       unordered_set(unordered_set&& __uset,
116                     const allocator_type& __a)
117         : _Base(std::move(__uset._M_base()), __a)
118       { }
120       unordered_set(initializer_list<value_type> __l,
121                     size_type __n = 0,
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)
126       { }
128       unordered_set&
129       operator=(const unordered_set&) = default;
131       unordered_set&
132       operator=(unordered_set&&) = default;
134       unordered_set&
135       operator=(initializer_list<value_type> __l)
136       {
137         this->_M_profile_destruct();
138         _M_base() = __l;
139         this->_M_profile_construct();
140         return *this;
141       }
143       void
144       swap(unordered_set& __x)
145       noexcept( noexcept(__x._M_base().swap(__x)) )
146       {
147         _Base::swap(__x);
148         this->_M_swap(__x);
149       }
151       void
152       clear() noexcept
153       {
154         this->_M_profile_destruct();
155         _Base::clear();
156         this->_M_profile_construct();
157       }
159       template<typename... _Args>
160         std::pair<iterator, bool>
161         emplace(_Args&&... __args)
162         {
163           size_type __old_size = _Base::bucket_count();
164           std::pair<iterator, bool> __res
165             = _Base::emplace(std::forward<_Args>(__args)...);
166           this->_M_profile_resize(__old_size);
167           return __res;
168         }
170       template<typename... _Args>
171         iterator
172         emplace_hint(const_iterator __it, _Args&&... __args)
173         {
174           size_type __old_size = _Base::bucket_count();
175           iterator __res
176             = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
177           this->_M_profile_resize(__old_size);
178           return __res;
179         }
181       void
182       insert(std::initializer_list<value_type> __l)
183       {
184         size_type __old_size = _Base::bucket_count();
185         _Base::insert(__l);
186         this->_M_profile_resize(__old_size);
187       }
189       std::pair<iterator, bool>
190       insert(const value_type& __obj)
191       {
192         size_type __old_size = _Base::bucket_count();
193         std::pair<iterator, bool> __res = _Base::insert(__obj);
194         this->_M_profile_resize(__old_size);
195         return __res;
196       }
198       iterator
199       insert(const_iterator __iter, const value_type& __v)
200       {
201         size_type __old_size = _Base::bucket_count();
202         iterator __res = _Base::insert(__iter, __v);
203         this->_M_profile_resize(__old_size);
204         return __res;
205       }
207       std::pair<iterator, bool>
208       insert(value_type&& __obj)
209       {
210         size_type __old_size = _Base::bucket_count();
211         std::pair<iterator, bool> __res = _Base::insert(std::move(__obj));
212         this->_M_profile_resize(__old_size);
213         return __res;
214       }
216       iterator
217       insert(const_iterator __iter, value_type&& __v)
218       {
219         size_type __old_size = _Base::bucket_count();
220         iterator __res = _Base::insert(__iter, std::move(__v));
221         this->_M_profile_resize(__old_size);
222         return __res;
223       }
225       template<typename _InputIter>
226         void
227         insert(_InputIter __first, _InputIter __last)
228         {
229           size_type __old_size = _Base::bucket_count();
230           _Base::insert(__first, __last);
231           this->_M_profile_resize(__old_size);
232         }
234       void
235       rehash(size_type __n)
236       {
237         size_type __old_size = _Base::bucket_count();
238         _Base::rehash(__n);
239         this->_M_profile_resize(__old_size);
240       }
241   };
243   template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
244     inline void
245     swap(unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
246          unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
247     { __x.swap(__y); }
249   template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
250     inline bool
251     operator==(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
252                const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
253     { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
255   template<typename _Key, typename _Hash, typename _Pred, typename _Alloc>
256     inline bool
257     operator!=(const unordered_set<_Key, _Hash, _Pred, _Alloc>& __x,
258                const unordered_set<_Key, _Hash, _Pred, _Alloc>& __y)
259     { return !(__x == __y); }
261 #undef _GLIBCXX_BASE
262 #undef _GLIBCXX_STD_BASE
263 #define _GLIBCXX_STD_BASE _GLIBCXX_STD_C::_GLIBCXX_BASE
264 #define _GLIBCXX_BASE unordered_multiset<_Value, _Hash, _Pred, _Alloc>
266   /** @brief Unordered_multiset wrapper with performance instrumentation.  */
267   template<typename _Value,
268            typename _Hash = std::hash<_Value>,
269            typename _Pred = std::equal_to<_Value>,
270            typename _Alloc =  std::allocator<_Value> >
271     class unordered_multiset
272     : public _GLIBCXX_STD_BASE,
273       public _Unordered_profile<unordered_multiset<_Value,
274                                                    _Hash, _Pred, _Alloc>,
275                                 false>
276     {
277       typedef _GLIBCXX_STD_BASE _Base;
279       _Base&
280       _M_base() noexcept       { return *this; }
282       const _Base&
283       _M_base() const noexcept { return *this; }
285     public:
286       typedef typename _Base::size_type       size_type;
287       typedef typename _Base::hasher      hasher;
288       typedef typename _Base::key_equal       key_equal;
289       typedef typename _Base::allocator_type  allocator_type;
290       typedef typename _Base::key_type  key_type;
291       typedef typename _Base::value_type      value_type;
292       typedef typename _Base::difference_type difference_type;
293       typedef typename _Base::reference       reference;
294       typedef typename _Base::const_reference const_reference;
296       typedef typename _Base::iterator iterator;
297       typedef typename _Base::const_iterator const_iterator;
299       unordered_multiset() = default;
301       explicit
302       unordered_multiset(size_type __n,
303                          const hasher& __hf = hasher(),
304                          const key_equal& __eql = key_equal(),
305                          const allocator_type& __a = allocator_type())
306         : _Base(__n, __hf, __eql, __a)
307       { }
309       template<typename _InputIterator>
310         unordered_multiset(_InputIterator __f, _InputIterator __l,
311                            size_type __n = 0,
312                            const hasher& __hf = hasher(),
313                            const key_equal& __eql = key_equal(),
314                            const allocator_type& __a = allocator_type())
315           : _Base(__f, __l, __n, __hf, __eql, __a)
316       { }
318       unordered_multiset(const unordered_multiset&) = default;
320       unordered_multiset(const _Base& __x)
321         : _Base(__x)
322       { }
324       unordered_multiset(unordered_multiset&&) = default;
326       explicit
327       unordered_multiset(const allocator_type& __a)
328         : _Base(__a)
329       { }
331       unordered_multiset(const unordered_multiset& __umset,
332                          const allocator_type& __a)
333         : _Base(__umset._M_base(), __a)
334       { }
336       unordered_multiset(unordered_multiset&& __umset,
337                          const allocator_type& __a)
338         : _Base(std::move(__umset._M_base()), __a)
339       { }
341       unordered_multiset(initializer_list<value_type> __l,
342                          size_type __n = 0,
343                          const hasher& __hf = hasher(),
344                          const key_equal& __eql = key_equal(),
345                          const allocator_type& __a = allocator_type())
346         : _Base(__l, __n, __hf, __eql, __a)
347       { }
349       unordered_multiset&
350       operator=(const unordered_multiset&) = default;
352       unordered_multiset&
353       operator=(unordered_multiset&&) = default;
355       unordered_multiset&
356       operator=(initializer_list<value_type> __l)
357       {
358         this->_M_profile_destruct();
359         _M_base() = __l;
360         this->_M_profile_construct();
361         return *this;
362       }
364       void
365       swap(unordered_multiset& __x)
366       noexcept( noexcept(__x._M_base().swap(__x)) )
367       {
368         _Base::swap(__x);
369         this->_M_swap(__x);
370       }
372       void
373       clear() noexcept
374       {
375         this->_M_profile_destruct();
376         _Base::clear();
377         this->_M_profile_construct();
378       }
380       template<typename... _Args>
381         iterator
382         emplace(_Args&&... __args)
383         {
384           size_type __old_size = _Base::bucket_count();
385           iterator __res = _Base::emplace(std::forward<_Args>(__args)...);
386           this->_M_profile_resize(__old_size);
387           return __res;
388         }
390       template<typename... _Args>
391         iterator
392         emplace_hint(const_iterator __it, _Args&&... __args)
393         {
394           size_type __old_size = _Base::bucket_count();
395           iterator __res
396             = _Base::emplace_hint(__it, std::forward<_Args>(__args)...);
397           this->_M_profile_resize(__old_size);
398           return __res;
399         }
401       void
402       insert(std::initializer_list<value_type> __l)
403       {
404         size_type __old_size = _Base::bucket_count();
405         _Base::insert(__l);
406         this->_M_profile_resize(__old_size);
407       }
409       iterator
410       insert(const value_type& __obj)
411       {
412         size_type __old_size = _Base::bucket_count();
413         iterator __res = _Base::insert(__obj);
414         this->_M_profile_resize(__old_size);
415         return __res;
416       }
418       iterator
419       insert(const_iterator __iter, const value_type& __v)
420       {
421         size_type __old_size = _Base::bucket_count();
422         iterator __res = _Base::insert(__iter, __v);
423         this->_M_profile_resize(__old_size);
424         return __res;
425       }
427       iterator
428       insert(value_type&& __obj)
429       {
430         size_type __old_size = _Base::bucket_count();
431         iterator __res = _Base::insert(std::move(__obj));
432         this->_M_profile_resize(__old_size);
433         return __res;
434       }
436       iterator
437       insert(const_iterator __iter, value_type&& __v)
438       {
439         size_type __old_size = _Base::bucket_count();
440         iterator __res = _Base::insert(__iter, std::move(__v));
441         this->_M_profile_resize(__old_size);
442         return __res;
443       }
445       template<typename _InputIter>
446         void
447         insert(_InputIter __first, _InputIter __last)
448         {
449           size_type __old_size = _Base::bucket_count();
450           _Base::insert(__first, __last);
451           this->_M_profile_resize(__old_size);
452         }
454       void
455       rehash(size_type __n)
456       {
457         size_type __old_size = _Base::bucket_count();
458         _Base::rehash(__n);
459         this->_M_profile_resize(__old_size);
460       }
461    };
463   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
464     inline void
465     swap(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
466          unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
467     { __x.swap(__y); }
469   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
470     inline bool
471     operator==(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
472                const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
473     { return static_cast<const _GLIBCXX_STD_BASE&>(__x) == __y; }
475   template<typename _Value, typename _Hash, typename _Pred, typename _Alloc>
476     inline bool
477     operator!=(const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
478                const unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
479     { return !(__x == __y); }
481 } // namespace __profile
482 } // namespace std
484 #undef _GLIBCXX_BASE
485 #undef _GLIBCXX_STD_BASE
487 #endif // C++11
489 #endif