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
00075       map(const _Compare& __comp = _Compare(),
00076       const _Allocator& __a = _Allocator())
00077       : _Base(__comp, __a)
00078       { __profcxx_map_to_unordered_map_construct(this); }
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       map(const map& __x)
00088       : _Base(__x)
00089       { __profcxx_map_to_unordered_map_construct(this); }
00090 
00091       map(const _Base& __x)
00092       : _Base(__x)
00093       { __profcxx_map_to_unordered_map_construct(this); }
00094 
00095 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00096       map(map&& __x)
00097       : _Base(std::forward<map>(__x))
00098       { }
00099 
00100       map(initializer_list<value_type> __l,
00101       const _Compare& __c = _Compare(),
00102       const allocator_type& __a = allocator_type())
00103       : _Base(__l, __c, __a) { }
00104 #endif
00105 
00106       ~map()
00107       { __profcxx_map_to_unordered_map_destruct(this); }
00108 
00109       map&
00110       operator=(const map& __x)
00111       {
00112     *static_cast<_Base*>(this) = __x;
00113     return *this;
00114       }
00115 
00116 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00117       map&
00118       operator=(map&& __x)
00119       {
00120     // NB: DR 1204.
00121     // NB: DR 675.
00122     this->clear();
00123     this->swap(__x);
00124     return *this;
00125       }
00126 
00127       map&
00128       operator=(initializer_list<value_type> __l)
00129       {
00130     this->clear();
00131     this->insert(__l);
00132     return *this;
00133       }
00134 #endif
00135 
00136       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00137       // 133. map missing get_allocator()
00138       using _Base::get_allocator;
00139 
00140       // iterators:
00141       iterator 
00142       begin()
00143       { return _Base::begin(); }
00144 
00145       const_iterator
00146       begin() const
00147       { return _Base::begin(); }
00148 
00149       iterator
00150       end()
00151       { return _Base::end(); }
00152 
00153       const_iterator
00154       end() const
00155       { return _Base::end(); }
00156 
00157       reverse_iterator
00158       rbegin()
00159       { 
00160         __profcxx_map_to_unordered_map_invalidate(this);
00161         return reverse_iterator(end()); 
00162       }
00163 
00164       const_reverse_iterator
00165       rbegin() const
00166       {
00167         __profcxx_map_to_unordered_map_invalidate(this);
00168         return const_reverse_iterator(end());
00169       }
00170 
00171       reverse_iterator
00172       rend()
00173       {
00174         __profcxx_map_to_unordered_map_invalidate(this);
00175         return reverse_iterator(begin());
00176       }
00177 
00178       const_reverse_iterator
00179       rend() const
00180       {
00181         __profcxx_map_to_unordered_map_invalidate(this);
00182         return const_reverse_iterator(begin());
00183       }
00184 
00185 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00186       const_iterator
00187       cbegin() const
00188       { return const_iterator(_Base::begin()); }
00189 
00190       const_iterator
00191       cend() const
00192       { return const_iterator(_Base::end()); }
00193 
00194       const_reverse_iterator
00195       crbegin() const
00196       {
00197         __profcxx_map_to_unordered_map_invalidate(this);
00198         return const_reverse_iterator(end());
00199       }
00200 
00201       const_reverse_iterator
00202       crend() const
00203       {
00204         __profcxx_map_to_unordered_map_invalidate(this);
00205         return const_reverse_iterator(begin());
00206       }
00207 #endif
00208 
00209       // capacity:
00210       using _Base::empty;
00211       using _Base::size;
00212       using _Base::max_size;
00213 
00214       // 23.3.1.2 element access:
00215       mapped_type&
00216       operator[](const key_type& __k)
00217       {
00218         __profcxx_map_to_unordered_map_find(this, size());
00219         return _Base::operator[](__k);
00220       }
00221 
00222       mapped_type&
00223       at(const key_type& __k)
00224       {
00225         __profcxx_map_to_unordered_map_find(this, size());
00226         return _Base::at(__k);
00227       }
00228 
00229       const mapped_type&
00230       at(const key_type& __k) const
00231       {
00232         __profcxx_map_to_unordered_map_find(this, size());
00233         return _Base::at(__k);
00234       }
00235 
00236       // modifiers:
00237       std::pair<iterator, bool>
00238       insert(const value_type& __x)
00239       {
00240         __profcxx_map_to_unordered_map_insert(this, size(), 1);
00241     typedef typename _Base::iterator _Base_iterator;
00242     std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
00243     return std::pair<iterator, bool>(iterator(__res.first),
00244                      __res.second);
00245       }
00246 
00247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00248       void
00249       insert(std::initializer_list<value_type> __list)
00250       { 
00251         size_type size_before = size();
00252         _Base::insert(__list); 
00253         __profcxx_map_to_unordered_map_insert(this, size_before, 
00254                                                 size() - size_before);
00255       }
00256 #endif
00257 
00258       iterator
00259       insert(iterator __position, const value_type& __x)
00260       {
00261         size_type size_before = size();
00262     return iterator(_Base::insert(__position, __x));
00263         __profcxx_map_to_unordered_map_insert(this, size_before, 
00264                                                 size() - size_before);
00265       }
00266 
00267       template<typename _InputIterator>
00268         void
00269         insert(_InputIterator __first, _InputIterator __last)
00270         {
00271           size_type size_before = size();
00272       _Base::insert(__first, __last);
00273           __profcxx_map_to_unordered_map_insert(this, size_before, 
00274                                                 size() - size_before);
00275     }
00276 
00277 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00278       iterator
00279       erase(iterator __position)
00280       {
00281     iterator __i = _Base::erase(__position);
00282         __profcxx_map_to_unordered_map_erase(this, size(), 1);
00283         return __i;
00284       }
00285 #else
00286       void
00287       erase(iterator __position)
00288       {
00289     _Base::erase(__position);
00290         __profcxx_map_to_unordered_map_erase(this, size(), 1);
00291       }
00292 #endif
00293 
00294       size_type
00295       erase(const key_type& __x)
00296       {
00297     iterator __victim = find(__x);
00298     if (__victim == end())
00299       return 0;
00300     else
00301     {
00302       _Base::erase(__victim);
00303       return 1;
00304     }
00305       }
00306 
00307 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00308       iterator
00309       erase(iterator __first, iterator __last)
00310       {
00311     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00312     // 151. can't currently clear() empty container
00313     while (__first != __last)
00314       this->erase(__first++);
00315     return __last;
00316       }
00317 #else
00318       void
00319       erase(iterator __first, iterator __last)
00320       {
00321     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00322     // 151. can't currently clear() empty container
00323     while (__first != __last)
00324       this->erase(__first++);
00325       }
00326 #endif
00327 
00328       void
00329 
00330       swap(map& __x)
00331       {
00332     _Base::swap(__x);
00333       }
00334 
00335       void
00336       clear()
00337       { this->erase(begin(), end()); }
00338 
00339       // observers:
00340       using _Base::key_comp;
00341       using _Base::value_comp;
00342 
00343       // 23.3.1.3 map operations:
00344       iterator
00345       find(const key_type& __x)
00346       {
00347         __profcxx_map_to_unordered_map_find(this, size());
00348         return iterator(_Base::find(__x));
00349       }
00350 
00351       const_iterator
00352       find(const key_type& __x) const
00353       {
00354         __profcxx_map_to_unordered_map_find(this, size());
00355         return const_iterator(_Base::find(__x));
00356       }
00357 
00358       size_type
00359       count(const key_type& __x) const
00360       {
00361         __profcxx_map_to_unordered_map_find(this, size());
00362         return _Base::count(__x);
00363       }
00364 
00365       iterator
00366       lower_bound(const key_type& __x)
00367       { 
00368         __profcxx_map_to_unordered_map_invalidate(this);
00369         return iterator(_Base::lower_bound(__x)); 
00370       }
00371 
00372       const_iterator
00373       lower_bound(const key_type& __x) const
00374       { 
00375         __profcxx_map_to_unordered_map_invalidate(this);
00376         return const_iterator(_Base::lower_bound(__x)); 
00377       }
00378 
00379       iterator
00380       upper_bound(const key_type& __x)
00381       { 
00382         __profcxx_map_to_unordered_map_invalidate(this);
00383         return iterator(_Base::upper_bound(__x)); 
00384       }
00385 
00386       const_iterator
00387       upper_bound(const key_type& __x) const
00388       { 
00389         __profcxx_map_to_unordered_map_invalidate(this);
00390         return const_iterator(_Base::upper_bound(__x)); 
00391       }
00392 
00393       std::pair<iterator,iterator>
00394       equal_range(const key_type& __x)
00395       {
00396     typedef typename _Base::iterator _Base_iterator;
00397     std::pair<_Base_iterator, _Base_iterator> __res =
00398     _Base::equal_range(__x);
00399     return std::make_pair(iterator(__res.first),
00400                   iterator(__res.second));
00401       }
00402 
00403       std::pair<const_iterator,const_iterator>
00404       equal_range(const key_type& __x) const
00405       {
00406         __profcxx_map_to_unordered_map_find(this, size());
00407     typedef typename _Base::const_iterator _Base_const_iterator;
00408     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00409     _Base::equal_range(__x);
00410     return std::make_pair(const_iterator(__res.first),
00411                   const_iterator(__res.second));
00412       }
00413 
00414       _Base& 
00415       _M_base() { return *this; }
00416 
00417       const _Base&
00418       _M_base() const { return *this; }
00419 
00420     };
00421 
00422   template<typename _Key, typename _Tp,
00423        typename _Compare, typename _Allocator>
00424     inline bool
00425     operator==(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00426            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00427     { 
00428       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00429       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00430       return __lhs._M_base() == __rhs._M_base(); 
00431     }
00432 
00433   template<typename _Key, typename _Tp,
00434        typename _Compare, typename _Allocator>
00435     inline bool
00436     operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00437            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00438     { 
00439       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00440       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00441       return __lhs._M_base() != __rhs._M_base(); 
00442     }
00443 
00444   template<typename _Key, typename _Tp,
00445        typename _Compare, typename _Allocator>
00446     inline bool
00447     operator<(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00448           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00449     {
00450       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00451       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00452       return __lhs._M_base() < __rhs._M_base(); 
00453     }
00454 
00455   template<typename _Key, typename _Tp,
00456        typename _Compare, typename _Allocator>
00457     inline bool
00458     operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00459            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00460     {
00461       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00462       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00463       return __lhs._M_base() <= __rhs._M_base();
00464     }
00465 
00466   template<typename _Key, typename _Tp,
00467        typename _Compare, typename _Allocator>
00468     inline bool
00469     operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00470            const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00471     {
00472       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00473       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00474       return __lhs._M_base() >= __rhs._M_base();
00475     }
00476 
00477   template<typename _Key, typename _Tp,
00478        typename _Compare, typename _Allocator>
00479     inline bool
00480     operator>(const map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00481           const map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00482     {
00483       __profcxx_map_to_unordered_map_invalidate(&__lhs);
00484       __profcxx_map_to_unordered_map_invalidate(&__rhs);
00485       return __lhs._M_base() > __rhs._M_base();
00486     }
00487 
00488   template<typename _Key, typename _Tp,
00489        typename _Compare, typename _Allocator>
00490     inline void
00491     swap(map<_Key, _Tp, _Compare, _Allocator>& __lhs,
00492      map<_Key, _Tp, _Compare, _Allocator>& __rhs)
00493     { __lhs.swap(__rhs); }
00494 
00495 } // namespace __profile
00496 } // namespace std
00497 
00498 #endif