xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/include/bits/stl_map.h (revision 03a78d155d6fff5698289342b62759a75b20d130)
1*03a78d15Sespie // Map implementation -*- C++ -*-
2*03a78d15Sespie 
3*03a78d15Sespie // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4*03a78d15Sespie //
5*03a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
6*03a78d15Sespie // software; you can redistribute it and/or modify it under the
7*03a78d15Sespie // terms of the GNU General Public License as published by the
8*03a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
9*03a78d15Sespie // any later version.
10*03a78d15Sespie 
11*03a78d15Sespie // This library is distributed in the hope that it will be useful,
12*03a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*03a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*03a78d15Sespie // GNU General Public License for more details.
15*03a78d15Sespie 
16*03a78d15Sespie // You should have received a copy of the GNU General Public License along
17*03a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
18*03a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19*03a78d15Sespie // USA.
20*03a78d15Sespie 
21*03a78d15Sespie // As a special exception, you may use this file as part of a free software
22*03a78d15Sespie // library without restriction.  Specifically, if other files instantiate
23*03a78d15Sespie // templates or use macros or inline functions from this file, or you compile
24*03a78d15Sespie // this file and link it with other files to produce an executable, this
25*03a78d15Sespie // file does not by itself cause the resulting executable to be covered by
26*03a78d15Sespie // the GNU General Public License.  This exception does not however
27*03a78d15Sespie // invalidate any other reasons why the executable file might be covered by
28*03a78d15Sespie // the GNU General Public License.
29*03a78d15Sespie 
30*03a78d15Sespie /*
31*03a78d15Sespie  *
32*03a78d15Sespie  * Copyright (c) 1994
33*03a78d15Sespie  * Hewlett-Packard Company
34*03a78d15Sespie  *
35*03a78d15Sespie  * Permission to use, copy, modify, distribute and sell this software
36*03a78d15Sespie  * and its documentation for any purpose is hereby granted without fee,
37*03a78d15Sespie  * provided that the above copyright notice appear in all copies and
38*03a78d15Sespie  * that both that copyright notice and this permission notice appear
39*03a78d15Sespie  * in supporting documentation.  Hewlett-Packard Company makes no
40*03a78d15Sespie  * representations about the suitability of this software for any
41*03a78d15Sespie  * purpose.  It is provided "as is" without express or implied warranty.
42*03a78d15Sespie  *
43*03a78d15Sespie  *
44*03a78d15Sespie  * Copyright (c) 1996,1997
45*03a78d15Sespie  * Silicon Graphics Computer Systems, Inc.
46*03a78d15Sespie  *
47*03a78d15Sespie  * Permission to use, copy, modify, distribute and sell this software
48*03a78d15Sespie  * and its documentation for any purpose is hereby granted without fee,
49*03a78d15Sespie  * provided that the above copyright notice appear in all copies and
50*03a78d15Sespie  * that both that copyright notice and this permission notice appear
51*03a78d15Sespie  * in supporting documentation.  Silicon Graphics makes no
52*03a78d15Sespie  * representations about the suitability of this software for any
53*03a78d15Sespie  * purpose.  It is provided "as is" without express or implied warranty.
54*03a78d15Sespie  */
55*03a78d15Sespie 
56*03a78d15Sespie /** @file stl_map.h
57*03a78d15Sespie  *  This is an internal header file, included by other library headers.
58*03a78d15Sespie  *  You should not attempt to use it directly.
59*03a78d15Sespie  */
60*03a78d15Sespie 
61*03a78d15Sespie #ifndef __GLIBCPP_INTERNAL_MAP_H
62*03a78d15Sespie #define __GLIBCPP_INTERNAL_MAP_H
63*03a78d15Sespie 
64*03a78d15Sespie #include <bits/concept_check.h>
65*03a78d15Sespie 
66*03a78d15Sespie namespace std
67*03a78d15Sespie {
68*03a78d15Sespie   /**
69*03a78d15Sespie    *  @brief A standard container made up of (key,value) pairs, which can be
70*03a78d15Sespie    *  retrieved based on a key, in logarithmic time.
71*03a78d15Sespie    *
72*03a78d15Sespie    *  @ingroup Containers
73*03a78d15Sespie    *  @ingroup Assoc_containers
74*03a78d15Sespie    *
75*03a78d15Sespie    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
76*03a78d15Sespie    *  <a href="tables.html#66">reversible container</a>, and an
77*03a78d15Sespie    *  <a href="tables.html#69">associative container</a> (using unique keys).
78*03a78d15Sespie    *  For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
79*03a78d15Sespie    *  value_type is std::pair<const Key,T>.
80*03a78d15Sespie    *
81*03a78d15Sespie    *  Maps support bidirectional iterators.
82*03a78d15Sespie    *
83*03a78d15Sespie    *  @if maint
84*03a78d15Sespie    *  The private tree data is declared exactly the same way for map and
85*03a78d15Sespie    *  multimap; the distinction is made entirely in how the tree functions are
86*03a78d15Sespie    *  called (*_unique versus *_equal, same as the standard).
87*03a78d15Sespie    *  @endif
88*03a78d15Sespie   */
89*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare = less<_Key>,
90*03a78d15Sespie             typename _Alloc = allocator<pair<const _Key, _Tp> > >
91*03a78d15Sespie     class map
92*03a78d15Sespie   {
93*03a78d15Sespie     // concept requirements
94*03a78d15Sespie     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
95*03a78d15Sespie     __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept)
96*03a78d15Sespie 
97*03a78d15Sespie   public:
98*03a78d15Sespie     typedef _Key                                          key_type;
99*03a78d15Sespie     typedef _Tp                                           mapped_type;
100*03a78d15Sespie     typedef pair<const _Key, _Tp>                         value_type;
101*03a78d15Sespie     typedef _Compare                                      key_compare;
102*03a78d15Sespie 
103*03a78d15Sespie     class value_compare
104*03a78d15Sespie       : public binary_function<value_type, value_type, bool>
105*03a78d15Sespie       {
106*03a78d15Sespie         friend class map<_Key,_Tp,_Compare,_Alloc>;
107*03a78d15Sespie       protected:
108*03a78d15Sespie         _Compare comp;
value_compare(_Compare __c)109*03a78d15Sespie         value_compare(_Compare __c) : comp(__c) {}
110*03a78d15Sespie       public:
operator()111*03a78d15Sespie         bool operator()(const value_type& __x, const value_type& __y) const
112*03a78d15Sespie         { return comp(__x.first, __y.first); }
113*03a78d15Sespie       };
114*03a78d15Sespie 
115*03a78d15Sespie   private:
116*03a78d15Sespie     /// @if maint  This turns a red-black tree into a [multi]map.  @endif
117*03a78d15Sespie     typedef _Rb_tree<key_type, value_type,
118*03a78d15Sespie                      _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
119*03a78d15Sespie     /// @if maint  The actual tree structure.  @endif
120*03a78d15Sespie     _Rep_type _M_t;
121*03a78d15Sespie 
122*03a78d15Sespie   public:
123*03a78d15Sespie     // many of these are specified differently in ISO, but the following are
124*03a78d15Sespie     // "functionally equivalent"
125*03a78d15Sespie     typedef typename _Rep_type::allocator_type            allocator_type;
126*03a78d15Sespie     typedef typename _Rep_type::reference                 reference;
127*03a78d15Sespie     typedef typename _Rep_type::const_reference           const_reference;
128*03a78d15Sespie     typedef typename _Rep_type::iterator                  iterator;
129*03a78d15Sespie     typedef typename _Rep_type::const_iterator            const_iterator;
130*03a78d15Sespie     typedef typename _Rep_type::size_type                 size_type;
131*03a78d15Sespie     typedef typename _Rep_type::difference_type           difference_type;
132*03a78d15Sespie     typedef typename _Rep_type::pointer                   pointer;
133*03a78d15Sespie     typedef typename _Rep_type::const_pointer             const_pointer;
134*03a78d15Sespie     typedef typename _Rep_type::reverse_iterator          reverse_iterator;
135*03a78d15Sespie     typedef typename _Rep_type::const_reverse_iterator    const_reverse_iterator;
136*03a78d15Sespie 
137*03a78d15Sespie 
138*03a78d15Sespie     // [23.3.1.1] construct/copy/destroy
139*03a78d15Sespie     // (get_allocator() is normally listed in this section, but seems to have
140*03a78d15Sespie     // been accidentally omitted in the printed standard)
141*03a78d15Sespie     /**
142*03a78d15Sespie      *  @brief  Default constructor creates no elements.
143*03a78d15Sespie     */
map()144*03a78d15Sespie     map() : _M_t(_Compare(), allocator_type()) { }
145*03a78d15Sespie 
146*03a78d15Sespie     // for some reason this was made a separate function
147*03a78d15Sespie     /**
148*03a78d15Sespie      *  @brief  Default constructor creates no elements.
149*03a78d15Sespie     */
150*03a78d15Sespie     explicit
151*03a78d15Sespie     map(const _Compare& __comp, const allocator_type& __a = allocator_type())
_M_t(__comp,__a)152*03a78d15Sespie       : _M_t(__comp, __a) { }
153*03a78d15Sespie 
154*03a78d15Sespie     /**
155*03a78d15Sespie      *  @brief  Map copy constructor.
156*03a78d15Sespie      *  @param  x  A %map of identical element and allocator types.
157*03a78d15Sespie      *
158*03a78d15Sespie      *  The newly-created %map uses a copy of the allocation object used
159*03a78d15Sespie      *  by @a x.
160*03a78d15Sespie     */
map(const map & __x)161*03a78d15Sespie     map(const map& __x)
162*03a78d15Sespie       : _M_t(__x._M_t) { }
163*03a78d15Sespie 
164*03a78d15Sespie     /**
165*03a78d15Sespie      *  @brief  Builds a %map from a range.
166*03a78d15Sespie      *  @param  first  An input iterator.
167*03a78d15Sespie      *  @param  last  An input iterator.
168*03a78d15Sespie      *
169*03a78d15Sespie      *  Create a %map consisting of copies of the elements from [first,last).
170*03a78d15Sespie      *  This is linear in N if the range is already sorted, and NlogN
171*03a78d15Sespie      *  otherwise (where N is distance(first,last)).
172*03a78d15Sespie     */
173*03a78d15Sespie     template <typename _InputIterator>
map(_InputIterator __first,_InputIterator __last)174*03a78d15Sespie       map(_InputIterator __first, _InputIterator __last)
175*03a78d15Sespie       : _M_t(_Compare(), allocator_type())
176*03a78d15Sespie       { _M_t.insert_unique(__first, __last); }
177*03a78d15Sespie 
178*03a78d15Sespie     /**
179*03a78d15Sespie      *  @brief  Builds a %map from a range.
180*03a78d15Sespie      *  @param  first  An input iterator.
181*03a78d15Sespie      *  @param  last  An input iterator.
182*03a78d15Sespie      *  @param  comp  A comparison functor.
183*03a78d15Sespie      *  @param  a  An allocator object.
184*03a78d15Sespie      *
185*03a78d15Sespie      *  Create a %map consisting of copies of the elements from [first,last).
186*03a78d15Sespie      *  This is linear in N if the range is already sorted, and NlogN
187*03a78d15Sespie      *  otherwise (where N is distance(first,last)).
188*03a78d15Sespie     */
189*03a78d15Sespie     template <typename _InputIterator>
190*03a78d15Sespie       map(_InputIterator __first, _InputIterator __last,
191*03a78d15Sespie           const _Compare& __comp, const allocator_type& __a = allocator_type())
_M_t(__comp,__a)192*03a78d15Sespie       : _M_t(__comp, __a)
193*03a78d15Sespie       { _M_t.insert_unique(__first, __last); }
194*03a78d15Sespie 
195*03a78d15Sespie     // FIXME There is no dtor declared, but we should have something generated
196*03a78d15Sespie     // by Doxygen.  I don't know what tags to add to this paragraph to make
197*03a78d15Sespie     // that happen:
198*03a78d15Sespie     /**
199*03a78d15Sespie      *  The dtor only erases the elements, and note that if the elements
200*03a78d15Sespie      *  themselves are pointers, the pointed-to memory is not touched in any
201*03a78d15Sespie      *  way.  Managing the pointer is the user's responsibilty.
202*03a78d15Sespie     */
203*03a78d15Sespie 
204*03a78d15Sespie     /**
205*03a78d15Sespie      *  @brief  Map assignment operator.
206*03a78d15Sespie      *  @param  x  A %map of identical element and allocator types.
207*03a78d15Sespie      *
208*03a78d15Sespie      *  All the elements of @a x are copied, but unlike the copy constructor,
209*03a78d15Sespie      *  the allocator object is not copied.
210*03a78d15Sespie     */
211*03a78d15Sespie     map&
212*03a78d15Sespie     operator=(const map& __x)
213*03a78d15Sespie     {
214*03a78d15Sespie       _M_t = __x._M_t;
215*03a78d15Sespie       return *this;
216*03a78d15Sespie     }
217*03a78d15Sespie 
218*03a78d15Sespie     /// Get a copy of the memory allocation object.
219*03a78d15Sespie     allocator_type
get_allocator()220*03a78d15Sespie     get_allocator() const { return _M_t.get_allocator(); }
221*03a78d15Sespie 
222*03a78d15Sespie     // iterators
223*03a78d15Sespie     /**
224*03a78d15Sespie      *  Returns a read/write iterator that points to the first pair in the %map.
225*03a78d15Sespie      *  Iteration is done in ascending order according to the keys.
226*03a78d15Sespie     */
227*03a78d15Sespie     iterator
begin()228*03a78d15Sespie     begin() { return _M_t.begin(); }
229*03a78d15Sespie 
230*03a78d15Sespie     /**
231*03a78d15Sespie      *  Returns a read-only (constant) iterator that points to the first pair
232*03a78d15Sespie      *  in the %map.  Iteration is done in ascending order according to the
233*03a78d15Sespie      *  keys.
234*03a78d15Sespie     */
235*03a78d15Sespie     const_iterator
begin()236*03a78d15Sespie     begin() const { return _M_t.begin(); }
237*03a78d15Sespie 
238*03a78d15Sespie     /**
239*03a78d15Sespie      *  Returns a read/write iterator that points one past the last pair in the
240*03a78d15Sespie      *  %map.  Iteration is done in ascending order according to the keys.
241*03a78d15Sespie     */
242*03a78d15Sespie     iterator
end()243*03a78d15Sespie     end() { return _M_t.end(); }
244*03a78d15Sespie 
245*03a78d15Sespie     /**
246*03a78d15Sespie      *  Returns a read-only (constant) iterator that points one past the last
247*03a78d15Sespie      *  pair in the %map.  Iteration is done in ascending order according to the
248*03a78d15Sespie      *  keys.
249*03a78d15Sespie     */
250*03a78d15Sespie     const_iterator
end()251*03a78d15Sespie     end() const { return _M_t.end(); }
252*03a78d15Sespie 
253*03a78d15Sespie     /**
254*03a78d15Sespie      *  Returns a read/write reverse iterator that points to the last pair in
255*03a78d15Sespie      *  the %map.  Iteration is done in descending order according to the keys.
256*03a78d15Sespie     */
257*03a78d15Sespie     reverse_iterator
rbegin()258*03a78d15Sespie     rbegin() { return _M_t.rbegin(); }
259*03a78d15Sespie 
260*03a78d15Sespie     /**
261*03a78d15Sespie      *  Returns a read-only (constant) reverse iterator that points to the last
262*03a78d15Sespie      *  pair in the %map.  Iteration is done in descending order according to
263*03a78d15Sespie      *  the keys.
264*03a78d15Sespie     */
265*03a78d15Sespie     const_reverse_iterator
rbegin()266*03a78d15Sespie     rbegin() const { return _M_t.rbegin(); }
267*03a78d15Sespie 
268*03a78d15Sespie     /**
269*03a78d15Sespie      *  Returns a read/write reverse iterator that points to one before the
270*03a78d15Sespie      *  first pair in the %map.  Iteration is done in descending order according
271*03a78d15Sespie      *  to the keys.
272*03a78d15Sespie     */
273*03a78d15Sespie     reverse_iterator
rend()274*03a78d15Sespie     rend() { return _M_t.rend(); }
275*03a78d15Sespie 
276*03a78d15Sespie     /**
277*03a78d15Sespie      *  Returns a read-only (constant) reverse iterator that points to one
278*03a78d15Sespie      *  before the first pair in the %map.  Iteration is done in descending
279*03a78d15Sespie      *  order according to the keys.
280*03a78d15Sespie     */
281*03a78d15Sespie     const_reverse_iterator
rend()282*03a78d15Sespie     rend() const { return _M_t.rend(); }
283*03a78d15Sespie 
284*03a78d15Sespie     // capacity
285*03a78d15Sespie     /** Returns true if the %map is empty.  (Thus begin() would equal end().) */
286*03a78d15Sespie     bool
empty()287*03a78d15Sespie     empty() const { return _M_t.empty(); }
288*03a78d15Sespie 
289*03a78d15Sespie     /** Returns the size of the %map.  */
290*03a78d15Sespie     size_type
size()291*03a78d15Sespie     size() const { return _M_t.size(); }
292*03a78d15Sespie 
293*03a78d15Sespie     /** Returns the maximum size of the %map.  */
294*03a78d15Sespie     size_type
max_size()295*03a78d15Sespie     max_size() const { return _M_t.max_size(); }
296*03a78d15Sespie 
297*03a78d15Sespie     // [23.3.1.2] element access
298*03a78d15Sespie     /**
299*03a78d15Sespie      *  @brief  Subscript ( @c [] ) access to %map data.
300*03a78d15Sespie      *  @param  k  The key for which data should be retrieved.
301*03a78d15Sespie      *  @return  A reference to the data of the (key,data) %pair.
302*03a78d15Sespie      *
303*03a78d15Sespie      *  Allows for easy lookup with the subscript ( @c [] ) operator.  Returns
304*03a78d15Sespie      *  data associated with the key specified in subscript.  If the key does
305*03a78d15Sespie      *  not exist, a pair with that key is created using default values, which
306*03a78d15Sespie      *  is then returned.
307*03a78d15Sespie      *
308*03a78d15Sespie      *  Lookup requires logarithmic time.
309*03a78d15Sespie     */
310*03a78d15Sespie     mapped_type&
311*03a78d15Sespie     operator[](const key_type& __k)
312*03a78d15Sespie     {
313*03a78d15Sespie       // concept requirements
314*03a78d15Sespie       __glibcpp_function_requires(_DefaultConstructibleConcept<mapped_type>)
315*03a78d15Sespie 
316*03a78d15Sespie       iterator __i = lower_bound(__k);
317*03a78d15Sespie       // __i->first is greater than or equivalent to __k.
318*03a78d15Sespie       if (__i == end() || key_comp()(__k, (*__i).first))
319*03a78d15Sespie         __i = insert(__i, value_type(__k, mapped_type()));
320*03a78d15Sespie       return (*__i).second;
321*03a78d15Sespie     }
322*03a78d15Sespie 
323*03a78d15Sespie     // modifiers
324*03a78d15Sespie     /**
325*03a78d15Sespie      *  @brief Attempts to insert a std::pair into the %map.
326*03a78d15Sespie      *  @param  x  Pair to be inserted (see std::make_pair for easy creation of
327*03a78d15Sespie      *             pairs).
328*03a78d15Sespie      *  @return  A pair, of which the first element is an iterator that points
329*03a78d15Sespie      *           to the possibly inserted pair, and the second is a bool that
330*03a78d15Sespie      *           is true if the pair was actually inserted.
331*03a78d15Sespie      *
332*03a78d15Sespie      *  This function attempts to insert a (key, value) %pair into the %map.
333*03a78d15Sespie      *  A %map relies on unique keys and thus a %pair is only inserted if its
334*03a78d15Sespie      *  first element (the key) is not already present in the %map.
335*03a78d15Sespie      *
336*03a78d15Sespie      *  Insertion requires logarithmic time.
337*03a78d15Sespie     */
338*03a78d15Sespie     pair<iterator,bool>
insert(const value_type & __x)339*03a78d15Sespie     insert(const value_type& __x)
340*03a78d15Sespie     { return _M_t.insert_unique(__x); }
341*03a78d15Sespie 
342*03a78d15Sespie     /**
343*03a78d15Sespie      *  @brief Attempts to insert a std::pair into the %map.
344*03a78d15Sespie      *  @param  position  An iterator that serves as a hint as to where the
345*03a78d15Sespie      *                    pair should be inserted.
346*03a78d15Sespie      *  @param  x  Pair to be inserted (see std::make_pair for easy creation of
347*03a78d15Sespie      *             pairs).
348*03a78d15Sespie      *  @return  An iterator that points to the element with key of @a x (may
349*03a78d15Sespie      *           or may not be the %pair passed in).
350*03a78d15Sespie      *
351*03a78d15Sespie      *  This function is not concerned about whether the insertion took place,
352*03a78d15Sespie      *  and thus does not return a boolean like the single-argument
353*03a78d15Sespie      *  insert() does.  Note that the first parameter is only a hint and can
354*03a78d15Sespie      *  potentially improve the performance of the insertion process.  A bad
355*03a78d15Sespie      *  hint would cause no gains in efficiency.
356*03a78d15Sespie      *
357*03a78d15Sespie      *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
358*03a78d15Sespie      *  for more on "hinting".
359*03a78d15Sespie      *
360*03a78d15Sespie      *  Insertion requires logarithmic time (if the hint is not taken).
361*03a78d15Sespie     */
362*03a78d15Sespie     iterator
insert(iterator position,const value_type & __x)363*03a78d15Sespie     insert(iterator position, const value_type& __x)
364*03a78d15Sespie     { return _M_t.insert_unique(position, __x); }
365*03a78d15Sespie 
366*03a78d15Sespie     /**
367*03a78d15Sespie      *  @brief A template function that attemps to insert a range of elements.
368*03a78d15Sespie      *  @param  first  Iterator pointing to the start of the range to be
369*03a78d15Sespie      *                 inserted.
370*03a78d15Sespie      *  @param  last  Iterator pointing to the end of the range.
371*03a78d15Sespie      *
372*03a78d15Sespie      *  Complexity similar to that of the range constructor.
373*03a78d15Sespie     */
374*03a78d15Sespie     template <typename _InputIterator>
375*03a78d15Sespie       void
insert(_InputIterator __first,_InputIterator __last)376*03a78d15Sespie       insert(_InputIterator __first, _InputIterator __last)
377*03a78d15Sespie       { _M_t.insert_unique(__first, __last); }
378*03a78d15Sespie 
379*03a78d15Sespie     /**
380*03a78d15Sespie      *  @brief Erases an element from a %map.
381*03a78d15Sespie      *  @param  position  An iterator pointing to the element to be erased.
382*03a78d15Sespie      *
383*03a78d15Sespie      *  This function erases an element, pointed to by the given iterator, from
384*03a78d15Sespie      *  a %map.  Note that this function only erases the element, and that if
385*03a78d15Sespie      *  the element is itself a pointer, the pointed-to memory is not touched
386*03a78d15Sespie      *  in any way.  Managing the pointer is the user's responsibilty.
387*03a78d15Sespie     */
388*03a78d15Sespie     void
erase(iterator __position)389*03a78d15Sespie     erase(iterator __position) { _M_t.erase(__position); }
390*03a78d15Sespie 
391*03a78d15Sespie     /**
392*03a78d15Sespie      *  @brief Erases elements according to the provided key.
393*03a78d15Sespie      *  @param  x  Key of element to be erased.
394*03a78d15Sespie      *  @return  The number of elements erased.
395*03a78d15Sespie      *
396*03a78d15Sespie      *  This function erases all the elements located by the given key from
397*03a78d15Sespie      *  a %map.
398*03a78d15Sespie      *  Note that this function only erases the element, and that if
399*03a78d15Sespie      *  the element is itself a pointer, the pointed-to memory is not touched
400*03a78d15Sespie      *  in any way.  Managing the pointer is the user's responsibilty.
401*03a78d15Sespie     */
402*03a78d15Sespie     size_type
erase(const key_type & __x)403*03a78d15Sespie     erase(const key_type& __x) { return _M_t.erase(__x); }
404*03a78d15Sespie 
405*03a78d15Sespie     /**
406*03a78d15Sespie      *  @brief Erases a [first,last) range of elements from a %map.
407*03a78d15Sespie      *  @param  first  Iterator pointing to the start of the range to be erased.
408*03a78d15Sespie      *  @param  last  Iterator pointing to the end of the range to be erased.
409*03a78d15Sespie      *
410*03a78d15Sespie      *  This function erases a sequence of elements from a %map.
411*03a78d15Sespie      *  Note that this function only erases the element, and that if
412*03a78d15Sespie      *  the element is itself a pointer, the pointed-to memory is not touched
413*03a78d15Sespie      *  in any way.  Managing the pointer is the user's responsibilty.
414*03a78d15Sespie     */
415*03a78d15Sespie     void
erase(iterator __first,iterator __last)416*03a78d15Sespie     erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
417*03a78d15Sespie 
418*03a78d15Sespie     /**
419*03a78d15Sespie      *  @brief  Swaps data with another %map.
420*03a78d15Sespie      *  @param  x  A %map of the same element and allocator types.
421*03a78d15Sespie      *
422*03a78d15Sespie      *  This exchanges the elements between two maps in constant time.
423*03a78d15Sespie      *  (It is only swapping a pointer, an integer, and an instance of
424*03a78d15Sespie      *  the @c Compare type (which itself is often stateless and empty), so it
425*03a78d15Sespie      *  should be quite fast.)
426*03a78d15Sespie      *  Note that the global std::swap() function is specialized such that
427*03a78d15Sespie      *  std::swap(m1,m2) will feed to this function.
428*03a78d15Sespie     */
429*03a78d15Sespie     void
swap(map & __x)430*03a78d15Sespie     swap(map& __x) { _M_t.swap(__x._M_t); }
431*03a78d15Sespie 
432*03a78d15Sespie     /**
433*03a78d15Sespie      *  Erases all elements in a %map.  Note that this function only erases
434*03a78d15Sespie      *  the elements, and that if the elements themselves are pointers, the
435*03a78d15Sespie      *  pointed-to memory is not touched in any way.  Managing the pointer is
436*03a78d15Sespie      *  the user's responsibilty.
437*03a78d15Sespie     */
438*03a78d15Sespie     void
clear()439*03a78d15Sespie     clear() { _M_t.clear(); }
440*03a78d15Sespie 
441*03a78d15Sespie     // observers
442*03a78d15Sespie     /**
443*03a78d15Sespie      *  Returns the key comparison object out of which the %map was constructed.
444*03a78d15Sespie     */
445*03a78d15Sespie     key_compare
key_comp()446*03a78d15Sespie     key_comp() const { return _M_t.key_comp(); }
447*03a78d15Sespie 
448*03a78d15Sespie     /**
449*03a78d15Sespie      *  Returns a value comparison object, built from the key comparison
450*03a78d15Sespie      *  object out of which the %map was constructed.
451*03a78d15Sespie     */
452*03a78d15Sespie     value_compare
value_comp()453*03a78d15Sespie     value_comp() const { return value_compare(_M_t.key_comp()); }
454*03a78d15Sespie 
455*03a78d15Sespie     // [23.3.1.3] map operations
456*03a78d15Sespie     /**
457*03a78d15Sespie      *  @brief Tries to locate an element in a %map.
458*03a78d15Sespie      *  @param  x  Key of (key, value) %pair to be located.
459*03a78d15Sespie      *  @return  Iterator pointing to sought-after element, or end() if not
460*03a78d15Sespie      *           found.
461*03a78d15Sespie      *
462*03a78d15Sespie      *  This function takes a key and tries to locate the element with which
463*03a78d15Sespie      *  the key matches.  If successful the function returns an iterator
464*03a78d15Sespie      *  pointing to the sought after %pair.  If unsuccessful it returns the
465*03a78d15Sespie      *  past-the-end ( @c end() ) iterator.
466*03a78d15Sespie     */
467*03a78d15Sespie     iterator
find(const key_type & __x)468*03a78d15Sespie     find(const key_type& __x) { return _M_t.find(__x); }
469*03a78d15Sespie 
470*03a78d15Sespie     /**
471*03a78d15Sespie      *  @brief Tries to locate an element in a %map.
472*03a78d15Sespie      *  @param  x  Key of (key, value) %pair to be located.
473*03a78d15Sespie      *  @return  Read-only (constant) iterator pointing to sought-after
474*03a78d15Sespie      *           element, or end() if not found.
475*03a78d15Sespie      *
476*03a78d15Sespie      *  This function takes a key and tries to locate the element with which
477*03a78d15Sespie      *  the key matches.  If successful the function returns a constant iterator
478*03a78d15Sespie      *  pointing to the sought after %pair. If unsuccessful it returns the
479*03a78d15Sespie      *  past-the-end ( @c end() ) iterator.
480*03a78d15Sespie     */
481*03a78d15Sespie     const_iterator
find(const key_type & __x)482*03a78d15Sespie     find(const key_type& __x) const { return _M_t.find(__x); }
483*03a78d15Sespie 
484*03a78d15Sespie     /**
485*03a78d15Sespie      *  @brief  Finds the number of elements with given key.
486*03a78d15Sespie      *  @param  x  Key of (key, value) pairs to be located.
487*03a78d15Sespie      *  @return  Number of elements with specified key.
488*03a78d15Sespie      *
489*03a78d15Sespie      *  This function only makes sense for multimaps; for map the result will
490*03a78d15Sespie      *  either be 0 (not present) or 1 (present).
491*03a78d15Sespie     */
492*03a78d15Sespie     size_type
count(const key_type & __x)493*03a78d15Sespie     count(const key_type& __x) const
494*03a78d15Sespie     { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
495*03a78d15Sespie 
496*03a78d15Sespie     /**
497*03a78d15Sespie      *  @brief Finds the beginning of a subsequence matching given key.
498*03a78d15Sespie      *  @param  x  Key of (key, value) pair to be located.
499*03a78d15Sespie      *  @return  Iterator pointing to first element matching given key, or
500*03a78d15Sespie      *           end() if not found.
501*03a78d15Sespie      *
502*03a78d15Sespie      *  This function is useful only with multimaps.  It returns the first
503*03a78d15Sespie      *  element of a subsequence of elements that matches the given key.  If
504*03a78d15Sespie      *  unsuccessful it returns an iterator pointing to the first element that
505*03a78d15Sespie      *  has a greater value than given key or end() if no such element exists.
506*03a78d15Sespie     */
507*03a78d15Sespie     iterator
lower_bound(const key_type & __x)508*03a78d15Sespie     lower_bound(const key_type& __x) { return _M_t.lower_bound(__x); }
509*03a78d15Sespie 
510*03a78d15Sespie     /**
511*03a78d15Sespie      *  @brief Finds the beginning of a subsequence matching given key.
512*03a78d15Sespie      *  @param  x  Key of (key, value) pair to be located.
513*03a78d15Sespie      *  @return  Read-only (constant) iterator pointing to first element
514*03a78d15Sespie      *           matching given key, or end() if not found.
515*03a78d15Sespie      *
516*03a78d15Sespie      *  This function is useful only with multimaps.  It returns the first
517*03a78d15Sespie      *  element of a subsequence of elements that matches the given key.  If
518*03a78d15Sespie      *  unsuccessful the iterator will point to the next greatest element or,
519*03a78d15Sespie      *  if no such greater element exists, to end().
520*03a78d15Sespie     */
521*03a78d15Sespie     const_iterator
lower_bound(const key_type & __x)522*03a78d15Sespie     lower_bound(const key_type& __x) const { return _M_t.lower_bound(__x); }
523*03a78d15Sespie 
524*03a78d15Sespie     /**
525*03a78d15Sespie      *  @brief Finds the end of a subsequence matching given key.
526*03a78d15Sespie      *  @param  x  Key of (key, value) pair to be located.
527*03a78d15Sespie      *  @return Iterator pointing to last element matching given key.
528*03a78d15Sespie      *
529*03a78d15Sespie      *  This function only makes sense with multimaps.
530*03a78d15Sespie     */
531*03a78d15Sespie     iterator
upper_bound(const key_type & __x)532*03a78d15Sespie     upper_bound(const key_type& __x) { return _M_t.upper_bound(__x); }
533*03a78d15Sespie 
534*03a78d15Sespie     /**
535*03a78d15Sespie      *  @brief Finds the end of a subsequence matching given key.
536*03a78d15Sespie      *  @param  x  Key of (key, value) pair to be located.
537*03a78d15Sespie      *  @return  Read-only (constant) iterator pointing to last element matching
538*03a78d15Sespie      *           given key.
539*03a78d15Sespie      *
540*03a78d15Sespie      *  This function only makes sense with multimaps.
541*03a78d15Sespie     */
542*03a78d15Sespie     const_iterator
upper_bound(const key_type & __x)543*03a78d15Sespie     upper_bound(const key_type& __x) const
544*03a78d15Sespie     { return _M_t.upper_bound(__x); }
545*03a78d15Sespie 
546*03a78d15Sespie     /**
547*03a78d15Sespie      *  @brief Finds a subsequence matching given key.
548*03a78d15Sespie      *  @param  x  Key of (key, value) pairs to be located.
549*03a78d15Sespie      *  @return  Pair of iterators that possibly points to the subsequence
550*03a78d15Sespie      *           matching given key.
551*03a78d15Sespie      *
552*03a78d15Sespie      *  This function returns a pair of which the first
553*03a78d15Sespie      *  element possibly points to the first element matching the given key
554*03a78d15Sespie      *  and the second element possibly points to the last element matching the
555*03a78d15Sespie      *  given key.  If unsuccessful the first element of the returned pair will
556*03a78d15Sespie      *  contain an iterator pointing to the next greatest element or, if no such
557*03a78d15Sespie      *  greater element exists, to end().
558*03a78d15Sespie      *
559*03a78d15Sespie      *  This function only makes sense for multimaps.
560*03a78d15Sespie     */
561*03a78d15Sespie     pair<iterator,iterator>
equal_range(const key_type & __x)562*03a78d15Sespie     equal_range(const key_type& __x)
563*03a78d15Sespie     { return _M_t.equal_range(__x); }
564*03a78d15Sespie 
565*03a78d15Sespie     /**
566*03a78d15Sespie      *  @brief Finds a subsequence matching given key.
567*03a78d15Sespie      *  @param  x  Key of (key, value) pairs to be located.
568*03a78d15Sespie      *  @return  Pair of read-only (constant) iterators that possibly points to
569*03a78d15Sespie      *           the subsequence matching given key.
570*03a78d15Sespie      *
571*03a78d15Sespie      *  This function returns a pair of which the first
572*03a78d15Sespie      *  element possibly points to the first element matching the given key
573*03a78d15Sespie      *  and the second element possibly points to the last element matching the
574*03a78d15Sespie      *  given key.  If unsuccessful the first element of the returned pair will
575*03a78d15Sespie      *  contain an iterator pointing to the next greatest element or, if no such
576*03a78d15Sespie      *  a greater element exists, to end().
577*03a78d15Sespie      *
578*03a78d15Sespie      *  This function only makes sense for multimaps.
579*03a78d15Sespie     */
580*03a78d15Sespie     pair<const_iterator,const_iterator>
equal_range(const key_type & __x)581*03a78d15Sespie     equal_range(const key_type& __x) const
582*03a78d15Sespie     { return _M_t.equal_range(__x); }
583*03a78d15Sespie 
584*03a78d15Sespie     template <typename _K1, typename _T1, typename _C1, typename _A1>
585*03a78d15Sespie     friend bool operator== (const map<_K1,_T1,_C1,_A1>&,
586*03a78d15Sespie                             const map<_K1,_T1,_C1,_A1>&);
587*03a78d15Sespie     template <typename _K1, typename _T1, typename _C1, typename _A1>
588*03a78d15Sespie     friend bool operator< (const map<_K1,_T1,_C1,_A1>&,
589*03a78d15Sespie                            const map<_K1,_T1,_C1,_A1>&);
590*03a78d15Sespie   };
591*03a78d15Sespie 
592*03a78d15Sespie 
593*03a78d15Sespie   /**
594*03a78d15Sespie    *  @brief  Map equality comparison.
595*03a78d15Sespie    *  @param  x  A %map.
596*03a78d15Sespie    *  @param  y  A %map of the same type as @a x.
597*03a78d15Sespie    *  @return  True iff the size and elements of the maps are equal.
598*03a78d15Sespie    *
599*03a78d15Sespie    *  This is an equivalence relation.  It is linear in the size of the
600*03a78d15Sespie    *  maps.  Maps are considered equivalent if their sizes are equal,
601*03a78d15Sespie    *  and if corresponding elements compare equal.
602*03a78d15Sespie   */
603*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
604*03a78d15Sespie     inline bool
605*03a78d15Sespie     operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x,
606*03a78d15Sespie                const map<_Key,_Tp,_Compare,_Alloc>& __y)
607*03a78d15Sespie     { return __x._M_t == __y._M_t; }
608*03a78d15Sespie 
609*03a78d15Sespie   /**
610*03a78d15Sespie    *  @brief  Map ordering relation.
611*03a78d15Sespie    *  @param  x  A %map.
612*03a78d15Sespie    *  @param  y  A %map of the same type as @a x.
613*03a78d15Sespie    *  @return  True iff @a x is lexographically less than @a y.
614*03a78d15Sespie    *
615*03a78d15Sespie    *  This is a total ordering relation.  It is linear in the size of the
616*03a78d15Sespie    *  maps.  The elements must be comparable with @c <.
617*03a78d15Sespie    *
618*03a78d15Sespie    *  See std::lexographical_compare() for how the determination is made.
619*03a78d15Sespie   */
620*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
621*03a78d15Sespie     inline bool
622*03a78d15Sespie     operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x,
623*03a78d15Sespie               const map<_Key,_Tp,_Compare,_Alloc>& __y)
624*03a78d15Sespie     { return __x._M_t < __y._M_t; }
625*03a78d15Sespie 
626*03a78d15Sespie   /// Based on operator==
627*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
628*03a78d15Sespie     inline bool
629*03a78d15Sespie     operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
630*03a78d15Sespie                const map<_Key,_Tp,_Compare,_Alloc>& __y)
631*03a78d15Sespie     { return !(__x == __y); }
632*03a78d15Sespie 
633*03a78d15Sespie   /// Based on operator<
634*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
635*03a78d15Sespie     inline bool
636*03a78d15Sespie     operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x,
637*03a78d15Sespie               const map<_Key,_Tp,_Compare,_Alloc>& __y)
638*03a78d15Sespie     { return __y < __x; }
639*03a78d15Sespie 
640*03a78d15Sespie   /// Based on operator<
641*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
642*03a78d15Sespie     inline bool
643*03a78d15Sespie     operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
644*03a78d15Sespie                const map<_Key,_Tp,_Compare,_Alloc>& __y)
645*03a78d15Sespie     { return !(__y < __x); }
646*03a78d15Sespie 
647*03a78d15Sespie   /// Based on operator<
648*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
649*03a78d15Sespie     inline bool
650*03a78d15Sespie     operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
651*03a78d15Sespie                const map<_Key,_Tp,_Compare,_Alloc>& __y)
652*03a78d15Sespie     { return !(__x < __y); }
653*03a78d15Sespie 
654*03a78d15Sespie   /// See std::map::swap().
655*03a78d15Sespie   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
656*03a78d15Sespie     inline void
swap(map<_Key,_Tp,_Compare,_Alloc> & __x,map<_Key,_Tp,_Compare,_Alloc> & __y)657*03a78d15Sespie     swap(map<_Key,_Tp,_Compare,_Alloc>& __x, map<_Key,_Tp,_Compare,_Alloc>& __y)
658*03a78d15Sespie     { __x.swap(__y); }
659*03a78d15Sespie } // namespace std
660*03a78d15Sespie 
661*03a78d15Sespie #endif /* __GLIBCPP_INTERNAL_MAP_H */
662