2012-10-21 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / profile / map.h
blobdcc693138674c870059fda52bda38c7d892e8da0
1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011, 2012 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 public:
47 // types:
48 typedef _Key key_type;
49 typedef _Tp mapped_type;
50 typedef std::pair<const _Key, _Tp> value_type;
51 typedef _Compare key_compare;
52 typedef _Allocator allocator_type;
53 typedef typename _Base::reference reference;
54 typedef typename _Base::const_reference const_reference;
56 typedef typename _Base::iterator iterator;
57 typedef typename _Base::const_iterator const_iterator;
58 typedef typename _Base::size_type size_type;
59 typedef typename _Base::difference_type difference_type;
60 typedef typename _Base::pointer pointer;
61 typedef typename _Base::const_pointer const_pointer;
62 typedef std::reverse_iterator<iterator> reverse_iterator;
63 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
65 // 23.3.1.1 construct/copy/destroy:
66 explicit
67 map(const _Compare& __comp = _Compare(),
68 const _Allocator& __a = _Allocator())
69 : _Base(__comp, __a)
70 { __profcxx_map_to_unordered_map_construct(this); }
72 template<typename _InputIterator>
73 map(_InputIterator __first, _InputIterator __last,
74 const _Compare& __comp = _Compare(),
75 const _Allocator& __a = _Allocator())
76 : _Base(__first, __last, __comp, __a)
77 { __profcxx_map_to_unordered_map_construct(this); }
79 map(const map& __x)
80 : _Base(__x)
81 { __profcxx_map_to_unordered_map_construct(this); }
83 map(const _Base& __x)
84 : _Base(__x)
85 { __profcxx_map_to_unordered_map_construct(this); }
87 #ifdef __GXX_EXPERIMENTAL_CXX0X__
88 map(map&& __x)
89 noexcept(is_nothrow_copy_constructible<_Compare>::value)
90 : _Base(std::move(__x))
91 { }
93 map(initializer_list<value_type> __l,
94 const _Compare& __c = _Compare(),
95 const allocator_type& __a = allocator_type())
96 : _Base(__l, __c, __a) { }
97 #endif
99 ~map() _GLIBCXX_NOEXCEPT
100 { __profcxx_map_to_unordered_map_destruct(this); }
102 map&
103 operator=(const map& __x)
105 *static_cast<_Base*>(this) = __x;
106 return *this;
109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
110 map&
111 operator=(map&& __x)
113 // NB: DR 1204.
114 // NB: DR 675.
115 this->clear();
116 this->swap(__x);
117 return *this;
120 map&
121 operator=(initializer_list<value_type> __l)
123 this->clear();
124 this->insert(__l);
125 return *this;
127 #endif
129 // _GLIBCXX_RESOLVE_LIB_DEFECTS
130 // 133. map missing get_allocator()
131 using _Base::get_allocator;
133 // iterators:
134 iterator
135 begin() _GLIBCXX_NOEXCEPT
136 { return _Base::begin(); }
138 const_iterator
139 begin() const _GLIBCXX_NOEXCEPT
140 { return _Base::begin(); }
142 iterator
143 end() _GLIBCXX_NOEXCEPT
144 { return _Base::end(); }
146 const_iterator
147 end() const _GLIBCXX_NOEXCEPT
148 { return _Base::end(); }
150 reverse_iterator
151 rbegin() _GLIBCXX_NOEXCEPT
153 __profcxx_map_to_unordered_map_invalidate(this);
154 return reverse_iterator(end());
157 const_reverse_iterator
158 rbegin() const _GLIBCXX_NOEXCEPT
160 __profcxx_map_to_unordered_map_invalidate(this);
161 return const_reverse_iterator(end());
164 reverse_iterator
165 rend() _GLIBCXX_NOEXCEPT
167 __profcxx_map_to_unordered_map_invalidate(this);
168 return reverse_iterator(begin());
171 const_reverse_iterator
172 rend() const _GLIBCXX_NOEXCEPT
174 __profcxx_map_to_unordered_map_invalidate(this);
175 return const_reverse_iterator(begin());
178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
179 const_iterator
180 cbegin() const noexcept
181 { return const_iterator(_Base::begin()); }
183 const_iterator
184 cend() const noexcept
185 { return const_iterator(_Base::end()); }
187 const_reverse_iterator
188 crbegin() const noexcept
190 __profcxx_map_to_unordered_map_invalidate(this);
191 return const_reverse_iterator(end());
194 const_reverse_iterator
195 crend() const noexcept
197 __profcxx_map_to_unordered_map_invalidate(this);
198 return const_reverse_iterator(begin());
200 #endif
202 // capacity:
203 using _Base::empty;
204 using _Base::size;
205 using _Base::max_size;
207 // 23.3.1.2 element access:
208 mapped_type&
209 operator[](const key_type& __k)
211 __profcxx_map_to_unordered_map_find(this, size());
212 return _Base::operator[](__k);
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 mapped_type&
217 operator[](key_type&& __k)
219 __profcxx_map_to_unordered_map_find(this, size());
220 return _Base::operator[](std::move(__k));
222 #endif
224 mapped_type&
225 at(const key_type& __k)
227 __profcxx_map_to_unordered_map_find(this, size());
228 return _Base::at(__k);
231 const mapped_type&
232 at(const key_type& __k) const
234 __profcxx_map_to_unordered_map_find(this, size());
235 return _Base::at(__k);
238 // modifiers:
239 #ifdef __GXX_EXPERIMENTAL_CXX0X__
240 template<typename... _Args>
241 std::pair<iterator, bool>
242 emplace(_Args&&... __args)
244 __profcxx_map_to_unordered_map_insert(this, size(), 1);
245 auto __res = _Base::emplace(std::forward<_Args>(__args)...);
246 return std::pair<iterator, bool>(iterator(__res.first),
247 __res.second);
250 template<typename... _Args>
251 iterator
252 emplace_hint(const_iterator __pos, _Args&&... __args)
254 size_type size_before = size();
255 auto __res = _Base::emplace_hint(__pos.base(),
256 std::forward<_Args>(__args)...);
257 __profcxx_map_to_unordered_map_insert(this, size_before,
258 size() - size_before);
260 #endif
262 std::pair<iterator, bool>
263 insert(const value_type& __x)
265 __profcxx_map_to_unordered_map_insert(this, size(), 1);
266 typedef typename _Base::iterator _Base_iterator;
267 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
268 return std::pair<iterator, bool>(iterator(__res.first),
269 __res.second);
272 #ifdef __GXX_EXPERIMENTAL_CXX0X__
273 template<typename _Pair, typename = typename
274 std::enable_if<std::is_constructible<value_type,
275 _Pair&&>::value>::type>
276 std::pair<iterator, bool>
277 insert(_Pair&& __x)
279 __profcxx_map_to_unordered_map_insert(this, size(), 1);
280 typedef typename _Base::iterator _Base_iterator;
281 std::pair<_Base_iterator, bool> __res
282 = _Base::insert(std::forward<_Pair>(__x));
283 return std::pair<iterator, bool>(iterator(__res.first),
284 __res.second);
286 #endif
288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
289 void
290 insert(std::initializer_list<value_type> __list)
292 size_type size_before = size();
293 _Base::insert(__list);
294 __profcxx_map_to_unordered_map_insert(this, size_before,
295 size() - size_before);
297 #endif
299 iterator
300 #ifdef __GXX_EXPERIMENTAL_CXX0X__
301 insert(const_iterator __position, const value_type& __x)
302 #else
303 insert(iterator __position, const value_type& __x)
304 #endif
306 size_type size_before = size();
307 iterator __i = iterator(_Base::insert(__position, __x));
308 __profcxx_map_to_unordered_map_insert(this, size_before,
309 size() - size_before);
310 return __i;
313 #ifdef __GXX_EXPERIMENTAL_CXX0X__
314 template<typename _Pair, typename = typename
315 std::enable_if<std::is_constructible<value_type,
316 _Pair&&>::value>::type>
317 iterator
318 insert(const_iterator __position, _Pair&& __x)
320 size_type size_before = size();
321 iterator __i
322 = iterator(_Base::insert(__position, std::forward<_Pair>(__x)));
323 __profcxx_map_to_unordered_map_insert(this, size_before,
324 size() - size_before);
325 return __i;
327 #endif
329 template<typename _InputIterator>
330 void
331 insert(_InputIterator __first, _InputIterator __last)
333 size_type size_before = size();
334 _Base::insert(__first, __last);
335 __profcxx_map_to_unordered_map_insert(this, size_before,
336 size() - size_before);
339 #ifdef __GXX_EXPERIMENTAL_CXX0X__
340 iterator
341 erase(const_iterator __position)
343 iterator __i = _Base::erase(__position);
344 __profcxx_map_to_unordered_map_erase(this, size(), 1);
345 return __i;
348 iterator
349 erase(iterator __position)
350 { return erase(const_iterator(__position)); }
351 #else
352 void
353 erase(iterator __position)
355 _Base::erase(__position);
356 __profcxx_map_to_unordered_map_erase(this, size(), 1);
358 #endif
360 size_type
361 erase(const key_type& __x)
363 iterator __victim = find(__x);
364 if (__victim == end())
365 return 0;
366 else
368 _Base::erase(__victim);
369 return 1;
373 #ifdef __GXX_EXPERIMENTAL_CXX0X__
374 iterator
375 erase(const_iterator __first, const_iterator __last)
376 { return iterator(_Base::erase(__first, __last)); }
377 #else
378 void
379 erase(iterator __first, iterator __last)
380 { _Base::erase(__first, __last); }
381 #endif
383 void
384 swap(map& __x)
385 { _Base::swap(__x); }
387 void
388 clear() _GLIBCXX_NOEXCEPT
389 { this->erase(begin(), end()); }
391 // observers:
392 using _Base::key_comp;
393 using _Base::value_comp;
395 // 23.3.1.3 map operations:
396 iterator
397 find(const key_type& __x)
399 __profcxx_map_to_unordered_map_find(this, size());
400 return iterator(_Base::find(__x));
403 const_iterator
404 find(const key_type& __x) const
406 __profcxx_map_to_unordered_map_find(this, size());
407 return const_iterator(_Base::find(__x));
410 size_type
411 count(const key_type& __x) const
413 __profcxx_map_to_unordered_map_find(this, size());
414 return _Base::count(__x);
417 iterator
418 lower_bound(const key_type& __x)
420 __profcxx_map_to_unordered_map_invalidate(this);
421 return iterator(_Base::lower_bound(__x));
424 const_iterator
425 lower_bound(const key_type& __x) const
427 __profcxx_map_to_unordered_map_invalidate(this);
428 return const_iterator(_Base::lower_bound(__x));
431 iterator
432 upper_bound(const key_type& __x)
434 __profcxx_map_to_unordered_map_invalidate(this);
435 return iterator(_Base::upper_bound(__x));
438 const_iterator
439 upper_bound(const key_type& __x) const
441 __profcxx_map_to_unordered_map_invalidate(this);
442 return const_iterator(_Base::upper_bound(__x));
445 std::pair<iterator,iterator>
446 equal_range(const key_type& __x)
448 typedef typename _Base::iterator _Base_iterator;
449 std::pair<_Base_iterator, _Base_iterator> __res =
450 _Base::equal_range(__x);
451 return std::make_pair(iterator(__res.first),
452 iterator(__res.second));
455 std::pair<const_iterator,const_iterator>
456 equal_range(const key_type& __x) const
458 __profcxx_map_to_unordered_map_find(this, size());
459 typedef typename _Base::const_iterator _Base_const_iterator;
460 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
461 _Base::equal_range(__x);
462 return std::make_pair(const_iterator(__res.first),
463 const_iterator(__res.second));
466 _Base&
467 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
469 const _Base&
470 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
474 template<typename _Key, typename _Tp,
475 typename _Compare, typename _Allocator>
476 inline bool
477 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
478 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
480 __profcxx_map_to_unordered_map_invalidate(&__lhs);
481 __profcxx_map_to_unordered_map_invalidate(&__rhs);
482 return __lhs._M_base() == __rhs._M_base();
485 template<typename _Key, typename _Tp,
486 typename _Compare, typename _Allocator>
487 inline bool
488 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
489 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
491 __profcxx_map_to_unordered_map_invalidate(&__lhs);
492 __profcxx_map_to_unordered_map_invalidate(&__rhs);
493 return __lhs._M_base() != __rhs._M_base();
496 template<typename _Key, typename _Tp,
497 typename _Compare, typename _Allocator>
498 inline bool
499 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
500 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
502 __profcxx_map_to_unordered_map_invalidate(&__lhs);
503 __profcxx_map_to_unordered_map_invalidate(&__rhs);
504 return __lhs._M_base() < __rhs._M_base();
507 template<typename _Key, typename _Tp,
508 typename _Compare, typename _Allocator>
509 inline bool
510 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
511 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
513 __profcxx_map_to_unordered_map_invalidate(&__lhs);
514 __profcxx_map_to_unordered_map_invalidate(&__rhs);
515 return __lhs._M_base() <= __rhs._M_base();
518 template<typename _Key, typename _Tp,
519 typename _Compare, typename _Allocator>
520 inline bool
521 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
522 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
524 __profcxx_map_to_unordered_map_invalidate(&__lhs);
525 __profcxx_map_to_unordered_map_invalidate(&__rhs);
526 return __lhs._M_base() >= __rhs._M_base();
529 template<typename _Key, typename _Tp,
530 typename _Compare, typename _Allocator>
531 inline bool
532 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
533 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
535 __profcxx_map_to_unordered_map_invalidate(&__lhs);
536 __profcxx_map_to_unordered_map_invalidate(&__rhs);
537 return __lhs._M_base() > __rhs._M_base();
540 template<typename _Key, typename _Tp,
541 typename _Compare, typename _Allocator>
542 inline void
543 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
544 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
545 { __lhs.swap(__rhs); }
547 } // namespace __profile
548 } // namespace std
550 #endif