1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009-2015 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
31 #include <profile/base.h>
32 #include <profile/ordered_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
>,
43 public _Ordered_profile
<map
<_Key
, _Tp
, _Compare
, _Allocator
> >
45 typedef _GLIBCXX_STD_C::map
<_Key
, _Tp
, _Compare
, _Allocator
> _Base
;
47 typedef typename
_Base::iterator _Base_iterator
;
48 typedef typename
_Base::const_iterator _Base_const_iterator
;
52 typedef _Key key_type
;
53 typedef _Tp mapped_type
;
54 typedef typename
_Base::value_type value_type
;
55 typedef _Compare key_compare
;
56 typedef typename
_Base::reference reference
;
57 typedef typename
_Base::const_reference const_reference
;
59 typedef __iterator_tracker
<_Base_iterator
, map
> iterator
;
60 typedef __iterator_tracker
<_Base_const_iterator
,
62 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
63 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
65 typedef typename
_Base::size_type size_type
;
66 typedef typename
_Base::difference_type difference_type
;
68 // 23.3.1.1 construct/copy/destroy:
70 #if __cplusplus < 201103L
79 map(const map
&) = default;
85 map(const _Compare
& __comp
,
86 const _Allocator
& __a
= _Allocator())
87 : _Base(__comp
, __a
) { }
89 #if __cplusplus >= 201103L
90 template<typename _InputIterator
,
91 typename
= std::_RequireInputIter
<_InputIterator
>>
93 template<typename _InputIterator
>
95 map(_InputIterator __first
, _InputIterator __last
,
96 const _Compare
& __comp
= _Compare(),
97 const _Allocator
& __a
= _Allocator())
98 : _Base(__first
, __last
, __comp
, __a
) { }
100 map(const _Base
& __x
)
103 #if __cplusplus >= 201103L
104 map(initializer_list
<value_type
> __l
,
105 const _Compare
& __c
= _Compare(),
106 const _Allocator
& __a
= _Allocator())
107 : _Base(__l
, __c
, __a
) { }
110 map(const _Allocator
& __a
)
113 map(const map
& __x
, const _Allocator
& __a
)
114 : _Base(__x
, __a
) { }
116 map(map
&& __x
, const _Allocator
& __a
)
117 noexcept( noexcept(_Base(std::move(__x
), __a
)) )
118 : _Base(std::move(__x
), __a
) { }
120 map(initializer_list
<value_type
> __l
, const _Allocator
& __a
)
121 : _Base(__l
, __a
) { }
123 template<typename _InputIterator
>
124 map(_InputIterator __first
, _InputIterator __last
,
125 const _Allocator
& __a
)
126 : _Base(__first
, __last
, __a
) { }
129 #if __cplusplus < 201103L
131 operator=(const map
& __x
)
133 this->_M_profile_destruct();
135 this->_M_profile_construct();
140 operator=(const map
&) = default;
143 operator=(map
&&) = default;
146 operator=(initializer_list
<value_type
> __l
)
148 this->_M_profile_destruct();
150 this->_M_profile_construct();
157 begin() _GLIBCXX_NOEXCEPT
158 { return iterator(_Base::begin(), this); }
161 begin() const _GLIBCXX_NOEXCEPT
162 { return const_iterator(_Base::begin(), this); }
165 end() _GLIBCXX_NOEXCEPT
166 { return iterator(_Base::end(), this); }
169 end() const _GLIBCXX_NOEXCEPT
170 { return const_iterator(_Base::end(), this); }
172 #if __cplusplus >= 201103L
174 cbegin() const noexcept
175 { return const_iterator(_Base::cbegin(), this); }
178 cend() const noexcept
179 { return const_iterator(_Base::cend(), this); }
183 rbegin() _GLIBCXX_NOEXCEPT
185 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
186 return reverse_iterator(end());
189 const_reverse_iterator
190 rbegin() const _GLIBCXX_NOEXCEPT
192 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
193 return const_reverse_iterator(end());
197 rend() _GLIBCXX_NOEXCEPT
199 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
200 return reverse_iterator(begin());
203 const_reverse_iterator
204 rend() const _GLIBCXX_NOEXCEPT
206 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
207 return const_reverse_iterator(begin());
210 #if __cplusplus >= 201103L
211 const_reverse_iterator
212 crbegin() const noexcept
214 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
215 return const_reverse_iterator(cend());
218 const_reverse_iterator
219 crend() const noexcept
221 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
222 return const_reverse_iterator(cbegin());
226 // 23.3.1.2 element access:
228 operator[](const key_type
& __k
)
230 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
231 return _Base::operator[](__k
);
234 #if __cplusplus >= 201103L
236 operator[](key_type
&& __k
)
238 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
239 return _Base::operator[](std::move(__k
));
244 at(const key_type
& __k
)
246 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
247 return _Base::at(__k
);
251 at(const key_type
& __k
) const
253 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
254 return _Base::at(__k
);
258 #if __cplusplus >= 201103L
259 template<typename
... _Args
>
260 std::pair
<iterator
, bool>
261 emplace(_Args
&&... __args
)
263 // The cost is the same whether or not the element is inserted so we
264 // always report insertion of 1 element.
265 __profcxx_map2umap_insert(this->_M_map2umap_info
, this->size(), 1);
266 auto __base_ret
= _Base::emplace(std::forward
<_Args
>(__args
)...);
267 return std::make_pair(iterator(__base_ret
.first
, this),
271 template<typename
... _Args
>
273 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
275 auto size_before
= this->size();
277 = _Base::emplace_hint(__pos
.base(), std::forward
<_Args
>(__args
)...);
278 __profcxx_map2umap_insert(this->_M_map2umap_info
,
279 size_before
, _M_hint_used(__pos
.base(), __res
) ? 0 : 1);
280 return iterator(__res
, this);
284 std::pair
<iterator
, bool>
285 insert(const value_type
& __x
)
287 __profcxx_map2umap_insert(this->_M_map2umap_info
, this->size(), 1);
288 std::pair
<_Base_iterator
, bool> __base_ret
= _Base::insert(__x
);
289 return std::make_pair(iterator(__base_ret
.first
, this),
293 #if __cplusplus >= 201103L
294 template<typename _Pair
, typename
= typename
295 std::enable_if
<std::is_constructible
<value_type
,
296 _Pair
&&>::value
>::type
>
297 std::pair
<iterator
, bool>
300 __profcxx_map2umap_insert(this->_M_map2umap_info
, this->size(), 1);
301 auto __base_ret
= _Base::insert(std::forward
<_Pair
>(__x
));
302 return std::make_pair(iterator(__base_ret
.first
, this),
307 #if __cplusplus >= 201103L
309 insert(std::initializer_list
<value_type
> __list
)
310 { insert(__list
.begin(), __list
.end()); }
314 #if __cplusplus >= 201103L
315 insert(const_iterator __pos
, const value_type
& __x
)
317 insert(iterator __pos
, const value_type
& __x
)
320 size_type size_before
= this->size();
321 _Base_iterator __res
= _Base::insert(__pos
.base(), __x
);
323 __profcxx_map2umap_insert(this->_M_map2umap_info
,
324 size_before
, _M_hint_used(__pos
.base(), __res
) ? 0 : 1);
325 return iterator(__res
, this);
328 #if __cplusplus >= 201103L
329 template<typename _Pair
, typename
= typename
330 std::enable_if
<std::is_constructible
<value_type
,
331 _Pair
&&>::value
>::type
>
333 insert(const_iterator __pos
, _Pair
&& __x
)
335 size_type size_before
= this->size();
336 auto __res
= _Base::insert(__pos
.base(), std::forward
<_Pair
>(__x
));
338 __profcxx_map2umap_insert(this->_M_map2umap_info
,
339 size_before
, _M_hint_used(__pos
.base(), __res
) ? 0 : 1);
340 return iterator(__res
, this);
344 template<typename _InputIterator
>
346 insert(_InputIterator __first
, _InputIterator __last
)
348 for (; __first
!= __last
; ++__first
)
352 #if __cplusplus >= 201103L
354 erase(const_iterator __pos
)
356 __profcxx_map2umap_erase(this->_M_map2umap_info
, this->size(), 1);
357 return iterator(_Base::erase(__pos
.base()), this);
361 erase(iterator __pos
)
363 __profcxx_map2umap_erase(this->_M_map2umap_info
, this->size(), 1);
364 return iterator(_Base::erase(__pos
.base()), this);
368 erase(iterator __pos
)
370 __profcxx_map2umap_erase(this->_M_map2umap_info
, this->size(), 1);
371 _Base::erase(__pos
.base());
376 erase(const key_type
& __x
)
378 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
379 __profcxx_map2umap_erase(this->_M_map2umap_info
, this->size(), 1);
380 return _Base::erase(__x
);
383 #if __cplusplus >= 201103L
385 erase(const_iterator __first
, const_iterator __last
)
387 if (__first
!= __last
)
390 for (; __first
!= __last
;)
391 __ret
= erase(__first
++);
395 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
399 erase(iterator __first
, iterator __last
)
401 for (; __first
!= __last
;)
408 #if __cplusplus >= 201103L
409 noexcept( noexcept(declval
<_Base
>().swap(__x
)) )
417 clear() _GLIBCXX_NOEXCEPT
419 this->_M_profile_destruct();
421 this->_M_profile_construct();
424 // 23.3.1.3 map operations:
426 find(const key_type
& __x
)
428 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
429 return iterator(_Base::find(__x
), this);
433 find(const key_type
& __x
) const
435 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
436 return const_iterator(_Base::find(__x
), this);
440 count(const key_type
& __x
) const
442 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
443 return _Base::count(__x
);
447 lower_bound(const key_type
& __x
)
449 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
450 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
451 return iterator(_Base::lower_bound(__x
), this);
455 lower_bound(const key_type
& __x
) const
457 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
458 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
459 return const_iterator(_Base::lower_bound(__x
), this);
463 upper_bound(const key_type
& __x
)
465 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
466 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
467 return iterator(_Base::upper_bound(__x
), this);
471 upper_bound(const key_type
& __x
) const
473 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
474 __profcxx_map2umap_invalidate(this->_M_map2umap_info
);
475 return const_iterator(_Base::upper_bound(__x
), this);
478 std::pair
<iterator
,iterator
>
479 equal_range(const key_type
& __x
)
481 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
482 std::pair
<_Base_iterator
, _Base_iterator
> __base_ret
483 = _Base::equal_range(__x
);
484 return std::make_pair(iterator(__base_ret
.first
, this),
485 iterator(__base_ret
.second
, this));
488 std::pair
<const_iterator
,const_iterator
>
489 equal_range(const key_type
& __x
) const
491 __profcxx_map2umap_find(this->_M_map2umap_info
, this->size());
492 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __base_ret
493 = _Base::equal_range(__x
);
494 return std::make_pair(const_iterator(__base_ret
.first
, this),
495 const_iterator(__base_ret
.second
, this));
499 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
502 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
505 /** If hint is used we consider that the map and unordered_map
506 * operations have equivalent insertion cost so we do not update metrics
508 * Note that to find out if hint has been used is libstdc++
509 * implementation dependent.
512 _M_hint_used(_Base_const_iterator __hint
, _Base_iterator __res
)
514 return (__hint
== __res
515 || (__hint
== _M_base().end() && ++__res
== _M_base().end())
516 || (__hint
!= _M_base().end() && (++__hint
== __res
517 || ++__res
== --__hint
)));
521 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
523 operator==(const map
<_K1
, _T1
, _C1
, _A1
>&,
524 const map
<_K1
, _T1
, _C1
, _A1
>&);
526 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
528 operator<(const map
<_K1
, _T1
, _C1
, _A1
>&,
529 const map
<_K1
, _T1
, _C1
, _A1
>&);
532 template<typename _Key
, typename _Tp
,
533 typename _Compare
, typename _Allocator
>
535 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
536 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
538 __profcxx_map2umap_invalidate(__lhs
._M_map2umap_info
);
539 __profcxx_map2umap_invalidate(__rhs
._M_map2umap_info
);
540 return __lhs
._M_base() == __rhs
._M_base();
543 template<typename _Key
, typename _Tp
,
544 typename _Compare
, typename _Allocator
>
546 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
547 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
549 __profcxx_map2umap_invalidate(__lhs
._M_map2umap_info
);
550 __profcxx_map2umap_invalidate(__rhs
._M_map2umap_info
);
551 return __lhs
._M_base() < __rhs
._M_base();
554 template<typename _Key
, typename _Tp
,
555 typename _Compare
, typename _Allocator
>
557 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
558 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
559 { return !(__lhs
== __rhs
); }
561 template<typename _Key
, typename _Tp
,
562 typename _Compare
, typename _Allocator
>
564 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
565 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
566 { return !(__rhs
< __lhs
); }
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
)
573 { return !(__lhs
< __rhs
); }
575 template<typename _Key
, typename _Tp
,
576 typename _Compare
, typename _Allocator
>
578 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
579 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
580 { return __rhs
< __lhs
; }
582 template<typename _Key
, typename _Tp
,
583 typename _Compare
, typename _Allocator
>
585 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
586 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
587 { __lhs
.swap(__rhs
); }
589 } // namespace __profile