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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file profile/map.h
31 * This file is a GNU profile extension to the Standard C++ Library.
34 #ifndef _GLIBCXX_PROFILE_MAP_H
35 #define _GLIBCXX_PROFILE_MAP_H 1
38 #include <profile/base.h>
40 namespace std _GLIBCXX_VISIBILITY(default)
44 /// Class std::map wrapper with performance instrumentation.
45 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
46 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
48 : public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
50 typedef _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator> _Base;
54 typedef _Key key_type;
55 typedef _Tp mapped_type;
56 typedef std::pair<const _Key, _Tp> value_type;
57 typedef _Compare key_compare;
58 typedef _Allocator allocator_type;
59 typedef typename _Base::reference reference;
60 typedef typename _Base::const_reference const_reference;
62 typedef typename _Base::iterator iterator;
63 typedef typename _Base::const_iterator const_iterator;
64 typedef typename _Base::size_type size_type;
65 typedef typename _Base::difference_type difference_type;
66 typedef typename _Base::pointer pointer;
67 typedef typename _Base::const_pointer const_pointer;
68 typedef std::reverse_iterator<iterator> reverse_iterator;
69 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
71 // 23.3.1.1 construct/copy/destroy:
73 map(const _Compare& __comp = _Compare(),
74 const _Allocator& __a = _Allocator())
76 { __profcxx_map_to_unordered_map_construct(this); }
78 template<typename _InputIterator>
79 map(_InputIterator __first, _InputIterator __last,
80 const _Compare& __comp = _Compare(),
81 const _Allocator& __a = _Allocator())
82 : _Base(__first, __last, __comp, __a)
83 { __profcxx_map_to_unordered_map_construct(this); }
87 { __profcxx_map_to_unordered_map_construct(this); }
91 { __profcxx_map_to_unordered_map_construct(this); }
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95 : _Base(std::move(__x))
98 map(initializer_list<value_type> __l,
99 const _Compare& __c = _Compare(),
100 const allocator_type& __a = allocator_type())
101 : _Base(__l, __c, __a) { }
105 { __profcxx_map_to_unordered_map_destruct(this); }
108 operator=(const map& __x)
110 *static_cast<_Base*>(this) = __x;
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
126 operator=(initializer_list<value_type> __l)
134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
135 // 133. map missing get_allocator()
136 using _Base::get_allocator;
141 { return _Base::begin(); }
145 { return _Base::begin(); }
149 { return _Base::end(); }
153 { return _Base::end(); }
158 __profcxx_map_to_unordered_map_invalidate(this);
159 return reverse_iterator(end());
162 const_reverse_iterator
165 __profcxx_map_to_unordered_map_invalidate(this);
166 return const_reverse_iterator(end());
172 __profcxx_map_to_unordered_map_invalidate(this);
173 return reverse_iterator(begin());
176 const_reverse_iterator
179 __profcxx_map_to_unordered_map_invalidate(this);
180 return const_reverse_iterator(begin());
183 #ifdef __GXX_EXPERIMENTAL_CXX0X__
186 { return const_iterator(_Base::begin()); }
190 { return const_iterator(_Base::end()); }
192 const_reverse_iterator
195 __profcxx_map_to_unordered_map_invalidate(this);
196 return const_reverse_iterator(end());
199 const_reverse_iterator
202 __profcxx_map_to_unordered_map_invalidate(this);
203 return const_reverse_iterator(begin());
210 using _Base::max_size;
212 // 23.3.1.2 element access:
214 operator[](const key_type& __k)
216 __profcxx_map_to_unordered_map_find(this, size());
217 return _Base::operator[](__k);
220 #ifdef __GXX_EXPERIMENTAL_CXX0X__
222 operator[](key_type&& __k)
224 __profcxx_map_to_unordered_map_find(this, size());
225 return _Base::operator[](std::move(__k));
230 at(const key_type& __k)
232 __profcxx_map_to_unordered_map_find(this, size());
233 return _Base::at(__k);
237 at(const key_type& __k) const
239 __profcxx_map_to_unordered_map_find(this, size());
240 return _Base::at(__k);
244 std::pair<iterator, bool>
245 insert(const value_type& __x)
247 __profcxx_map_to_unordered_map_insert(this, size(), 1);
248 typedef typename _Base::iterator _Base_iterator;
249 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
250 return std::pair<iterator, bool>(iterator(__res.first),
254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
255 template<typename _Pair, typename = typename
256 std::enable_if<std::is_convertible<_Pair,
257 value_type>::value>::type>
258 std::pair<iterator, bool>
261 __profcxx_map_to_unordered_map_insert(this, size(), 1);
262 typedef typename _Base::iterator _Base_iterator;
263 std::pair<_Base_iterator, bool> __res
264 = _Base::insert(std::forward<_Pair>(__x));
265 return std::pair<iterator, bool>(iterator(__res.first),
270 #ifdef __GXX_EXPERIMENTAL_CXX0X__
272 insert(std::initializer_list<value_type> __list)
274 size_type size_before = size();
275 _Base::insert(__list);
276 __profcxx_map_to_unordered_map_insert(this, size_before,
277 size() - size_before);
282 #ifdef __GXX_EXPERIMENTAL_CXX0X__
283 insert(const_iterator __position, const value_type& __x)
285 insert(iterator __position, const value_type& __x)
288 size_type size_before = size();
289 iterator __i = iterator(_Base::insert(__position, __x));
290 __profcxx_map_to_unordered_map_insert(this, size_before,
291 size() - size_before);
295 #ifdef __GXX_EXPERIMENTAL_CXX0X__
296 template<typename _Pair, typename = typename
297 std::enable_if<std::is_convertible<_Pair,
298 value_type>::value>::type>
300 insert(const_iterator __position, _Pair&& __x)
302 size_type size_before = size();
304 = iterator(_Base::insert(__position, std::forward<_Pair>(__x)));
305 __profcxx_map_to_unordered_map_insert(this, size_before,
306 size() - size_before);
311 template<typename _InputIterator>
313 insert(_InputIterator __first, _InputIterator __last)
315 size_type size_before = size();
316 _Base::insert(__first, __last);
317 __profcxx_map_to_unordered_map_insert(this, size_before,
318 size() - size_before);
321 #ifdef __GXX_EXPERIMENTAL_CXX0X__
323 erase(const_iterator __position)
325 iterator __i = _Base::erase(__position);
326 __profcxx_map_to_unordered_map_erase(this, size(), 1);
331 erase(iterator __position)
333 _Base::erase(__position);
334 __profcxx_map_to_unordered_map_erase(this, size(), 1);
339 erase(const key_type& __x)
341 iterator __victim = find(__x);
342 if (__victim == end())
346 _Base::erase(__victim);
351 #ifdef __GXX_EXPERIMENTAL_CXX0X__
353 erase(const_iterator __first, const_iterator __last)
354 { return iterator(_Base::erase(__first, __last)); }
357 erase(iterator __first, iterator __last)
358 { _Base::erase(__first, __last); }
364 { _Base::swap(__x); }
368 { this->erase(begin(), end()); }
371 using _Base::key_comp;
372 using _Base::value_comp;
374 // 23.3.1.3 map operations:
376 find(const key_type& __x)
378 __profcxx_map_to_unordered_map_find(this, size());
379 return iterator(_Base::find(__x));
383 find(const key_type& __x) const
385 __profcxx_map_to_unordered_map_find(this, size());
386 return const_iterator(_Base::find(__x));
390 count(const key_type& __x) const
392 __profcxx_map_to_unordered_map_find(this, size());
393 return _Base::count(__x);
397 lower_bound(const key_type& __x)
399 __profcxx_map_to_unordered_map_invalidate(this);
400 return iterator(_Base::lower_bound(__x));
404 lower_bound(const key_type& __x) const
406 __profcxx_map_to_unordered_map_invalidate(this);
407 return const_iterator(_Base::lower_bound(__x));
411 upper_bound(const key_type& __x)
413 __profcxx_map_to_unordered_map_invalidate(this);
414 return iterator(_Base::upper_bound(__x));
418 upper_bound(const key_type& __x) const
420 __profcxx_map_to_unordered_map_invalidate(this);
421 return const_iterator(_Base::upper_bound(__x));
424 std::pair<iterator,iterator>
425 equal_range(const key_type& __x)
427 typedef typename _Base::iterator _Base_iterator;
428 std::pair<_Base_iterator, _Base_iterator> __res =
429 _Base::equal_range(__x);
430 return std::make_pair(iterator(__res.first),
431 iterator(__res.second));
434 std::pair<const_iterator,const_iterator>
435 equal_range(const key_type& __x) const
437 __profcxx_map_to_unordered_map_find(this, size());
438 typedef typename _Base::const_iterator _Base_const_iterator;
439 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
440 _Base::equal_range(__x);
441 return std::make_pair(const_iterator(__res.first),
442 const_iterator(__res.second));
446 _M_base() { return *this; }
449 _M_base() const { return *this; }
453 template<typename _Key, typename _Tp,
454 typename _Compare, typename _Allocator>
456 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
457 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
459 __profcxx_map_to_unordered_map_invalidate(&__lhs);
460 __profcxx_map_to_unordered_map_invalidate(&__rhs);
461 return __lhs._M_base() == __rhs._M_base();
464 template<typename _Key, typename _Tp,
465 typename _Compare, typename _Allocator>
467 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
468 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
470 __profcxx_map_to_unordered_map_invalidate(&__lhs);
471 __profcxx_map_to_unordered_map_invalidate(&__rhs);
472 return __lhs._M_base() != __rhs._M_base();
475 template<typename _Key, typename _Tp,
476 typename _Compare, typename _Allocator>
478 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
479 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
481 __profcxx_map_to_unordered_map_invalidate(&__lhs);
482 __profcxx_map_to_unordered_map_invalidate(&__rhs);
483 return __lhs._M_base() < __rhs._M_base();
486 template<typename _Key, typename _Tp,
487 typename _Compare, typename _Allocator>
489 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
490 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
492 __profcxx_map_to_unordered_map_invalidate(&__lhs);
493 __profcxx_map_to_unordered_map_invalidate(&__rhs);
494 return __lhs._M_base() <= __rhs._M_base();
497 template<typename _Key, typename _Tp,
498 typename _Compare, typename _Allocator>
500 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
501 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
503 __profcxx_map_to_unordered_map_invalidate(&__lhs);
504 __profcxx_map_to_unordered_map_invalidate(&__rhs);
505 return __lhs._M_base() >= __rhs._M_base();
508 template<typename _Key, typename _Tp,
509 typename _Compare, typename _Allocator>
511 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
512 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
514 __profcxx_map_to_unordered_map_invalidate(&__lhs);
515 __profcxx_map_to_unordered_map_invalidate(&__rhs);
516 return __lhs._M_base() > __rhs._M_base();
519 template<typename _Key, typename _Tp,
520 typename _Compare, typename _Allocator>
522 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
523 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
524 { __lhs.swap(__rhs); }
526 } // namespace __profile