xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/bits/stl_map.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino // Map implementation -*- C++ -*-
2*e4b17023SJohn Marino 
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4*e4b17023SJohn Marino // 2011, 2012 Free Software Foundation, Inc.
5*e4b17023SJohn Marino //
6*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library.  This library is free
7*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
8*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
9*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino // any later version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*e4b17023SJohn Marino // GNU General Public License for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
18*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
19*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
22*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
23*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
25*e4b17023SJohn Marino 
26*e4b17023SJohn Marino /*
27*e4b17023SJohn Marino  *
28*e4b17023SJohn Marino  * Copyright (c) 1994
29*e4b17023SJohn Marino  * Hewlett-Packard Company
30*e4b17023SJohn Marino  *
31*e4b17023SJohn Marino  * Permission to use, copy, modify, distribute and sell this software
32*e4b17023SJohn Marino  * and its documentation for any purpose is hereby granted without fee,
33*e4b17023SJohn Marino  * provided that the above copyright notice appear in all copies and
34*e4b17023SJohn Marino  * that both that copyright notice and this permission notice appear
35*e4b17023SJohn Marino  * in supporting documentation.  Hewlett-Packard Company makes no
36*e4b17023SJohn Marino  * representations about the suitability of this software for any
37*e4b17023SJohn Marino  * purpose.  It is provided "as is" without express or implied warranty.
38*e4b17023SJohn Marino  *
39*e4b17023SJohn Marino  *
40*e4b17023SJohn Marino  * Copyright (c) 1996,1997
41*e4b17023SJohn Marino  * Silicon Graphics Computer Systems, Inc.
42*e4b17023SJohn Marino  *
43*e4b17023SJohn Marino  * Permission to use, copy, modify, distribute and sell this software
44*e4b17023SJohn Marino  * and its documentation for any purpose is hereby granted without fee,
45*e4b17023SJohn Marino  * provided that the above copyright notice appear in all copies and
46*e4b17023SJohn Marino  * that both that copyright notice and this permission notice appear
47*e4b17023SJohn Marino  * in supporting documentation.  Silicon Graphics makes no
48*e4b17023SJohn Marino  * representations about the suitability of this software for any
49*e4b17023SJohn Marino  * purpose.  It is provided "as is" without express or implied warranty.
50*e4b17023SJohn Marino  */
51*e4b17023SJohn Marino 
52*e4b17023SJohn Marino /** @file bits/stl_map.h
53*e4b17023SJohn Marino  *  This is an internal header file, included by other library headers.
54*e4b17023SJohn Marino  *  Do not attempt to use it directly. @headername{map}
55*e4b17023SJohn Marino  */
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino #ifndef _STL_MAP_H
58*e4b17023SJohn Marino #define _STL_MAP_H 1
59*e4b17023SJohn Marino 
60*e4b17023SJohn Marino #include <bits/functexcept.h>
61*e4b17023SJohn Marino #include <bits/concept_check.h>
62*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
63*e4b17023SJohn Marino #include <initializer_list>
64*e4b17023SJohn Marino #endif
65*e4b17023SJohn Marino 
_GLIBCXX_VISIBILITY(default)66*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
67*e4b17023SJohn Marino {
68*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69*e4b17023SJohn Marino 
70*e4b17023SJohn Marino   /**
71*e4b17023SJohn Marino    *  @brief A standard container made up of (key,value) pairs, which can be
72*e4b17023SJohn Marino    *  retrieved based on a key, in logarithmic time.
73*e4b17023SJohn Marino    *
74*e4b17023SJohn Marino    *  @ingroup associative_containers
75*e4b17023SJohn Marino    *
76*e4b17023SJohn Marino    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
77*e4b17023SJohn Marino    *  <a href="tables.html#66">reversible container</a>, and an
78*e4b17023SJohn Marino    *  <a href="tables.html#69">associative container</a> (using unique keys).
79*e4b17023SJohn Marino    *  For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
80*e4b17023SJohn Marino    *  value_type is std::pair<const Key,T>.
81*e4b17023SJohn Marino    *
82*e4b17023SJohn Marino    *  Maps support bidirectional iterators.
83*e4b17023SJohn Marino    *
84*e4b17023SJohn Marino    *  The private tree data is declared exactly the same way for map and
85*e4b17023SJohn Marino    *  multimap; the distinction is made entirely in how the tree functions are
86*e4b17023SJohn Marino    *  called (*_unique versus *_equal, same as the standard).
87*e4b17023SJohn Marino   */
88*e4b17023SJohn Marino   template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
89*e4b17023SJohn Marino             typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
90*e4b17023SJohn Marino     class map
91*e4b17023SJohn Marino     {
92*e4b17023SJohn Marino     public:
93*e4b17023SJohn Marino       typedef _Key                                          key_type;
94*e4b17023SJohn Marino       typedef _Tp                                           mapped_type;
95*e4b17023SJohn Marino       typedef std::pair<const _Key, _Tp>                    value_type;
96*e4b17023SJohn Marino       typedef _Compare                                      key_compare;
97*e4b17023SJohn Marino       typedef _Alloc                                        allocator_type;
98*e4b17023SJohn Marino 
99*e4b17023SJohn Marino     private:
100*e4b17023SJohn Marino       // concept requirements
101*e4b17023SJohn Marino       typedef typename _Alloc::value_type                   _Alloc_value_type;
102*e4b17023SJohn Marino       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
103*e4b17023SJohn Marino       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
104*e4b17023SJohn Marino 				_BinaryFunctionConcept)
105*e4b17023SJohn Marino       __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
106*e4b17023SJohn Marino 
107*e4b17023SJohn Marino     public:
108*e4b17023SJohn Marino       class value_compare
109*e4b17023SJohn Marino       : public std::binary_function<value_type, value_type, bool>
110*e4b17023SJohn Marino       {
111*e4b17023SJohn Marino 	friend class map<_Key, _Tp, _Compare, _Alloc>;
112*e4b17023SJohn Marino       protected:
113*e4b17023SJohn Marino 	_Compare comp;
114*e4b17023SJohn Marino 
115*e4b17023SJohn Marino 	value_compare(_Compare __c)
116*e4b17023SJohn Marino 	: comp(__c) { }
117*e4b17023SJohn Marino 
118*e4b17023SJohn Marino       public:
119*e4b17023SJohn Marino 	bool operator()(const value_type& __x, const value_type& __y) const
120*e4b17023SJohn Marino 	{ return comp(__x.first, __y.first); }
121*e4b17023SJohn Marino       };
122*e4b17023SJohn Marino 
123*e4b17023SJohn Marino     private:
124*e4b17023SJohn Marino       /// This turns a red-black tree into a [multi]map.
125*e4b17023SJohn Marino       typedef typename _Alloc::template rebind<value_type>::other
126*e4b17023SJohn Marino         _Pair_alloc_type;
127*e4b17023SJohn Marino 
128*e4b17023SJohn Marino       typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
129*e4b17023SJohn Marino 		       key_compare, _Pair_alloc_type> _Rep_type;
130*e4b17023SJohn Marino 
131*e4b17023SJohn Marino       /// The actual tree structure.
132*e4b17023SJohn Marino       _Rep_type _M_t;
133*e4b17023SJohn Marino 
134*e4b17023SJohn Marino     public:
135*e4b17023SJohn Marino       // many of these are specified differently in ISO, but the following are
136*e4b17023SJohn Marino       // "functionally equivalent"
137*e4b17023SJohn Marino       typedef typename _Pair_alloc_type::pointer         pointer;
138*e4b17023SJohn Marino       typedef typename _Pair_alloc_type::const_pointer   const_pointer;
139*e4b17023SJohn Marino       typedef typename _Pair_alloc_type::reference       reference;
140*e4b17023SJohn Marino       typedef typename _Pair_alloc_type::const_reference const_reference;
141*e4b17023SJohn Marino       typedef typename _Rep_type::iterator               iterator;
142*e4b17023SJohn Marino       typedef typename _Rep_type::const_iterator         const_iterator;
143*e4b17023SJohn Marino       typedef typename _Rep_type::size_type              size_type;
144*e4b17023SJohn Marino       typedef typename _Rep_type::difference_type        difference_type;
145*e4b17023SJohn Marino       typedef typename _Rep_type::reverse_iterator       reverse_iterator;
146*e4b17023SJohn Marino       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
147*e4b17023SJohn Marino 
148*e4b17023SJohn Marino       // [23.3.1.1] construct/copy/destroy
149*e4b17023SJohn Marino       // (get_allocator() is normally listed in this section, but seems to have
150*e4b17023SJohn Marino       // been accidentally omitted in the printed standard)
151*e4b17023SJohn Marino       /**
152*e4b17023SJohn Marino        *  @brief  Default constructor creates no elements.
153*e4b17023SJohn Marino        */
154*e4b17023SJohn Marino       map()
155*e4b17023SJohn Marino       : _M_t() { }
156*e4b17023SJohn Marino 
157*e4b17023SJohn Marino       /**
158*e4b17023SJohn Marino        *  @brief  Creates a %map with no elements.
159*e4b17023SJohn Marino        *  @param  __comp  A comparison object.
160*e4b17023SJohn Marino        *  @param  __a  An allocator object.
161*e4b17023SJohn Marino        */
162*e4b17023SJohn Marino       explicit
163*e4b17023SJohn Marino       map(const _Compare& __comp,
164*e4b17023SJohn Marino 	  const allocator_type& __a = allocator_type())
165*e4b17023SJohn Marino       : _M_t(__comp, _Pair_alloc_type(__a)) { }
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino       /**
168*e4b17023SJohn Marino        *  @brief  %Map copy constructor.
169*e4b17023SJohn Marino        *  @param  __x  A %map of identical element and allocator types.
170*e4b17023SJohn Marino        *
171*e4b17023SJohn Marino        *  The newly-created %map uses a copy of the allocation object
172*e4b17023SJohn Marino        *  used by @a __x.
173*e4b17023SJohn Marino        */
174*e4b17023SJohn Marino       map(const map& __x)
175*e4b17023SJohn Marino       : _M_t(__x._M_t) { }
176*e4b17023SJohn Marino 
177*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
178*e4b17023SJohn Marino       /**
179*e4b17023SJohn Marino        *  @brief  %Map move constructor.
180*e4b17023SJohn Marino        *  @param  __x  A %map of identical element and allocator types.
181*e4b17023SJohn Marino        *
182*e4b17023SJohn Marino        *  The newly-created %map contains the exact contents of @a __x.
183*e4b17023SJohn Marino        *  The contents of @a __x are a valid, but unspecified %map.
184*e4b17023SJohn Marino        */
185*e4b17023SJohn Marino       map(map&& __x)
186*e4b17023SJohn Marino       noexcept(is_nothrow_copy_constructible<_Compare>::value)
187*e4b17023SJohn Marino       : _M_t(std::move(__x._M_t)) { }
188*e4b17023SJohn Marino 
189*e4b17023SJohn Marino       /**
190*e4b17023SJohn Marino        *  @brief  Builds a %map from an initializer_list.
191*e4b17023SJohn Marino        *  @param  __l  An initializer_list.
192*e4b17023SJohn Marino        *  @param  __comp  A comparison object.
193*e4b17023SJohn Marino        *  @param  __a  An allocator object.
194*e4b17023SJohn Marino        *
195*e4b17023SJohn Marino        *  Create a %map consisting of copies of the elements in the
196*e4b17023SJohn Marino        *  initializer_list @a __l.
197*e4b17023SJohn Marino        *  This is linear in N if the range is already sorted, and NlogN
198*e4b17023SJohn Marino        *  otherwise (where N is @a __l.size()).
199*e4b17023SJohn Marino        */
200*e4b17023SJohn Marino       map(initializer_list<value_type> __l,
201*e4b17023SJohn Marino 	  const _Compare& __comp = _Compare(),
202*e4b17023SJohn Marino 	  const allocator_type& __a = allocator_type())
203*e4b17023SJohn Marino       : _M_t(__comp, _Pair_alloc_type(__a))
204*e4b17023SJohn Marino       { _M_t._M_insert_unique(__l.begin(), __l.end()); }
205*e4b17023SJohn Marino #endif
206*e4b17023SJohn Marino 
207*e4b17023SJohn Marino       /**
208*e4b17023SJohn Marino        *  @brief  Builds a %map from a range.
209*e4b17023SJohn Marino        *  @param  __first  An input iterator.
210*e4b17023SJohn Marino        *  @param  __last  An input iterator.
211*e4b17023SJohn Marino        *
212*e4b17023SJohn Marino        *  Create a %map consisting of copies of the elements from
213*e4b17023SJohn Marino        *  [__first,__last).  This is linear in N if the range is
214*e4b17023SJohn Marino        *  already sorted, and NlogN otherwise (where N is
215*e4b17023SJohn Marino        *  distance(__first,__last)).
216*e4b17023SJohn Marino        */
217*e4b17023SJohn Marino       template<typename _InputIterator>
218*e4b17023SJohn Marino         map(_InputIterator __first, _InputIterator __last)
219*e4b17023SJohn Marino 	: _M_t()
220*e4b17023SJohn Marino         { _M_t._M_insert_unique(__first, __last); }
221*e4b17023SJohn Marino 
222*e4b17023SJohn Marino       /**
223*e4b17023SJohn Marino        *  @brief  Builds a %map from a range.
224*e4b17023SJohn Marino        *  @param  __first  An input iterator.
225*e4b17023SJohn Marino        *  @param  __last  An input iterator.
226*e4b17023SJohn Marino        *  @param  __comp  A comparison functor.
227*e4b17023SJohn Marino        *  @param  __a  An allocator object.
228*e4b17023SJohn Marino        *
229*e4b17023SJohn Marino        *  Create a %map consisting of copies of the elements from
230*e4b17023SJohn Marino        *  [__first,__last).  This is linear in N if the range is
231*e4b17023SJohn Marino        *  already sorted, and NlogN otherwise (where N is
232*e4b17023SJohn Marino        *  distance(__first,__last)).
233*e4b17023SJohn Marino        */
234*e4b17023SJohn Marino       template<typename _InputIterator>
235*e4b17023SJohn Marino         map(_InputIterator __first, _InputIterator __last,
236*e4b17023SJohn Marino 	    const _Compare& __comp,
237*e4b17023SJohn Marino 	    const allocator_type& __a = allocator_type())
238*e4b17023SJohn Marino 	: _M_t(__comp, _Pair_alloc_type(__a))
239*e4b17023SJohn Marino         { _M_t._M_insert_unique(__first, __last); }
240*e4b17023SJohn Marino 
241*e4b17023SJohn Marino       // FIXME There is no dtor declared, but we should have something
242*e4b17023SJohn Marino       // generated by Doxygen.  I don't know what tags to add to this
243*e4b17023SJohn Marino       // paragraph to make that happen:
244*e4b17023SJohn Marino       /**
245*e4b17023SJohn Marino        *  The dtor only erases the elements, and note that if the elements
246*e4b17023SJohn Marino        *  themselves are pointers, the pointed-to memory is not touched in any
247*e4b17023SJohn Marino        *  way.  Managing the pointer is the user's responsibility.
248*e4b17023SJohn Marino        */
249*e4b17023SJohn Marino 
250*e4b17023SJohn Marino       /**
251*e4b17023SJohn Marino        *  @brief  %Map assignment operator.
252*e4b17023SJohn Marino        *  @param  __x  A %map of identical element and allocator types.
253*e4b17023SJohn Marino        *
254*e4b17023SJohn Marino        *  All the elements of @a __x are copied, but unlike the copy
255*e4b17023SJohn Marino        *  constructor, the allocator object is not copied.
256*e4b17023SJohn Marino        */
257*e4b17023SJohn Marino       map&
258*e4b17023SJohn Marino       operator=(const map& __x)
259*e4b17023SJohn Marino       {
260*e4b17023SJohn Marino 	_M_t = __x._M_t;
261*e4b17023SJohn Marino 	return *this;
262*e4b17023SJohn Marino       }
263*e4b17023SJohn Marino 
264*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
265*e4b17023SJohn Marino       /**
266*e4b17023SJohn Marino        *  @brief  %Map move assignment operator.
267*e4b17023SJohn Marino        *  @param  __x  A %map of identical element and allocator types.
268*e4b17023SJohn Marino        *
269*e4b17023SJohn Marino        *  The contents of @a __x are moved into this map (without copying).
270*e4b17023SJohn Marino        *  @a __x is a valid, but unspecified %map.
271*e4b17023SJohn Marino        */
272*e4b17023SJohn Marino       map&
273*e4b17023SJohn Marino       operator=(map&& __x)
274*e4b17023SJohn Marino       {
275*e4b17023SJohn Marino 	// NB: DR 1204.
276*e4b17023SJohn Marino 	// NB: DR 675.
277*e4b17023SJohn Marino 	this->clear();
278*e4b17023SJohn Marino 	this->swap(__x);
279*e4b17023SJohn Marino 	return *this;
280*e4b17023SJohn Marino       }
281*e4b17023SJohn Marino 
282*e4b17023SJohn Marino       /**
283*e4b17023SJohn Marino        *  @brief  %Map list assignment operator.
284*e4b17023SJohn Marino        *  @param  __l  An initializer_list.
285*e4b17023SJohn Marino        *
286*e4b17023SJohn Marino        *  This function fills a %map with copies of the elements in the
287*e4b17023SJohn Marino        *  initializer list @a __l.
288*e4b17023SJohn Marino        *
289*e4b17023SJohn Marino        *  Note that the assignment completely changes the %map and
290*e4b17023SJohn Marino        *  that the resulting %map's size is the same as the number
291*e4b17023SJohn Marino        *  of elements assigned.  Old data may be lost.
292*e4b17023SJohn Marino        */
293*e4b17023SJohn Marino       map&
294*e4b17023SJohn Marino       operator=(initializer_list<value_type> __l)
295*e4b17023SJohn Marino       {
296*e4b17023SJohn Marino 	this->clear();
297*e4b17023SJohn Marino 	this->insert(__l.begin(), __l.end());
298*e4b17023SJohn Marino 	return *this;
299*e4b17023SJohn Marino       }
300*e4b17023SJohn Marino #endif
301*e4b17023SJohn Marino 
302*e4b17023SJohn Marino       /// Get a copy of the memory allocation object.
303*e4b17023SJohn Marino       allocator_type
304*e4b17023SJohn Marino       get_allocator() const _GLIBCXX_NOEXCEPT
305*e4b17023SJohn Marino       { return allocator_type(_M_t.get_allocator()); }
306*e4b17023SJohn Marino 
307*e4b17023SJohn Marino       // iterators
308*e4b17023SJohn Marino       /**
309*e4b17023SJohn Marino        *  Returns a read/write iterator that points to the first pair in the
310*e4b17023SJohn Marino        *  %map.
311*e4b17023SJohn Marino        *  Iteration is done in ascending order according to the keys.
312*e4b17023SJohn Marino        */
313*e4b17023SJohn Marino       iterator
314*e4b17023SJohn Marino       begin() _GLIBCXX_NOEXCEPT
315*e4b17023SJohn Marino       { return _M_t.begin(); }
316*e4b17023SJohn Marino 
317*e4b17023SJohn Marino       /**
318*e4b17023SJohn Marino        *  Returns a read-only (constant) iterator that points to the first pair
319*e4b17023SJohn Marino        *  in the %map.  Iteration is done in ascending order according to the
320*e4b17023SJohn Marino        *  keys.
321*e4b17023SJohn Marino        */
322*e4b17023SJohn Marino       const_iterator
323*e4b17023SJohn Marino       begin() const _GLIBCXX_NOEXCEPT
324*e4b17023SJohn Marino       { return _M_t.begin(); }
325*e4b17023SJohn Marino 
326*e4b17023SJohn Marino       /**
327*e4b17023SJohn Marino        *  Returns a read/write iterator that points one past the last
328*e4b17023SJohn Marino        *  pair in the %map.  Iteration is done in ascending order
329*e4b17023SJohn Marino        *  according to the keys.
330*e4b17023SJohn Marino        */
331*e4b17023SJohn Marino       iterator
332*e4b17023SJohn Marino       end() _GLIBCXX_NOEXCEPT
333*e4b17023SJohn Marino       { return _M_t.end(); }
334*e4b17023SJohn Marino 
335*e4b17023SJohn Marino       /**
336*e4b17023SJohn Marino        *  Returns a read-only (constant) iterator that points one past the last
337*e4b17023SJohn Marino        *  pair in the %map.  Iteration is done in ascending order according to
338*e4b17023SJohn Marino        *  the keys.
339*e4b17023SJohn Marino        */
340*e4b17023SJohn Marino       const_iterator
341*e4b17023SJohn Marino       end() const _GLIBCXX_NOEXCEPT
342*e4b17023SJohn Marino       { return _M_t.end(); }
343*e4b17023SJohn Marino 
344*e4b17023SJohn Marino       /**
345*e4b17023SJohn Marino        *  Returns a read/write reverse iterator that points to the last pair in
346*e4b17023SJohn Marino        *  the %map.  Iteration is done in descending order according to the
347*e4b17023SJohn Marino        *  keys.
348*e4b17023SJohn Marino        */
349*e4b17023SJohn Marino       reverse_iterator
350*e4b17023SJohn Marino       rbegin() _GLIBCXX_NOEXCEPT
351*e4b17023SJohn Marino       { return _M_t.rbegin(); }
352*e4b17023SJohn Marino 
353*e4b17023SJohn Marino       /**
354*e4b17023SJohn Marino        *  Returns a read-only (constant) reverse iterator that points to the
355*e4b17023SJohn Marino        *  last pair in the %map.  Iteration is done in descending order
356*e4b17023SJohn Marino        *  according to the keys.
357*e4b17023SJohn Marino        */
358*e4b17023SJohn Marino       const_reverse_iterator
359*e4b17023SJohn Marino       rbegin() const _GLIBCXX_NOEXCEPT
360*e4b17023SJohn Marino       { return _M_t.rbegin(); }
361*e4b17023SJohn Marino 
362*e4b17023SJohn Marino       /**
363*e4b17023SJohn Marino        *  Returns a read/write reverse iterator that points to one before the
364*e4b17023SJohn Marino        *  first pair in the %map.  Iteration is done in descending order
365*e4b17023SJohn Marino        *  according to the keys.
366*e4b17023SJohn Marino        */
367*e4b17023SJohn Marino       reverse_iterator
368*e4b17023SJohn Marino       rend() _GLIBCXX_NOEXCEPT
369*e4b17023SJohn Marino       { return _M_t.rend(); }
370*e4b17023SJohn Marino 
371*e4b17023SJohn Marino       /**
372*e4b17023SJohn Marino        *  Returns a read-only (constant) reverse iterator that points to one
373*e4b17023SJohn Marino        *  before the first pair in the %map.  Iteration is done in descending
374*e4b17023SJohn Marino        *  order according to the keys.
375*e4b17023SJohn Marino        */
376*e4b17023SJohn Marino       const_reverse_iterator
377*e4b17023SJohn Marino       rend() const _GLIBCXX_NOEXCEPT
378*e4b17023SJohn Marino       { return _M_t.rend(); }
379*e4b17023SJohn Marino 
380*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
381*e4b17023SJohn Marino       /**
382*e4b17023SJohn Marino        *  Returns a read-only (constant) iterator that points to the first pair
383*e4b17023SJohn Marino        *  in the %map.  Iteration is done in ascending order according to the
384*e4b17023SJohn Marino        *  keys.
385*e4b17023SJohn Marino        */
386*e4b17023SJohn Marino       const_iterator
387*e4b17023SJohn Marino       cbegin() const noexcept
388*e4b17023SJohn Marino       { return _M_t.begin(); }
389*e4b17023SJohn Marino 
390*e4b17023SJohn Marino       /**
391*e4b17023SJohn Marino        *  Returns a read-only (constant) iterator that points one past the last
392*e4b17023SJohn Marino        *  pair in the %map.  Iteration is done in ascending order according to
393*e4b17023SJohn Marino        *  the keys.
394*e4b17023SJohn Marino        */
395*e4b17023SJohn Marino       const_iterator
396*e4b17023SJohn Marino       cend() const noexcept
397*e4b17023SJohn Marino       { return _M_t.end(); }
398*e4b17023SJohn Marino 
399*e4b17023SJohn Marino       /**
400*e4b17023SJohn Marino        *  Returns a read-only (constant) reverse iterator that points to the
401*e4b17023SJohn Marino        *  last pair in the %map.  Iteration is done in descending order
402*e4b17023SJohn Marino        *  according to the keys.
403*e4b17023SJohn Marino        */
404*e4b17023SJohn Marino       const_reverse_iterator
405*e4b17023SJohn Marino       crbegin() const noexcept
406*e4b17023SJohn Marino       { return _M_t.rbegin(); }
407*e4b17023SJohn Marino 
408*e4b17023SJohn Marino       /**
409*e4b17023SJohn Marino        *  Returns a read-only (constant) reverse iterator that points to one
410*e4b17023SJohn Marino        *  before the first pair in the %map.  Iteration is done in descending
411*e4b17023SJohn Marino        *  order according to the keys.
412*e4b17023SJohn Marino        */
413*e4b17023SJohn Marino       const_reverse_iterator
414*e4b17023SJohn Marino       crend() const noexcept
415*e4b17023SJohn Marino       { return _M_t.rend(); }
416*e4b17023SJohn Marino #endif
417*e4b17023SJohn Marino 
418*e4b17023SJohn Marino       // capacity
419*e4b17023SJohn Marino       /** Returns true if the %map is empty.  (Thus begin() would equal
420*e4b17023SJohn Marino        *  end().)
421*e4b17023SJohn Marino       */
422*e4b17023SJohn Marino       bool
423*e4b17023SJohn Marino       empty() const _GLIBCXX_NOEXCEPT
424*e4b17023SJohn Marino       { return _M_t.empty(); }
425*e4b17023SJohn Marino 
426*e4b17023SJohn Marino       /** Returns the size of the %map.  */
427*e4b17023SJohn Marino       size_type
428*e4b17023SJohn Marino       size() const _GLIBCXX_NOEXCEPT
429*e4b17023SJohn Marino       { return _M_t.size(); }
430*e4b17023SJohn Marino 
431*e4b17023SJohn Marino       /** Returns the maximum size of the %map.  */
432*e4b17023SJohn Marino       size_type
433*e4b17023SJohn Marino       max_size() const _GLIBCXX_NOEXCEPT
434*e4b17023SJohn Marino       { return _M_t.max_size(); }
435*e4b17023SJohn Marino 
436*e4b17023SJohn Marino       // [23.3.1.2] element access
437*e4b17023SJohn Marino       /**
438*e4b17023SJohn Marino        *  @brief  Subscript ( @c [] ) access to %map data.
439*e4b17023SJohn Marino        *  @param  __k  The key for which data should be retrieved.
440*e4b17023SJohn Marino        *  @return  A reference to the data of the (key,data) %pair.
441*e4b17023SJohn Marino        *
442*e4b17023SJohn Marino        *  Allows for easy lookup with the subscript ( @c [] )
443*e4b17023SJohn Marino        *  operator.  Returns data associated with the key specified in
444*e4b17023SJohn Marino        *  subscript.  If the key does not exist, a pair with that key
445*e4b17023SJohn Marino        *  is created using default values, which is then returned.
446*e4b17023SJohn Marino        *
447*e4b17023SJohn Marino        *  Lookup requires logarithmic time.
448*e4b17023SJohn Marino        */
449*e4b17023SJohn Marino       mapped_type&
450*e4b17023SJohn Marino       operator[](const key_type& __k)
451*e4b17023SJohn Marino       {
452*e4b17023SJohn Marino 	// concept requirements
453*e4b17023SJohn Marino 	__glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
454*e4b17023SJohn Marino 
455*e4b17023SJohn Marino 	iterator __i = lower_bound(__k);
456*e4b17023SJohn Marino 	// __i->first is greater than or equivalent to __k.
457*e4b17023SJohn Marino 	if (__i == end() || key_comp()(__k, (*__i).first))
458*e4b17023SJohn Marino           __i = insert(__i, value_type(__k, mapped_type()));
459*e4b17023SJohn Marino 	return (*__i).second;
460*e4b17023SJohn Marino       }
461*e4b17023SJohn Marino 
462*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
463*e4b17023SJohn Marino       mapped_type&
464*e4b17023SJohn Marino       operator[](key_type&& __k)
465*e4b17023SJohn Marino       {
466*e4b17023SJohn Marino 	// concept requirements
467*e4b17023SJohn Marino 	__glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
468*e4b17023SJohn Marino 
469*e4b17023SJohn Marino 	iterator __i = lower_bound(__k);
470*e4b17023SJohn Marino 	// __i->first is greater than or equivalent to __k.
471*e4b17023SJohn Marino 	if (__i == end() || key_comp()(__k, (*__i).first))
472*e4b17023SJohn Marino           __i = insert(__i, std::make_pair(std::move(__k), mapped_type()));
473*e4b17023SJohn Marino 	return (*__i).second;
474*e4b17023SJohn Marino       }
475*e4b17023SJohn Marino #endif
476*e4b17023SJohn Marino 
477*e4b17023SJohn Marino       // _GLIBCXX_RESOLVE_LIB_DEFECTS
478*e4b17023SJohn Marino       // DR 464. Suggestion for new member functions in standard containers.
479*e4b17023SJohn Marino       /**
480*e4b17023SJohn Marino        *  @brief  Access to %map data.
481*e4b17023SJohn Marino        *  @param  __k  The key for which data should be retrieved.
482*e4b17023SJohn Marino        *  @return  A reference to the data whose key is equivalent to @a __k, if
483*e4b17023SJohn Marino        *           such a data is present in the %map.
484*e4b17023SJohn Marino        *  @throw  std::out_of_range  If no such data is present.
485*e4b17023SJohn Marino        */
486*e4b17023SJohn Marino       mapped_type&
487*e4b17023SJohn Marino       at(const key_type& __k)
488*e4b17023SJohn Marino       {
489*e4b17023SJohn Marino 	iterator __i = lower_bound(__k);
490*e4b17023SJohn Marino 	if (__i == end() || key_comp()(__k, (*__i).first))
491*e4b17023SJohn Marino 	  __throw_out_of_range(__N("map::at"));
492*e4b17023SJohn Marino 	return (*__i).second;
493*e4b17023SJohn Marino       }
494*e4b17023SJohn Marino 
495*e4b17023SJohn Marino       const mapped_type&
496*e4b17023SJohn Marino       at(const key_type& __k) const
497*e4b17023SJohn Marino       {
498*e4b17023SJohn Marino 	const_iterator __i = lower_bound(__k);
499*e4b17023SJohn Marino 	if (__i == end() || key_comp()(__k, (*__i).first))
500*e4b17023SJohn Marino 	  __throw_out_of_range(__N("map::at"));
501*e4b17023SJohn Marino 	return (*__i).second;
502*e4b17023SJohn Marino       }
503*e4b17023SJohn Marino 
504*e4b17023SJohn Marino       // modifiers
505*e4b17023SJohn Marino       /**
506*e4b17023SJohn Marino        *  @brief Attempts to insert a std::pair into the %map.
507*e4b17023SJohn Marino 
508*e4b17023SJohn Marino        *  @param __x Pair to be inserted (see std::make_pair for easy
509*e4b17023SJohn Marino        *	     creation of pairs).
510*e4b17023SJohn Marino        *
511*e4b17023SJohn Marino        *  @return  A pair, of which the first element is an iterator that
512*e4b17023SJohn Marino        *           points to the possibly inserted pair, and the second is
513*e4b17023SJohn Marino        *           a bool that is true if the pair was actually inserted.
514*e4b17023SJohn Marino        *
515*e4b17023SJohn Marino        *  This function attempts to insert a (key, value) %pair into the %map.
516*e4b17023SJohn Marino        *  A %map relies on unique keys and thus a %pair is only inserted if its
517*e4b17023SJohn Marino        *  first element (the key) is not already present in the %map.
518*e4b17023SJohn Marino        *
519*e4b17023SJohn Marino        *  Insertion requires logarithmic time.
520*e4b17023SJohn Marino        */
521*e4b17023SJohn Marino       std::pair<iterator, bool>
522*e4b17023SJohn Marino       insert(const value_type& __x)
523*e4b17023SJohn Marino       { return _M_t._M_insert_unique(__x); }
524*e4b17023SJohn Marino 
525*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
526*e4b17023SJohn Marino       template<typename _Pair, typename = typename
527*e4b17023SJohn Marino 	       std::enable_if<std::is_constructible<value_type,
528*e4b17023SJohn Marino 						    _Pair&&>::value>::type>
529*e4b17023SJohn Marino         std::pair<iterator, bool>
530*e4b17023SJohn Marino         insert(_Pair&& __x)
531*e4b17023SJohn Marino         { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); }
532*e4b17023SJohn Marino #endif
533*e4b17023SJohn Marino 
534*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
535*e4b17023SJohn Marino       /**
536*e4b17023SJohn Marino        *  @brief Attempts to insert a list of std::pairs into the %map.
537*e4b17023SJohn Marino        *  @param  __list  A std::initializer_list<value_type> of pairs to be
538*e4b17023SJohn Marino        *                  inserted.
539*e4b17023SJohn Marino        *
540*e4b17023SJohn Marino        *  Complexity similar to that of the range constructor.
541*e4b17023SJohn Marino        */
542*e4b17023SJohn Marino       void
543*e4b17023SJohn Marino       insert(std::initializer_list<value_type> __list)
544*e4b17023SJohn Marino       { insert(__list.begin(), __list.end()); }
545*e4b17023SJohn Marino #endif
546*e4b17023SJohn Marino 
547*e4b17023SJohn Marino       /**
548*e4b17023SJohn Marino        *  @brief Attempts to insert a std::pair into the %map.
549*e4b17023SJohn Marino        *  @param  __position  An iterator that serves as a hint as to where the
550*e4b17023SJohn Marino        *                    pair should be inserted.
551*e4b17023SJohn Marino        *  @param  __x  Pair to be inserted (see std::make_pair for easy creation
552*e4b17023SJohn Marino        *               of pairs).
553*e4b17023SJohn Marino        *  @return An iterator that points to the element with key of
554*e4b17023SJohn Marino        *           @a __x (may or may not be the %pair passed in).
555*e4b17023SJohn Marino        *
556*e4b17023SJohn Marino 
557*e4b17023SJohn Marino        *  This function is not concerned about whether the insertion
558*e4b17023SJohn Marino        *  took place, and thus does not return a boolean like the
559*e4b17023SJohn Marino        *  single-argument insert() does.  Note that the first
560*e4b17023SJohn Marino        *  parameter is only a hint and can potentially improve the
561*e4b17023SJohn Marino        *  performance of the insertion process.  A bad hint would
562*e4b17023SJohn Marino        *  cause no gains in efficiency.
563*e4b17023SJohn Marino        *
564*e4b17023SJohn Marino        *  See
565*e4b17023SJohn Marino        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
566*e4b17023SJohn Marino        *  for more on @a hinting.
567*e4b17023SJohn Marino        *
568*e4b17023SJohn Marino        *  Insertion requires logarithmic time (if the hint is not taken).
569*e4b17023SJohn Marino        */
570*e4b17023SJohn Marino       iterator
571*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
572*e4b17023SJohn Marino       insert(const_iterator __position, const value_type& __x)
573*e4b17023SJohn Marino #else
574*e4b17023SJohn Marino       insert(iterator __position, const value_type& __x)
575*e4b17023SJohn Marino #endif
576*e4b17023SJohn Marino       { return _M_t._M_insert_unique_(__position, __x); }
577*e4b17023SJohn Marino 
578*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
579*e4b17023SJohn Marino       template<typename _Pair, typename = typename
580*e4b17023SJohn Marino 	       std::enable_if<std::is_constructible<value_type,
581*e4b17023SJohn Marino 						    _Pair&&>::value>::type>
582*e4b17023SJohn Marino         iterator
583*e4b17023SJohn Marino         insert(const_iterator __position, _Pair&& __x)
584*e4b17023SJohn Marino         { return _M_t._M_insert_unique_(__position,
585*e4b17023SJohn Marino 					std::forward<_Pair>(__x)); }
586*e4b17023SJohn Marino #endif
587*e4b17023SJohn Marino 
588*e4b17023SJohn Marino       /**
589*e4b17023SJohn Marino        *  @brief Template function that attempts to insert a range of elements.
590*e4b17023SJohn Marino        *  @param  __first  Iterator pointing to the start of the range to be
591*e4b17023SJohn Marino        *                   inserted.
592*e4b17023SJohn Marino        *  @param  __last  Iterator pointing to the end of the range.
593*e4b17023SJohn Marino        *
594*e4b17023SJohn Marino        *  Complexity similar to that of the range constructor.
595*e4b17023SJohn Marino        */
596*e4b17023SJohn Marino       template<typename _InputIterator>
597*e4b17023SJohn Marino         void
598*e4b17023SJohn Marino         insert(_InputIterator __first, _InputIterator __last)
599*e4b17023SJohn Marino         { _M_t._M_insert_unique(__first, __last); }
600*e4b17023SJohn Marino 
601*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
602*e4b17023SJohn Marino       // _GLIBCXX_RESOLVE_LIB_DEFECTS
603*e4b17023SJohn Marino       // DR 130. Associative erase should return an iterator.
604*e4b17023SJohn Marino       /**
605*e4b17023SJohn Marino        *  @brief Erases an element from a %map.
606*e4b17023SJohn Marino        *  @param  __position  An iterator pointing to the element to be erased.
607*e4b17023SJohn Marino        *  @return An iterator pointing to the element immediately following
608*e4b17023SJohn Marino        *          @a position prior to the element being erased. If no such
609*e4b17023SJohn Marino        *          element exists, end() is returned.
610*e4b17023SJohn Marino        *
611*e4b17023SJohn Marino        *  This function erases an element, pointed to by the given
612*e4b17023SJohn Marino        *  iterator, from a %map.  Note that this function only erases
613*e4b17023SJohn Marino        *  the element, and that if the element is itself a pointer,
614*e4b17023SJohn Marino        *  the pointed-to memory is not touched in any way.  Managing
615*e4b17023SJohn Marino        *  the pointer is the user's responsibility.
616*e4b17023SJohn Marino        */
617*e4b17023SJohn Marino       iterator
618*e4b17023SJohn Marino       erase(const_iterator __position)
619*e4b17023SJohn Marino       { return _M_t.erase(__position); }
620*e4b17023SJohn Marino 
621*e4b17023SJohn Marino       // LWG 2059.
622*e4b17023SJohn Marino       iterator
623*e4b17023SJohn Marino       erase(iterator __position)
624*e4b17023SJohn Marino       { return _M_t.erase(__position); }
625*e4b17023SJohn Marino #else
626*e4b17023SJohn Marino       /**
627*e4b17023SJohn Marino        *  @brief Erases an element from a %map.
628*e4b17023SJohn Marino        *  @param  __position  An iterator pointing to the element to be erased.
629*e4b17023SJohn Marino        *
630*e4b17023SJohn Marino        *  This function erases an element, pointed to by the given
631*e4b17023SJohn Marino        *  iterator, from a %map.  Note that this function only erases
632*e4b17023SJohn Marino        *  the element, and that if the element is itself a pointer,
633*e4b17023SJohn Marino        *  the pointed-to memory is not touched in any way.  Managing
634*e4b17023SJohn Marino        *  the pointer is the user's responsibility.
635*e4b17023SJohn Marino        */
636*e4b17023SJohn Marino       void
637*e4b17023SJohn Marino       erase(iterator __position)
638*e4b17023SJohn Marino       { _M_t.erase(__position); }
639*e4b17023SJohn Marino #endif
640*e4b17023SJohn Marino 
641*e4b17023SJohn Marino       /**
642*e4b17023SJohn Marino        *  @brief Erases elements according to the provided key.
643*e4b17023SJohn Marino        *  @param  __x  Key of element to be erased.
644*e4b17023SJohn Marino        *  @return  The number of elements erased.
645*e4b17023SJohn Marino        *
646*e4b17023SJohn Marino        *  This function erases all the elements located by the given key from
647*e4b17023SJohn Marino        *  a %map.
648*e4b17023SJohn Marino        *  Note that this function only erases the element, and that if
649*e4b17023SJohn Marino        *  the element is itself a pointer, the pointed-to memory is not touched
650*e4b17023SJohn Marino        *  in any way.  Managing the pointer is the user's responsibility.
651*e4b17023SJohn Marino        */
652*e4b17023SJohn Marino       size_type
653*e4b17023SJohn Marino       erase(const key_type& __x)
654*e4b17023SJohn Marino       { return _M_t.erase(__x); }
655*e4b17023SJohn Marino 
656*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
657*e4b17023SJohn Marino       // _GLIBCXX_RESOLVE_LIB_DEFECTS
658*e4b17023SJohn Marino       // DR 130. Associative erase should return an iterator.
659*e4b17023SJohn Marino       /**
660*e4b17023SJohn Marino        *  @brief Erases a [first,last) range of elements from a %map.
661*e4b17023SJohn Marino        *  @param  __first  Iterator pointing to the start of the range to be
662*e4b17023SJohn Marino        *                   erased.
663*e4b17023SJohn Marino        *  @param __last Iterator pointing to the end of the range to
664*e4b17023SJohn Marino        *                be erased.
665*e4b17023SJohn Marino        *  @return The iterator @a __last.
666*e4b17023SJohn Marino        *
667*e4b17023SJohn Marino        *  This function erases a sequence of elements from a %map.
668*e4b17023SJohn Marino        *  Note that this function only erases the element, and that if
669*e4b17023SJohn Marino        *  the element is itself a pointer, the pointed-to memory is not touched
670*e4b17023SJohn Marino        *  in any way.  Managing the pointer is the user's responsibility.
671*e4b17023SJohn Marino        */
672*e4b17023SJohn Marino       iterator
673*e4b17023SJohn Marino       erase(const_iterator __first, const_iterator __last)
674*e4b17023SJohn Marino       { return _M_t.erase(__first, __last); }
675*e4b17023SJohn Marino #else
676*e4b17023SJohn Marino       /**
677*e4b17023SJohn Marino        *  @brief Erases a [__first,__last) range of elements from a %map.
678*e4b17023SJohn Marino        *  @param  __first  Iterator pointing to the start of the range to be
679*e4b17023SJohn Marino        *                   erased.
680*e4b17023SJohn Marino        *  @param __last Iterator pointing to the end of the range to
681*e4b17023SJohn Marino        *                be erased.
682*e4b17023SJohn Marino        *
683*e4b17023SJohn Marino        *  This function erases a sequence of elements from a %map.
684*e4b17023SJohn Marino        *  Note that this function only erases the element, and that if
685*e4b17023SJohn Marino        *  the element is itself a pointer, the pointed-to memory is not touched
686*e4b17023SJohn Marino        *  in any way.  Managing the pointer is the user's responsibility.
687*e4b17023SJohn Marino        */
688*e4b17023SJohn Marino       void
689*e4b17023SJohn Marino       erase(iterator __first, iterator __last)
690*e4b17023SJohn Marino       { _M_t.erase(__first, __last); }
691*e4b17023SJohn Marino #endif
692*e4b17023SJohn Marino 
693*e4b17023SJohn Marino       /**
694*e4b17023SJohn Marino        *  @brief  Swaps data with another %map.
695*e4b17023SJohn Marino        *  @param  __x  A %map of the same element and allocator types.
696*e4b17023SJohn Marino        *
697*e4b17023SJohn Marino        *  This exchanges the elements between two maps in constant
698*e4b17023SJohn Marino        *  time.  (It is only swapping a pointer, an integer, and an
699*e4b17023SJohn Marino        *  instance of the @c Compare type (which itself is often
700*e4b17023SJohn Marino        *  stateless and empty), so it should be quite fast.)  Note
701*e4b17023SJohn Marino        *  that the global std::swap() function is specialized such
702*e4b17023SJohn Marino        *  that std::swap(m1,m2) will feed to this function.
703*e4b17023SJohn Marino        */
704*e4b17023SJohn Marino       void
705*e4b17023SJohn Marino       swap(map& __x)
706*e4b17023SJohn Marino       { _M_t.swap(__x._M_t); }
707*e4b17023SJohn Marino 
708*e4b17023SJohn Marino       /**
709*e4b17023SJohn Marino        *  Erases all elements in a %map.  Note that this function only
710*e4b17023SJohn Marino        *  erases the elements, and that if the elements themselves are
711*e4b17023SJohn Marino        *  pointers, the pointed-to memory is not touched in any way.
712*e4b17023SJohn Marino        *  Managing the pointer is the user's responsibility.
713*e4b17023SJohn Marino        */
714*e4b17023SJohn Marino       void
715*e4b17023SJohn Marino       clear() _GLIBCXX_NOEXCEPT
716*e4b17023SJohn Marino       { _M_t.clear(); }
717*e4b17023SJohn Marino 
718*e4b17023SJohn Marino       // observers
719*e4b17023SJohn Marino       /**
720*e4b17023SJohn Marino        *  Returns the key comparison object out of which the %map was
721*e4b17023SJohn Marino        *  constructed.
722*e4b17023SJohn Marino        */
723*e4b17023SJohn Marino       key_compare
724*e4b17023SJohn Marino       key_comp() const
725*e4b17023SJohn Marino       { return _M_t.key_comp(); }
726*e4b17023SJohn Marino 
727*e4b17023SJohn Marino       /**
728*e4b17023SJohn Marino        *  Returns a value comparison object, built from the key comparison
729*e4b17023SJohn Marino        *  object out of which the %map was constructed.
730*e4b17023SJohn Marino        */
731*e4b17023SJohn Marino       value_compare
732*e4b17023SJohn Marino       value_comp() const
733*e4b17023SJohn Marino       { return value_compare(_M_t.key_comp()); }
734*e4b17023SJohn Marino 
735*e4b17023SJohn Marino       // [23.3.1.3] map operations
736*e4b17023SJohn Marino       /**
737*e4b17023SJohn Marino        *  @brief Tries to locate an element in a %map.
738*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) %pair to be located.
739*e4b17023SJohn Marino        *  @return  Iterator pointing to sought-after element, or end() if not
740*e4b17023SJohn Marino        *           found.
741*e4b17023SJohn Marino        *
742*e4b17023SJohn Marino        *  This function takes a key and tries to locate the element with which
743*e4b17023SJohn Marino        *  the key matches.  If successful the function returns an iterator
744*e4b17023SJohn Marino        *  pointing to the sought after %pair.  If unsuccessful it returns the
745*e4b17023SJohn Marino        *  past-the-end ( @c end() ) iterator.
746*e4b17023SJohn Marino        */
747*e4b17023SJohn Marino       iterator
748*e4b17023SJohn Marino       find(const key_type& __x)
749*e4b17023SJohn Marino       { return _M_t.find(__x); }
750*e4b17023SJohn Marino 
751*e4b17023SJohn Marino       /**
752*e4b17023SJohn Marino        *  @brief Tries to locate an element in a %map.
753*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) %pair to be located.
754*e4b17023SJohn Marino        *  @return  Read-only (constant) iterator pointing to sought-after
755*e4b17023SJohn Marino        *           element, or end() if not found.
756*e4b17023SJohn Marino        *
757*e4b17023SJohn Marino        *  This function takes a key and tries to locate the element with which
758*e4b17023SJohn Marino        *  the key matches.  If successful the function returns a constant
759*e4b17023SJohn Marino        *  iterator pointing to the sought after %pair. If unsuccessful it
760*e4b17023SJohn Marino        *  returns the past-the-end ( @c end() ) iterator.
761*e4b17023SJohn Marino        */
762*e4b17023SJohn Marino       const_iterator
763*e4b17023SJohn Marino       find(const key_type& __x) const
764*e4b17023SJohn Marino       { return _M_t.find(__x); }
765*e4b17023SJohn Marino 
766*e4b17023SJohn Marino       /**
767*e4b17023SJohn Marino        *  @brief  Finds the number of elements with given key.
768*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pairs to be located.
769*e4b17023SJohn Marino        *  @return  Number of elements with specified key.
770*e4b17023SJohn Marino        *
771*e4b17023SJohn Marino        *  This function only makes sense for multimaps; for map the result will
772*e4b17023SJohn Marino        *  either be 0 (not present) or 1 (present).
773*e4b17023SJohn Marino        */
774*e4b17023SJohn Marino       size_type
775*e4b17023SJohn Marino       count(const key_type& __x) const
776*e4b17023SJohn Marino       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
777*e4b17023SJohn Marino 
778*e4b17023SJohn Marino       /**
779*e4b17023SJohn Marino        *  @brief Finds the beginning of a subsequence matching given key.
780*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pair to be located.
781*e4b17023SJohn Marino        *  @return  Iterator pointing to first element equal to or greater
782*e4b17023SJohn Marino        *           than key, or end().
783*e4b17023SJohn Marino        *
784*e4b17023SJohn Marino        *  This function returns the first element of a subsequence of elements
785*e4b17023SJohn Marino        *  that matches the given key.  If unsuccessful it returns an iterator
786*e4b17023SJohn Marino        *  pointing to the first element that has a greater value than given key
787*e4b17023SJohn Marino        *  or end() if no such element exists.
788*e4b17023SJohn Marino        */
789*e4b17023SJohn Marino       iterator
790*e4b17023SJohn Marino       lower_bound(const key_type& __x)
791*e4b17023SJohn Marino       { return _M_t.lower_bound(__x); }
792*e4b17023SJohn Marino 
793*e4b17023SJohn Marino       /**
794*e4b17023SJohn Marino        *  @brief Finds the beginning of a subsequence matching given key.
795*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pair to be located.
796*e4b17023SJohn Marino        *  @return  Read-only (constant) iterator pointing to first element
797*e4b17023SJohn Marino        *           equal to or greater than key, or end().
798*e4b17023SJohn Marino        *
799*e4b17023SJohn Marino        *  This function returns the first element of a subsequence of elements
800*e4b17023SJohn Marino        *  that matches the given key.  If unsuccessful it returns an iterator
801*e4b17023SJohn Marino        *  pointing to the first element that has a greater value than given key
802*e4b17023SJohn Marino        *  or end() if no such element exists.
803*e4b17023SJohn Marino        */
804*e4b17023SJohn Marino       const_iterator
805*e4b17023SJohn Marino       lower_bound(const key_type& __x) const
806*e4b17023SJohn Marino       { return _M_t.lower_bound(__x); }
807*e4b17023SJohn Marino 
808*e4b17023SJohn Marino       /**
809*e4b17023SJohn Marino        *  @brief Finds the end of a subsequence matching given key.
810*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pair to be located.
811*e4b17023SJohn Marino        *  @return Iterator pointing to the first element
812*e4b17023SJohn Marino        *          greater than key, or end().
813*e4b17023SJohn Marino        */
814*e4b17023SJohn Marino       iterator
815*e4b17023SJohn Marino       upper_bound(const key_type& __x)
816*e4b17023SJohn Marino       { return _M_t.upper_bound(__x); }
817*e4b17023SJohn Marino 
818*e4b17023SJohn Marino       /**
819*e4b17023SJohn Marino        *  @brief Finds the end of a subsequence matching given key.
820*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pair to be located.
821*e4b17023SJohn Marino        *  @return  Read-only (constant) iterator pointing to first iterator
822*e4b17023SJohn Marino        *           greater than key, or end().
823*e4b17023SJohn Marino        */
824*e4b17023SJohn Marino       const_iterator
825*e4b17023SJohn Marino       upper_bound(const key_type& __x) const
826*e4b17023SJohn Marino       { return _M_t.upper_bound(__x); }
827*e4b17023SJohn Marino 
828*e4b17023SJohn Marino       /**
829*e4b17023SJohn Marino        *  @brief Finds a subsequence matching given key.
830*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pairs to be located.
831*e4b17023SJohn Marino        *  @return  Pair of iterators that possibly points to the subsequence
832*e4b17023SJohn Marino        *           matching given key.
833*e4b17023SJohn Marino        *
834*e4b17023SJohn Marino        *  This function is equivalent to
835*e4b17023SJohn Marino        *  @code
836*e4b17023SJohn Marino        *    std::make_pair(c.lower_bound(val),
837*e4b17023SJohn Marino        *                   c.upper_bound(val))
838*e4b17023SJohn Marino        *  @endcode
839*e4b17023SJohn Marino        *  (but is faster than making the calls separately).
840*e4b17023SJohn Marino        *
841*e4b17023SJohn Marino        *  This function probably only makes sense for multimaps.
842*e4b17023SJohn Marino        */
843*e4b17023SJohn Marino       std::pair<iterator, iterator>
844*e4b17023SJohn Marino       equal_range(const key_type& __x)
845*e4b17023SJohn Marino       { return _M_t.equal_range(__x); }
846*e4b17023SJohn Marino 
847*e4b17023SJohn Marino       /**
848*e4b17023SJohn Marino        *  @brief Finds a subsequence matching given key.
849*e4b17023SJohn Marino        *  @param  __x  Key of (key, value) pairs to be located.
850*e4b17023SJohn Marino        *  @return  Pair of read-only (constant) iterators that possibly points
851*e4b17023SJohn Marino        *           to the subsequence matching given key.
852*e4b17023SJohn Marino        *
853*e4b17023SJohn Marino        *  This function is equivalent to
854*e4b17023SJohn Marino        *  @code
855*e4b17023SJohn Marino        *    std::make_pair(c.lower_bound(val),
856*e4b17023SJohn Marino        *                   c.upper_bound(val))
857*e4b17023SJohn Marino        *  @endcode
858*e4b17023SJohn Marino        *  (but is faster than making the calls separately).
859*e4b17023SJohn Marino        *
860*e4b17023SJohn Marino        *  This function probably only makes sense for multimaps.
861*e4b17023SJohn Marino        */
862*e4b17023SJohn Marino       std::pair<const_iterator, const_iterator>
863*e4b17023SJohn Marino       equal_range(const key_type& __x) const
864*e4b17023SJohn Marino       { return _M_t.equal_range(__x); }
865*e4b17023SJohn Marino 
866*e4b17023SJohn Marino       template<typename _K1, typename _T1, typename _C1, typename _A1>
867*e4b17023SJohn Marino         friend bool
868*e4b17023SJohn Marino         operator==(const map<_K1, _T1, _C1, _A1>&,
869*e4b17023SJohn Marino 		   const map<_K1, _T1, _C1, _A1>&);
870*e4b17023SJohn Marino 
871*e4b17023SJohn Marino       template<typename _K1, typename _T1, typename _C1, typename _A1>
872*e4b17023SJohn Marino         friend bool
873*e4b17023SJohn Marino         operator<(const map<_K1, _T1, _C1, _A1>&,
874*e4b17023SJohn Marino 		  const map<_K1, _T1, _C1, _A1>&);
875*e4b17023SJohn Marino     };
876*e4b17023SJohn Marino 
877*e4b17023SJohn Marino   /**
878*e4b17023SJohn Marino    *  @brief  Map equality comparison.
879*e4b17023SJohn Marino    *  @param  __x  A %map.
880*e4b17023SJohn Marino    *  @param  __y  A %map of the same type as @a x.
881*e4b17023SJohn Marino    *  @return  True iff the size and elements of the maps are equal.
882*e4b17023SJohn Marino    *
883*e4b17023SJohn Marino    *  This is an equivalence relation.  It is linear in the size of the
884*e4b17023SJohn Marino    *  maps.  Maps are considered equivalent if their sizes are equal,
885*e4b17023SJohn Marino    *  and if corresponding elements compare equal.
886*e4b17023SJohn Marino   */
887*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
888*e4b17023SJohn Marino     inline bool
889*e4b17023SJohn Marino     operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
890*e4b17023SJohn Marino                const map<_Key, _Tp, _Compare, _Alloc>& __y)
891*e4b17023SJohn Marino     { return __x._M_t == __y._M_t; }
892*e4b17023SJohn Marino 
893*e4b17023SJohn Marino   /**
894*e4b17023SJohn Marino    *  @brief  Map ordering relation.
895*e4b17023SJohn Marino    *  @param  __x  A %map.
896*e4b17023SJohn Marino    *  @param  __y  A %map of the same type as @a x.
897*e4b17023SJohn Marino    *  @return  True iff @a x is lexicographically less than @a y.
898*e4b17023SJohn Marino    *
899*e4b17023SJohn Marino    *  This is a total ordering relation.  It is linear in the size of the
900*e4b17023SJohn Marino    *  maps.  The elements must be comparable with @c <.
901*e4b17023SJohn Marino    *
902*e4b17023SJohn Marino    *  See std::lexicographical_compare() for how the determination is made.
903*e4b17023SJohn Marino   */
904*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
905*e4b17023SJohn Marino     inline bool
906*e4b17023SJohn Marino     operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
907*e4b17023SJohn Marino               const map<_Key, _Tp, _Compare, _Alloc>& __y)
908*e4b17023SJohn Marino     { return __x._M_t < __y._M_t; }
909*e4b17023SJohn Marino 
910*e4b17023SJohn Marino   /// Based on operator==
911*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
912*e4b17023SJohn Marino     inline bool
913*e4b17023SJohn Marino     operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
914*e4b17023SJohn Marino                const map<_Key, _Tp, _Compare, _Alloc>& __y)
915*e4b17023SJohn Marino     { return !(__x == __y); }
916*e4b17023SJohn Marino 
917*e4b17023SJohn Marino   /// Based on operator<
918*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
919*e4b17023SJohn Marino     inline bool
920*e4b17023SJohn Marino     operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
921*e4b17023SJohn Marino               const map<_Key, _Tp, _Compare, _Alloc>& __y)
922*e4b17023SJohn Marino     { return __y < __x; }
923*e4b17023SJohn Marino 
924*e4b17023SJohn Marino   /// Based on operator<
925*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
926*e4b17023SJohn Marino     inline bool
927*e4b17023SJohn Marino     operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
928*e4b17023SJohn Marino                const map<_Key, _Tp, _Compare, _Alloc>& __y)
929*e4b17023SJohn Marino     { return !(__y < __x); }
930*e4b17023SJohn Marino 
931*e4b17023SJohn Marino   /// Based on operator<
932*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
933*e4b17023SJohn Marino     inline bool
934*e4b17023SJohn Marino     operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
935*e4b17023SJohn Marino                const map<_Key, _Tp, _Compare, _Alloc>& __y)
936*e4b17023SJohn Marino     { return !(__x < __y); }
937*e4b17023SJohn Marino 
938*e4b17023SJohn Marino   /// See std::map::swap().
939*e4b17023SJohn Marino   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
940*e4b17023SJohn Marino     inline void
941*e4b17023SJohn Marino     swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
942*e4b17023SJohn Marino 	 map<_Key, _Tp, _Compare, _Alloc>& __y)
943*e4b17023SJohn Marino     { __x.swap(__y); }
944*e4b17023SJohn Marino 
945*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_CONTAINER
946*e4b17023SJohn Marino } // namespace std
947*e4b17023SJohn Marino 
948*e4b17023SJohn Marino #endif /* _STL_MAP_H */
949