profile/map.h

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