1 // Profiling map implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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 // 23.3.1.1 construct/copy/destroy:
73 map(const _Compare
& __comp
= _Compare(),
74 const _Allocator
& __a
= _Allocator())
76 { __profcxx_map_to_unordered_map_construct(this); }
78 template<typename _InputIterator
>
79 map(_InputIterator __first
, _InputIterator __last
,
80 const _Compare
& __comp
= _Compare(),
81 const _Allocator
& __a
= _Allocator())
82 : _Base(__first
, __last
, __comp
, __a
)
83 { __profcxx_map_to_unordered_map_construct(this); }
87 { __profcxx_map_to_unordered_map_construct(this); }
91 { __profcxx_map_to_unordered_map_construct(this); }
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95 : _Base(std::move(__x
))
98 map(initializer_list
<value_type
> __l
,
99 const _Compare
& __c
= _Compare(),
100 const allocator_type
& __a
= allocator_type())
101 : _Base(__l
, __c
, __a
) { }
105 { __profcxx_map_to_unordered_map_destruct(this); }
108 operator=(const map
& __x
)
110 *static_cast<_Base
*>(this) = __x
;
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
126 operator=(initializer_list
<value_type
> __l
)
134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
135 // 133. map missing get_allocator()
136 using _Base::get_allocator
;
140 begin() _GLIBCXX_NOEXCEPT
141 { return _Base::begin(); }
144 begin() const _GLIBCXX_NOEXCEPT
145 { return _Base::begin(); }
148 end() _GLIBCXX_NOEXCEPT
149 { return _Base::end(); }
152 end() const _GLIBCXX_NOEXCEPT
153 { return _Base::end(); }
156 rbegin() _GLIBCXX_NOEXCEPT
158 __profcxx_map_to_unordered_map_invalidate(this);
159 return reverse_iterator(end());
162 const_reverse_iterator
163 rbegin() const _GLIBCXX_NOEXCEPT
165 __profcxx_map_to_unordered_map_invalidate(this);
166 return const_reverse_iterator(end());
170 rend() _GLIBCXX_NOEXCEPT
172 __profcxx_map_to_unordered_map_invalidate(this);
173 return reverse_iterator(begin());
176 const_reverse_iterator
177 rend() const _GLIBCXX_NOEXCEPT
179 __profcxx_map_to_unordered_map_invalidate(this);
180 return const_reverse_iterator(begin());
183 #ifdef __GXX_EXPERIMENTAL_CXX0X__
185 cbegin() const noexcept
186 { return const_iterator(_Base::begin()); }
189 cend() const noexcept
190 { return const_iterator(_Base::end()); }
192 const_reverse_iterator
193 crbegin() const noexcept
195 __profcxx_map_to_unordered_map_invalidate(this);
196 return const_reverse_iterator(end());
199 const_reverse_iterator
200 crend() const noexcept
202 __profcxx_map_to_unordered_map_invalidate(this);
203 return const_reverse_iterator(begin());
210 using _Base::max_size
;
212 // 23.3.1.2 element access:
214 operator[](const key_type
& __k
)
216 __profcxx_map_to_unordered_map_find(this, size());
217 return _Base::operator[](__k
);
220 #ifdef __GXX_EXPERIMENTAL_CXX0X__
222 operator[](key_type
&& __k
)
224 __profcxx_map_to_unordered_map_find(this, size());
225 return _Base::operator[](std::move(__k
));
230 at(const key_type
& __k
)
232 __profcxx_map_to_unordered_map_find(this, size());
233 return _Base::at(__k
);
237 at(const key_type
& __k
) const
239 __profcxx_map_to_unordered_map_find(this, size());
240 return _Base::at(__k
);
244 std::pair
<iterator
, bool>
245 insert(const value_type
& __x
)
247 __profcxx_map_to_unordered_map_insert(this, size(), 1);
248 typedef typename
_Base::iterator _Base_iterator
;
249 std::pair
<_Base_iterator
, bool> __res
= _Base::insert(__x
);
250 return std::pair
<iterator
, bool>(iterator(__res
.first
),
254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
255 template<typename _Pair
, typename
= typename
256 std::enable_if
<std::is_convertible
<_Pair
,
257 value_type
>::value
>::type
>
258 std::pair
<iterator
, bool>
261 __profcxx_map_to_unordered_map_insert(this, size(), 1);
262 typedef typename
_Base::iterator _Base_iterator
;
263 std::pair
<_Base_iterator
, bool> __res
264 = _Base::insert(std::forward
<_Pair
>(__x
));
265 return std::pair
<iterator
, bool>(iterator(__res
.first
),
270 #ifdef __GXX_EXPERIMENTAL_CXX0X__
272 insert(std::initializer_list
<value_type
> __list
)
274 size_type size_before
= size();
275 _Base::insert(__list
);
276 __profcxx_map_to_unordered_map_insert(this, size_before
,
277 size() - size_before
);
282 #ifdef __GXX_EXPERIMENTAL_CXX0X__
283 insert(const_iterator __position
, const value_type
& __x
)
285 insert(iterator __position
, const value_type
& __x
)
288 size_type size_before
= size();
289 iterator __i
= iterator(_Base::insert(__position
, __x
));
290 __profcxx_map_to_unordered_map_insert(this, size_before
,
291 size() - size_before
);
295 #ifdef __GXX_EXPERIMENTAL_CXX0X__
296 template<typename _Pair
, typename
= typename
297 std::enable_if
<std::is_convertible
<_Pair
,
298 value_type
>::value
>::type
>
300 insert(const_iterator __position
, _Pair
&& __x
)
302 size_type size_before
= size();
304 = iterator(_Base::insert(__position
, std::forward
<_Pair
>(__x
)));
305 __profcxx_map_to_unordered_map_insert(this, size_before
,
306 size() - size_before
);
311 template<typename _InputIterator
>
313 insert(_InputIterator __first
, _InputIterator __last
)
315 size_type size_before
= size();
316 _Base::insert(__first
, __last
);
317 __profcxx_map_to_unordered_map_insert(this, size_before
,
318 size() - size_before
);
321 #ifdef __GXX_EXPERIMENTAL_CXX0X__
323 erase(const_iterator __position
)
325 iterator __i
= _Base::erase(__position
);
326 __profcxx_map_to_unordered_map_erase(this, size(), 1);
331 erase(iterator __position
)
333 _Base::erase(__position
);
334 __profcxx_map_to_unordered_map_erase(this, size(), 1);
339 erase(const key_type
& __x
)
341 iterator __victim
= find(__x
);
342 if (__victim
== end())
346 _Base::erase(__victim
);
351 #ifdef __GXX_EXPERIMENTAL_CXX0X__
353 erase(const_iterator __first
, const_iterator __last
)
354 { return iterator(_Base::erase(__first
, __last
)); }
357 erase(iterator __first
, iterator __last
)
358 { _Base::erase(__first
, __last
); }
363 { _Base::swap(__x
); }
366 clear() _GLIBCXX_NOEXCEPT
367 { this->erase(begin(), end()); }
370 using _Base::key_comp
;
371 using _Base::value_comp
;
373 // 23.3.1.3 map operations:
375 find(const key_type
& __x
)
377 __profcxx_map_to_unordered_map_find(this, size());
378 return iterator(_Base::find(__x
));
382 find(const key_type
& __x
) const
384 __profcxx_map_to_unordered_map_find(this, size());
385 return const_iterator(_Base::find(__x
));
389 count(const key_type
& __x
) const
391 __profcxx_map_to_unordered_map_find(this, size());
392 return _Base::count(__x
);
396 lower_bound(const key_type
& __x
)
398 __profcxx_map_to_unordered_map_invalidate(this);
399 return iterator(_Base::lower_bound(__x
));
403 lower_bound(const key_type
& __x
) const
405 __profcxx_map_to_unordered_map_invalidate(this);
406 return const_iterator(_Base::lower_bound(__x
));
410 upper_bound(const key_type
& __x
)
412 __profcxx_map_to_unordered_map_invalidate(this);
413 return iterator(_Base::upper_bound(__x
));
417 upper_bound(const key_type
& __x
) const
419 __profcxx_map_to_unordered_map_invalidate(this);
420 return const_iterator(_Base::upper_bound(__x
));
423 std::pair
<iterator
,iterator
>
424 equal_range(const key_type
& __x
)
426 typedef typename
_Base::iterator _Base_iterator
;
427 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
428 _Base::equal_range(__x
);
429 return std::make_pair(iterator(__res
.first
),
430 iterator(__res
.second
));
433 std::pair
<const_iterator
,const_iterator
>
434 equal_range(const key_type
& __x
) const
436 __profcxx_map_to_unordered_map_find(this, size());
437 typedef typename
_Base::const_iterator _Base_const_iterator
;
438 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
439 _Base::equal_range(__x
);
440 return std::make_pair(const_iterator(__res
.first
),
441 const_iterator(__res
.second
));
445 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
448 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
452 template<typename _Key
, typename _Tp
,
453 typename _Compare
, typename _Allocator
>
455 operator==(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
456 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
458 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
459 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
460 return __lhs
._M_base() == __rhs
._M_base();
463 template<typename _Key
, typename _Tp
,
464 typename _Compare
, typename _Allocator
>
466 operator!=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
467 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
469 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
470 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
471 return __lhs
._M_base() != __rhs
._M_base();
474 template<typename _Key
, typename _Tp
,
475 typename _Compare
, typename _Allocator
>
477 operator<(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
478 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
480 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
481 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
482 return __lhs
._M_base() < __rhs
._M_base();
485 template<typename _Key
, typename _Tp
,
486 typename _Compare
, typename _Allocator
>
488 operator<=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
489 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
491 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
492 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
493 return __lhs
._M_base() <= __rhs
._M_base();
496 template<typename _Key
, typename _Tp
,
497 typename _Compare
, typename _Allocator
>
499 operator>=(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
500 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
502 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
503 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
504 return __lhs
._M_base() >= __rhs
._M_base();
507 template<typename _Key
, typename _Tp
,
508 typename _Compare
, typename _Allocator
>
510 operator>(const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
511 const map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
513 __profcxx_map_to_unordered_map_invalidate(&__lhs
);
514 __profcxx_map_to_unordered_map_invalidate(&__rhs
);
515 return __lhs
._M_base() > __rhs
._M_base();
518 template<typename _Key
, typename _Tp
,
519 typename _Compare
, typename _Allocator
>
521 swap(map
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
522 map
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
523 { __lhs
.swap(__rhs
); }
525 } // namespace __profile