1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009 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>
44 /** @brief 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_D::map<_Key, _Tp, _Compare, _Allocator>
50 typedef _GLIBCXX_STD_D::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 using _Base::value_compare;
73 // 23.3.1.1 construct/copy/destroy:
74 explicit map(const _Compare& __comp = _Compare(),
75 const _Allocator& __a = _Allocator())
76 : _Base(__comp, __a) {
77 __profcxx_map_to_unordered_map_construct(this);
80 template<typename _InputIterator>
81 map(_InputIterator __first, _InputIterator __last,
82 const _Compare& __comp = _Compare(),
83 const _Allocator& __a = _Allocator())
84 : _Base(__first, __last, __comp, __a) {
85 __profcxx_map_to_unordered_map_construct(this);
90 __profcxx_map_to_unordered_map_construct(this);
95 __profcxx_map_to_unordered_map_construct(this);
98 #ifdef __GXX_EXPERIMENTAL_CXX0X__
100 : _Base(std::forward<map>(__x))
103 map(initializer_list<value_type> __l,
104 const _Compare& __c = _Compare(),
105 const allocator_type& __a = allocator_type())
106 : _Base(__l, __c, __a) { }
110 __profcxx_map_to_unordered_map_destruct(this);
114 operator=(const map& __x)
116 *static_cast<_Base*>(this) = __x;
120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
131 operator=(initializer_list<value_type> __l)
139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
140 // 133. map missing get_allocator()
141 using _Base::get_allocator;
146 { return _Base::begin(); }
150 { return _Base::begin(); }
154 { return _Base::end(); }
158 { return _Base::end(); }
163 __profcxx_map_to_unordered_map_invalidate(this);
164 return reverse_iterator(end());
167 const_reverse_iterator
170 __profcxx_map_to_unordered_map_invalidate(this);
171 return const_reverse_iterator(end());
177 __profcxx_map_to_unordered_map_invalidate(this);
178 return reverse_iterator(begin());
181 const_reverse_iterator
184 __profcxx_map_to_unordered_map_invalidate(this);
185 return const_reverse_iterator(begin());
188 #ifdef __GXX_EXPERIMENTAL_CXX0X__
191 { return const_iterator(_Base::begin()); }
195 { return const_iterator(_Base::end()); }
197 const_reverse_iterator
200 __profcxx_map_to_unordered_map_invalidate(this);
201 return const_reverse_iterator(end());
204 const_reverse_iterator
207 __profcxx_map_to_unordered_map_invalidate(this);
208 return const_reverse_iterator(begin());
215 using _Base::max_size;
217 // 23.3.1.2 element access:
219 operator[](const key_type& __k)
221 __profcxx_map_to_unordered_map_find(this, size());
222 return _Base::operator[](__k);
226 at(const key_type& __k)
228 __profcxx_map_to_unordered_map_find(this, size());
229 return _Base::at(__k);
233 at(const key_type& __k) const
235 __profcxx_map_to_unordered_map_find(this, size());
236 return _Base::at(__k);
240 std::pair<iterator, bool>
241 insert(const value_type& __x)
243 __profcxx_map_to_unordered_map_insert(this, size(), 1);
244 typedef typename _Base::iterator _Base_iterator;
245 std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
246 return std::pair<iterator, bool>(iterator(__res.first),
250 #ifdef __GXX_EXPERIMENTAL_CXX0X__
252 insert(std::initializer_list<value_type> __list)
254 size_type size_before = size();
255 _Base::insert(__list);
256 __profcxx_map_to_unordered_map_insert(this, size_before,
257 size() - size_before);
262 insert(iterator __position, const value_type& __x)
264 size_type size_before = size();
265 return iterator(_Base::insert(__position, __x));
266 __profcxx_map_to_unordered_map_insert(this, size_before,
267 size() - size_before);
270 template<typename _InputIterator>
272 insert(_InputIterator __first, _InputIterator __last)
274 size_type size_before = size();
275 _Base::insert(__first, __last);
276 __profcxx_map_to_unordered_map_insert(this, size_before,
277 size() - size_before);
280 #ifdef __GXX_EXPERIMENTAL_CXX0X__
282 erase(iterator __position)
284 iterator __i = _Base::erase(__position);
285 __profcxx_map_to_unordered_map_erase(this, size(), 1);
290 erase(iterator __position)
292 _Base::erase(__position);
293 __profcxx_map_to_unordered_map_erase(this, size(), 1);
298 erase(const key_type& __x)
300 iterator __victim = find(__x);
301 if (__victim == end())
305 _Base::erase(__victim);
311 erase(iterator __first, iterator __last)
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // 151. can't currently clear() empty container
315 while (__first != __last)
316 this->erase(__first++);
328 { this->erase(begin(), end()); }
331 using _Base::key_comp;
332 using _Base::value_comp;
334 // 23.3.1.3 map operations:
336 find(const key_type& __x)
338 __profcxx_map_to_unordered_map_find(this, size());
339 return iterator(_Base::find(__x));
343 find(const key_type& __x) const
345 __profcxx_map_to_unordered_map_find(this, size());
346 return const_iterator(_Base::find(__x));
350 count(const key_type& __x) const
352 __profcxx_map_to_unordered_map_find(this, size());
353 return _Base::count(__x);
357 lower_bound(const key_type& __x)
359 __profcxx_map_to_unordered_map_invalidate(this);
360 return iterator(_Base::lower_bound(__x));
364 lower_bound(const key_type& __x) const
366 __profcxx_map_to_unordered_map_invalidate(this);
367 return const_iterator(_Base::lower_bound(__x));
371 upper_bound(const key_type& __x)
373 __profcxx_map_to_unordered_map_invalidate(this);
374 return iterator(_Base::upper_bound(__x));
378 upper_bound(const key_type& __x) const
380 __profcxx_map_to_unordered_map_invalidate(this);
381 return const_iterator(_Base::upper_bound(__x));
384 std::pair<iterator,iterator>
385 equal_range(const key_type& __x)
387 typedef typename _Base::iterator _Base_iterator;
388 std::pair<_Base_iterator, _Base_iterator> __res =
389 _Base::equal_range(__x);
390 return std::make_pair(iterator(__res.first),
391 iterator(__res.second));
394 std::pair<const_iterator,const_iterator>
395 equal_range(const key_type& __x) const
397 __profcxx_map_to_unordered_map_find(this, size());
398 typedef typename _Base::const_iterator _Base_const_iterator;
399 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
400 _Base::equal_range(__x);
401 return std::make_pair(const_iterator(__res.first),
402 const_iterator(__res.second));
406 _M_base() { return *this; }
409 _M_base() const { return *this; }
413 template<typename _Key, typename _Tp,
414 typename _Compare, typename _Allocator>
416 operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
417 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
419 __profcxx_map_to_unordered_map_invalidate(&__lhs);
420 __profcxx_map_to_unordered_map_invalidate(&__rhs);
421 return __lhs._M_base() == __rhs._M_base();
424 template<typename _Key, typename _Tp,
425 typename _Compare, typename _Allocator>
427 operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
428 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
430 __profcxx_map_to_unordered_map_invalidate(&__lhs);
431 __profcxx_map_to_unordered_map_invalidate(&__rhs);
432 return __lhs._M_base() != __rhs._M_base();
435 template<typename _Key, typename _Tp,
436 typename _Compare, typename _Allocator>
438 operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
439 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
441 __profcxx_map_to_unordered_map_invalidate(&__lhs);
442 __profcxx_map_to_unordered_map_invalidate(&__rhs);
443 return __lhs._M_base() < __rhs._M_base();
446 template<typename _Key, typename _Tp,
447 typename _Compare, typename _Allocator>
449 operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
450 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
452 __profcxx_map_to_unordered_map_invalidate(&__lhs);
453 __profcxx_map_to_unordered_map_invalidate(&__rhs);
454 return __lhs._M_base() <= __rhs._M_base();
457 template<typename _Key, typename _Tp,
458 typename _Compare, typename _Allocator>
460 operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
461 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
463 __profcxx_map_to_unordered_map_invalidate(&__lhs);
464 __profcxx_map_to_unordered_map_invalidate(&__rhs);
465 return __lhs._M_base() >= __rhs._M_base();
468 template<typename _Key, typename _Tp,
469 typename _Compare, typename _Allocator>
471 operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
472 const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
474 __profcxx_map_to_unordered_map_invalidate(&__lhs);
475 __profcxx_map_to_unordered_map_invalidate(&__rhs);
476 return __lhs._M_base() > __rhs._M_base();
479 template<typename _Key, typename _Tp,
480 typename _Compare, typename _Allocator>
482 swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
483 map<_Key, _Tp, _Compare, _Allocator>& __rhs)
484 { __lhs.swap(__rhs); }
486 } // namespace __profile