1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009-2014 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
;
46 #if __cplusplus >= 201103L
47 typedef __gnu_cxx::__alloc_traits
<_Allocator
> _Alloc_traits
;
52 typedef _Key key_type
;
53 typedef _Tp mapped_type
;
54 typedef std::pair
<const _Key
, _Tp
> value_type
;
55 typedef _Compare key_compare
;
56 typedef _Allocator allocator_type
;
57 typedef typename
_Base::reference reference
;
58 typedef typename
_Base::const_reference const_reference
;
60 typedef typename
_Base::iterator iterator
;
61 typedef typename
_Base::const_iterator const_iterator
;
62 typedef typename
_Base::size_type size_type
;
63 typedef typename
_Base::difference_type difference_type
;
64 typedef typename
_Base::pointer pointer
;
65 typedef typename
_Base::const_pointer const_pointer
;
66 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
67 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
69 // 23.3.1.1 construct/copy/destroy:
71 map(const _Compare
& __comp
= _Compare(),
72 const _Allocator
& __a
= _Allocator())
74 { __profcxx_map_to_unordered_map_construct(this); }
76 #if __cplusplus >= 201103L
77 template<typename _InputIterator
,
78 typename
= std::_RequireInputIter
<_InputIterator
>>
80 template<typename _InputIterator
>
82 map(_InputIterator __first
, _InputIterator __last
,
83 const _Compare
& __comp
= _Compare(),
84 const _Allocator
& __a
= _Allocator())
85 : _Base(__first
, __last
, __comp
, __a
)
86 { __profcxx_map_to_unordered_map_construct(this); }
90 { __profcxx_map_to_unordered_map_construct(this); }
94 { __profcxx_map_to_unordered_map_construct(this); }
96 #if __cplusplus >= 201103L
98 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
)
99 : _Base(std::move(__x
))
100 { __profcxx_map_to_unordered_map_construct(this); }
102 map(initializer_list
<value_type
> __l
,
103 const _Compare
& __c
= _Compare(),
104 const allocator_type
& __a
= allocator_type())
105 : _Base(__l
, __c
, __a
)
106 { __profcxx_map_to_unordered_map_construct(this); }
109 map(const allocator_type
& __a
)
111 { __profcxx_map_to_unordered_map_construct(this); }
113 map(const map
& __x
, const allocator_type
& __a
)
115 { __profcxx_map_to_unordered_map_construct(this); }
117 map(map
&& __x
, const allocator_type
& __a
)
118 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
119 && _Alloc_traits::_S_always_equal())
120 : _Base(std::move(__x
), __a
)
121 { __profcxx_map_to_unordered_map_construct(this); }
123 map(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
125 { __profcxx_map_to_unordered_map_construct(this); }
127 template<typename _InputIterator
>
128 map(_InputIterator __first
, _InputIterator __last
,
129 const allocator_type
& __a
)
130 : _Base(__first
, __last
, __a
)
131 { __profcxx_map_to_unordered_map_construct(this); }
134 ~map() _GLIBCXX_NOEXCEPT
135 { __profcxx_map_to_unordered_map_destruct(this); }
137 #if __cplusplus < 201103L
139 operator=(const map
& __x
)
146 operator=(const map
&) = default;
149 operator=(map
&&) = default;
152 operator=(initializer_list
<value_type
> __l
)
159 // _GLIBCXX_RESOLVE_LIB_DEFECTS
160 // 133. map missing get_allocator()
161 using _Base::get_allocator
;
165 begin() _GLIBCXX_NOEXCEPT
166 { return _Base::begin(); }
169 begin() const _GLIBCXX_NOEXCEPT
170 { return _Base::begin(); }
173 end() _GLIBCXX_NOEXCEPT
174 { return _Base::end(); }
177 end() const _GLIBCXX_NOEXCEPT
178 { return _Base::end(); }
181 rbegin() _GLIBCXX_NOEXCEPT
183 __profcxx_map_to_unordered_map_invalidate(this);
184 return reverse_iterator(end());
187 const_reverse_iterator
188 rbegin() const _GLIBCXX_NOEXCEPT
190 __profcxx_map_to_unordered_map_invalidate(this);
191 return const_reverse_iterator(end());
195 rend() _GLIBCXX_NOEXCEPT
197 __profcxx_map_to_unordered_map_invalidate(this);
198 return reverse_iterator(begin());
201 const_reverse_iterator
202 rend() const _GLIBCXX_NOEXCEPT
204 __profcxx_map_to_unordered_map_invalidate(this);
205 return const_reverse_iterator(begin());
208 #if __cplusplus >= 201103L
210 cbegin() const noexcept
211 { return const_iterator(_Base::begin()); }
214 cend() const noexcept
215 { return const_iterator(_Base::end()); }
217 const_reverse_iterator
218 crbegin() const noexcept
220 __profcxx_map_to_unordered_map_invalidate(this);
221 return const_reverse_iterator(end());
224 const_reverse_iterator
225 crend() const noexcept
227 __profcxx_map_to_unordered_map_invalidate(this);
228 return const_reverse_iterator(begin());
235 using _Base::max_size
;
237 // 23.3.1.2 element access:
239 operator[](const key_type
& __k
)
241 __profcxx_map_to_unordered_map_find(this, size());
242 return _Base::operator[](__k
);
245 #if __cplusplus >= 201103L
247 operator[](key_type
&& __k
)
249 __profcxx_map_to_unordered_map_find(this, size());
250 return _Base::operator[](std::move(__k
));
255 at(const key_type
& __k
)
257 __profcxx_map_to_unordered_map_find(this, size());
258 return _Base::at(__k
);
262 at(const key_type
& __k
) const
264 __profcxx_map_to_unordered_map_find(this, size());
265 return _Base::at(__k
);
269 #if __cplusplus >= 201103L
270 template<typename
... _Args
>
271 std::pair
<iterator
, bool>
272 emplace(_Args
&&... __args
)
274 __profcxx_map_to_unordered_map_insert(this, size(), 1);
275 auto __res
= _Base::emplace(std::forward
<_Args
>(__args
)...);
276 return std::pair
<iterator
, bool>(iterator(__res
.first
),
280 template<typename
... _Args
>
282 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
284 size_type size_before
= size();
285 auto __res
= _Base::emplace_hint(__pos
,
286 std::forward
<_Args
>(__args
)...);
287 __profcxx_map_to_unordered_map_insert(this, size_before
,
288 size() - size_before
);
293 std::pair
<iterator
, bool>
294 insert(const value_type
& __x
)
296 __profcxx_map_to_unordered_map_insert(this, size(), 1);
297 typedef typename
_Base::iterator _Base_iterator
;
298 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
299 return std::pair
<iterator
, bool>(iterator(__res
.first
),
303 #if __cplusplus >= 201103L
304 template<typename _Pair
, typename
= typename
305 std::enable_if
<std::is_constructible
<value_type
,
306 _Pair
&&>::value
>::type
>
307 std::pair
<iterator
, bool>
310 __profcxx_map_to_unordered_map_insert(this, size(), 1);
311 typedef typename
_Base::iterator _Base_iterator
;
312 std::pair
<_Base_iterator
, bool> __res
313 = _Base::insert(std::forward
<_Pair
>(__x
));
314 return std::pair
<iterator
, bool>(iterator(__res
.first
),
319 #if __cplusplus >= 201103L
321 insert(std::initializer_list
<value_type
> __list
)
323 size_type size_before
= size();
324 _Base::insert(__list
);
325 __profcxx_map_to_unordered_map_insert(this, size_before
,
326 size() - size_before
);
331 #if __cplusplus >= 201103L
332 insert(const_iterator __position
, const value_type
& __x
)
334 insert(iterator __position
, const value_type
& __x
)
337 size_type size_before
= size();
338 iterator __i
= iterator(_Base::insert(__position
, __x
));
339 __profcxx_map_to_unordered_map_insert(this, size_before
,
340 size() - size_before
);
344 #if __cplusplus >= 201103L
345 template<typename _Pair
, typename
= typename
346 std::enable_if
<std::is_constructible
<value_type
,
347 _Pair
&&>::value
>::type
>
349 insert(const_iterator __position
, _Pair
&& __x
)
351 size_type size_before
= size();
353 = iterator(_Base::insert(__position
, std::forward
<_Pair
>(__x
)));
354 __profcxx_map_to_unordered_map_insert(this, size_before
,
355 size() - size_before
);
360 #if __cplusplus >= 201103L
361 template<typename _InputIterator
,
362 typename
= std::_RequireInputIter
<_InputIterator
>>
364 template<typename _InputIterator
>
367 insert(_InputIterator __first
, _InputIterator __last
)
369 size_type size_before
= size();
370 _Base::insert(__first
, __last
);
371 __profcxx_map_to_unordered_map_insert(this, size_before
,
372 size() - size_before
);
375 #if __cplusplus >= 201103L
377 erase(const_iterator __position
)
379 iterator __i
= _Base::erase(__position
);
380 __profcxx_map_to_unordered_map_erase(this, size(), 1);
385 erase(iterator __position
)
386 { return erase(const_iterator(__position
)); }
389 erase(iterator __position
)
391 _Base::erase(__position
);
392 __profcxx_map_to_unordered_map_erase(this, size(), 1);
397 erase(const key_type
& __x
)
399 iterator __victim
= find(__x
);
400 if (__victim
== end())
404 _Base::erase(__victim
);
409 #if __cplusplus >= 201103L
411 erase(const_iterator __first
, const_iterator __last
)
412 { return iterator(_Base::erase(__first
, __last
)); }
415 erase(iterator __first
, iterator __last
)
416 { _Base::erase(__first
, __last
); }
421 #if __cplusplus >= 201103L
422 noexcept(_Alloc_traits::_S_nothrow_swap())
424 { _Base::swap(__x
); }
427 clear() _GLIBCXX_NOEXCEPT
428 { this->erase(begin(), end()); }
431 using _Base::key_comp
;
432 using _Base::value_comp
;
434 // 23.3.1.3 map operations:
436 find(const key_type
& __x
)
438 __profcxx_map_to_unordered_map_find(this, size());
439 return iterator(_Base::find(__x
));
443 find(const key_type
& __x
) const
445 __profcxx_map_to_unordered_map_find(this, size());
446 return const_iterator(_Base::find(__x
));
450 count(const key_type
& __x
) const
452 __profcxx_map_to_unordered_map_find(this, size());
453 return _Base::count(__x
);
457 lower_bound(const key_type
& __x
)
459 __profcxx_map_to_unordered_map_invalidate(this);
460 return iterator(_Base::lower_bound(__x
));
464 lower_bound(const key_type
& __x
) const
466 __profcxx_map_to_unordered_map_invalidate(this);
467 return const_iterator(_Base::lower_bound(__x
));
471 upper_bound(const key_type
& __x
)
473 __profcxx_map_to_unordered_map_invalidate(this);
474 return iterator(_Base::upper_bound(__x
));
478 upper_bound(const key_type
& __x
) const
480 __profcxx_map_to_unordered_map_invalidate(this);
481 return const_iterator(_Base::upper_bound(__x
));
484 std::pair
<iterator
,iterator
>
485 equal_range(const key_type
& __x
)
487 typedef typename
_Base::iterator _Base_iterator
;
488 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
489 _Base::equal_range(__x
);
490 return std::make_pair(iterator(__res
.first
),
491 iterator(__res
.second
));
494 std::pair
<const_iterator
,const_iterator
>
495 equal_range(const key_type
& __x
) const
497 __profcxx_map_to_unordered_map_find(this, size());
498 typedef typename
_Base::const_iterator _Base_const_iterator
;
499 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
500 _Base::equal_range(__x
);
501 return std::make_pair(const_iterator(__res
.first
),
502 const_iterator(__res
.second
));
506 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
509 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
513 template<typename _Key
, typename _Tp
,
514 typename _Compare
, typename _Allocator
>
516 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
517 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
519 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
520 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
521 return __lhs
._M_base() == __rhs
._M_base();
524 template<typename _Key
, typename _Tp
,
525 typename _Compare
, typename _Allocator
>
527 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
528 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
530 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
531 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
532 return __lhs
._M_base() != __rhs
._M_base();
535 template<typename _Key
, typename _Tp
,
536 typename _Compare
, typename _Allocator
>
538 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
539 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
541 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
542 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
543 return __lhs
._M_base() < __rhs
._M_base();
546 template<typename _Key
, typename _Tp
,
547 typename _Compare
, typename _Allocator
>
549 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
550 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
552 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
553 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
554 return __lhs
._M_base() <= __rhs
._M_base();
557 template<typename _Key
, typename _Tp
,
558 typename _Compare
, typename _Allocator
>
560 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
561 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
563 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
564 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
565 return __lhs
._M_base() >= __rhs
._M_base();
568 template<typename _Key
, typename _Tp
,
569 typename _Compare
, typename _Allocator
>
571 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
572 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
574 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
575 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
576 return __lhs
._M_base() > __rhs
._M_base();
579 template<typename _Key
, typename _Tp
,
580 typename _Compare
, typename _Allocator
>
582 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
583 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
584 { __lhs
.swap(__rhs
); }
586 } // namespace __profile