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       : _Base(std::move(__x))
00090       { }
00091 
00092       map(initializer_list<value_type> __l,
00093       const _Compare& __c = _Compare(),
00094       const allocator_type& __a = allocator_type())
00095       : _Base(__l, __c, __a) { }
00096 #endif
00097 
00098       ~map()
00099       { __profcxx_map_to_unordered_map_destruct(this); }
00100 
00101       map&
00102       operator=(const map& __x)
00103       {
00104     *static_cast<_Base*>(this) = __x;
00105     return *this;
00106       }
00107 
00108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00109       map&
00110       operator=(map&& __x)
00111       {
00112     // NB: DR 1204.
00113     // NB: DR 675.
00114     this->clear();
00115     this->swap(__x);
00116     return *this;
00117       }
00118 
00119       map&
00120       operator=(initializer_list<value_type> __l)
00121       {
00122     this->clear();
00123     this->insert(__l);
00124     return *this;
00125       }
00126 #endif
00127 
00128       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00129       // 133. map missing get_allocator()
00130       using _Base::get_allocator;
00131 
00132       // iterators:
00133       iterator 
00134       begin()
00135       { return _Base::begin(); }
00136 
00137       const_iterator
00138       begin() const
00139       { return _Base::begin(); }
00140 
00141       iterator
00142       end()
00143       { return _Base::end(); }
00144 
00145       const_iterator
00146       end() const
00147       { return _Base::end(); }
00148 
00149       reverse_iterator
00150       rbegin()
00151       { 
00152         __profcxx_map_to_unordered_map_invalidate(this);
00153         return reverse_iterator(end()); 
00154       }
00155 
00156       const_reverse_iterator
00157       rbegin() const
00158       {
00159         __profcxx_map_to_unordered_map_invalidate(this);
00160         return const_reverse_iterator(end());
00161       }
00162 
00163       reverse_iterator
00164       rend()
00165       {
00166         __profcxx_map_to_unordered_map_invalidate(this);
00167         return reverse_iterator(begin());
00168       }
00169 
00170       const_reverse_iterator
00171       rend() const
00172       {
00173         __profcxx_map_to_unordered_map_invalidate(this);
00174         return const_reverse_iterator(begin());
00175       }
00176 
00177 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00178       const_iterator
00179       cbegin() const
00180       { return const_iterator(_Base::begin()); }
00181 
00182       const_iterator
00183       cend() const
00184       { return const_iterator(_Base::end()); }
00185 
00186       const_reverse_iterator
00187       crbegin() const
00188       {
00189         __profcxx_map_to_unordered_map_invalidate(this);
00190         return const_reverse_iterator(end());
00191       }
00192 
00193       const_reverse_iterator
00194       crend() const
00195       {
00196         __profcxx_map_to_unordered_map_invalidate(this);
00197         return const_reverse_iterator(begin());
00198       }
00199 #endif
00200 
00201       // capacity:
00202       using _Base::empty;
00203       using _Base::size;
00204       using _Base::max_size;
00205 
00206       // 23.3.1.2 element access:
00207       mapped_type&
00208       operator[](const key_type& __k)
00209       {
00210         __profcxx_map_to_unordered_map_find(this, size());
00211         return _Base::operator[](__k);
00212       }
00213 
00214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00215       mapped_type&
00216       operator[](key_type&& __k)
00217       {
00218         __profcxx_map_to_unordered_map_find(this, size());
00219         return _Base::operator[](std::move(__k));
00220       }
00221 #endif
00222 
00223       mapped_type&
00224       at(const key_type& __k)
00225       {
00226         __profcxx_map_to_unordered_map_find(this, size());
00227         return _Base::at(__k);
00228       }
00229 
00230       const mapped_type&
00231       at(const key_type& __k) const
00232       {
00233         __profcxx_map_to_unordered_map_find(this, size());
00234         return _Base::at(__k);
00235       }
00236 
00237       // modifiers:
00238       std::pair<iterator, bool>
00239       insert(const value_type& __x)
00240       {
00241         __profcxx_map_to_unordered_map_insert(this, size(), 1);
00242     typedef typename _Base::iterator _Base_iterator;
00243     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00244     return std::pair<iterator, bool>(iterator(__res.first),
00245                      __res.second);
00246       }
00247 
00248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00249       template<typename _Pair, typename = typename
00250            std::enable_if<std::is_convertible<_Pair,
00251                           value_type>::value>::type>
00252         std::pair<iterator, bool>
00253         insert(_Pair&& __x)
00254         {
00255       __profcxx_map_to_unordered_map_insert(this, size(), 1);
00256       typedef typename _Base::iterator _Base_iterator;
00257       std::pair<_Base_iterator, bool> __res
00258         = _Base::insert(std::forward<_Pair>(__x));
00259       return std::pair<iterator, bool>(iterator(__res.first),
00260                        __res.second);
00261     }
00262 #endif
00263 
00264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00265       void
00266       insert(std::initializer_list<value_type> __list)
00267       { 
00268         size_type size_before = size();
00269         _Base::insert(__list); 
00270         __profcxx_map_to_unordered_map_insert(this, size_before, 
00271                           size() - size_before);
00272       }
00273 #endif
00274 
00275       iterator
00276 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00277       insert(const_iterator __position, const value_type& __x)
00278 #else
00279       insert(iterator __position, const value_type& __x)
00280 #endif
00281       {
00282         size_type size_before = size();
00283     iterator __i = iterator(_Base::insert(__position, __x));
00284         __profcxx_map_to_unordered_map_insert(this, size_before, 
00285                           size() - size_before);
00286     return __i;
00287       }
00288 
00289 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00290       template<typename _Pair, typename = typename
00291            std::enable_if<std::is_convertible<_Pair,
00292                           value_type>::value>::type>
00293         iterator
00294         insert(const_iterator __position, _Pair&& __x)
00295         {
00296       size_type size_before = size();
00297       iterator __i
00298         = iterator(_Base::insert(__position, std::forward<_Pair>(__x)));
00299       __profcxx_map_to_unordered_map_insert(this, size_before, 
00300                         size() - size_before);
00301       return __i;
00302       }
00303 #endif
00304 
00305       template<typename _InputIterator>
00306         void
00307         insert(_InputIterator __first, _InputIterator __last)
00308         {
00309           size_type size_before = size();
00310       _Base::insert(__first, __last);
00311           __profcxx_map_to_unordered_map_insert(this, size_before, 
00312                                                 size() - size_before);
00313     }
00314 
00315 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00316       iterator
00317       erase(const_iterator __position)
00318       {
00319     iterator __i = _Base::erase(__position);
00320         __profcxx_map_to_unordered_map_erase(this, size(), 1);
00321         return __i;
00322       }
00323 
00324       iterator
00325       erase(iterator __position)
00326       { return erase(const_iterator(__position)); }
00327 #else
00328       void
00329       erase(iterator __position)
00330       {
00331     _Base::erase(__position);
00332         __profcxx_map_to_unordered_map_erase(this, size(), 1);
00333       }
00334 #endif
00335 
00336       size_type
00337       erase(const key_type& __x)
00338       {
00339     iterator __victim = find(__x);
00340     if (__victim == end())
00341       return 0;
00342     else
00343     {
00344       _Base::erase(__victim);
00345       return 1;
00346     }
00347       }
00348 
00349 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00350       iterator
00351       erase(const_iterator __first, const_iterator __last)
00352       { return iterator(_Base::erase(__first, __last)); }
00353 #else
00354       void
00355       erase(iterator __first, iterator __last)
00356       { _Base::erase(__first, __last); }
00357 #endif
00358 
00359       void
00360 
00361       swap(map& __x)
00362       { _Base::swap(__x); }
00363 
00364       void
00365       clear()
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() { return *this; }
00445 
00446       const _Base&
00447       _M_base() const { 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