multimap.h

Go to the documentation of this file.
00001 // Debugging multimap implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005
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 2, 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 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file debug/multimap.h
00032  *  This file is a GNU debug extension to the Standard C++ Library.
00033  */
00034 
00035 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
00036 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
00037 
00038 #include <debug/safe_sequence.h>
00039 #include <debug/safe_iterator.h>
00040 #include <utility>
00041 
00042 namespace std
00043 {
00044 namespace __debug
00045 {
00046   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00047        typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
00048     class multimap
00049     : public _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator>,
00050     public __gnu_debug::_Safe_sequence<multimap<_Key,_Tp,_Compare,_Allocator> >
00051     {
00052       typedef _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
00053       typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
00054 
00055     public:
00056       // types:
00057       typedef _Key                   key_type;
00058       typedef _Tp                    mapped_type;
00059       typedef std::pair<const _Key, _Tp>             value_type;
00060       typedef _Compare                               key_compare;
00061       typedef _Allocator                             allocator_type;
00062       typedef typename _Base::reference              reference;
00063       typedef typename _Base::const_reference        const_reference;
00064 
00065       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
00066                                                      iterator;
00067       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
00068                                            multimap> const_iterator;
00069 
00070       typedef typename _Base::size_type              size_type;
00071       typedef typename _Base::difference_type        difference_type;
00072       typedef typename _Base::pointer                pointer;
00073       typedef typename _Base::const_pointer          const_pointer;
00074       typedef std::reverse_iterator<iterator>        reverse_iterator;
00075       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00076 
00077       using _Base::value_compare;
00078 
00079       // 23.3.1.1 construct/copy/destroy:
00080       explicit multimap(const _Compare& __comp = _Compare(),
00081             const _Allocator& __a = _Allocator())
00082       : _Base(__comp, __a) { }
00083 
00084       template<typename _InputIterator>
00085       multimap(_InputIterator __first, _InputIterator __last,
00086            const _Compare& __comp = _Compare(),
00087            const _Allocator& __a = _Allocator())
00088       : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
00089           __comp, __a) { }
00090 
00091       multimap(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
00092       : _Base(__x), _Safe_base() { }
00093 
00094       multimap(const _Base& __x) : _Base(__x), _Safe_base() { }
00095 
00096       ~multimap() { }
00097 
00098       multimap<_Key,_Tp,_Compare,_Allocator>&
00099       operator=(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
00100       {
00101     *static_cast<_Base*>(this) = __x;
00102     this->_M_invalidate_all();
00103     return *this;
00104       }
00105 
00106       using _Base::get_allocator;
00107 
00108       // iterators:
00109       iterator
00110       begin()
00111       { return iterator(_Base::begin(), this); }
00112 
00113       const_iterator
00114       begin() const
00115       { return const_iterator(_Base::begin(), this); }
00116 
00117       iterator
00118       end()
00119       { return iterator(_Base::end(), this); }
00120 
00121       const_iterator
00122       end() const
00123       { return const_iterator(_Base::end(), this); }
00124 
00125       reverse_iterator
00126       rbegin()
00127       { return reverse_iterator(end()); }
00128 
00129       const_reverse_iterator
00130       rbegin() const
00131       { return const_reverse_iterator(end()); }
00132 
00133       reverse_iterator
00134       rend()
00135       { return reverse_iterator(begin()); }
00136 
00137       const_reverse_iterator
00138       rend() const
00139       { return const_reverse_iterator(begin()); }
00140 
00141       // capacity:
00142       using _Base::empty;
00143       using _Base::size;
00144       using _Base::max_size;
00145 
00146       // modifiers:
00147       iterator
00148       insert(const value_type& __x)
00149       { return iterator(_Base::insert(__x), this); }
00150 
00151       iterator
00152       insert(iterator __position, const value_type& __x)
00153       {
00154     __glibcxx_check_insert(__position);
00155     return iterator(_Base::insert(__position.base(), __x), this);
00156       }
00157 
00158       template<typename _InputIterator>
00159         void
00160         insert(_InputIterator __first, _InputIterator __last)
00161         {
00162       __glibcxx_check_valid_range(__first, __last);
00163       _Base::insert(__first, __last);
00164     }
00165 
00166       void
00167       erase(iterator __position)
00168       {
00169     __glibcxx_check_erase(__position);
00170     __position._M_invalidate();
00171     _Base::erase(__position.base());
00172       }
00173 
00174       size_type
00175       erase(const key_type& __x)
00176       {
00177     std::pair<iterator, iterator> __victims = this->equal_range(__x);
00178     size_type __count = 0;
00179     while (__victims.first != __victims.second)
00180     {
00181       iterator __victim = __victims.first++;
00182       __victim._M_invalidate();
00183       _Base::erase(__victim.base());
00184       ++__count;
00185     }
00186     return __count;
00187       }
00188 
00189       void
00190       erase(iterator __first, iterator __last)
00191       {
00192     // _GLIBCXX_RESOLVE_LIB_DEFECTS
00193     // 151. can't currently clear() empty container
00194     __glibcxx_check_erase_range(__first, __last);
00195     while (__first != __last)
00196     this->erase(__first++);
00197       }
00198 
00199       void
00200       swap(multimap<_Key,_Tp,_Compare,_Allocator>& __x)
00201       {
00202     _Base::swap(__x);
00203     this->_M_swap(__x);
00204       }
00205 
00206       void
00207       clear()
00208       { this->erase(begin(), end()); }
00209 
00210       // observers:
00211       using _Base::key_comp;
00212       using _Base::value_comp;
00213 
00214       // 23.3.1.3 multimap operations:
00215       iterator
00216       find(const key_type& __x)
00217       { return iterator(_Base::find(__x), this); }
00218 
00219       const_iterator
00220       find(const key_type& __x) const
00221       { return const_iterator(_Base::find(__x), this); }
00222 
00223       using _Base::count;
00224 
00225       iterator
00226       lower_bound(const key_type& __x)
00227       { return iterator(_Base::lower_bound(__x), this); }
00228 
00229       const_iterator
00230       lower_bound(const key_type& __x) const
00231       { return const_iterator(_Base::lower_bound(__x), this); }
00232 
00233       iterator
00234       upper_bound(const key_type& __x)
00235       { return iterator(_Base::upper_bound(__x), this); }
00236 
00237       const_iterator
00238       upper_bound(const key_type& __x) const
00239       { return const_iterator(_Base::upper_bound(__x), this); }
00240 
00241       std::pair<iterator,iterator>
00242       equal_range(const key_type& __x)
00243       {
00244     typedef typename _Base::iterator _Base_iterator;
00245     std::pair<_Base_iterator, _Base_iterator> __res =
00246     _Base::equal_range(__x);
00247     return std::make_pair(iterator(__res.first, this),
00248                   iterator(__res.second, this));
00249       }
00250 
00251       std::pair<const_iterator,const_iterator>
00252       equal_range(const key_type& __x) const
00253       {
00254     typedef typename _Base::const_iterator _Base_const_iterator;
00255     std::pair<_Base_const_iterator, _Base_const_iterator> __res =
00256     _Base::equal_range(__x);
00257     return std::make_pair(const_iterator(__res.first, this),
00258                   const_iterator(__res.second, this));
00259       }
00260 
00261       _Base&
00262       _M_base() { return *this; }
00263 
00264       const _Base&
00265       _M_base() const { return *this; }
00266 
00267     private:
00268       void
00269       _M_invalidate_all()
00270       {
00271     typedef typename _Base::const_iterator _Base_const_iterator;
00272     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
00273     this->_M_invalidate_if(_Not_equal(_M_base().end()));
00274       }
00275     };
00276 
00277   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00278     inline bool
00279     operator==(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00280            const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00281     { return __lhs._M_base() == __rhs._M_base(); }
00282 
00283   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00284     inline bool
00285     operator!=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00286            const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00287     { return __lhs._M_base() != __rhs._M_base(); }
00288 
00289   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00290     inline bool
00291     operator<(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00292           const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00293     { return __lhs._M_base() < __rhs._M_base(); }
00294 
00295   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00296     inline bool
00297     operator<=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00298            const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00299     { return __lhs._M_base() <= __rhs._M_base(); }
00300 
00301   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00302     inline bool
00303     operator>=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00304            const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00305     { return __lhs._M_base() >= __rhs._M_base(); }
00306 
00307   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00308     inline bool
00309     operator>(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00310           const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00311     { return __lhs._M_base() > __rhs._M_base(); }
00312 
00313   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
00314     inline void
00315     swap(multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
00316      multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
00317     { __lhs.swap(__rhs); }
00318 } // namespace __debug
00319 } // namespace std
00320 
00321 #endif

Generated on Thu Nov 1 13:12:14 2007 for libstdc++ by  doxygen 1.5.1