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:
73 { __profcxx_map_to_unordered_map_construct(this); }
76 map(const _Compare
& __comp
,
77 const _Allocator
& __a
= _Allocator())
79 { __profcxx_map_to_unordered_map_construct(this); }
81 #if __cplusplus >= 201103L
82 template<typename _InputIterator
,
83 typename
= std::_RequireInputIter
<_InputIterator
>>
85 template<typename _InputIterator
>
87 map(_InputIterator __first
, _InputIterator __last
,
88 const _Compare
& __comp
= _Compare(),
89 const _Allocator
& __a
= _Allocator())
90 : _Base(__first
, __last
, __comp
, __a
)
91 { __profcxx_map_to_unordered_map_construct(this); }
95 { __profcxx_map_to_unordered_map_construct(this); }
99 { __profcxx_map_to_unordered_map_construct(this); }
101 #if __cplusplus >= 201103L
103 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
)
104 : _Base(std::move(__x
))
105 { __profcxx_map_to_unordered_map_construct(this); }
107 map(initializer_list
<value_type
> __l
,
108 const _Compare
& __c
= _Compare(),
109 const allocator_type
& __a
= allocator_type())
110 : _Base(__l
, __c
, __a
)
111 { __profcxx_map_to_unordered_map_construct(this); }
114 map(const allocator_type
& __a
)
116 { __profcxx_map_to_unordered_map_construct(this); }
118 map(const map
& __x
, const allocator_type
& __a
)
120 { __profcxx_map_to_unordered_map_construct(this); }
122 map(map
&& __x
, const allocator_type
& __a
)
123 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
124 && _Alloc_traits::_S_always_equal())
125 : _Base(std::move(__x
), __a
)
126 { __profcxx_map_to_unordered_map_construct(this); }
128 map(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
130 { __profcxx_map_to_unordered_map_construct(this); }
132 template<typename _InputIterator
>
133 map(_InputIterator __first
, _InputIterator __last
,
134 const allocator_type
& __a
)
135 : _Base(__first
, __last
, __a
)
136 { __profcxx_map_to_unordered_map_construct(this); }
139 ~map() _GLIBCXX_NOEXCEPT
140 { __profcxx_map_to_unordered_map_destruct(this); }
142 #if __cplusplus < 201103L
144 operator=(const map
& __x
)
151 operator=(const map
&) = default;
154 operator=(map
&&) = default;
157 operator=(initializer_list
<value_type
> __l
)
164 // _GLIBCXX_RESOLVE_LIB_DEFECTS
165 // 133. map missing get_allocator()
166 using _Base::get_allocator
;
170 begin() _GLIBCXX_NOEXCEPT
171 { return _Base::begin(); }
174 begin() const _GLIBCXX_NOEXCEPT
175 { return _Base::begin(); }
178 end() _GLIBCXX_NOEXCEPT
179 { return _Base::end(); }
182 end() const _GLIBCXX_NOEXCEPT
183 { return _Base::end(); }
186 rbegin() _GLIBCXX_NOEXCEPT
188 __profcxx_map_to_unordered_map_invalidate(this);
189 return reverse_iterator(end());
192 const_reverse_iterator
193 rbegin() const _GLIBCXX_NOEXCEPT
195 __profcxx_map_to_unordered_map_invalidate(this);
196 return const_reverse_iterator(end());
200 rend() _GLIBCXX_NOEXCEPT
202 __profcxx_map_to_unordered_map_invalidate(this);
203 return reverse_iterator(begin());
206 const_reverse_iterator
207 rend() const _GLIBCXX_NOEXCEPT
209 __profcxx_map_to_unordered_map_invalidate(this);
210 return const_reverse_iterator(begin());
213 #if __cplusplus >= 201103L
215 cbegin() const noexcept
216 { return const_iterator(_Base::begin()); }
219 cend() const noexcept
220 { return const_iterator(_Base::end()); }
222 const_reverse_iterator
223 crbegin() const noexcept
225 __profcxx_map_to_unordered_map_invalidate(this);
226 return const_reverse_iterator(end());
229 const_reverse_iterator
230 crend() const noexcept
232 __profcxx_map_to_unordered_map_invalidate(this);
233 return const_reverse_iterator(begin());
240 using _Base::max_size
;
242 // 23.3.1.2 element access:
244 operator[](const key_type
& __k
)
246 __profcxx_map_to_unordered_map_find(this, size());
247 return _Base::operator[](__k
);
250 #if __cplusplus >= 201103L
252 operator[](key_type
&& __k
)
254 __profcxx_map_to_unordered_map_find(this, size());
255 return _Base::operator[](std::move(__k
));
260 at(const key_type
& __k
)
262 __profcxx_map_to_unordered_map_find(this, size());
263 return _Base::at(__k
);
267 at(const key_type
& __k
) const
269 __profcxx_map_to_unordered_map_find(this, size());
270 return _Base::at(__k
);
274 #if __cplusplus >= 201103L
275 template<typename
... _Args
>
276 std::pair
<iterator
, bool>
277 emplace(_Args
&&... __args
)
279 __profcxx_map_to_unordered_map_insert(this, size(), 1);
280 auto __res
= _Base::emplace(std::forward
<_Args
>(__args
)...);
281 return std::pair
<iterator
, bool>(iterator(__res
.first
),
285 template<typename
... _Args
>
287 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
289 size_type size_before
= size();
290 auto __res
= _Base::emplace_hint(__pos
,
291 std::forward
<_Args
>(__args
)...);
292 __profcxx_map_to_unordered_map_insert(this, size_before
,
293 size() - size_before
);
298 std::pair
<iterator
, bool>
299 insert(const value_type
& __x
)
301 __profcxx_map_to_unordered_map_insert(this, size(), 1);
302 typedef typename
_Base::iterator _Base_iterator
;
303 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
304 return std::pair
<iterator
, bool>(iterator(__res
.first
),
308 #if __cplusplus >= 201103L
309 template<typename _Pair
, typename
= typename
310 std::enable_if
<std::is_constructible
<value_type
,
311 _Pair
&&>::value
>::type
>
312 std::pair
<iterator
, bool>
315 __profcxx_map_to_unordered_map_insert(this, size(), 1);
316 typedef typename
_Base::iterator _Base_iterator
;
317 std::pair
<_Base_iterator
, bool> __res
318 = _Base::insert(std::forward
<_Pair
>(__x
));
319 return std::pair
<iterator
, bool>(iterator(__res
.first
),
324 #if __cplusplus >= 201103L
326 insert(std::initializer_list
<value_type
> __list
)
328 size_type size_before
= size();
329 _Base::insert(__list
);
330 __profcxx_map_to_unordered_map_insert(this, size_before
,
331 size() - size_before
);
336 #if __cplusplus >= 201103L
337 insert(const_iterator __position
, const value_type
& __x
)
339 insert(iterator __position
, const value_type
& __x
)
342 size_type size_before
= size();
343 iterator __i
= iterator(_Base::insert(__position
, __x
));
344 __profcxx_map_to_unordered_map_insert(this, size_before
,
345 size() - size_before
);
349 #if __cplusplus >= 201103L
350 template<typename _Pair
, typename
= typename
351 std::enable_if
<std::is_constructible
<value_type
,
352 _Pair
&&>::value
>::type
>
354 insert(const_iterator __position
, _Pair
&& __x
)
356 size_type size_before
= size();
358 = iterator(_Base::insert(__position
, std::forward
<_Pair
>(__x
)));
359 __profcxx_map_to_unordered_map_insert(this, size_before
,
360 size() - size_before
);
365 #if __cplusplus >= 201103L
366 template<typename _InputIterator
,
367 typename
= std::_RequireInputIter
<_InputIterator
>>
369 template<typename _InputIterator
>
372 insert(_InputIterator __first
, _InputIterator __last
)
374 size_type size_before
= size();
375 _Base::insert(__first
, __last
);
376 __profcxx_map_to_unordered_map_insert(this, size_before
,
377 size() - size_before
);
380 #if __cplusplus >= 201103L
382 erase(const_iterator __position
)
384 iterator __i
= _Base::erase(__position
);
385 __profcxx_map_to_unordered_map_erase(this, size(), 1);
390 erase(iterator __position
)
391 { return erase(const_iterator(__position
)); }
394 erase(iterator __position
)
396 _Base::erase(__position
);
397 __profcxx_map_to_unordered_map_erase(this, size(), 1);
402 erase(const key_type
& __x
)
404 iterator __victim
= find(__x
);
405 if (__victim
== end())
409 _Base::erase(__victim
);
414 #if __cplusplus >= 201103L
416 erase(const_iterator __first
, const_iterator __last
)
417 { return iterator(_Base::erase(__first
, __last
)); }
420 erase(iterator __first
, iterator __last
)
421 { _Base::erase(__first
, __last
); }
426 #if __cplusplus >= 201103L
427 noexcept(_Alloc_traits::_S_nothrow_swap())
429 { _Base::swap(__x
); }
432 clear() _GLIBCXX_NOEXCEPT
433 { this->erase(begin(), end()); }
436 using _Base::key_comp
;
437 using _Base::value_comp
;
439 // 23.3.1.3 map operations:
441 find(const key_type
& __x
)
443 __profcxx_map_to_unordered_map_find(this, size());
444 return iterator(_Base::find(__x
));
448 find(const key_type
& __x
) const
450 __profcxx_map_to_unordered_map_find(this, size());
451 return const_iterator(_Base::find(__x
));
455 count(const key_type
& __x
) const
457 __profcxx_map_to_unordered_map_find(this, size());
458 return _Base::count(__x
);
462 lower_bound(const key_type
& __x
)
464 __profcxx_map_to_unordered_map_invalidate(this);
465 return iterator(_Base::lower_bound(__x
));
469 lower_bound(const key_type
& __x
) const
471 __profcxx_map_to_unordered_map_invalidate(this);
472 return const_iterator(_Base::lower_bound(__x
));
476 upper_bound(const key_type
& __x
)
478 __profcxx_map_to_unordered_map_invalidate(this);
479 return iterator(_Base::upper_bound(__x
));
483 upper_bound(const key_type
& __x
) const
485 __profcxx_map_to_unordered_map_invalidate(this);
486 return const_iterator(_Base::upper_bound(__x
));
489 std::pair
<iterator
,iterator
>
490 equal_range(const key_type
& __x
)
492 typedef typename
_Base::iterator _Base_iterator
;
493 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
494 _Base::equal_range(__x
);
495 return std::make_pair(iterator(__res
.first
),
496 iterator(__res
.second
));
499 std::pair
<const_iterator
,const_iterator
>
500 equal_range(const key_type
& __x
) const
502 __profcxx_map_to_unordered_map_find(this, size());
503 typedef typename
_Base::const_iterator _Base_const_iterator
;
504 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
505 _Base::equal_range(__x
);
506 return std::make_pair(const_iterator(__res
.first
),
507 const_iterator(__res
.second
));
511 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
514 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
518 template<typename _Key
, typename _Tp
,
519 typename _Compare
, typename _Allocator
>
521 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
522 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
524 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
525 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
526 return __lhs
._M_base() == __rhs
._M_base();
529 template<typename _Key
, typename _Tp
,
530 typename _Compare
, typename _Allocator
>
532 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
533 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
535 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
536 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
537 return __lhs
._M_base() != __rhs
._M_base();
540 template<typename _Key
, typename _Tp
,
541 typename _Compare
, typename _Allocator
>
543 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
544 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
546 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
547 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
548 return __lhs
._M_base() < __rhs
._M_base();
551 template<typename _Key
, typename _Tp
,
552 typename _Compare
, typename _Allocator
>
554 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
555 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
557 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
558 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
559 return __lhs
._M_base() <= __rhs
._M_base();
562 template<typename _Key
, typename _Tp
,
563 typename _Compare
, typename _Allocator
>
565 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
566 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
568 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
569 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
570 return __lhs
._M_base() >= __rhs
._M_base();
573 template<typename _Key
, typename _Tp
,
574 typename _Compare
, typename _Allocator
>
576 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
577 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
579 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
580 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
581 return __lhs
._M_base() > __rhs
._M_base();
584 template<typename _Key
, typename _Tp
,
585 typename _Compare
, typename _Allocator
>
587 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
588 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
589 { __lhs
.swap(__rhs
); }
591 } // namespace __profile