libstdc++
profile/map.h
Go to the documentation of this file.
00001 // Profiling map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License along
00021 // with this library; see the file COPYING3.  If not see
00022 // <http://www.gnu.org/licenses/>.
00023 
00024 /** @file profile/map.h
00025  *  This file is a GNU profile extension to the Standard C++ Library.
00026  */
00027 
00028 #ifndef _GLIBCXX_PROFILE_MAP_H
00029 #define _GLIBCXX_PROFILE_MAP_H 1
00030 
00031 #include <utility>
00032 #include <profile/base.h>
00033 
00034 namespace std _GLIBCXX_VISIBILITY(default)
00035 {
00036 namespace __profile
00037 {
00038   /// Class std::map wrapper with performance instrumentation.
00039   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00040        typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
00041     class map
00042     : public _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator>
00043     {
00044       typedef _GLIBCXX_STD_C::map<_Key, _Tp, _Compare, _Allocator> _Base;
00045 
00046     public:
00047       // types:
00048       typedef _Key                                  key_type;
00049       typedef _Tp                                   mapped_type;
00050       typedef std::pair<const _Key, _Tp>            value_type;
00051       typedef _Compare                              key_compare;
00052       typedef _Allocator                            allocator_type;
00053       typedef typename _Base::reference             reference;
00054       typedef typename _Base::const_reference       const_reference;
00055 
00056       typedef typename _Base::iterator       iterator;
00057       typedef typename _Base::const_iterator       const_iterator;
00058       typedef typename _Base::size_type             size_type;
00059       typedef typename _Base::difference_type       difference_type;
00060       typedef typename _Base::pointer               pointer;
00061       typedef typename _Base::const_pointer         const_pointer;
00062       typedef std::reverse_iterator<iterator>       reverse_iterator;
00063       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00064 
00065       // 23.3.1.1 construct/copy/destroy:
00066       explicit
00067       map(const _Compare& __comp = _Compare(),
00068       const _Allocator& __a = _Allocator())
00069       : _Base(__comp, __a)
00070       { __profcxx_map_to_unordered_map_construct(this); }
00071 
00072       template<typename _InputIterator>
00073         map(_InputIterator __first, _InputIterator __last,
00074         const _Compare& __comp = _Compare(),
00075         const _Allocator& __a = _Allocator())
00076     : _Base(__first, __last, __comp, __a)
00077         { __profcxx_map_to_unordered_map_construct(this); }
00078 
00079       map(const map& __x)
00080       : _Base(__x)
00081       { __profcxx_map_to_unordered_map_construct(this); }
00082 
00083       map(const _Base& __x)
00084       : _Base(__x)
00085       { __profcxx_map_to_unordered_map_construct(this); }
00086 
00087 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00088       map(map&& __x)
00089       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00090       : _Base(std::move(__x))
00091       { }
00092 
00093       map(initializer_list<value_type> __l,
00094       const _Compare& __c = _Compare(),
00095       const allocator_type& __a = allocator_type())
00096       : _Base(__l, __c, __a) { }
00097 #endif
00098 
00099       ~map() _GLIBCXX_NOEXCEPT
00100       { __profcxx_map_to_unordered_map_destruct(this); }
00101 
00102       map&
00103       operator=(const map& __x)
00104       {
00105     *static_cast<_Base*>(this) = __x;
00106     return *this;
00107       }
00108 
00109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00110       map&
00111       operator=(map&& __x)
00112       {
00113     // NB: DR 1204.
00114     // NB: DR 675.
00115     this->clear();
00116     this->swap(__x);
00117     return *this;
00118       }
00119 
00120       map&
00121       operator=(initializer_list<value_type> __l)
00122       {
00123     this->clear();
00124     this->insert(__l);
00125     return *this;
00126       }
00127 #endif
00128 
00129       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00130       // 133. map missing get_allocator()
00131       using _Base::get_allocator;
00132 
00133       // iterators:
00134       iterator 
00135       begin() _GLIBCXX_NOEXCEPT
00136       { return _Base::begin(); }
00137 
00138       const_iterator
00139       begin() const _GLIBCXX_NOEXCEPT
00140       { return _Base::begin(); }
00141 
00142       iterator
00143       end() _GLIBCXX_NOEXCEPT
00144       { return _Base::end(); }
00145 
00146       const_iterator
00147       end() const _GLIBCXX_NOEXCEPT
00148       { return _Base::end(); }
00149 
00150       reverse_iterator
00151       rbegin() _GLIBCXX_NOEXCEPT
00152       { 
00153         __profcxx_map_to_unordered_map_invalidate(this);
00154         return reverse_iterator(end()); 
00155       }
00156 
00157       const_reverse_iterator
00158       rbegin() const _GLIBCXX_NOEXCEPT
00159       {
00160         __profcxx_map_to_unordered_map_invalidate(this);
00161         return const_reverse_iterator(end());
00162       }
00163 
00164       reverse_iterator
00165       rend() _GLIBCXX_NOEXCEPT
00166       {
00167         __profcxx_map_to_unordered_map_invalidate(this);
00168         return reverse_iterator(begin());
00169       }
00170 
00171       const_reverse_iterator
00172       rend() const _GLIBCXX_NOEXCEPT
00173       {
00174         __profcxx_map_to_unordered_map_invalidate(this);
00175         return const_reverse_iterator(begin());
00176       }
00177 
00178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00179       const_iterator
00180       cbegin() const noexcept
00181       { return const_iterator(_Base::begin()); }
00182 
00183       const_iterator
00184       cend() const noexcept
00185       { return const_iterator(_Base::end()); }
00186 
00187       const_reverse_iterator
00188       crbegin() const noexcept
00189       {
00190         __profcxx_map_to_unordered_map_invalidate(this);
00191         return const_reverse_iterator(end());
00192       }
00193 
00194       const_reverse_iterator
00195       crend() const noexcept
00196       {
00197         __profcxx_map_to_unordered_map_invalidate(this);
00198         return const_reverse_iterator(begin());
00199       }
00200 #endif
00201 
00202       // capacity:
00203       using _Base::empty;
00204       using _Base::size;
00205       using _Base::max_size;
00206 
00207       // 23.3.1.2 element access:
00208       mapped_type&
00209       operator[](const key_type& __k)
00210       {
00211         __profcxx_map_to_unordered_map_find(this, size());
00212         return _Base::operator[](__k);
00213       }
00214 
00215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00216       mapped_type&
00217       operator[](key_type&& __k)
00218       {
00219         __profcxx_map_to_unordered_map_find(this, size());
00220         return _Base::operator[](std::move(__k));
00221       }
00222 #endif
00223 
00224       mapped_type&
00225       at(const key_type& __k)
00226       {
00227         __profcxx_map_to_unordered_map_find(this, size());
00228         return _Base::at(__k);
00229       }
00230 
00231       const mapped_type&
00232       at(const key_type& __k) const
00233       {
00234         __profcxx_map_to_unordered_map_find(this, size());
00235         return _Base::at(__k);
00236       }
00237 
00238       // modifiers:
00239       std::pair<iterator, bool>
00240       insert(const value_type& __x)
00241       {
00242         __profcxx_map_to_unordered_map_insert(this, size(), 1);
00243     typedef typename _Base::iterator _Base_iterator;
00244     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00245     return std::pair<iterator, bool>(iterator(__res.first),
00246                      __res.second);
00247       }
00248 
00249 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00250       template<typename _Pair, typename = typename
00251            std::enable_if<std::is_convertible<_Pair,
00252                           value_type>::value>::type>
00253         std::pair<iterator, bool>
00254         insert(_Pair&& __x)
00255         {
00256       __profcxx_map_to_unordered_map_insert(this, size(), 1);
00257       typedef typename _Base::iterator _Base_iterator;
00258       std::pair<_Base_iterator, bool> __res
00259         = _Base::insert(std::forward<_Pair>(__x));
00260       return std::pair<iterator, bool>(iterator(__res.first),
00261                        __res.second);
00262     }
00263 #endif
00264 
00265 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00266       void
00267       insert(std::initializer_list<value_type> __list)
00268       { 
00269         size_type size_before = size();
00270         _Base::insert(__list); 
00271         __profcxx_map_to_unordered_map_insert(this, size_before, 
00272                           size() - size_before);
00273       }
00274 #endif
00275 
00276       iterator
00277 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00278       insert(const_iterator __position, const value_type& __x)
00279 #else
00280       insert(iterator __position, const value_type& __x)
00281 #endif
00282       {
00283         size_type size_before = size();
00284     iterator __i = iterator(_Base::insert(__position, __x));
00285         __profcxx_map_to_unordered_map_insert(this, size_before, 
00286                           size() - size_before);
00287     return __i;
00288       }
00289 
00290 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00291       template<typename _Pair, typename = typename
00292            std::enable_if<std::is_convertible<_Pair,
00293                           value_type>::value>::type>
00294         iterator
00295         insert(const_iterator __position, _Pair&& __x)
00296         {
00297       size_type size_before = size();
00298       iterator __i
00299         = iterator(_Base::insert(__position, std::forward<_Pair>(__x)));
00300       __profcxx_map_to_unordered_map_insert(this, size_before, 
00301                         size() - size_before);
00302       return __i;
00303       }
00304 #endif
00305 
00306       template<typename _InputIterator>
00307         void
00308         insert(_InputIterator __first, _InputIterator __last)
00309         {
00310           size_type size_before = size();
00311       _Base::insert(__first, __last);
00312           __profcxx_map_to_unordered_map_insert(this, size_before, 
00313                                                 size() - size_before);
00314     }
00315 
00316 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00317       iterator
00318       erase(const_iterator __position)
00319       {
00320     iterator __i = _Base::erase(__position);
00321         __profcxx_map_to_unordered_map_erase(this, size(), 1);
00322         return __i;
00323       }
00324 
00325       iterator
00326       erase(iterator __position)
00327       { return erase(const_iterator(__position)); }
00328 #else
00329       void
00330       erase(iterator __position)
00331       {
00332     _Base::erase(__position);
00333         __profcxx_map_to_unordered_map_erase(this, size(), 1);
00334       }
00335 #endif
00336 
00337       size_type
00338       erase(const key_type& __x)
00339       {
00340     iterator __victim = find(__x);
00341     if (__victim == end())
00342       return 0;
00343     else
00344     {
00345       _Base::erase(__victim);
00346       return 1;
00347     }
00348       }
00349 
00350 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00351       iterator
00352       erase(const_iterator __first, const_iterator __last)
00353       { return iterator(_Base::erase(__first, __last)); }
00354 #else
00355       void
00356       erase(iterator __first, iterator __last)
00357       { _Base::erase(__first, __last); }
00358 #endif
00359 
00360       void
00361       swap(map& __x)
00362       { _Base::swap(__x); }
00363 
00364       void
00365       clear() _GLIBCXX_NOEXCEPT
00366       { this->erase(begin(), end()); }
00367 
00368       // observers:
00369       using _Base::key_comp;
00370       using _Base::value_comp;
00371 
00372       // 23.3.1.3 map operations:
00373       iterator
00374       find(const key_type& __x)
00375       {
00376         __profcxx_map_to_unordered_map_find(this, size());
00377         return iterator(_Base::find(__x));
00378       }
00379 
00380       const_iterator
00381       find(const key_type& __x) const
00382       {
00383         __profcxx_map_to_unordered_map_find(this, size());
00384         return const_iterator(_Base::find(__x));
00385       }
00386 
00387       size_type
00388       count(const key_type& __x) const
00389       {
00390         __profcxx_map_to_unordered_map_find(this, size());
00391         return _Base::count(__x);
00392       }
00393 
00394       iterator
00395       lower_bound(const key_type& __x)
00396       { 
00397         __profcxx_map_to_unordered_map_invalidate(this);
00398         return iterator(_Base::lower_bound(__x)); 
00399       }
00400 
00401       const_iterator
00402       lower_bound(const key_type& __x) const
00403       { 
00404         __profcxx_map_to_unordered_map_invalidate(this);
00405         return const_iterator(_Base::lower_bound(__x)); 
00406       }
00407 
00408       iterator
00409       upper_bound(const key_type& __x)
00410       { 
00411         __profcxx_map_to_unordered_map_invalidate(this);
00412         return iterator(_Base::upper_bound(__x)); 
00413       }
00414 
00415       const_iterator
00416       upper_bound(const key_type& __x) const
00417       { 
00418         __profcxx_map_to_unordered_map_invalidate(this);
00419         return const_iterator(_Base::upper_bound(__x)); 
00420       }
00421 
00422       std::pair<iterator,iterator>
00423       equal_range(const key_type& __x)
00424       {
00425     typedef typename _Base::iterator _Base_iterator;
00426     std::pair<_Base_iterator, _Base_iterator> __res =
00427     _Base::equal_range(__x);
00428     return std::make_pair(iterator(__res.first),
00429                   iterator(__res.second));
00430       }
00431 
00432       std::pair<const_iterator,const_iterator>
00433       equal_range(const key_type& __x) const
00434       {
00435         __profcxx_map_to_unordered_map_find(this, size());
00436     typedef typename _Base::const_iterator _Base_const_iterator;
00437     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00438     _Base::equal_range(__x);
00439     return std::make_pair(const_iterator(__res.first),
00440                   const_iterator(__res.second));
00441       }
00442 
00443       _Base& 
00444       _M_base() _GLIBCXX_NOEXCEPT       { return *this; }
00445 
00446       const _Base&
00447       _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
00448 
00449     };
00450 
00451   template<typename _Key, typename _Tp,
00452        typename _Compare, typename _Allocator>
00453     inline bool
00454     operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00455            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00456     { 
00457       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00458       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00459       return __lhs._M_base() == __rhs._M_base(); 
00460     }
00461 
00462   template<typename _Key, typename _Tp,
00463        typename _Compare, typename _Allocator>
00464     inline bool
00465     operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00466            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00467     { 
00468       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00469       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00470       return __lhs._M_base() != __rhs._M_base(); 
00471     }
00472 
00473   template<typename _Key, typename _Tp,
00474        typename _Compare, typename _Allocator>
00475     inline bool
00476     operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00477           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00478     {
00479       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00480       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00481       return __lhs._M_base() < __rhs._M_base(); 
00482     }
00483 
00484   template<typename _Key, typename _Tp,
00485        typename _Compare, typename _Allocator>
00486     inline bool
00487     operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00488            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00489     {
00490       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00491       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00492       return __lhs._M_base() <= __rhs._M_base();
00493     }
00494 
00495   template<typename _Key, typename _Tp,
00496        typename _Compare, typename _Allocator>
00497     inline bool
00498     operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00499            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00500     {
00501       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00502       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00503       return __lhs._M_base() >= __rhs._M_base();
00504     }
00505 
00506   template<typename _Key, typename _Tp,
00507        typename _Compare, typename _Allocator>
00508     inline bool
00509     operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00510           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00511     {
00512       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00513       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00514       return __lhs._M_base() > __rhs._M_base();
00515     }
00516 
00517   template<typename _Key, typename _Tp,
00518        typename _Compare, typename _Allocator>
00519     inline void
00520     swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00521      map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00522     { __lhs.swap(__rhs); }
00523 
00524 } // namespace __profile
00525 } // namespace std
00526 
00527 #endif