1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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/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
32 #include <profile/base.h>
34 namespace std _GLIBCXX_VISIBILITY(default)
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> > >
42 : public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
44 typedef _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator> _Base;
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:
67 map(const _Compare& __comp = _Compare(),
68 const _Allocator& __a = _Allocator())
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); }
81 { __profcxx_map_to_unordered_map_construct(this); }
85 { __profcxx_map_to_unordered_map_construct(this); }
87 #ifdef __GXX_EXPERIMENTAL_CXX0X__
89 : _Base(std::move(__x))
92 map(initializer_list<value_type> __l,
93 const _Compare& __c = _Compare(),
94 const allocator_type& __a = allocator_type())
95 : _Base(__l, __c, __a) { }
99 { __profcxx_map_to_unordered_map_destruct(this); }
102 operator=(const map& __x)
104 *static_cast<_Base*>(this) = __x;
108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
120 operator=(initializer_list<value_type> __l)
128 // _GLIBCXX_RESOLVE_LIB_DEFECTS
129 // 133. map missing get_allocator()
130 using _Base::get_allocator;
135 { return _Base::begin(); }
139 { return _Base::begin(); }
143 { return _Base::end(); }
147 { return _Base::end(); }
152 __profcxx_map_to_unordered_map_invalidate(this);
153 return reverse_iterator(end());
156 const_reverse_iterator
159 __profcxx_map_to_unordered_map_invalidate(this);
160 return const_reverse_iterator(end());
166 __profcxx_map_to_unordered_map_invalidate(this);
167 return reverse_iterator(begin());
170 const_reverse_iterator
173 __profcxx_map_to_unordered_map_invalidate(this);
174 return const_reverse_iterator(begin());
177 #ifdef __GXX_EXPERIMENTAL_CXX0X__
180 { return const_iterator(_Base::begin()); }
184 { return const_iterator(_Base::end()); }
186 const_reverse_iterator
189 __profcxx_map_to_unordered_map_invalidate(this);
190 return const_reverse_iterator(end());
193 const_reverse_iterator
196 __profcxx_map_to_unordered_map_invalidate(this);
197 return const_reverse_iterator(begin());
204 using _Base::max_size;
206 // 23.3.1.2 element access:
208 operator[](const key_type& __k)
210 __profcxx_map_to_unordered_map_find(this, size());
211 return _Base::operator[](__k);
214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 operator[](key_type&& __k)
218 __profcxx_map_to_unordered_map_find(this, size());
219 return _Base::operator[](std::move(__k));
224 at(const key_type& __k)
226 __profcxx_map_to_unordered_map_find(this, size());
227 return _Base::at(__k);
231 at(const key_type& __k) const
233 __profcxx_map_to_unordered_map_find(this, size());
234 return _Base::at(__k);
238 std::pair<iterator, bool>
239 insert(const value_type& __x)
241 __profcxx_map_to_unordered_map_insert(this, size(), 1);
242 typedef typename _Base::iterator _Base_iterator;
243 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
244 return std::pair<iterator, bool>(iterator(__res.first),
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
249 template<typename _Pair, typename = typename
250 std::enable_if<std::is_convertible<_Pair,
251 value_type>::value>::type>
252 std::pair<iterator, bool>
255 __profcxx_map_to_unordered_map_insert(this, size(), 1);
256 typedef typename _Base::iterator _Base_iterator;
257 std::pair<_Base_iterator, bool> __res
258 = _Base::insert(std::forward<_Pair>(__x));
259 return std::pair<iterator, bool>(iterator(__res.first),
264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
266 insert(std::initializer_list<value_type> __list)
268 size_type size_before = size();
269 _Base::insert(__list);
270 __profcxx_map_to_unordered_map_insert(this, size_before,
271 size() - size_before);
276 #ifdef __GXX_EXPERIMENTAL_CXX0X__
277 insert(const_iterator __position, const value_type& __x)
279 insert(iterator __position, const value_type& __x)
282 size_type size_before = size();
283 iterator __i = iterator(_Base::insert(__position, __x));
284 __profcxx_map_to_unordered_map_insert(this, size_before,
285 size() - size_before);
289 #ifdef __GXX_EXPERIMENTAL_CXX0X__
290 template<typename _Pair, typename = typename
291 std::enable_if<std::is_convertible<_Pair,
292 value_type>::value>::type>
294 insert(const_iterator __position, _Pair&& __x)
296 size_type size_before = size();
298 = iterator(_Base::insert(__position, std::forward<_Pair>(__x)));
299 __profcxx_map_to_unordered_map_insert(this, size_before,
300 size() - size_before);
305 template<typename _InputIterator>
307 insert(_InputIterator __first, _InputIterator __last)
309 size_type size_before = size();
310 _Base::insert(__first, __last);
311 __profcxx_map_to_unordered_map_insert(this, size_before,
312 size() - size_before);
315 #ifdef __GXX_EXPERIMENTAL_CXX0X__
317 erase(const_iterator __position)
319 iterator __i = _Base::erase(__position);
320 __profcxx_map_to_unordered_map_erase(this, size(), 1);
325 erase(iterator __position)
326 { return erase(const_iterator(__position)); }
329 erase(iterator __position)
331 _Base::erase(__position);
332 __profcxx_map_to_unordered_map_erase(this, size(), 1);
337 erase(const key_type& __x)
339 iterator __victim = find(__x);
340 if (__victim == end())
344 _Base::erase(__victim);
349 #ifdef __GXX_EXPERIMENTAL_CXX0X__
351 erase(const_iterator __first, const_iterator __last)
352 { return iterator(_Base::erase(__first, __last)); }
355 erase(iterator __first, iterator __last)
356 { _Base::erase(__first, __last); }
362 { _Base::swap(__x); }
366 { this->erase(begin(), end()); }
369 using _Base::key_comp;
370 using _Base::value_comp;
372 // 23.3.1.3 map operations:
374 find(const key_type& __x)
376 __profcxx_map_to_unordered_map_find(this, size());
377 return iterator(_Base::find(__x));
381 find(const key_type& __x) const
383 __profcxx_map_to_unordered_map_find(this, size());
384 return const_iterator(_Base::find(__x));
388 count(const key_type& __x) const
390 __profcxx_map_to_unordered_map_find(this, size());
391 return _Base::count(__x);
395 lower_bound(const key_type& __x)
397 __profcxx_map_to_unordered_map_invalidate(this);
398 return iterator(_Base::lower_bound(__x));
402 lower_bound(const key_type& __x) const
404 __profcxx_map_to_unordered_map_invalidate(this);
405 return const_iterator(_Base::lower_bound(__x));
409 upper_bound(const key_type& __x)
411 __profcxx_map_to_unordered_map_invalidate(this);
412 return iterator(_Base::upper_bound(__x));
416 upper_bound(const key_type& __x) const
418 __profcxx_map_to_unordered_map_invalidate(this);
419 return const_iterator(_Base::upper_bound(__x));
422 std::pair<iterator,iterator>
423 equal_range(const key_type& __x)
425 typedef typename _Base::iterator _Base_iterator;
426 std::pair<_Base_iterator, _Base_iterator> __res =
427 _Base::equal_range(__x);
428 return std::make_pair(iterator(__res.first),
429 iterator(__res.second));
432 std::pair<const_iterator,const_iterator>
433 equal_range(const key_type& __x) const
435 __profcxx_map_to_unordered_map_find(this, size());
436 typedef typename _Base::const_iterator _Base_const_iterator;
437 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
438 _Base::equal_range(__x);
439 return std::make_pair(const_iterator(__res.first),
440 const_iterator(__res.second));
444 _M_base() { return *this; }
447 _M_base() const { return *this; }
451 template<typename _Key, typename _Tp,
452 typename _Compare, typename _Allocator>
454 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
455 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
457 __profcxx_map_to_unordered_map_invalidate(&__lhs);
458 __profcxx_map_to_unordered_map_invalidate(&__rhs);
459 return __lhs._M_base() == __rhs._M_base();
462 template<typename _Key, typename _Tp,
463 typename _Compare, typename _Allocator>
465 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
466 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
468 __profcxx_map_to_unordered_map_invalidate(&__lhs);
469 __profcxx_map_to_unordered_map_invalidate(&__rhs);
470 return __lhs._M_base() != __rhs._M_base();
473 template<typename _Key, typename _Tp,
474 typename _Compare, typename _Allocator>
476 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
477 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
479 __profcxx_map_to_unordered_map_invalidate(&__lhs);
480 __profcxx_map_to_unordered_map_invalidate(&__rhs);
481 return __lhs._M_base() < __rhs._M_base();
484 template<typename _Key, typename _Tp,
485 typename _Compare, typename _Allocator>
487 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
488 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
490 __profcxx_map_to_unordered_map_invalidate(&__lhs);
491 __profcxx_map_to_unordered_map_invalidate(&__rhs);
492 return __lhs._M_base() <= __rhs._M_base();
495 template<typename _Key, typename _Tp,
496 typename _Compare, typename _Allocator>
498 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
499 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
501 __profcxx_map_to_unordered_map_invalidate(&__lhs);
502 __profcxx_map_to_unordered_map_invalidate(&__rhs);
503 return __lhs._M_base() >= __rhs._M_base();
506 template<typename _Key, typename _Tp,
507 typename _Compare, typename _Allocator>
509 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
510 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
512 __profcxx_map_to_unordered_map_invalidate(&__lhs);
513 __profcxx_map_to_unordered_map_invalidate(&__rhs);
514 return __lhs._M_base() > __rhs._M_base();
517 template<typename _Key, typename _Tp,
518 typename _Compare, typename _Allocator>
520 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
521 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
522 { __lhs.swap(__rhs); }
524 } // namespace __profile