1 // Map implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/functexcept.h>
66 #include <bits/concept_check.h>
68 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD
)
71 * @brief A standard container made up of (key,value) pairs, which can be
72 * retrieved based on a key, in logarithmic time.
75 * @ingroup Assoc_containers
77 * Meets the requirements of a <a href="tables.html#65">container</a>, a
78 * <a href="tables.html#66">reversible container</a>, and an
79 * <a href="tables.html#69">associative container</a> (using unique keys).
80 * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
81 * value_type is std::pair<const Key,T>.
83 * Maps support bidirectional iterators.
86 * The private tree data is declared exactly the same way for map and
87 * multimap; the distinction is made entirely in how the tree functions are
88 * called (*_unique versus *_equal, same as the standard).
91 template <typename _Key
, typename _Tp
, typename _Compare
= std::less
<_Key
>,
92 typename _Alloc
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
96 typedef _Key key_type
;
97 typedef _Tp mapped_type
;
98 typedef std::pair
<const _Key
, _Tp
> value_type
;
99 typedef _Compare key_compare
;
100 typedef _Alloc allocator_type
;
103 // concept requirements
104 typedef typename
_Alloc::value_type _Alloc_value_type
;
105 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
106 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
107 _BinaryFunctionConcept
)
108 __glibcxx_class_requires2(value_type
, _Alloc_value_type
, _SameTypeConcept
)
112 : public std::binary_function
<value_type
, value_type
, bool>
114 friend class map
<_Key
, _Tp
, _Compare
, _Alloc
>;
118 value_compare(_Compare __c
)
122 bool operator()(const value_type
& __x
, const value_type
& __y
) const
123 { return comp(__x
.first
, __y
.first
); }
127 /// @if maint This turns a red-black tree into a [multi]map. @endif
128 typedef typename
_Alloc::template rebind
<value_type
>::other
131 typedef _Rb_tree
<key_type
, value_type
, _Select1st
<value_type
>,
132 key_compare
, _Pair_alloc_type
> _Rep_type
;
134 /// @if maint The actual tree structure. @endif
138 // many of these are specified differently in ISO, but the following are
139 // "functionally equivalent"
140 typedef typename
_Pair_alloc_type::pointer pointer
;
141 typedef typename
_Pair_alloc_type::const_pointer const_pointer
;
142 typedef typename
_Pair_alloc_type::reference reference
;
143 typedef typename
_Pair_alloc_type::const_reference const_reference
;
144 typedef typename
_Rep_type::iterator iterator
;
145 typedef typename
_Rep_type::const_iterator const_iterator
;
146 typedef typename
_Rep_type::size_type size_type
;
147 typedef typename
_Rep_type::difference_type difference_type
;
148 typedef typename
_Rep_type::reverse_iterator reverse_iterator
;
149 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
151 // [23.3.1.1] construct/copy/destroy
152 // (get_allocator() is normally listed in this section, but seems to have
153 // been accidentally omitted in the printed standard)
155 * @brief Default constructor creates no elements.
158 : _M_t(_Compare(), allocator_type()) { }
160 // for some reason this was made a separate function
162 * @brief Default constructor creates no elements.
165 map(const _Compare
& __comp
, const allocator_type
& __a
= allocator_type())
166 : _M_t(__comp
, __a
) { }
169 * @brief Map copy constructor.
170 * @param x A %map of identical element and allocator types.
172 * The newly-created %map uses a copy of the allocation object used
179 * @brief Builds a %map from a range.
180 * @param first An input iterator.
181 * @param last An input iterator.
183 * Create a %map consisting of copies of the elements from [first,last).
184 * This is linear in N if the range is already sorted, and NlogN
185 * otherwise (where N is distance(first,last)).
187 template <typename _InputIterator
>
188 map(_InputIterator __first
, _InputIterator __last
)
189 : _M_t(_Compare(), allocator_type())
190 { _M_t
._M_insert_unique(__first
, __last
); }
193 * @brief Builds a %map from a range.
194 * @param first An input iterator.
195 * @param last An input iterator.
196 * @param comp A comparison functor.
197 * @param a An allocator object.
199 * Create a %map consisting of copies of the elements from [first,last).
200 * This is linear in N if the range is already sorted, and NlogN
201 * otherwise (where N is distance(first,last)).
203 template <typename _InputIterator
>
204 map(_InputIterator __first
, _InputIterator __last
,
205 const _Compare
& __comp
, const allocator_type
& __a
= allocator_type())
207 { _M_t
._M_insert_unique(__first
, __last
); }
209 // FIXME There is no dtor declared, but we should have something
210 // generated by Doxygen. I don't know what tags to add to this
211 // paragraph to make that happen:
213 * The dtor only erases the elements, and note that if the elements
214 * themselves are pointers, the pointed-to memory is not touched in any
215 * way. Managing the pointer is the user's responsibilty.
219 * @brief Map assignment operator.
220 * @param x A %map of identical element and allocator types.
222 * All the elements of @a x are copied, but unlike the copy constructor,
223 * the allocator object is not copied.
226 operator=(const map
& __x
)
232 /// Get a copy of the memory allocation object.
234 get_allocator() const
235 { return _M_t
.get_allocator(); }
239 * Returns a read/write iterator that points to the first pair in the
241 * Iteration is done in ascending order according to the keys.
245 { return _M_t
.begin(); }
248 * Returns a read-only (constant) iterator that points to the first pair
249 * in the %map. Iteration is done in ascending order according to the
254 { return _M_t
.begin(); }
257 * Returns a read/write iterator that points one past the last
258 * pair in the %map. Iteration is done in ascending order
259 * according to the keys.
263 { return _M_t
.end(); }
266 * Returns a read-only (constant) iterator that points one past the last
267 * pair in the %map. Iteration is done in ascending order according to
272 { return _M_t
.end(); }
275 * Returns a read/write reverse iterator that points to the last pair in
276 * the %map. Iteration is done in descending order according to the
281 { return _M_t
.rbegin(); }
284 * Returns a read-only (constant) reverse iterator that points to the
285 * last pair in the %map. Iteration is done in descending order
286 * according to the keys.
288 const_reverse_iterator
290 { return _M_t
.rbegin(); }
293 * Returns a read/write reverse iterator that points to one before the
294 * first pair in the %map. Iteration is done in descending order
295 * according to the keys.
299 { return _M_t
.rend(); }
302 * Returns a read-only (constant) reverse iterator that points to one
303 * before the first pair in the %map. Iteration is done in descending
304 * order according to the keys.
306 const_reverse_iterator
308 { return _M_t
.rend(); }
311 /** Returns true if the %map is empty. (Thus begin() would equal
316 { return _M_t
.empty(); }
318 /** Returns the size of the %map. */
321 { return _M_t
.size(); }
323 /** Returns the maximum size of the %map. */
326 { return _M_t
.max_size(); }
328 // [23.3.1.2] element access
330 * @brief Subscript ( @c [] ) access to %map data.
331 * @param k The key for which data should be retrieved.
332 * @return A reference to the data of the (key,data) %pair.
334 * Allows for easy lookup with the subscript ( @c [] )
335 * operator. Returns data associated with the key specified in
336 * subscript. If the key does not exist, a pair with that key
337 * is created using default values, which is then returned.
339 * Lookup requires logarithmic time.
342 operator[](const key_type
& __k
)
344 // concept requirements
345 __glibcxx_function_requires(_DefaultConstructibleConcept
<mapped_type
>)
347 iterator __i
= lower_bound(__k
);
348 // __i->first is greater than or equivalent to __k.
349 if (__i
== end() || key_comp()(__k
, (*__i
).first
))
350 __i
= insert(__i
, value_type(__k
, mapped_type()));
351 return (*__i
).second
;
354 // _GLIBCXX_RESOLVE_LIB_DEFECTS
355 // DR 464. Suggestion for new member functions in standard containers.
357 * @brief Access to %map data.
358 * @param k The key for which data should be retrieved.
359 * @return A reference to the data whose key is equivalent to @a k, if
360 * such a data is present in the %map.
361 * @throw std::out_of_range If no such data is present.
364 at(const key_type
& __k
)
366 iterator __i
= lower_bound(__k
);
367 if (__i
== end() || key_comp()(__k
, (*__i
).first
))
368 __throw_out_of_range(__N("map::at"));
369 return (*__i
).second
;
373 at(const key_type
& __k
) const
375 const_iterator __i
= lower_bound(__k
);
376 if (__i
== end() || key_comp()(__k
, (*__i
).first
))
377 __throw_out_of_range(__N("map::at"));
378 return (*__i
).second
;
383 * @brief Attempts to insert a std::pair into the %map.
385 * @param x Pair to be inserted (see std::make_pair for easy creation
388 * @return A pair, of which the first element is an iterator that
389 * points to the possibly inserted pair, and the second is
390 * a bool that is true if the pair was actually inserted.
392 * This function attempts to insert a (key, value) %pair into the %map.
393 * A %map relies on unique keys and thus a %pair is only inserted if its
394 * first element (the key) is not already present in the %map.
396 * Insertion requires logarithmic time.
398 std::pair
<iterator
, bool>
399 insert(const value_type
& __x
)
400 { return _M_t
._M_insert_unique(__x
); }
403 * @brief Attempts to insert a std::pair into the %map.
404 * @param position An iterator that serves as a hint as to where the
405 * pair should be inserted.
406 * @param x Pair to be inserted (see std::make_pair for easy creation
408 * @return An iterator that points to the element with key of @a x (may
409 * or may not be the %pair passed in).
412 * This function is not concerned about whether the insertion
413 * took place, and thus does not return a boolean like the
414 * single-argument insert() does. Note that the first
415 * parameter is only a hint and can potentially improve the
416 * performance of the insertion process. A bad hint would
417 * cause no gains in efficiency.
420 * http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
421 * for more on "hinting".
423 * Insertion requires logarithmic time (if the hint is not taken).
426 insert(iterator __position
, const value_type
& __x
)
427 { return _M_t
._M_insert_unique_(__position
, __x
); }
430 * @brief Template function that attemps to insert a range of elements.
431 * @param first Iterator pointing to the start of the range to be
433 * @param last Iterator pointing to the end of the range.
435 * Complexity similar to that of the range constructor.
437 template <typename _InputIterator
>
439 insert(_InputIterator __first
, _InputIterator __last
)
440 { _M_t
._M_insert_unique(__first
, __last
); }
443 * @brief Erases an element from a %map.
444 * @param position An iterator pointing to the element to be erased.
446 * This function erases an element, pointed to by the given
447 * iterator, from a %map. Note that this function only erases
448 * the element, and that if the element is itself a pointer,
449 * the pointed-to memory is not touched in any way. Managing
450 * the pointer is the user's responsibilty.
453 erase(iterator __position
)
454 { _M_t
.erase(__position
); }
457 * @brief Erases elements according to the provided key.
458 * @param x Key of element to be erased.
459 * @return The number of elements erased.
461 * This function erases all the elements located by the given key from
463 * Note that this function only erases the element, and that if
464 * the element is itself a pointer, the pointed-to memory is not touched
465 * in any way. Managing the pointer is the user's responsibilty.
468 erase(const key_type
& __x
)
469 { return _M_t
.erase(__x
); }
472 * @brief Erases a [first,last) range of elements from a %map.
473 * @param first Iterator pointing to the start of the range to be
475 * @param last Iterator pointing to the end of the range to be erased.
477 * This function erases a sequence of elements from a %map.
478 * Note that this function only erases the element, and that if
479 * the element is itself a pointer, the pointed-to memory is not touched
480 * in any way. Managing the pointer is the user's responsibilty.
483 erase(iterator __first
, iterator __last
)
484 { _M_t
.erase(__first
, __last
); }
487 * @brief Swaps data with another %map.
488 * @param x A %map of the same element and allocator types.
490 * This exchanges the elements between two maps in constant
491 * time. (It is only swapping a pointer, an integer, and an
492 * instance of the @c Compare type (which itself is often
493 * stateless and empty), so it should be quite fast.) Note
494 * that the global std::swap() function is specialized such
495 * that std::swap(m1,m2) will feed to this function.
499 { _M_t
.swap(__x
._M_t
); }
502 * Erases all elements in a %map. Note that this function only
503 * erases the elements, and that if the elements themselves are
504 * pointers, the pointed-to memory is not touched in any way.
505 * Managing the pointer is the user's responsibilty.
513 * Returns the key comparison object out of which the %map was
518 { return _M_t
.key_comp(); }
521 * Returns a value comparison object, built from the key comparison
522 * object out of which the %map was constructed.
526 { return value_compare(_M_t
.key_comp()); }
528 // [23.3.1.3] map operations
530 * @brief Tries to locate an element in a %map.
531 * @param x Key of (key, value) %pair to be located.
532 * @return Iterator pointing to sought-after element, or end() if not
535 * This function takes a key and tries to locate the element with which
536 * the key matches. If successful the function returns an iterator
537 * pointing to the sought after %pair. If unsuccessful it returns the
538 * past-the-end ( @c end() ) iterator.
541 find(const key_type
& __x
)
542 { return _M_t
.find(__x
); }
545 * @brief Tries to locate an element in a %map.
546 * @param x Key of (key, value) %pair to be located.
547 * @return Read-only (constant) iterator pointing to sought-after
548 * element, or end() if not found.
550 * This function takes a key and tries to locate the element with which
551 * the key matches. If successful the function returns a constant
552 * iterator pointing to the sought after %pair. If unsuccessful it
553 * returns the past-the-end ( @c end() ) iterator.
556 find(const key_type
& __x
) const
557 { return _M_t
.find(__x
); }
560 * @brief Finds the number of elements with given key.
561 * @param x Key of (key, value) pairs to be located.
562 * @return Number of elements with specified key.
564 * This function only makes sense for multimaps; for map the result will
565 * either be 0 (not present) or 1 (present).
568 count(const key_type
& __x
) const
569 { return _M_t
.find(__x
) == _M_t
.end() ? 0 : 1; }
572 * @brief Finds the beginning of a subsequence matching given key.
573 * @param x Key of (key, value) pair to be located.
574 * @return Iterator pointing to first element equal to or greater
575 * than key, or end().
577 * This function returns the first element of a subsequence of elements
578 * that matches the given key. If unsuccessful it returns an iterator
579 * pointing to the first element that has a greater value than given key
580 * or end() if no such element exists.
583 lower_bound(const key_type
& __x
)
584 { return _M_t
.lower_bound(__x
); }
587 * @brief Finds the beginning of a subsequence matching given key.
588 * @param x Key of (key, value) pair to be located.
589 * @return Read-only (constant) iterator pointing to first element
590 * equal to or greater than key, or end().
592 * This function returns the first element of a subsequence of elements
593 * that matches the given key. If unsuccessful it returns an iterator
594 * pointing to the first element that has a greater value than given key
595 * or end() if no such element exists.
598 lower_bound(const key_type
& __x
) const
599 { return _M_t
.lower_bound(__x
); }
602 * @brief Finds the end of a subsequence matching given key.
603 * @param x Key of (key, value) pair to be located.
604 * @return Iterator pointing to the first element
605 * greater than key, or end().
608 upper_bound(const key_type
& __x
)
609 { return _M_t
.upper_bound(__x
); }
612 * @brief Finds the end of a subsequence matching given key.
613 * @param x Key of (key, value) pair to be located.
614 * @return Read-only (constant) iterator pointing to first iterator
615 * greater than key, or end().
618 upper_bound(const key_type
& __x
) const
619 { return _M_t
.upper_bound(__x
); }
622 * @brief Finds a subsequence matching given key.
623 * @param x Key of (key, value) pairs to be located.
624 * @return Pair of iterators that possibly points to the subsequence
625 * matching given key.
627 * This function is equivalent to
629 * std::make_pair(c.lower_bound(val),
630 * c.upper_bound(val))
632 * (but is faster than making the calls separately).
634 * This function probably only makes sense for multimaps.
636 std::pair
<iterator
, iterator
>
637 equal_range(const key_type
& __x
)
638 { return _M_t
.equal_range(__x
); }
641 * @brief Finds a subsequence matching given key.
642 * @param x Key of (key, value) pairs to be located.
643 * @return Pair of read-only (constant) iterators that possibly points
644 * to the subsequence matching given key.
646 * This function is equivalent to
648 * std::make_pair(c.lower_bound(val),
649 * c.upper_bound(val))
651 * (but is faster than making the calls separately).
653 * This function probably only makes sense for multimaps.
655 std::pair
<const_iterator
, const_iterator
>
656 equal_range(const key_type
& __x
) const
657 { return _M_t
.equal_range(__x
); }
659 template <typename _K1
, typename _T1
, typename _C1
, typename _A1
>
661 operator== (const map
<_K1
, _T1
, _C1
, _A1
>&,
662 const map
<_K1
, _T1
, _C1
, _A1
>&);
664 template <typename _K1
, typename _T1
, typename _C1
, typename _A1
>
666 operator< (const map
<_K1
, _T1
, _C1
, _A1
>&,
667 const map
<_K1
, _T1
, _C1
, _A1
>&);
671 * @brief Map equality comparison.
673 * @param y A %map of the same type as @a x.
674 * @return True iff the size and elements of the maps are equal.
676 * This is an equivalence relation. It is linear in the size of the
677 * maps. Maps are considered equivalent if their sizes are equal,
678 * and if corresponding elements compare equal.
680 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
682 operator==(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
683 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
684 { return __x
._M_t
== __y
._M_t
; }
687 * @brief Map ordering relation.
689 * @param y A %map of the same type as @a x.
690 * @return True iff @a x is lexicographically less than @a y.
692 * This is a total ordering relation. It is linear in the size of the
693 * maps. The elements must be comparable with @c <.
695 * See std::lexicographical_compare() for how the determination is made.
697 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
699 operator<(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
700 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
701 { return __x
._M_t
< __y
._M_t
; }
703 /// Based on operator==
704 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
706 operator!=(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
707 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
708 { return !(__x
== __y
); }
710 /// Based on operator<
711 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
713 operator>(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
714 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
715 { return __y
< __x
; }
717 /// Based on operator<
718 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
720 operator<=(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
721 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
722 { return !(__y
< __x
); }
724 /// Based on operator<
725 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
727 operator>=(const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
728 const map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
729 { return !(__x
< __y
); }
731 /// See std::map::swap().
732 template <typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
734 swap(map
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
735 map
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
738 _GLIBCXX_END_NESTED_NAMESPACE