197403Sobrien// Map implementation -*- C++ -*-
297403Sobrien
3169691Skan// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4169691Skan// Free Software Foundation, Inc.
597403Sobrien//
697403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
797403Sobrien// software; you can redistribute it and/or modify it under the
897403Sobrien// terms of the GNU General Public License as published by the
997403Sobrien// Free Software Foundation; either version 2, or (at your option)
1097403Sobrien// any later version.
1197403Sobrien
1297403Sobrien// This library is distributed in the hope that it will be useful,
1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1597403Sobrien// GNU General Public License for more details.
1697403Sobrien
1797403Sobrien// You should have received a copy of the GNU General Public License along
1897403Sobrien// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
2097403Sobrien// USA.
2197403Sobrien
2297403Sobrien// As a special exception, you may use this file as part of a free software
2397403Sobrien// library without restriction.  Specifically, if other files instantiate
2497403Sobrien// templates or use macros or inline functions from this file, or you compile
2597403Sobrien// this file and link it with other files to produce an executable, this
2697403Sobrien// file does not by itself cause the resulting executable to be covered by
2797403Sobrien// the GNU General Public License.  This exception does not however
2897403Sobrien// invalidate any other reasons why the executable file might be covered by
2997403Sobrien// the GNU General Public License.
3097403Sobrien
3197403Sobrien/*
3297403Sobrien *
3397403Sobrien * Copyright (c) 1994
3497403Sobrien * Hewlett-Packard Company
3597403Sobrien *
3697403Sobrien * Permission to use, copy, modify, distribute and sell this software
3797403Sobrien * and its documentation for any purpose is hereby granted without fee,
3897403Sobrien * provided that the above copyright notice appear in all copies and
3997403Sobrien * that both that copyright notice and this permission notice appear
4097403Sobrien * in supporting documentation.  Hewlett-Packard Company makes no
4197403Sobrien * representations about the suitability of this software for any
4297403Sobrien * purpose.  It is provided "as is" without express or implied warranty.
4397403Sobrien *
4497403Sobrien *
4597403Sobrien * Copyright (c) 1996,1997
4697403Sobrien * Silicon Graphics Computer Systems, Inc.
4797403Sobrien *
4897403Sobrien * Permission to use, copy, modify, distribute and sell this software
4997403Sobrien * and its documentation for any purpose is hereby granted without fee,
5097403Sobrien * provided that the above copyright notice appear in all copies and
5197403Sobrien * that both that copyright notice and this permission notice appear
5297403Sobrien * in supporting documentation.  Silicon Graphics makes no
5397403Sobrien * representations about the suitability of this software for any
5497403Sobrien * purpose.  It is provided "as is" without express or implied warranty.
5597403Sobrien */
5697403Sobrien
5797403Sobrien/** @file stl_map.h
5897403Sobrien *  This is an internal header file, included by other library headers.
5997403Sobrien *  You should not attempt to use it directly.
6097403Sobrien */
6197403Sobrien
62132720Skan#ifndef _MAP_H
63132720Skan#define _MAP_H 1
6497403Sobrien
65169691Skan#include <bits/functexcept.h>
6697403Sobrien#include <bits/concept_check.h>
6797403Sobrien
68169691Skan_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
69169691Skan
7097403Sobrien  /**
71117397Skan   *  @brief A standard container made up of (key,value) pairs, which can be
72117397Skan   *  retrieved based on a key, in logarithmic time.
7397403Sobrien   *
74117397Skan   *  @ingroup Containers
75117397Skan   *  @ingroup Assoc_containers
7697403Sobrien   *
77117397Skan   *  Meets the requirements of a <a href="tables.html#65">container</a>, a
78117397Skan   *  <a href="tables.html#66">reversible container</a>, and an
79117397Skan   *  <a href="tables.html#69">associative container</a> (using unique keys).
80117397Skan   *  For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
81117397Skan   *  value_type is std::pair<const Key,T>.
8297403Sobrien   *
83117397Skan   *  Maps support bidirectional iterators.
8497403Sobrien   *
85117397Skan   *  @if maint
86117397Skan   *  The private tree data is declared exactly the same way for map and
87117397Skan   *  multimap; the distinction is made entirely in how the tree functions are
88117397Skan   *  called (*_unique versus *_equal, same as the standard).
89117397Skan   *  @endif
9097403Sobrien  */
91169691Skan  template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
92169691Skan            typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
93117397Skan    class map
94132720Skan    {
95169691Skan    public:
96169691Skan      typedef _Key                                          key_type;
97169691Skan      typedef _Tp                                           mapped_type;
98169691Skan      typedef std::pair<const _Key, _Tp>                    value_type;
99169691Skan      typedef _Compare                                      key_compare;
100169691Skan      typedef _Alloc                                        allocator_type;
101169691Skan
102169691Skan    private:
103132720Skan      // concept requirements
104169691Skan      typedef typename _Alloc::value_type                   _Alloc_value_type;
105132720Skan      __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
106132720Skan      __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
107132720Skan				_BinaryFunctionConcept)
108169691Skan      __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
109132720Skan
110132720Skan    public:
111132720Skan      class value_compare
112169691Skan      : public std::binary_function<value_type, value_type, bool>
113117397Skan      {
114169691Skan	friend class map<_Key, _Tp, _Compare, _Alloc>;
115117397Skan      protected:
116132720Skan	_Compare comp;
117132720Skan
118132720Skan	value_compare(_Compare __c)
119132720Skan	: comp(__c) { }
120132720Skan
121117397Skan      public:
122132720Skan	bool operator()(const value_type& __x, const value_type& __y) const
123132720Skan	{ return comp(__x.first, __y.first); }
124117397Skan      };
125132720Skan
126132720Skan    private:
127132720Skan      /// @if maint  This turns a red-black tree into a [multi]map.  @endif
128169691Skan      typedef typename _Alloc::template rebind<value_type>::other
129169691Skan        _Pair_alloc_type;
130169691Skan
131169691Skan      typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
132169691Skan		       key_compare, _Pair_alloc_type> _Rep_type;
133169691Skan
134132720Skan      /// @if maint  The actual tree structure.  @endif
135132720Skan      _Rep_type _M_t;
136132720Skan
137132720Skan    public:
138132720Skan      // many of these are specified differently in ISO, but the following are
139132720Skan      // "functionally equivalent"
140169691Skan      typedef typename _Pair_alloc_type::pointer         pointer;
141169691Skan      typedef typename _Pair_alloc_type::const_pointer   const_pointer;
142169691Skan      typedef typename _Pair_alloc_type::reference       reference;
143169691Skan      typedef typename _Pair_alloc_type::const_reference const_reference;
144132720Skan      typedef typename _Rep_type::iterator               iterator;
145132720Skan      typedef typename _Rep_type::const_iterator         const_iterator;
146132720Skan      typedef typename _Rep_type::size_type              size_type;
147132720Skan      typedef typename _Rep_type::difference_type        difference_type;
148132720Skan      typedef typename _Rep_type::reverse_iterator       reverse_iterator;
149132720Skan      typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
150132720Skan
151132720Skan      // [23.3.1.1] construct/copy/destroy
152132720Skan      // (get_allocator() is normally listed in this section, but seems to have
153132720Skan      // been accidentally omitted in the printed standard)
154132720Skan      /**
155132720Skan       *  @brief  Default constructor creates no elements.
156132720Skan       */
157132720Skan      map()
158236829Spfg      : _M_t() { }
159132720Skan
160132720Skan      // for some reason this was made a separate function
161132720Skan      /**
162132720Skan       *  @brief  Default constructor creates no elements.
163132720Skan       */
164132720Skan      explicit
165132720Skan      map(const _Compare& __comp, const allocator_type& __a = allocator_type())
166117397Skan      : _M_t(__comp, __a) { }
167132720Skan
168132720Skan      /**
169132720Skan       *  @brief  Map copy constructor.
170132720Skan       *  @param  x  A %map of identical element and allocator types.
171132720Skan       *
172132720Skan       *  The newly-created %map uses a copy of the allocation object used
173132720Skan       *  by @a x.
174132720Skan       */
175132720Skan      map(const map& __x)
176117397Skan      : _M_t(__x._M_t) { }
177132720Skan
178132720Skan      /**
179132720Skan       *  @brief  Builds a %map from a range.
180132720Skan       *  @param  first  An input iterator.
181132720Skan       *  @param  last  An input iterator.
182132720Skan       *
183132720Skan       *  Create a %map consisting of copies of the elements from [first,last).
184132720Skan       *  This is linear in N if the range is already sorted, and NlogN
185132720Skan       *  otherwise (where N is distance(first,last)).
186132720Skan       */
187132720Skan      template <typename _InputIterator>
188132720Skan        map(_InputIterator __first, _InputIterator __last)
189236829Spfg	: _M_t()
190169691Skan        { _M_t._M_insert_unique(__first, __last); }
191132720Skan
192132720Skan      /**
193132720Skan       *  @brief  Builds a %map from a range.
194132720Skan       *  @param  first  An input iterator.
195132720Skan       *  @param  last  An input iterator.
196132720Skan       *  @param  comp  A comparison functor.
197132720Skan       *  @param  a  An allocator object.
198132720Skan       *
199132720Skan       *  Create a %map consisting of copies of the elements from [first,last).
200132720Skan       *  This is linear in N if the range is already sorted, and NlogN
201132720Skan       *  otherwise (where N is distance(first,last)).
202132720Skan       */
203132720Skan      template <typename _InputIterator>
204132720Skan        map(_InputIterator __first, _InputIterator __last,
205132720Skan	    const _Compare& __comp, const allocator_type& __a = allocator_type())
206132720Skan	: _M_t(__comp, __a)
207169691Skan        { _M_t._M_insert_unique(__first, __last); }
208132720Skan
209169691Skan      // FIXME There is no dtor declared, but we should have something
210169691Skan      // generated by Doxygen.  I don't know what tags to add to this
211169691Skan      // paragraph to make that happen:
212132720Skan      /**
213132720Skan       *  The dtor only erases the elements, and note that if the elements
214132720Skan       *  themselves are pointers, the pointed-to memory is not touched in any
215132720Skan       *  way.  Managing the pointer is the user's responsibilty.
216132720Skan       */
217132720Skan
218132720Skan      /**
219132720Skan       *  @brief  Map assignment operator.
220132720Skan       *  @param  x  A %map of identical element and allocator types.
221132720Skan       *
222132720Skan       *  All the elements of @a x are copied, but unlike the copy constructor,
223132720Skan       *  the allocator object is not copied.
224132720Skan       */
225132720Skan      map&
226132720Skan      operator=(const map& __x)
227132720Skan      {
228132720Skan	_M_t = __x._M_t;
229132720Skan	return *this;
230132720Skan      }
231132720Skan
232132720Skan      /// Get a copy of the memory allocation object.
233132720Skan      allocator_type
234132720Skan      get_allocator() const
235132720Skan      { return _M_t.get_allocator(); }
236132720Skan
237132720Skan      // iterators
238132720Skan      /**
239132720Skan       *  Returns a read/write iterator that points to the first pair in the
240132720Skan       *  %map.
241132720Skan       *  Iteration is done in ascending order according to the keys.
242132720Skan       */
243132720Skan      iterator
244132720Skan      begin()
245132720Skan      { return _M_t.begin(); }
246132720Skan
247132720Skan      /**
248132720Skan       *  Returns a read-only (constant) iterator that points to the first pair
249132720Skan       *  in the %map.  Iteration is done in ascending order according to the
250132720Skan       *  keys.
251132720Skan       */
252132720Skan      const_iterator
253132720Skan      begin() const
254132720Skan      { return _M_t.begin(); }
255132720Skan
256132720Skan      /**
257169691Skan       *  Returns a read/write iterator that points one past the last
258169691Skan       *  pair in the %map.  Iteration is done in ascending order
259169691Skan       *  according to the keys.
260132720Skan       */
261132720Skan      iterator
262132720Skan      end()
263132720Skan      { return _M_t.end(); }
264132720Skan
265132720Skan      /**
266132720Skan       *  Returns a read-only (constant) iterator that points one past the last
267132720Skan       *  pair in the %map.  Iteration is done in ascending order according to
268132720Skan       *  the keys.
269132720Skan       */
270132720Skan      const_iterator
271132720Skan      end() const
272132720Skan      { return _M_t.end(); }
273132720Skan
274132720Skan      /**
275132720Skan       *  Returns a read/write reverse iterator that points to the last pair in
276132720Skan       *  the %map.  Iteration is done in descending order according to the
277132720Skan       *  keys.
278132720Skan       */
279132720Skan      reverse_iterator
280132720Skan      rbegin()
281132720Skan      { return _M_t.rbegin(); }
282132720Skan
283132720Skan      /**
284132720Skan       *  Returns a read-only (constant) reverse iterator that points to the
285132720Skan       *  last pair in the %map.  Iteration is done in descending order
286132720Skan       *  according to the keys.
287132720Skan       */
288132720Skan      const_reverse_iterator
289132720Skan      rbegin() const
290132720Skan      { return _M_t.rbegin(); }
291132720Skan
292132720Skan      /**
293132720Skan       *  Returns a read/write reverse iterator that points to one before the
294132720Skan       *  first pair in the %map.  Iteration is done in descending order
295132720Skan       *  according to the keys.
296132720Skan       */
297132720Skan      reverse_iterator
298132720Skan      rend()
299132720Skan      { return _M_t.rend(); }
300132720Skan
301132720Skan      /**
302132720Skan       *  Returns a read-only (constant) reverse iterator that points to one
303132720Skan       *  before the first pair in the %map.  Iteration is done in descending
304132720Skan       *  order according to the keys.
305132720Skan       */
306132720Skan      const_reverse_iterator
307132720Skan      rend() const
308132720Skan      { return _M_t.rend(); }
309132720Skan
310132720Skan      // capacity
311132720Skan      /** Returns true if the %map is empty.  (Thus begin() would equal
312132720Skan       *  end().)
313132720Skan      */
314132720Skan      bool
315132720Skan      empty() const
316132720Skan      { return _M_t.empty(); }
317132720Skan
318132720Skan      /** Returns the size of the %map.  */
319132720Skan      size_type
320132720Skan      size() const
321132720Skan      { return _M_t.size(); }
322132720Skan
323132720Skan      /** Returns the maximum size of the %map.  */
324132720Skan      size_type
325132720Skan      max_size() const
326132720Skan      { return _M_t.max_size(); }
327132720Skan
328132720Skan      // [23.3.1.2] element access
329132720Skan      /**
330132720Skan       *  @brief  Subscript ( @c [] ) access to %map data.
331132720Skan       *  @param  k  The key for which data should be retrieved.
332132720Skan       *  @return  A reference to the data of the (key,data) %pair.
333132720Skan       *
334169691Skan       *  Allows for easy lookup with the subscript ( @c [] )
335169691Skan       *  operator.  Returns data associated with the key specified in
336169691Skan       *  subscript.  If the key does not exist, a pair with that key
337169691Skan       *  is created using default values, which is then returned.
338132720Skan       *
339132720Skan       *  Lookup requires logarithmic time.
340132720Skan       */
341132720Skan      mapped_type&
342132720Skan      operator[](const key_type& __k)
343132720Skan      {
344132720Skan	// concept requirements
345132720Skan	__glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
346132720Skan
347132720Skan	iterator __i = lower_bound(__k);
348132720Skan	// __i->first is greater than or equivalent to __k.
349132720Skan	if (__i == end() || key_comp()(__k, (*__i).first))
350132720Skan          __i = insert(__i, value_type(__k, mapped_type()));
351132720Skan	return (*__i).second;
352132720Skan      }
353132720Skan
354169691Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
355169691Skan      // DR 464. Suggestion for new member functions in standard containers.
356169691Skan      /**
357169691Skan       *  @brief  Access to %map data.
358169691Skan       *  @param  k  The key for which data should be retrieved.
359169691Skan       *  @return  A reference to the data whose key is equivalent to @a k, if
360169691Skan       *           such a data is present in the %map.
361169691Skan       *  @throw  std::out_of_range  If no such data is present.
362169691Skan       */
363169691Skan      mapped_type&
364169691Skan      at(const key_type& __k)
365169691Skan      {
366169691Skan	iterator __i = lower_bound(__k);
367169691Skan	if (__i == end() || key_comp()(__k, (*__i).first))
368169691Skan	  __throw_out_of_range(__N("map::at"));
369169691Skan	return (*__i).second;
370169691Skan      }
371169691Skan
372169691Skan      const mapped_type&
373169691Skan      at(const key_type& __k) const
374169691Skan      {
375169691Skan	const_iterator __i = lower_bound(__k);
376169691Skan	if (__i == end() || key_comp()(__k, (*__i).first))
377169691Skan	  __throw_out_of_range(__N("map::at"));
378169691Skan	return (*__i).second;
379169691Skan      }
380169691Skan
381132720Skan      // modifiers
382132720Skan      /**
383132720Skan       *  @brief Attempts to insert a std::pair into the %map.
384169691Skan
385169691Skan       *  @param  x  Pair to be inserted (see std::make_pair for easy creation
386169691Skan       *	     of pairs).
387169691Skan
388169691Skan       *  @return  A pair, of which the first element is an iterator that
389169691Skan       *           points to the possibly inserted pair, and the second is
390169691Skan       *           a bool that is true if the pair was actually inserted.
391132720Skan       *
392132720Skan       *  This function attempts to insert a (key, value) %pair into the %map.
393132720Skan       *  A %map relies on unique keys and thus a %pair is only inserted if its
394132720Skan       *  first element (the key) is not already present in the %map.
395132720Skan       *
396132720Skan       *  Insertion requires logarithmic time.
397132720Skan       */
398169691Skan      std::pair<iterator, bool>
399132720Skan      insert(const value_type& __x)
400169691Skan      { return _M_t._M_insert_unique(__x); }
401132720Skan
402132720Skan      /**
403132720Skan       *  @brief Attempts to insert a std::pair into the %map.
404132720Skan       *  @param  position  An iterator that serves as a hint as to where the
405132720Skan       *                    pair should be inserted.
406169691Skan       *  @param  x  Pair to be inserted (see std::make_pair for easy creation
407169691Skan       *             of pairs).
408132720Skan       *  @return  An iterator that points to the element with key of @a x (may
409132720Skan       *           or may not be the %pair passed in).
410132720Skan       *
411169691Skan
412169691Skan       *  This function is not concerned about whether the insertion
413169691Skan       *  took place, and thus does not return a boolean like the
414169691Skan       *  single-argument insert() does.  Note that the first
415169691Skan       *  parameter is only a hint and can potentially improve the
416169691Skan       *  performance of the insertion process.  A bad hint would
417169691Skan       *  cause no gains in efficiency.
418132720Skan       *
419169691Skan       *  See
420169691Skan       *  http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
421132720Skan       *  for more on "hinting".
422132720Skan       *
423132720Skan       *  Insertion requires logarithmic time (if the hint is not taken).
424132720Skan       */
425132720Skan      iterator
426169691Skan      insert(iterator __position, const value_type& __x)
427169691Skan      { return _M_t._M_insert_unique(__position, __x); }
428132720Skan
429132720Skan      /**
430169691Skan       *  @brief Template function that attemps to insert a range of elements.
431132720Skan       *  @param  first  Iterator pointing to the start of the range to be
432132720Skan       *                 inserted.
433132720Skan       *  @param  last  Iterator pointing to the end of the range.
434132720Skan       *
435132720Skan       *  Complexity similar to that of the range constructor.
436132720Skan       */
437132720Skan      template <typename _InputIterator>
438132720Skan        void
439132720Skan        insert(_InputIterator __first, _InputIterator __last)
440169691Skan        { _M_t._M_insert_unique(__first, __last); }
441132720Skan
442132720Skan      /**
443132720Skan       *  @brief Erases an element from a %map.
444132720Skan       *  @param  position  An iterator pointing to the element to be erased.
445132720Skan       *
446169691Skan       *  This function erases an element, pointed to by the given
447169691Skan       *  iterator, from a %map.  Note that this function only erases
448169691Skan       *  the element, and that if the element is itself a pointer,
449169691Skan       *  the pointed-to memory is not touched in any way.  Managing
450169691Skan       *  the pointer is the user's responsibilty.
451132720Skan       */
452117397Skan      void
453132720Skan      erase(iterator __position)
454132720Skan      { _M_t.erase(__position); }
455132720Skan
456132720Skan      /**
457132720Skan       *  @brief Erases elements according to the provided key.
458132720Skan       *  @param  x  Key of element to be erased.
459132720Skan       *  @return  The number of elements erased.
460132720Skan       *
461132720Skan       *  This function erases all the elements located by the given key from
462132720Skan       *  a %map.
463132720Skan       *  Note that this function only erases the element, and that if
464132720Skan       *  the element is itself a pointer, the pointed-to memory is not touched
465132720Skan       *  in any way.  Managing the pointer is the user's responsibilty.
466132720Skan       */
467132720Skan      size_type
468132720Skan      erase(const key_type& __x)
469132720Skan      { return _M_t.erase(__x); }
470132720Skan
471132720Skan      /**
472132720Skan       *  @brief Erases a [first,last) range of elements from a %map.
473132720Skan       *  @param  first  Iterator pointing to the start of the range to be
474132720Skan       *                 erased.
475132720Skan       *  @param  last  Iterator pointing to the end of the range to be erased.
476132720Skan       *
477132720Skan       *  This function erases a sequence of elements from a %map.
478132720Skan       *  Note that this function only erases the element, and that if
479132720Skan       *  the element is itself a pointer, the pointed-to memory is not touched
480132720Skan       *  in any way.  Managing the pointer is the user's responsibilty.
481132720Skan       */
482132720Skan      void
483132720Skan      erase(iterator __first, iterator __last)
484132720Skan      { _M_t.erase(__first, __last); }
485132720Skan
486132720Skan      /**
487132720Skan       *  @brief  Swaps data with another %map.
488132720Skan       *  @param  x  A %map of the same element and allocator types.
489132720Skan       *
490169691Skan       *  This exchanges the elements between two maps in constant
491169691Skan       *  time.  (It is only swapping a pointer, an integer, and an
492169691Skan       *  instance of the @c Compare type (which itself is often
493169691Skan       *  stateless and empty), so it should be quite fast.)  Note
494169691Skan       *  that the global std::swap() function is specialized such
495169691Skan       *  that std::swap(m1,m2) will feed to this function.
496132720Skan       */
497132720Skan      void
498132720Skan      swap(map& __x)
499132720Skan      { _M_t.swap(__x._M_t); }
500132720Skan
501132720Skan      /**
502169691Skan       *  Erases all elements in a %map.  Note that this function only
503169691Skan       *  erases the elements, and that if the elements themselves are
504169691Skan       *  pointers, the pointed-to memory is not touched in any way.
505169691Skan       *  Managing the pointer is the user's responsibilty.
506132720Skan       */
507132720Skan      void
508132720Skan      clear()
509132720Skan      { _M_t.clear(); }
510132720Skan
511132720Skan      // observers
512132720Skan      /**
513132720Skan       *  Returns the key comparison object out of which the %map was
514132720Skan       *  constructed.
515132720Skan       */
516132720Skan      key_compare
517132720Skan      key_comp() const
518132720Skan      { return _M_t.key_comp(); }
519132720Skan
520132720Skan      /**
521132720Skan       *  Returns a value comparison object, built from the key comparison
522132720Skan       *  object out of which the %map was constructed.
523132720Skan       */
524132720Skan      value_compare
525132720Skan      value_comp() const
526132720Skan      { return value_compare(_M_t.key_comp()); }
527132720Skan
528132720Skan      // [23.3.1.3] map operations
529132720Skan      /**
530132720Skan       *  @brief Tries to locate an element in a %map.
531132720Skan       *  @param  x  Key of (key, value) %pair to be located.
532132720Skan       *  @return  Iterator pointing to sought-after element, or end() if not
533132720Skan       *           found.
534132720Skan       *
535132720Skan       *  This function takes a key and tries to locate the element with which
536132720Skan       *  the key matches.  If successful the function returns an iterator
537132720Skan       *  pointing to the sought after %pair.  If unsuccessful it returns the
538132720Skan       *  past-the-end ( @c end() ) iterator.
539132720Skan       */
540132720Skan      iterator
541132720Skan      find(const key_type& __x)
542132720Skan      { return _M_t.find(__x); }
543132720Skan
544132720Skan      /**
545132720Skan       *  @brief Tries to locate an element in a %map.
546132720Skan       *  @param  x  Key of (key, value) %pair to be located.
547132720Skan       *  @return  Read-only (constant) iterator pointing to sought-after
548132720Skan       *           element, or end() if not found.
549132720Skan       *
550132720Skan       *  This function takes a key and tries to locate the element with which
551132720Skan       *  the key matches.  If successful the function returns a constant
552132720Skan       *  iterator pointing to the sought after %pair. If unsuccessful it
553132720Skan       *  returns the past-the-end ( @c end() ) iterator.
554132720Skan       */
555132720Skan      const_iterator
556132720Skan      find(const key_type& __x) const
557132720Skan      { return _M_t.find(__x); }
558132720Skan
559132720Skan      /**
560132720Skan       *  @brief  Finds the number of elements with given key.
561132720Skan       *  @param  x  Key of (key, value) pairs to be located.
562132720Skan       *  @return  Number of elements with specified key.
563132720Skan       *
564132720Skan       *  This function only makes sense for multimaps; for map the result will
565132720Skan       *  either be 0 (not present) or 1 (present).
566132720Skan       */
567132720Skan      size_type
568132720Skan      count(const key_type& __x) const
569132720Skan      { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
570132720Skan
571132720Skan      /**
572132720Skan       *  @brief Finds the beginning of a subsequence matching given key.
573132720Skan       *  @param  x  Key of (key, value) pair to be located.
574132720Skan       *  @return  Iterator pointing to first element equal to or greater
575132720Skan       *           than key, or end().
576132720Skan       *
577132720Skan       *  This function returns the first element of a subsequence of elements
578132720Skan       *  that matches the given key.  If unsuccessful it returns an iterator
579132720Skan       *  pointing to the first element that has a greater value than given key
580132720Skan       *  or end() if no such element exists.
581132720Skan       */
582132720Skan      iterator
583132720Skan      lower_bound(const key_type& __x)
584132720Skan      { return _M_t.lower_bound(__x); }
585132720Skan
586132720Skan      /**
587132720Skan       *  @brief Finds the beginning of a subsequence matching given key.
588132720Skan       *  @param  x  Key of (key, value) pair to be located.
589132720Skan       *  @return  Read-only (constant) iterator pointing to first element
590132720Skan       *           equal to or greater than key, or end().
591132720Skan       *
592132720Skan       *  This function returns the first element of a subsequence of elements
593132720Skan       *  that matches the given key.  If unsuccessful it returns an iterator
594132720Skan       *  pointing to the first element that has a greater value than given key
595132720Skan       *  or end() if no such element exists.
596132720Skan       */
597132720Skan      const_iterator
598132720Skan      lower_bound(const key_type& __x) const
599132720Skan      { return _M_t.lower_bound(__x); }
600132720Skan
601132720Skan      /**
602132720Skan       *  @brief Finds the end of a subsequence matching given key.
603132720Skan       *  @param  x  Key of (key, value) pair to be located.
604132720Skan       *  @return Iterator pointing to the first element
605132720Skan       *          greater than key, or end().
606132720Skan       */
607132720Skan      iterator
608132720Skan      upper_bound(const key_type& __x)
609132720Skan      { return _M_t.upper_bound(__x); }
610132720Skan
611132720Skan      /**
612132720Skan       *  @brief Finds the end of a subsequence matching given key.
613132720Skan       *  @param  x  Key of (key, value) pair to be located.
614132720Skan       *  @return  Read-only (constant) iterator pointing to first iterator
615132720Skan       *           greater than key, or end().
616132720Skan       */
617132720Skan      const_iterator
618132720Skan      upper_bound(const key_type& __x) const
619132720Skan      { return _M_t.upper_bound(__x); }
620132720Skan
621132720Skan      /**
622132720Skan       *  @brief Finds a subsequence matching given key.
623132720Skan       *  @param  x  Key of (key, value) pairs to be located.
624132720Skan       *  @return  Pair of iterators that possibly points to the subsequence
625132720Skan       *           matching given key.
626132720Skan       *
627132720Skan       *  This function is equivalent to
628132720Skan       *  @code
629132720Skan       *    std::make_pair(c.lower_bound(val),
630132720Skan       *                   c.upper_bound(val))
631132720Skan       *  @endcode
632132720Skan       *  (but is faster than making the calls separately).
633132720Skan       *
634132720Skan       *  This function probably only makes sense for multimaps.
635132720Skan       */
636169691Skan      std::pair<iterator, iterator>
637132720Skan      equal_range(const key_type& __x)
638132720Skan      { return _M_t.equal_range(__x); }
639132720Skan
640132720Skan      /**
641132720Skan       *  @brief Finds a subsequence matching given key.
642132720Skan       *  @param  x  Key of (key, value) pairs to be located.
643132720Skan       *  @return  Pair of read-only (constant) iterators that possibly points
644132720Skan       *           to the subsequence matching given key.
645132720Skan       *
646132720Skan       *  This function is equivalent to
647132720Skan       *  @code
648132720Skan       *    std::make_pair(c.lower_bound(val),
649132720Skan       *                   c.upper_bound(val))
650132720Skan       *  @endcode
651132720Skan       *  (but is faster than making the calls separately).
652132720Skan       *
653132720Skan       *  This function probably only makes sense for multimaps.
654132720Skan       */
655169691Skan      std::pair<const_iterator, const_iterator>
656132720Skan      equal_range(const key_type& __x) const
657132720Skan      { return _M_t.equal_range(__x); }
658132720Skan
659132720Skan      template <typename _K1, typename _T1, typename _C1, typename _A1>
660132720Skan        friend bool
661169691Skan        operator== (const map<_K1, _T1, _C1, _A1>&,
662169691Skan		    const map<_K1, _T1, _C1, _A1>&);
663132720Skan
664132720Skan      template <typename _K1, typename _T1, typename _C1, typename _A1>
665132720Skan        friend bool
666169691Skan        operator< (const map<_K1, _T1, _C1, _A1>&,
667169691Skan		   const map<_K1, _T1, _C1, _A1>&);
668132720Skan    };
669132720Skan
67097403Sobrien  /**
671117397Skan   *  @brief  Map equality comparison.
672117397Skan   *  @param  x  A %map.
673117397Skan   *  @param  y  A %map of the same type as @a x.
674117397Skan   *  @return  True iff the size and elements of the maps are equal.
67597403Sobrien   *
676117397Skan   *  This is an equivalence relation.  It is linear in the size of the
677117397Skan   *  maps.  Maps are considered equivalent if their sizes are equal,
678117397Skan   *  and if corresponding elements compare equal.
67997403Sobrien  */
680117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
681117397Skan    inline bool
682169691Skan    operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
683169691Skan               const map<_Key, _Tp, _Compare, _Alloc>& __y)
684117397Skan    { return __x._M_t == __y._M_t; }
685132720Skan
68697403Sobrien  /**
687117397Skan   *  @brief  Map ordering relation.
688117397Skan   *  @param  x  A %map.
689117397Skan   *  @param  y  A %map of the same type as @a x.
690132720Skan   *  @return  True iff @a x is lexicographically less than @a y.
69197403Sobrien   *
692117397Skan   *  This is a total ordering relation.  It is linear in the size of the
693117397Skan   *  maps.  The elements must be comparable with @c <.
69497403Sobrien   *
695132720Skan   *  See std::lexicographical_compare() for how the determination is made.
69697403Sobrien  */
697117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
698117397Skan    inline bool
699169691Skan    operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
700169691Skan              const map<_Key, _Tp, _Compare, _Alloc>& __y)
701117397Skan    { return __x._M_t < __y._M_t; }
702132720Skan
703117397Skan  /// Based on operator==
704117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
705117397Skan    inline bool
706169691Skan    operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
707169691Skan               const map<_Key, _Tp, _Compare, _Alloc>& __y)
708117397Skan    { return !(__x == __y); }
709132720Skan
710117397Skan  /// Based on operator<
711117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
712117397Skan    inline bool
713169691Skan    operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
714169691Skan              const map<_Key, _Tp, _Compare, _Alloc>& __y)
715117397Skan    { return __y < __x; }
716132720Skan
717117397Skan  /// Based on operator<
718117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
719117397Skan    inline bool
720169691Skan    operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
721169691Skan               const map<_Key, _Tp, _Compare, _Alloc>& __y)
722117397Skan    { return !(__y < __x); }
723132720Skan
724117397Skan  /// Based on operator<
725117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
726117397Skan    inline bool
727169691Skan    operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
728169691Skan               const map<_Key, _Tp, _Compare, _Alloc>& __y)
729117397Skan    { return !(__x < __y); }
730132720Skan
731117397Skan  /// See std::map::swap().
732117397Skan  template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
733117397Skan    inline void
734169691Skan    swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
735169691Skan	 map<_Key, _Tp, _Compare, _Alloc>& __y)
736117397Skan    { __x.swap(__y); }
73797403Sobrien
738169691Skan_GLIBCXX_END_NESTED_NAMESPACE
739169691Skan
740132720Skan#endif /* _MAP_H */
741