# stl_multimap.h

Go to the documentation of this file.

    00001 // Multimap implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    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 /*
    00031  *
    00032  * Copyright (c) 1994
    00033  * Hewlett-Packard Company
    00034  *
    00035  * Permission to use, copy, modify, distribute and sell this software
    00036  * and its documentation for any purpose is hereby granted without fee,
    00037  * provided that the above copyright notice appear in all copies and
    00038  * that both that copyright notice and this permission notice appear
    00039  * in supporting documentation.  Hewlett-Packard Company makes no
    00040  * representations about the suitability of this software for any
    00041  * purpose.  It is provided "as is" without express or implied warranty.
    00042  *
    00043  *
    00044  * Copyright (c) 1996,1997
    00045  * Silicon Graphics Computer Systems, Inc.
    00046  *
    00047  * Permission to use, copy, modify, distribute and sell this software
    00048  * and its documentation for any purpose is hereby granted without fee,
    00049  * provided that the above copyright notice appear in all copies and
    00050  * that both that copyright notice and this permission notice appear
    00051  * in supporting documentation.  Silicon Graphics makes no
    00052  * representations about the suitability of this software for any
    00053  * purpose.  It is provided "as is" without express or implied warranty.
    00054  */
    00055
    00061 #ifndef __GLIBCPP_INTERNAL_MULTIMAP_H
    00062 #define __GLIBCPP_INTERNAL_MULTIMAP_H
    00063
    00064 #include <bits/concept_check.h>
    00065
    00066 namespace std
    00067 {
    00068   // Forward declaration of operators < and ==, needed for friend declaration.
    00069
    00070   template <typename _Key, typename _Tp,
    00071             typename _Compare = less<_Key>,
    00072             typename _Alloc = allocator<pair<const _Key, _Tp> > >
    00073   class multimap;
    00074
    00075   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00076   inline bool operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00077                          const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
    00078
    00079   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00080   inline bool operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00081                         const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
    00082
    00104   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00105     class multimap
    00106   {
    00107     // concept requirements
    00108     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
    00109     __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept)
    00110
    00111   public:
    00112     typedef _Key                                          key_type;
    00113     typedef _Tp                                           mapped_type;
    00114     typedef pair<const _Key, _Tp>                         value_type;
    00115     typedef _Compare                                      key_compare;
    00116
    00117     class value_compare
    00118       : public binary_function<value_type, value_type, bool>
    00119       {
    00120         friend class multimap<_Key,_Tp,_Compare,_Alloc>;
    00121       protected:
    00122         _Compare comp;
    00123         value_compare(_Compare __c) : comp(__c) {}
    00124       public:
    00125         bool operator()(const value_type& __x, const value_type& __y) const
    00126         { return comp(__x.first, __y.first); }
    00127     };
    00128
    00129   private:
    00131     typedef _Rb_tree<key_type, value_type,
    00132                     _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
    00134     _Rep_type _M_t;
    00135
    00136   public:
    00137     // many of these are specified differently in ISO, but the following are
    00138     // "functionally equivalent"
    00139     typedef typename _Rep_type::allocator_type            allocator_type;
    00140     typedef typename _Rep_type::reference                 reference;
    00141     typedef typename _Rep_type::const_reference           const_reference;
    00142     typedef typename _Rep_type::iterator                  iterator;
    00143     typedef typename _Rep_type::const_iterator            const_iterator;
    00144     typedef typename _Rep_type::size_type                 size_type;
    00145     typedef typename _Rep_type::difference_type           difference_type;
    00146     typedef typename _Rep_type::pointer                   pointer;
    00147     typedef typename _Rep_type::const_pointer             const_pointer;
    00148     typedef typename _Rep_type::reverse_iterator          reverse_iterator;
    00149     typedef typename _Rep_type::const_reverse_iterator    const_reverse_iterator;
    00150
    00151
    00152     // [23.3.2] construct/copy/destroy
    00153     // (get_allocator() is also listed in this section)
    00157     multimap() : _M_t(_Compare(), allocator_type()) { }
    00158
    00159     // for some reason this was made a separate function
    00163     explicit
    00164     multimap(const _Compare& __comp, const allocator_type& __a = allocator_type())
    00165       : _M_t(__comp, __a) { }
    00166
    00174     multimap(const multimap& __x)
    00175       : _M_t(__x._M_t) { }
    00176
    00186     template <typename _InputIterator>
    00187       multimap(_InputIterator __first, _InputIterator __last)
    00188         : _M_t(_Compare(), allocator_type())
    00189         { _M_t.insert_equal(__first, __last); }
    00190
    00202     template <typename _InputIterator>
    00203       multimap(_InputIterator __first, _InputIterator __last,
    00204                const _Compare& __comp,
    00205                const allocator_type& __a = allocator_type())
    00206         : _M_t(__comp, __a)
    00207         { _M_t.insert_equal(__first, __last); }
    00208
    00209     // FIXME There is no dtor declared, but we should have something generated
    00210     // by Doxygen.  I don't know what tags to add to this paragraph to make
    00211     // that happen:
    00225     multimap&
    00226     operator=(const multimap& __x)
    00227     {
    00228       _M_t = __x._M_t;
    00229       return *this;
    00230     }
    00231
    00233     allocator_type
    00234     get_allocator() const { return _M_t.get_allocator(); }
    00235
    00236     // iterators
    00241     iterator
    00242     begin() { return _M_t.begin(); }
    00243
    00249     const_iterator
    00250     begin() const { return _M_t.begin(); }
    00251
    00256     iterator
    00257     end() { return _M_t.end(); }
    00258
    00264     const_iterator
    00265     end() const { return _M_t.end(); }
    00266
    00272     reverse_iterator
    00273     rbegin() { return _M_t.rbegin(); }
    00274
    00280     const_reverse_iterator
    00281     rbegin() const { return _M_t.rbegin(); }
    00282
    00288     reverse_iterator
    00289     rend() { return _M_t.rend(); }
    00290
    00296     const_reverse_iterator
    00297     rend() const { return _M_t.rend(); }
    00298
    00299     // capacity
    00301     bool
    00302     empty() const { return _M_t.empty(); }
    00303
    00305     size_type
    00306     size() const { return _M_t.size(); }
    00307
    00309     size_type
    00310     max_size() const { return _M_t.max_size(); }
    00311
    00312     // modifiers
    00325     iterator
    00326     insert(const value_type& __x) { return _M_t.insert_equal(__x); }
    00327
    00348     iterator
    00349     insert(iterator __position, const value_type& __x)
    00350     { return _M_t.insert_equal(__position, __x); }
    00351
    00360     template <typename _InputIterator>
    00361       void
    00362       insert(_InputIterator __first, _InputIterator __last)
    00363       { _M_t.insert_equal(__first, __last); }
    00364
    00374     void
    00375     erase(iterator __position) { _M_t.erase(__position); }
    00376
    00388     size_type
    00389     erase(const key_type& __x) { return _M_t.erase(__x); }
    00390
    00401     void
    00402     erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
    00403
    00415     void
    00416     swap(multimap& __x) { _M_t.swap(__x._M_t); }
    00417
    00424     void
    00425     clear() { _M_t.clear(); }
    00426
    00427     // observers
    00432     key_compare
    00433     key_comp() const { return _M_t.key_comp(); }
    00434
    00439     value_compare
    00440     value_comp() const { return value_compare(_M_t.key_comp()); }
    00441
    00442     // multimap operations
    00454     iterator
    00455     find(const key_type& __x) { return _M_t.find(__x); }
    00456
    00468     const_iterator
    00469     find(const key_type& __x) const { return _M_t.find(__x); }
    00470
    00476     size_type
    00477     count(const key_type& __x) const { return _M_t.count(__x); }
    00478
    00490     iterator
    00491     lower_bound(const key_type& __x) { return _M_t.lower_bound(__x); }
    00492
    00504     const_iterator
    00505     lower_bound(const key_type& __x) const { return _M_t.lower_bound(__x); }
    00506
    00512     iterator
    00513     upper_bound(const key_type& __x) { return _M_t.upper_bound(__x); }
    00514
    00521     const_iterator
    00522     upper_bound(const key_type& __x) const { return _M_t.upper_bound(__x); }
    00523
    00537     pair<iterator,iterator>
    00538     equal_range(const key_type& __x) { return _M_t.equal_range(__x); }
    00539
    00553     pair<const_iterator,const_iterator>
    00554     equal_range(const key_type& __x) const { return _M_t.equal_range(__x); }
    00555
    00556     template <typename _K1, typename _T1, typename _C1, typename _A1>
    00557     friend bool operator== (const multimap<_K1,_T1,_C1,_A1>&,
    00558                             const multimap<_K1,_T1,_C1,_A1>&);
    00559     template <typename _K1, typename _T1, typename _C1, typename _A1>
    00560     friend bool operator< (const multimap<_K1,_T1,_C1,_A1>&,
    00561                            const multimap<_K1,_T1,_C1,_A1>&);
    00562   };
    00563
    00564
    00575   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00576     inline bool
    00577     operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00578                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00579     {
    00580       return __x._M_t == __y._M_t;
    00581     }
    00582
    00594   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00595     inline bool
    00596     operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00597               const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00598     { return __x._M_t < __y._M_t; }
    00599
    00601   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00602     inline bool
    00603     operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00604                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00605     { return !(__x == __y); }
    00606
    00608   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00609     inline bool
    00610     operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00611               const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00612     { return __y < __x; }
    00613
    00615   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00616     inline bool
    00617     operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00618                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00619     { return !(__y < __x); }
    00620
    00622   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00623     inline bool
    00624     operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00625                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00626     { return !(__x < __y); }
    00627
    00629   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
    00630     inline void
    00631     swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
    00632          multimap<_Key,_Tp,_Compare,_Alloc>& __y)
    00633     { __x.swap(__y); }
    00634 } // namespace std
    00635
    00636 #endif /* __GLIBCPP_INTERNAL_MULTIMAP_H */