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