1 // Map implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/functexcept.h>
65 #include <bits/concept_check.h>
67 namespace _GLIBCXX_STD
70 * @brief A standard container made up of (key,value) pairs, which can be
71 * retrieved based on a key, in logarithmic time.
74 * @ingroup Assoc_containers
76 * Meets the requirements of a <a href="tables.html#65">container</a>, a
77 * <a href="tables.html#66">reversible container</a>, and an
78 * <a href="tables.html#69">associative container</a> (using unique keys).
79 * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
80 * value_type is std::pair<const Key,T>.
82 * Maps support bidirectional iterators.
85 * The private tree data is declared exactly the same way for map and
86 * multimap; the distinction is made entirely in how the tree functions are
87 * called (*_unique versus *_equal, same as the standard).
90 template <typename _Key
, typename _Tp
, typename _Compare
= std::less
<_Key
>,
91 typename _Alloc
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
95 typedef _Key key_type
;
96 typedef _Tp mapped_type
;
97 typedef std::pair
<const _Key
, _Tp
> value_type
;
98 typedef _Compare key_compare
;
99 typedef _Alloc allocator_type
;
102 // concept requirements
103 typedef typename
_Alloc::value_type _Alloc_value_type
;
104 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
105 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
106 _BinaryFunctionConcept
)
107 __glibcxx_class_requires2(value_type
, _Alloc_value_type
, _SameTypeConcept
)
111 : public std::binary_function
<value_type
, value_type
, bool>
113 friend class map
<_Key
, _Tp
, _Compare
, _Alloc
>;
117 value_compare(_Compare __c
)
121 bool operator()(const value_type
& __x
, const value_type
& __y
) const
122 { return comp(__x
.first
, __y
.first
); }
126 /// @if maint This turns a red-black tree into a [multi]map. @endif
127 typedef typename
_Alloc::template rebind
<value_type
>::other
130 typedef _Rb_tree
<key_type
, value_type
, _Select1st
<value_type
>,
131 key_compare
, _Pair_alloc_type
> _Rep_type
;
133 /// @if maint The actual tree structure. @endif
137 // many of these are specified differently in ISO, but the following are
138 // "functionally equivalent"
139 typedef typename
_Pair_alloc_type::pointer pointer
;
140 typedef typename
_Pair_alloc_type::const_pointer const_pointer
;
141 typedef typename
_Pair_alloc_type::reference reference
;
142 typedef typename
_Pair_alloc_type::const_reference const_reference
;
143 typedef typename
_Rep_type::iterator iterator
;
144 typedef typename
_Rep_type::const_iterator const_iterator
;
145 typedef typename
_Rep_type::size_type size_type
;
146 typedef typename
_Rep_type::difference_type difference_type
;
147 typedef typename
_Rep_type::reverse_iterator reverse_iterator
;
148 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
150 // [23.3.1.1] construct/copy/destroy
151 // (get_allocator() is normally listed in this section, but seems to have
152 // been accidentally omitted in the printed standard)
154 * @brief Default constructor creates no elements.
157 : _M_t(_Compare(), allocator_type()) { }
159 // for some reason this was made a separate function
161 * @brief Default constructor creates no elements.
164 map(const _Compare
& __comp
, const allocator_type
& __a
= allocator_type())
165 : _M_t(__comp
, __a
) { }
168 * @brief Map copy constructor.
169 * @param x A %map of identical element and allocator types.
171 * The newly-created %map uses a copy of the allocation object used
178 * @brief Builds a %map from a range.
179 * @param first An input iterator.
180 * @param last An input iterator.
182 * Create a %map consisting of copies of the elements from [first,last).
183 * This is linear in N if the range is already sorted, and NlogN
184 * otherwise (where N is distance(first,last)).
186 template <typename _InputIterator
>
187 map(_InputIterator __first
, _InputIterator __last
)
188 : _M_t(_Compare(), allocator_type())
189 { _M_t
.insert_unique(__first
, __last
); }
192 * @brief Builds a %map from a range.
193 * @param first An input iterator.
194 * @param last An input iterator.
195 * @param comp A comparison functor.
196 * @param a An allocator object.
198 * Create a %map consisting of copies of the elements from [first,last).
199 * This is linear in N if the range is already sorted, and NlogN
200 * otherwise (where N is distance(first,last)).
202 template <typename _InputIterator
>
203 map(_InputIterator __first
, _InputIterator __last
,
204 const _Compare
& __comp
, const allocator_type
& __a
= allocator_type())
206 { _M_t
.insert_unique(__first
, __last
); }
208 // FIXME There is no dtor declared, but we should have something generated
209 // by Doxygen. I don't know what tags to add to this paragraph to make
212 * The dtor only erases the elements, and note that if the elements
213 * themselves are pointers, the pointed-to memory is not touched in any
214 * way. Managing the pointer is the user's responsibilty.
218 * @brief Map assignment operator.
219 * @param x A %map of identical element and allocator types.
221 * All the elements of @a x are copied, but unlike the copy constructor,
222 * the allocator object is not copied.
225 operator=(const map
& __x
)
231 /// Get a copy of the memory allocation object.
233 get_allocator() const
234 { return _M_t
.get_allocator(); }
238 * Returns a read/write iterator that points to the first pair in the
240 * Iteration is done in ascending order according to the keys.
244 { return _M_t
.begin(); }
247 * Returns a read-only (constant) iterator that points to the first pair
248 * in the %map. Iteration is done in ascending order according to the
253 { return _M_t
.begin(); }
256 * Returns a read/write iterator that points one past the last pair in
257 * the %map. Iteration is done in ascending order according to the keys.
261 { return _M_t
.end(); }
264 * Returns a read-only (constant) iterator that points one past the last
265 * pair in the %map. Iteration is done in ascending order according to
270 { return _M_t
.end(); }
273 * Returns a read/write reverse iterator that points to the last pair in
274 * the %map. Iteration is done in descending order according to the
279 { return _M_t
.rbegin(); }
282 * Returns a read-only (constant) reverse iterator that points to the
283 * last pair in the %map. Iteration is done in descending order
284 * according to the keys.
286 const_reverse_iterator
288 { return _M_t
.rbegin(); }
291 * Returns a read/write reverse iterator that points to one before the
292 * first pair in the %map. Iteration is done in descending order
293 * according to the keys.
297 { return _M_t
.rend(); }
300 * Returns a read-only (constant) reverse iterator that points to one
301 * before the first pair in the %map. Iteration is done in descending
302 * order according to the keys.
304 const_reverse_iterator
306 { return _M_t
.rend(); }
309 /** Returns true if the %map is empty. (Thus begin() would equal
314 { return _M_t
.empty(); }
316 /** Returns the size of the %map. */
319 { return _M_t
.size(); }
321 /** Returns the maximum size of the %map. */
324 { return _M_t
.max_size(); }
326 // [23.3.1.2] element access
328 * @brief Subscript ( @c [] ) access to %map data.
329 * @param k The key for which data should be retrieved.
330 * @return A reference to the data of the (key,data) %pair.
332 * Allows for easy lookup with the subscript ( @c [] ) operator. Returns
333 * data associated with the key specified in subscript. If the key does
334 * not exist, a pair with that key is created using default values, which
337 * Lookup requires logarithmic time.
340 operator[](const key_type
& __k
)
342 // concept requirements
343 __glibcxx_function_requires(_DefaultConstructibleConcept
<mapped_type
>)
345 iterator __i
= lower_bound(__k
);
346 // __i->first is greater than or equivalent to __k.
347 if (__i
== end() || key_comp()(__k
, (*__i
).first
))
348 __i
= insert(__i
, value_type(__k
, mapped_type()));
349 return (*__i
).second
;
352 // _GLIBCXX_RESOLVE_LIB_DEFECTS
353 // DR 464. Suggestion for new member functions in standard containers.
355 * @brief Access to %map data.
356 * @param k The key for which data should be retrieved.
357 * @return A reference to the data whose key is equivalent to @a k, if
358 * such a data is present in the %map.
359 * @throw std::out_of_range If no such data is present.
362 at(const key_type
& __k
)
364 iterator __i
= lower_bound(__k
);
365 if (__i
== end() || key_comp()(__k
, (*__i
).first
))
366 __throw_out_of_range(__N("map::at"));
367 return (*__i
).second
;
371 at(const key_type
& __k
) const
373 const_iterator __i
= lower_bound(__k
);
374 if (__i
== end() || key_comp()(__k
, (*__i
).first
))
375 __throw_out_of_range(__N("map::at"));
376 return (*__i
).second
;
381 * @brief Attempts to insert a std::pair into the %map.
382 * @param x Pair to be inserted (see std::make_pair for easy creation of
384 * @return A pair, of which the first element is an iterator that points
385 * to the possibly inserted pair, and the second is a bool that
386 * is true if the pair was actually inserted.
388 * This function attempts to insert a (key, value) %pair into the %map.
389 * A %map relies on unique keys and thus a %pair is only inserted if its
390 * first element (the key) is not already present in the %map.
392 * Insertion requires logarithmic time.
394 std::pair
<iterator
,bool>
395 insert(const value_type
& __x
)
396 { return _M_t
.insert_unique(__x
); }
399 * @brief Attempts to insert a std::pair into the %map.
400 * @param position An iterator that serves as a hint as to where the
401 * pair should be inserted.
402 * @param x Pair to be inserted (see std::make_pair for easy creation of
404 * @return An iterator that points to the element with key of @a x (may
405 * or may not be the %pair passed in).
407 * This function is not concerned about whether the insertion took place,
408 * and thus does not return a boolean like the single-argument
409 * insert() does. Note that the first parameter is only a hint and can
410 * potentially improve the performance of the insertion process. A bad
411 * hint would cause no gains in efficiency.
413 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
414 * for more on "hinting".
416 * Insertion requires logarithmic time (if the hint is not taken).
419 insert(iterator position
, const value_type
& __x
)
420 { return _M_t
.insert_unique(position
, __x
); }
423 * @brief A template function that attemps to insert a range of elements.
424 * @param first Iterator pointing to the start of the range to be
426 * @param last Iterator pointing to the end of the range.
428 * Complexity similar to that of the range constructor.
430 template <typename _InputIterator
>
432 insert(_InputIterator __first
, _InputIterator __last
)
433 { _M_t
.insert_unique(__first
, __last
); }
436 * @brief Erases an element from a %map.
437 * @param position An iterator pointing to the element to be erased.
439 * This function erases an element, pointed to by the given iterator,
440 * from a %map. Note that this function only erases the element, and
441 * that if the element is itself a pointer, the pointed-to memory is not
442 * touched in any way. Managing the pointer is the user's responsibilty.
445 erase(iterator __position
)
446 { _M_t
.erase(__position
); }
449 * @brief Erases elements according to the provided key.
450 * @param x Key of element to be erased.
451 * @return The number of elements erased.
453 * This function erases all the elements located by the given key from
455 * Note that this function only erases the element, and that if
456 * the element is itself a pointer, the pointed-to memory is not touched
457 * in any way. Managing the pointer is the user's responsibilty.
460 erase(const key_type
& __x
)
461 { return _M_t
.erase(__x
); }
464 * @brief Erases a [first,last) range of elements from a %map.
465 * @param first Iterator pointing to the start of the range to be
467 * @param last Iterator pointing to the end of the range to be erased.
469 * This function erases a sequence of elements from a %map.
470 * Note that this function only erases the element, and that if
471 * the element is itself a pointer, the pointed-to memory is not touched
472 * in any way. Managing the pointer is the user's responsibilty.
475 erase(iterator __first
, iterator __last
)
476 { _M_t
.erase(__first
, __last
); }
479 * @brief Swaps data with another %map.
480 * @param x A %map of the same element and allocator types.
482 * This exchanges the elements between two maps in constant time.
483 * (It is only swapping a pointer, an integer, and an instance of
484 * the @c Compare type (which itself is often stateless and empty), so it
485 * should be quite fast.)
486 * Note that the global std::swap() function is specialized such that
487 * std::swap(m1,m2) will feed to this function.
491 { _M_t
.swap(__x
._M_t
); }
494 * Erases all elements in a %map. Note that this function only erases
495 * the elements, and that if the elements themselves are pointers, the
496 * pointed-to memory is not touched in any way. Managing the pointer is
497 * the user's responsibilty.
505 * Returns the key comparison object out of which the %map was
510 { return _M_t
.key_comp(); }
513 * Returns a value comparison object, built from the key comparison
514 * object out of which the %map was constructed.
518 { return value_compare(_M_t
.key_comp()); }
520 // [23.3.1.3] map operations
522 * @brief Tries to locate an element in a %map.
523 * @param x Key of (key, value) %pair to be located.
524 * @return Iterator pointing to sought-after element, or end() if not
527 * This function takes a key and tries to locate the element with which
528 * the key matches. If successful the function returns an iterator
529 * pointing to the sought after %pair. If unsuccessful it returns the
530 * past-the-end ( @c end() ) iterator.
533 find(const key_type
& __x
)
534 { return _M_t
.find(__x
); }
537 * @brief Tries to locate an element in a %map.
538 * @param x Key of (key, value) %pair to be located.
539 * @return Read-only (constant) iterator pointing to sought-after
540 * element, or end() if not found.
542 * This function takes a key and tries to locate the element with which
543 * the key matches. If successful the function returns a constant
544 * iterator pointing to the sought after %pair. If unsuccessful it
545 * returns the past-the-end ( @c end() ) iterator.
548 find(const key_type
& __x
) const
549 { return _M_t
.find(__x
); }
552 * @brief Finds the number of elements with given key.
553 * @param x Key of (key, value) pairs to be located.
554 * @return Number of elements with specified key.
556 * This function only makes sense for multimaps; for map the result will
557 * either be 0 (not present) or 1 (present).
560 count(const key_type
& __x
) const
561 { return _M_t
.find(__x
) == _M_t
.end() ? 0 : 1; }
564 * @brief Finds the beginning of a subsequence matching given key.
565 * @param x Key of (key, value) pair to be located.
566 * @return Iterator pointing to first element equal to or greater
567 * than key, or end().
569 * This function returns the first element of a subsequence of elements
570 * that matches the given key. If unsuccessful it returns an iterator
571 * pointing to the first element that has a greater value than given key
572 * or end() if no such element exists.
575 lower_bound(const key_type
& __x
)
576 { return _M_t
.lower_bound(__x
); }
579 * @brief Finds the beginning of a subsequence matching given key.
580 * @param x Key of (key, value) pair to be located.
581 * @return Read-only (constant) iterator pointing to first element
582 * equal to or greater than key, or end().
584 * This function returns the first element of a subsequence of elements
585 * that matches the given key. If unsuccessful it returns an iterator
586 * pointing to the first element that has a greater value than given key
587 * or end() if no such element exists.
590 lower_bound(const key_type
& __x
) const
591 { return _M_t
.lower_bound(__x
); }
594 * @brief Finds the end of a subsequence matching given key.
595 * @param x Key of (key, value) pair to be located.
596 * @return Iterator pointing to the first element
597 * greater than key, or end().
600 upper_bound(const key_type
& __x
)
601 { return _M_t
.upper_bound(__x
); }
604 * @brief Finds the end of a subsequence matching given key.
605 * @param x Key of (key, value) pair to be located.
606 * @return Read-only (constant) iterator pointing to first iterator
607 * greater than key, or end().
610 upper_bound(const key_type
& __x
) const
611 { return _M_t
.upper_bound(__x
); }
614 * @brief Finds a subsequence matching given key.
615 * @param x Key of (key, value) pairs to be located.
616 * @return Pair of iterators that possibly points to the subsequence
617 * matching given key.
619 * This function is equivalent to
621 * std::make_pair(c.lower_bound(val),
622 * c.upper_bound(val))
624 * (but is faster than making the calls separately).
626 * This function probably only makes sense for multimaps.
628 std::pair
<iterator
, iterator
>
629 equal_range(const key_type
& __x
)
630 { return _M_t
.equal_range(__x
); }
633 * @brief Finds a subsequence matching given key.
634 * @param x Key of (key, value) pairs to be located.
635 * @return Pair of read-only (constant) iterators that possibly points
636 * to the subsequence matching given key.
638 * This function is equivalent to
640 * std::make_pair(c.lower_bound(val),
641 * c.upper_bound(val))
643 * (but is faster than making the calls separately).
645 * This function probably only makes sense for multimaps.
647 std::pair
<const_iterator
, const_iterator
>
648 equal_range(const key_type
& __x
) const
649 { return _M_t
.equal_range(__x
); }
651 template <typename _K1
, typename _T1
, typename _C1
, typename _A1
>
653 operator== (const map
<_K1
, _T1
, _C1
, _A1
>&,
654 const map
<_K1
, _T1
, _C1
, _A1
>&);
656 template <typename _K1
, typename _T1
, typename _C1
, typename _A1
>
658 operator< (const map
<_K1
, _T1
, _C1
, _A1
>&,
659 const map
<_K1
, _T1
, _C1
, _A1
>&);
663 * @brief Map equality comparison.
665 * @param y A %map of the same type as @a x.
666 * @return True iff the size and elements of the maps are equal.
668 * This is an equivalence relation. It is linear in the size of the
669 * maps. Maps are considered equivalent if their sizes are equal,
670 * and if corresponding elements compare equal.
672 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
674 operator==(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
675 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
676 { return __x
._M_t
== __y
._M_t
; }
679 * @brief Map ordering relation.
681 * @param y A %map of the same type as @a x.
682 * @return True iff @a x is lexicographically less than @a y.
684 * This is a total ordering relation. It is linear in the size of the
685 * maps. The elements must be comparable with @c <.
687 * See std::lexicographical_compare() for how the determination is made.
689 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
691 operator<(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
692 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
693 { return __x
._M_t
< __y
._M_t
; }
695 /// Based on operator==
696 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
698 operator!=(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
699 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
700 { return !(__x
== __y
); }
702 /// Based on operator<
703 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
705 operator>(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
706 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
707 { return __y
< __x
; }
709 /// Based on operator<
710 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
712 operator<=(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
713 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
714 { return !(__y
< __x
); }
716 /// Based on operator<
717 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
719 operator>=(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
720 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
721 { return !(__x
< __y
); }
723 /// See std::map::swap().
724 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
726 swap(map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
727 map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)