1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010 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 using _Base::value_compare
;
73 // 23.3.1.1 construct/copy/destroy:
75 map(const _Compare
& __comp
= _Compare(),
76 const _Allocator
& __a
= _Allocator())
78 { __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); }
89 { __profcxx_map_to_unordered_map_construct(this); }
93 { __profcxx_map_to_unordered_map_construct(this); }
95 #ifdef __GXX_EXPERIMENTAL_CXX0X__
97 : _Base(std::move(__x
))
100 map(initializer_list
<value_type
> __l
,
101 const _Compare
& __c
= _Compare(),
102 const allocator_type
& __a
= allocator_type())
103 : _Base(__l
, __c
, __a
) { }
107 { __profcxx_map_to_unordered_map_destruct(this); }
110 operator=(const map
& __x
)
112 *static_cast<_Base
*>(this) = __x
;
116 #ifdef __GXX_EXPERIMENTAL_CXX0X__
128 operator=(initializer_list
<value_type
> __l
)
136 // _GLIBCXX_RESOLVE_LIB_DEFECTS
137 // 133. map missing get_allocator()
138 using _Base::get_allocator
;
143 { return _Base::begin(); }
147 { return _Base::begin(); }
151 { return _Base::end(); }
155 { return _Base::end(); }
160 __profcxx_map_to_unordered_map_invalidate(this);
161 return reverse_iterator(end());
164 const_reverse_iterator
167 __profcxx_map_to_unordered_map_invalidate(this);
168 return const_reverse_iterator(end());
174 __profcxx_map_to_unordered_map_invalidate(this);
175 return reverse_iterator(begin());
178 const_reverse_iterator
181 __profcxx_map_to_unordered_map_invalidate(this);
182 return const_reverse_iterator(begin());
185 #ifdef __GXX_EXPERIMENTAL_CXX0X__
188 { return const_iterator(_Base::begin()); }
192 { return const_iterator(_Base::end()); }
194 const_reverse_iterator
197 __profcxx_map_to_unordered_map_invalidate(this);
198 return const_reverse_iterator(end());
201 const_reverse_iterator
204 __profcxx_map_to_unordered_map_invalidate(this);
205 return const_reverse_iterator(begin());
212 using _Base::max_size
;
214 // 23.3.1.2 element access:
216 operator[](const key_type
& __k
)
218 __profcxx_map_to_unordered_map_find(this, size());
219 return _Base::operator[](__k
);
222 #ifdef __GXX_EXPERIMENTAL_CXX0X__
224 operator[](key_type
&& __k
)
226 __profcxx_map_to_unordered_map_find(this, size());
227 return _Base::operator[](std::move(__k
));
232 at(const key_type
& __k
)
234 __profcxx_map_to_unordered_map_find(this, size());
235 return _Base::at(__k
);
239 at(const key_type
& __k
) const
241 __profcxx_map_to_unordered_map_find(this, size());
242 return _Base::at(__k
);
246 std::pair
<iterator
, bool>
247 insert(const value_type
& __x
)
249 __profcxx_map_to_unordered_map_insert(this, size(), 1);
250 typedef typename
_Base::iterator _Base_iterator
;
251 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
252 return std::pair
<iterator
, bool>(iterator(__res
.first
),
256 #ifdef __GXX_EXPERIMENTAL_CXX0X__
257 template<typename _Pair
, typename
= typename
258 std::enable_if
<std::is_convertible
<_Pair
,
259 value_type
>::value
>::type
>
260 std::pair
<iterator
, bool>
263 __profcxx_map_to_unordered_map_insert(this, size(), 1);
264 typedef typename
_Base::iterator _Base_iterator
;
265 std::pair
<_Base_iterator
, bool> __res
266 = _Base::insert(std::forward
<_Pair
>(__x
));
267 return std::pair
<iterator
, bool>(iterator(__res
.first
),
272 #ifdef __GXX_EXPERIMENTAL_CXX0X__
274 insert(std::initializer_list
<value_type
> __list
)
276 size_type size_before
= size();
277 _Base::insert(__list
);
278 __profcxx_map_to_unordered_map_insert(this, size_before
,
279 size() - size_before
);
284 #ifdef __GXX_EXPERIMENTAL_CXX0X__
285 insert(const_iterator __position
, const value_type
& __x
)
287 insert(iterator __position
, const value_type
& __x
)
290 size_type size_before
= size();
291 iterator __i
= iterator(_Base::insert(__position
, __x
));
292 __profcxx_map_to_unordered_map_insert(this, size_before
,
293 size() - size_before
);
297 #ifdef __GXX_EXPERIMENTAL_CXX0X__
298 template<typename _Pair
, typename
= typename
299 std::enable_if
<std::is_convertible
<_Pair
,
300 value_type
>::value
>::type
>
302 insert(const_iterator __position
, _Pair
&& __x
)
304 size_type size_before
= size();
306 = iterator(_Base::insert(__position
, std::forward
<_Pair
>(__x
)));
307 __profcxx_map_to_unordered_map_insert(this, size_before
,
308 size() - size_before
);
313 template<typename _InputIterator
>
315 insert(_InputIterator __first
, _InputIterator __last
)
317 size_type size_before
= size();
318 _Base::insert(__first
, __last
);
319 __profcxx_map_to_unordered_map_insert(this, size_before
,
320 size() - size_before
);
323 #ifdef __GXX_EXPERIMENTAL_CXX0X__
325 erase(const_iterator __position
)
327 iterator __i
= _Base::erase(__position
);
328 __profcxx_map_to_unordered_map_erase(this, size(), 1);
333 erase(iterator __position
)
335 _Base::erase(__position
);
336 __profcxx_map_to_unordered_map_erase(this, size(), 1);
341 erase(const key_type
& __x
)
343 iterator __victim
= find(__x
);
344 if (__victim
== end())
348 _Base::erase(__victim
);
353 #ifdef __GXX_EXPERIMENTAL_CXX0X__
355 erase(const_iterator __first
, const_iterator __last
)
356 { return iterator(_Base::erase(__first
, __last
)); }
359 erase(iterator __first
, iterator __last
)
360 { _Base::erase(__first
, __last
); }
366 { _Base::swap(__x
); }
370 { this->erase(begin(), end()); }
373 using _Base::key_comp
;
374 using _Base::value_comp
;
376 // 23.3.1.3 map operations:
378 find(const key_type
& __x
)
380 __profcxx_map_to_unordered_map_find(this, size());
381 return iterator(_Base::find(__x
));
385 find(const key_type
& __x
) const
387 __profcxx_map_to_unordered_map_find(this, size());
388 return const_iterator(_Base::find(__x
));
392 count(const key_type
& __x
) const
394 __profcxx_map_to_unordered_map_find(this, size());
395 return _Base::count(__x
);
399 lower_bound(const key_type
& __x
)
401 __profcxx_map_to_unordered_map_invalidate(this);
402 return iterator(_Base::lower_bound(__x
));
406 lower_bound(const key_type
& __x
) const
408 __profcxx_map_to_unordered_map_invalidate(this);
409 return const_iterator(_Base::lower_bound(__x
));
413 upper_bound(const key_type
& __x
)
415 __profcxx_map_to_unordered_map_invalidate(this);
416 return iterator(_Base::upper_bound(__x
));
420 upper_bound(const key_type
& __x
) const
422 __profcxx_map_to_unordered_map_invalidate(this);
423 return const_iterator(_Base::upper_bound(__x
));
426 std::pair
<iterator
,iterator
>
427 equal_range(const key_type
& __x
)
429 typedef typename
_Base::iterator _Base_iterator
;
430 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
431 _Base::equal_range(__x
);
432 return std::make_pair(iterator(__res
.first
),
433 iterator(__res
.second
));
436 std::pair
<const_iterator
,const_iterator
>
437 equal_range(const key_type
& __x
) const
439 __profcxx_map_to_unordered_map_find(this, size());
440 typedef typename
_Base::const_iterator _Base_const_iterator
;
441 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
442 _Base::equal_range(__x
);
443 return std::make_pair(const_iterator(__res
.first
),
444 const_iterator(__res
.second
));
448 _M_base() { return *this; }
451 _M_base() const { return *this; }
455 template<typename _Key
, typename _Tp
,
456 typename _Compare
, typename _Allocator
>
458 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
459 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
461 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
462 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
463 return __lhs
._M_base() == __rhs
._M_base();
466 template<typename _Key
, typename _Tp
,
467 typename _Compare
, typename _Allocator
>
469 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
470 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
472 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
473 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
474 return __lhs
._M_base() != __rhs
._M_base();
477 template<typename _Key
, typename _Tp
,
478 typename _Compare
, typename _Allocator
>
480 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
481 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
483 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
484 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
485 return __lhs
._M_base() < __rhs
._M_base();
488 template<typename _Key
, typename _Tp
,
489 typename _Compare
, typename _Allocator
>
491 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
492 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
494 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
495 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
496 return __lhs
._M_base() <= __rhs
._M_base();
499 template<typename _Key
, typename _Tp
,
500 typename _Compare
, typename _Allocator
>
502 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
503 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
505 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
506 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
507 return __lhs
._M_base() >= __rhs
._M_base();
510 template<typename _Key
, typename _Tp
,
511 typename _Compare
, typename _Allocator
>
513 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
514 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
516 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
517 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
518 return __lhs
._M_base() > __rhs
._M_base();
521 template<typename _Key
, typename _Tp
,
522 typename _Compare
, typename _Allocator
>
524 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
525 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
526 { __lhs
.swap(__rhs
); }
528 } // namespace __profile