2014-01-17 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / profile / map.h
blob63fb0cbb227319db6cee9ea749becf2ffe080073
1 // Profiling map 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/map.h
25 * This file is a GNU profile extension to the Standard C++ Library.
28 #ifndef _GLIBCXX_PROFILE_MAP_H
29 #define _GLIBCXX_PROFILE_MAP_H 1
31 #include <utility>
32 #include <profile/base.h>
34 namespace std _GLIBCXX_VISIBILITY(default)
36 namespace __profile
38 /// Class std::map wrapper with performance instrumentation.
39 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
40 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
41 class map
42 : public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
44 typedef _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator> _Base;
46 #if __cplusplus >= 201103L
47 typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits;
48 #endif
50 public:
51 // types:
52 typedef _Key key_type;
53 typedef _Tp mapped_type;
54 typedef std::pair<const _Key, _Tp> value_type;
55 typedef _Compare key_compare;
56 typedef _Allocator allocator_type;
57 typedef typename _Base::reference reference;
58 typedef typename _Base::const_reference const_reference;
60 typedef typename _Base::iterator iterator;
61 typedef typename _Base::const_iterator const_iterator;
62 typedef typename _Base::size_type size_type;
63 typedef typename _Base::difference_type difference_type;
64 typedef typename _Base::pointer pointer;
65 typedef typename _Base::const_pointer const_pointer;
66 typedef std::reverse_iterator<iterator> reverse_iterator;
67 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69 // 23.3.1.1 construct/copy/destroy:
70 explicit
71 map(const _Compare& __comp = _Compare(),
72 const _Allocator& __a = _Allocator())
73 : _Base(__comp, __a)
74 { __profcxx_map_to_unordered_map_construct(this); }
76 #if __cplusplus >= 201103L
77 template<typename _InputIterator,
78 typename = std::_RequireInputIter<_InputIterator>>
79 #else
80 template<typename _InputIterator>
81 #endif
82 map(_InputIterator __first, _InputIterator __last,
83 const _Compare& __comp = _Compare(),
84 const _Allocator& __a = _Allocator())
85 : _Base(__first, __last, __comp, __a)
86 { __profcxx_map_to_unordered_map_construct(this); }
88 map(const map& __x)
89 : _Base(__x)
90 { __profcxx_map_to_unordered_map_construct(this); }
92 map(const _Base& __x)
93 : _Base(__x)
94 { __profcxx_map_to_unordered_map_construct(this); }
96 #if __cplusplus >= 201103L
97 map(map&& __x)
98 noexcept(is_nothrow_copy_constructible<_Compare>::value)
99 : _Base(std::move(__x))
100 { __profcxx_map_to_unordered_map_construct(this); }
102 map(initializer_list<value_type> __l,
103 const _Compare& __c = _Compare(),
104 const allocator_type& __a = allocator_type())
105 : _Base(__l, __c, __a)
106 { __profcxx_map_to_unordered_map_construct(this); }
108 explicit
109 map(const allocator_type& __a)
110 : _Base(__a)
111 { __profcxx_map_to_unordered_map_construct(this); }
113 map(const map& __x, const allocator_type& __a)
114 : _Base(__x, __a)
115 { __profcxx_map_to_unordered_map_construct(this); }
117 map(map&& __x, const allocator_type& __a)
118 noexcept(is_nothrow_copy_constructible<_Compare>::value
119 && _Alloc_traits::_S_always_equal())
120 : _Base(std::move(__x), __a)
121 { __profcxx_map_to_unordered_map_construct(this); }
123 map(initializer_list<value_type> __l, const allocator_type& __a)
124 : _Base(__l, __a)
125 { __profcxx_map_to_unordered_map_construct(this); }
127 template<typename _InputIterator>
128 map(_InputIterator __first, _InputIterator __last,
129 const allocator_type& __a)
130 : _Base(__first, __last, __a)
131 { __profcxx_map_to_unordered_map_construct(this); }
132 #endif
134 ~map() _GLIBCXX_NOEXCEPT
135 { __profcxx_map_to_unordered_map_destruct(this); }
137 #if __cplusplus < 201103L
138 map&
139 operator=(const map& __x)
141 _M_base() = __x;
142 return *this;
144 #else
145 map&
146 operator=(const map&) = default;
148 map&
149 operator=(map&&) = default;
151 map&
152 operator=(initializer_list<value_type> __l)
154 _M_base() = __l;
155 return *this;
157 #endif
159 // _GLIBCXX_RESOLVE_LIB_DEFECTS
160 // 133. map missing get_allocator()
161 using _Base::get_allocator;
163 // iterators:
164 iterator
165 begin() _GLIBCXX_NOEXCEPT
166 { return _Base::begin(); }
168 const_iterator
169 begin() const _GLIBCXX_NOEXCEPT
170 { return _Base::begin(); }
172 iterator
173 end() _GLIBCXX_NOEXCEPT
174 { return _Base::end(); }
176 const_iterator
177 end() const _GLIBCXX_NOEXCEPT
178 { return _Base::end(); }
180 reverse_iterator
181 rbegin() _GLIBCXX_NOEXCEPT
183 __profcxx_map_to_unordered_map_invalidate(this);
184 return reverse_iterator(end());
187 const_reverse_iterator
188 rbegin() const _GLIBCXX_NOEXCEPT
190 __profcxx_map_to_unordered_map_invalidate(this);
191 return const_reverse_iterator(end());
194 reverse_iterator
195 rend() _GLIBCXX_NOEXCEPT
197 __profcxx_map_to_unordered_map_invalidate(this);
198 return reverse_iterator(begin());
201 const_reverse_iterator
202 rend() const _GLIBCXX_NOEXCEPT
204 __profcxx_map_to_unordered_map_invalidate(this);
205 return const_reverse_iterator(begin());
208 #if __cplusplus >= 201103L
209 const_iterator
210 cbegin() const noexcept
211 { return const_iterator(_Base::begin()); }
213 const_iterator
214 cend() const noexcept
215 { return const_iterator(_Base::end()); }
217 const_reverse_iterator
218 crbegin() const noexcept
220 __profcxx_map_to_unordered_map_invalidate(this);
221 return const_reverse_iterator(end());
224 const_reverse_iterator
225 crend() const noexcept
227 __profcxx_map_to_unordered_map_invalidate(this);
228 return const_reverse_iterator(begin());
230 #endif
232 // capacity:
233 using _Base::empty;
234 using _Base::size;
235 using _Base::max_size;
237 // 23.3.1.2 element access:
238 mapped_type&
239 operator[](const key_type& __k)
241 __profcxx_map_to_unordered_map_find(this, size());
242 return _Base::operator[](__k);
245 #if __cplusplus >= 201103L
246 mapped_type&
247 operator[](key_type&& __k)
249 __profcxx_map_to_unordered_map_find(this, size());
250 return _Base::operator[](std::move(__k));
252 #endif
254 mapped_type&
255 at(const key_type& __k)
257 __profcxx_map_to_unordered_map_find(this, size());
258 return _Base::at(__k);
261 const mapped_type&
262 at(const key_type& __k) const
264 __profcxx_map_to_unordered_map_find(this, size());
265 return _Base::at(__k);
268 // modifiers:
269 #if __cplusplus >= 201103L
270 template<typename... _Args>
271 std::pair<iterator, bool>
272 emplace(_Args&&... __args)
274 __profcxx_map_to_unordered_map_insert(this, size(), 1);
275 auto __res = _Base::emplace(std::forward<_Args>(__args)...);
276 return std::pair<iterator, bool>(iterator(__res.first),
277 __res.second);
280 template<typename... _Args>
281 iterator
282 emplace_hint(const_iterator __pos, _Args&&... __args)
284 size_type size_before = size();
285 auto __res = _Base::emplace_hint(__pos,
286 std::forward<_Args>(__args)...);
287 __profcxx_map_to_unordered_map_insert(this, size_before,
288 size() - size_before);
289 return __res;
291 #endif
293 std::pair<iterator, bool>
294 insert(const value_type& __x)
296 __profcxx_map_to_unordered_map_insert(this, size(), 1);
297 typedef typename _Base::iterator _Base_iterator;
298 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
299 return std::pair<iterator, bool>(iterator(__res.first),
300 __res.second);
303 #if __cplusplus >= 201103L
304 template<typename _Pair, typename = typename
305 std::enable_if<std::is_constructible<value_type,
306 _Pair&&>::value>::type>
307 std::pair<iterator, bool>
308 insert(_Pair&& __x)
310 __profcxx_map_to_unordered_map_insert(this, size(), 1);
311 typedef typename _Base::iterator _Base_iterator;
312 std::pair<_Base_iterator, bool> __res
313 = _Base::insert(std::forward<_Pair>(__x));
314 return std::pair<iterator, bool>(iterator(__res.first),
315 __res.second);
317 #endif
319 #if __cplusplus >= 201103L
320 void
321 insert(std::initializer_list<value_type> __list)
323 size_type size_before = size();
324 _Base::insert(__list);
325 __profcxx_map_to_unordered_map_insert(this, size_before,
326 size() - size_before);
328 #endif
330 iterator
331 #if __cplusplus >= 201103L
332 insert(const_iterator __position, const value_type& __x)
333 #else
334 insert(iterator __position, const value_type& __x)
335 #endif
337 size_type size_before = size();
338 iterator __i = iterator(_Base::insert(__position, __x));
339 __profcxx_map_to_unordered_map_insert(this, size_before,
340 size() - size_before);
341 return __i;
344 #if __cplusplus >= 201103L
345 template<typename _Pair, typename = typename
346 std::enable_if<std::is_constructible<value_type,
347 _Pair&&>::value>::type>
348 iterator
349 insert(const_iterator __position, _Pair&& __x)
351 size_type size_before = size();
352 iterator __i
353 = iterator(_Base::insert(__position, std::forward<_Pair>(__x)));
354 __profcxx_map_to_unordered_map_insert(this, size_before,
355 size() - size_before);
356 return __i;
358 #endif
360 #if __cplusplus >= 201103L
361 template<typename _InputIterator,
362 typename = std::_RequireInputIter<_InputIterator>>
363 #else
364 template<typename _InputIterator>
365 #endif
366 void
367 insert(_InputIterator __first, _InputIterator __last)
369 size_type size_before = size();
370 _Base::insert(__first, __last);
371 __profcxx_map_to_unordered_map_insert(this, size_before,
372 size() - size_before);
375 #if __cplusplus >= 201103L
376 iterator
377 erase(const_iterator __position)
379 iterator __i = _Base::erase(__position);
380 __profcxx_map_to_unordered_map_erase(this, size(), 1);
381 return __i;
384 iterator
385 erase(iterator __position)
386 { return erase(const_iterator(__position)); }
387 #else
388 void
389 erase(iterator __position)
391 _Base::erase(__position);
392 __profcxx_map_to_unordered_map_erase(this, size(), 1);
394 #endif
396 size_type
397 erase(const key_type& __x)
399 iterator __victim = find(__x);
400 if (__victim == end())
401 return 0;
402 else
404 _Base::erase(__victim);
405 return 1;
409 #if __cplusplus >= 201103L
410 iterator
411 erase(const_iterator __first, const_iterator __last)
412 { return iterator(_Base::erase(__first, __last)); }
413 #else
414 void
415 erase(iterator __first, iterator __last)
416 { _Base::erase(__first, __last); }
417 #endif
419 void
420 swap(map& __x)
421 #if __cplusplus >= 201103L
422 noexcept(_Alloc_traits::_S_nothrow_swap())
423 #endif
424 { _Base::swap(__x); }
426 void
427 clear() _GLIBCXX_NOEXCEPT
428 { this->erase(begin(), end()); }
430 // observers:
431 using _Base::key_comp;
432 using _Base::value_comp;
434 // 23.3.1.3 map operations:
435 iterator
436 find(const key_type& __x)
438 __profcxx_map_to_unordered_map_find(this, size());
439 return iterator(_Base::find(__x));
442 const_iterator
443 find(const key_type& __x) const
445 __profcxx_map_to_unordered_map_find(this, size());
446 return const_iterator(_Base::find(__x));
449 size_type
450 count(const key_type& __x) const
452 __profcxx_map_to_unordered_map_find(this, size());
453 return _Base::count(__x);
456 iterator
457 lower_bound(const key_type& __x)
459 __profcxx_map_to_unordered_map_invalidate(this);
460 return iterator(_Base::lower_bound(__x));
463 const_iterator
464 lower_bound(const key_type& __x) const
466 __profcxx_map_to_unordered_map_invalidate(this);
467 return const_iterator(_Base::lower_bound(__x));
470 iterator
471 upper_bound(const key_type& __x)
473 __profcxx_map_to_unordered_map_invalidate(this);
474 return iterator(_Base::upper_bound(__x));
477 const_iterator
478 upper_bound(const key_type& __x) const
480 __profcxx_map_to_unordered_map_invalidate(this);
481 return const_iterator(_Base::upper_bound(__x));
484 std::pair<iterator,iterator>
485 equal_range(const key_type& __x)
487 typedef typename _Base::iterator _Base_iterator;
488 std::pair<_Base_iterator, _Base_iterator> __res =
489 _Base::equal_range(__x);
490 return std::make_pair(iterator(__res.first),
491 iterator(__res.second));
494 std::pair<const_iterator,const_iterator>
495 equal_range(const key_type& __x) const
497 __profcxx_map_to_unordered_map_find(this, size());
498 typedef typename _Base::const_iterator _Base_const_iterator;
499 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
500 _Base::equal_range(__x);
501 return std::make_pair(const_iterator(__res.first),
502 const_iterator(__res.second));
505 _Base&
506 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
508 const _Base&
509 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
513 template<typename _Key, typename _Tp,
514 typename _Compare, typename _Allocator>
515 inline bool
516 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
517 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
519 __profcxx_map_to_unordered_map_invalidate(&__lhs);
520 __profcxx_map_to_unordered_map_invalidate(&__rhs);
521 return __lhs._M_base() == __rhs._M_base();
524 template<typename _Key, typename _Tp,
525 typename _Compare, typename _Allocator>
526 inline bool
527 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
528 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
530 __profcxx_map_to_unordered_map_invalidate(&__lhs);
531 __profcxx_map_to_unordered_map_invalidate(&__rhs);
532 return __lhs._M_base() != __rhs._M_base();
535 template<typename _Key, typename _Tp,
536 typename _Compare, typename _Allocator>
537 inline bool
538 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
539 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
541 __profcxx_map_to_unordered_map_invalidate(&__lhs);
542 __profcxx_map_to_unordered_map_invalidate(&__rhs);
543 return __lhs._M_base() < __rhs._M_base();
546 template<typename _Key, typename _Tp,
547 typename _Compare, typename _Allocator>
548 inline bool
549 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
550 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
552 __profcxx_map_to_unordered_map_invalidate(&__lhs);
553 __profcxx_map_to_unordered_map_invalidate(&__rhs);
554 return __lhs._M_base() <= __rhs._M_base();
557 template<typename _Key, typename _Tp,
558 typename _Compare, typename _Allocator>
559 inline bool
560 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
561 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
563 __profcxx_map_to_unordered_map_invalidate(&__lhs);
564 __profcxx_map_to_unordered_map_invalidate(&__rhs);
565 return __lhs._M_base() >= __rhs._M_base();
568 template<typename _Key, typename _Tp,
569 typename _Compare, typename _Allocator>
570 inline bool
571 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
572 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
574 __profcxx_map_to_unordered_map_invalidate(&__lhs);
575 __profcxx_map_to_unordered_map_invalidate(&__rhs);
576 return __lhs._M_base() > __rhs._M_base();
579 template<typename _Key, typename _Tp,
580 typename _Compare, typename _Allocator>
581 inline void
582 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
583 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
584 { __lhs.swap(__rhs); }
586 } // namespace __profile
587 } // namespace std
589 #endif