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_D)
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.
161 * @brief Creates a %map with no elements.
162 * @param comp A comparison object.
163 * @param a An allocator object.
166 map(const _Compare& __comp,
167 const allocator_type& __a = allocator_type())
168 : _M_t(__comp, __a) { }
171 * @brief %Map copy constructor.
172 * @param x A %map of identical element and allocator types.
174 * The newly-created %map uses a copy of the allocation object
180 #ifdef __GXX_EXPERIMENTAL_CXX0X__
182 * @brief %Map move constructor.
183 * @param x A %map of identical element and allocator types.
185 * The newly-created %map contains the exact contents of @a x.
186 * The contents of @a x are a valid, but unspecified %map.
189 : _M_t(std::forward<_Rep_type>(__x._M_t)) { }
193 * @brief Builds a %map from a range.
194 * @param first An input iterator.
195 * @param last An input iterator.
197 * Create a %map consisting of copies of the elements from [first,last).
198 * This is linear in N if the range is already sorted, and NlogN
199 * otherwise (where N is distance(first,last)).
201 template<typename _InputIterator>
202 map(_InputIterator __first, _InputIterator __last)
204 { _M_t._M_insert_unique(__first, __last); }
207 * @brief Builds a %map from a range.
208 * @param first An input iterator.
209 * @param last An input iterator.
210 * @param comp A comparison functor.
211 * @param a An allocator object.
213 * Create a %map consisting of copies of the elements from [first,last).
214 * This is linear in N if the range is already sorted, and NlogN
215 * otherwise (where N is distance(first,last)).
217 template<typename _InputIterator>
218 map(_InputIterator __first, _InputIterator __last,
219 const _Compare& __comp,
220 const allocator_type& __a = allocator_type())
222 { _M_t._M_insert_unique(__first, __last); }
224 // FIXME There is no dtor declared, but we should have something
225 // generated by Doxygen. I don't know what tags to add to this
226 // paragraph to make that happen:
228 * The dtor only erases the elements, and note that if the elements
229 * themselves are pointers, the pointed-to memory is not touched in any
230 * way. Managing the pointer is the user's responsibilty.
234 * @brief %Map assignment operator.
235 * @param x A %map of identical element and allocator types.
237 * All the elements of @a x are copied, but unlike the copy constructor,
238 * the allocator object is not copied.
241 operator=(const map& __x)
247 #ifdef __GXX_EXPERIMENTAL_CXX0X__
249 * @brief %Map move assignment operator.
250 * @param x A %map of identical element and allocator types.
252 * The contents of @a x are moved into this map (without copying).
253 * @a x is a valid, but unspecified %map.
265 /// Get a copy of the memory allocation object.
267 get_allocator() const
268 { return _M_t.get_allocator(); }
272 * Returns a read/write iterator that points to the first pair in the
274 * Iteration is done in ascending order according to the keys.
278 { return _M_t.begin(); }
281 * Returns a read-only (constant) iterator that points to the first pair
282 * in the %map. Iteration is done in ascending order according to the
287 { return _M_t.begin(); }
290 * Returns a read/write iterator that points one past the last
291 * pair in the %map. Iteration is done in ascending order
292 * according to the keys.
296 { return _M_t.end(); }
299 * Returns a read-only (constant) iterator that points one past the last
300 * pair in the %map. Iteration is done in ascending order according to
305 { return _M_t.end(); }
308 * Returns a read/write reverse iterator that points to the last pair in
309 * the %map. Iteration is done in descending order according to the
314 { return _M_t.rbegin(); }
317 * Returns a read-only (constant) reverse iterator that points to the
318 * last pair in the %map. Iteration is done in descending order
319 * according to the keys.
321 const_reverse_iterator
323 { return _M_t.rbegin(); }
326 * Returns a read/write reverse iterator that points to one before the
327 * first pair in the %map. Iteration is done in descending order
328 * according to the keys.
332 { return _M_t.rend(); }
335 * Returns a read-only (constant) reverse iterator that points to one
336 * before the first pair in the %map. Iteration is done in descending
337 * order according to the keys.
339 const_reverse_iterator
341 { return _M_t.rend(); }
343 #ifdef __GXX_EXPERIMENTAL_CXX0X__
345 * Returns a read-only (constant) iterator that points to the first pair
346 * in the %map. Iteration is done in ascending order according to the
351 { return _M_t.begin(); }
354 * Returns a read-only (constant) iterator that points one past the last
355 * pair in the %map. Iteration is done in ascending order according to
360 { return _M_t.end(); }
363 * Returns a read-only (constant) reverse iterator that points to the
364 * last pair in the %map. Iteration is done in descending order
365 * according to the keys.
367 const_reverse_iterator
369 { return _M_t.rbegin(); }
372 * Returns a read-only (constant) reverse iterator that points to one
373 * before the first pair in the %map. Iteration is done in descending
374 * order according to the keys.
376 const_reverse_iterator
378 { return _M_t.rend(); }
382 /** Returns true if the %map is empty. (Thus begin() would equal
387 { return _M_t.empty(); }
389 /** Returns the size of the %map. */
392 { return _M_t.size(); }
394 /** Returns the maximum size of the %map. */
397 { return _M_t.max_size(); }
399 // [23.3.1.2] element access
401 * @brief Subscript ( @c [] ) access to %map data.
402 * @param k The key for which data should be retrieved.
403 * @return A reference to the data of the (key,data) %pair.
405 * Allows for easy lookup with the subscript ( @c [] )
406 * operator. Returns data associated with the key specified in
407 * subscript. If the key does not exist, a pair with that key
408 * is created using default values, which is then returned.
410 * Lookup requires logarithmic time.
413 operator[](const key_type& __k)
415 // concept requirements
416 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
418 iterator __i = lower_bound(__k);
419 // __i->first is greater than or equivalent to __k.
420 if (__i == end() || key_comp()(__k, (*__i).first))
421 __i = insert(__i, value_type(__k, mapped_type()));
422 return (*__i).second;
425 // _GLIBCXX_RESOLVE_LIB_DEFECTS
426 // DR 464. Suggestion for new member functions in standard containers.
428 * @brief Access to %map data.
429 * @param k The key for which data should be retrieved.
430 * @return A reference to the data whose key is equivalent to @a k, if
431 * such a data is present in the %map.
432 * @throw std::out_of_range If no such data is present.
435 at(const key_type& __k)
437 iterator __i = lower_bound(__k);
438 if (__i == end() || key_comp()(__k, (*__i).first))
439 __throw_out_of_range(__N("map::at"));
440 return (*__i).second;
444 at(const key_type& __k) const
446 const_iterator __i = lower_bound(__k);
447 if (__i == end() || key_comp()(__k, (*__i).first))
448 __throw_out_of_range(__N("map::at"));
449 return (*__i).second;
454 * @brief Attempts to insert a std::pair into the %map.
456 * @param x Pair to be inserted (see std::make_pair for easy creation
459 * @return A pair, of which the first element is an iterator that
460 * points to the possibly inserted pair, and the second is
461 * a bool that is true if the pair was actually inserted.
463 * This function attempts to insert a (key, value) %pair into the %map.
464 * A %map relies on unique keys and thus a %pair is only inserted if its
465 * first element (the key) is not already present in the %map.
467 * Insertion requires logarithmic time.
469 std::pair<iterator, bool>
470 insert(const value_type& __x)
471 { return _M_t._M_insert_unique(__x); }
474 * @brief Attempts to insert a std::pair into the %map.
475 * @param position An iterator that serves as a hint as to where the
476 * pair should be inserted.
477 * @param x Pair to be inserted (see std::make_pair for easy creation
479 * @return An iterator that points to the element with key of @a x (may
480 * or may not be the %pair passed in).
483 * This function is not concerned about whether the insertion
484 * took place, and thus does not return a boolean like the
485 * single-argument insert() does. Note that the first
486 * parameter is only a hint and can potentially improve the
487 * performance of the insertion process. A bad hint would
488 * cause no gains in efficiency.
491 * http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
492 * for more on "hinting".
494 * Insertion requires logarithmic time (if the hint is not taken).
497 insert(iterator __position, const value_type& __x)
498 { return _M_t._M_insert_unique_(__position, __x); }
501 * @brief Template function that attemps to insert a range of elements.
502 * @param first Iterator pointing to the start of the range to be
504 * @param last Iterator pointing to the end of the range.
506 * Complexity similar to that of the range constructor.
508 template<typename _InputIterator>
510 insert(_InputIterator __first, _InputIterator __last)
511 { _M_t._M_insert_unique(__first, __last); }
514 * @brief Erases an element from a %map.
515 * @param position An iterator pointing to the element to be erased.
517 * This function erases an element, pointed to by the given
518 * iterator, from a %map. Note that this function only erases
519 * the element, and that if the element is itself a pointer,
520 * the pointed-to memory is not touched in any way. Managing
521 * the pointer is the user's responsibilty.
524 erase(iterator __position)
525 { _M_t.erase(__position); }
528 * @brief Erases elements according to the provided key.
529 * @param x Key of element to be erased.
530 * @return The number of elements erased.
532 * This function erases all the elements located by the given key from
534 * Note that this function only erases the element, and that if
535 * the element is itself a pointer, the pointed-to memory is not touched
536 * in any way. Managing the pointer is the user's responsibilty.
539 erase(const key_type& __x)
540 { return _M_t.erase(__x); }
543 * @brief Erases a [first,last) range of elements from a %map.
544 * @param first Iterator pointing to the start of the range to be
546 * @param last Iterator pointing to the end of the range to be erased.
548 * This function erases a sequence of elements from a %map.
549 * Note that this function only erases the element, and that if
550 * the element is itself a pointer, the pointed-to memory is not touched
551 * in any way. Managing the pointer is the user's responsibilty.
554 erase(iterator __first, iterator __last)
555 { _M_t.erase(__first, __last); }
558 * @brief Swaps data with another %map.
559 * @param x A %map of the same element and allocator types.
561 * This exchanges the elements between two maps in constant
562 * time. (It is only swapping a pointer, an integer, and an
563 * instance of the @c Compare type (which itself is often
564 * stateless and empty), so it should be quite fast.) Note
565 * that the global std::swap() function is specialized such
566 * that std::swap(m1,m2) will feed to this function.
569 #ifdef __GXX_EXPERIMENTAL_CXX0X__
574 { _M_t.swap(__x._M_t); }
577 * Erases all elements in a %map. Note that this function only
578 * erases the elements, and that if the elements themselves are
579 * pointers, the pointed-to memory is not touched in any way.
580 * Managing the pointer is the user's responsibilty.
588 * Returns the key comparison object out of which the %map was
593 { return _M_t.key_comp(); }
596 * Returns a value comparison object, built from the key comparison
597 * object out of which the %map was constructed.
601 { return value_compare(_M_t.key_comp()); }
603 // [23.3.1.3] map operations
605 * @brief Tries to locate an element in a %map.
606 * @param x Key of (key, value) %pair to be located.
607 * @return Iterator pointing to sought-after element, or end() if not
610 * This function takes a key and tries to locate the element with which
611 * the key matches. If successful the function returns an iterator
612 * pointing to the sought after %pair. If unsuccessful it returns the
613 * past-the-end ( @c end() ) iterator.
616 find(const key_type& __x)
617 { return _M_t.find(__x); }
620 * @brief Tries to locate an element in a %map.
621 * @param x Key of (key, value) %pair to be located.
622 * @return Read-only (constant) iterator pointing to sought-after
623 * element, or end() if not found.
625 * This function takes a key and tries to locate the element with which
626 * the key matches. If successful the function returns a constant
627 * iterator pointing to the sought after %pair. If unsuccessful it
628 * returns the past-the-end ( @c end() ) iterator.
631 find(const key_type& __x) const
632 { return _M_t.find(__x); }
635 * @brief Finds the number of elements with given key.
636 * @param x Key of (key, value) pairs to be located.
637 * @return Number of elements with specified key.
639 * This function only makes sense for multimaps; for map the result will
640 * either be 0 (not present) or 1 (present).
643 count(const key_type& __x) const
644 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
647 * @brief Finds the beginning of a subsequence matching given key.
648 * @param x Key of (key, value) pair to be located.
649 * @return Iterator pointing to first element equal to or greater
650 * than key, or end().
652 * This function returns the first element of a subsequence of elements
653 * that matches the given key. If unsuccessful it returns an iterator
654 * pointing to the first element that has a greater value than given key
655 * or end() if no such element exists.
658 lower_bound(const key_type& __x)
659 { return _M_t.lower_bound(__x); }
662 * @brief Finds the beginning of a subsequence matching given key.
663 * @param x Key of (key, value) pair to be located.
664 * @return Read-only (constant) iterator pointing to first element
665 * equal to or greater than key, or end().
667 * This function returns the first element of a subsequence of elements
668 * that matches the given key. If unsuccessful it returns an iterator
669 * pointing to the first element that has a greater value than given key
670 * or end() if no such element exists.
673 lower_bound(const key_type& __x) const
674 { return _M_t.lower_bound(__x); }
677 * @brief Finds the end of a subsequence matching given key.
678 * @param x Key of (key, value) pair to be located.
679 * @return Iterator pointing to the first element
680 * greater than key, or end().
683 upper_bound(const key_type& __x)
684 { return _M_t.upper_bound(__x); }
687 * @brief Finds the end of a subsequence matching given key.
688 * @param x Key of (key, value) pair to be located.
689 * @return Read-only (constant) iterator pointing to first iterator
690 * greater than key, or end().
693 upper_bound(const key_type& __x) const
694 { return _M_t.upper_bound(__x); }
697 * @brief Finds a subsequence matching given key.
698 * @param x Key of (key, value) pairs to be located.
699 * @return Pair of iterators that possibly points to the subsequence
700 * matching given key.
702 * This function is equivalent to
704 * std::make_pair(c.lower_bound(val),
705 * c.upper_bound(val))
707 * (but is faster than making the calls separately).
709 * This function probably only makes sense for multimaps.
711 std::pair<iterator, iterator>
712 equal_range(const key_type& __x)
713 { return _M_t.equal_range(__x); }
716 * @brief Finds a subsequence matching given key.
717 * @param x Key of (key, value) pairs to be located.
718 * @return Pair of read-only (constant) iterators that possibly points
719 * to the subsequence matching given key.
721 * This function is equivalent to
723 * std::make_pair(c.lower_bound(val),
724 * c.upper_bound(val))
726 * (but is faster than making the calls separately).
728 * This function probably only makes sense for multimaps.
730 std::pair<const_iterator, const_iterator>
731 equal_range(const key_type& __x) const
732 { return _M_t.equal_range(__x); }
734 template<typename _K1, typename _T1, typename _C1, typename _A1>
736 operator==(const map<_K1, _T1, _C1, _A1>&,
737 const map<_K1, _T1, _C1, _A1>&);
739 template<typename _K1, typename _T1, typename _C1, typename _A1>
741 operator<(const map<_K1, _T1, _C1, _A1>&,
742 const map<_K1, _T1, _C1, _A1>&);
746 * @brief Map equality comparison.
748 * @param y A %map of the same type as @a x.
749 * @return True iff the size and elements of the maps are equal.
751 * This is an equivalence relation. It is linear in the size of the
752 * maps. Maps are considered equivalent if their sizes are equal,
753 * and if corresponding elements compare equal.
755 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
757 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
758 const map<_Key, _Tp, _Compare, _Alloc>& __y)
759 { return __x._M_t == __y._M_t; }
762 * @brief Map ordering relation.
764 * @param y A %map of the same type as @a x.
765 * @return True iff @a x is lexicographically less than @a y.
767 * This is a total ordering relation. It is linear in the size of the
768 * maps. The elements must be comparable with @c <.
770 * See std::lexicographical_compare() for how the determination is made.
772 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
774 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
775 const map<_Key, _Tp, _Compare, _Alloc>& __y)
776 { return __x._M_t < __y._M_t; }
778 /// Based on operator==
779 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
781 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
782 const map<_Key, _Tp, _Compare, _Alloc>& __y)
783 { return !(__x == __y); }
785 /// Based on operator<
786 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
788 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
789 const map<_Key, _Tp, _Compare, _Alloc>& __y)
790 { return __y < __x; }
792 /// Based on operator<
793 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
795 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
796 const map<_Key, _Tp, _Compare, _Alloc>& __y)
797 { return !(__y < __x); }
799 /// Based on operator<
800 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
802 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
803 const map<_Key, _Tp, _Compare, _Alloc>& __y)
804 { return !(__x < __y); }
806 /// See std::map::swap().
807 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
809 swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
810 map<_Key, _Tp, _Compare, _Alloc>& __y)
813 #ifdef __GXX_EXPERIMENTAL_CXX0X__
814 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
816 swap(map<_Key, _Tp, _Compare, _Alloc>&& __x,
817 map<_Key, _Tp, _Compare, _Alloc>& __y)
820 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
822 swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
823 map<_Key, _Tp, _Compare, _Alloc>&& __y)
827 _GLIBCXX_END_NESTED_NAMESPACE
829 #endif /* _STL_MAP_H */