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