1 // Map implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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.
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).
89 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
90 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
94 typedef _Key key_type;
95 typedef _Tp mapped_type;
96 typedef std::pair<const _Key, _Tp> value_type;
97 typedef _Compare key_compare;
98 typedef _Alloc allocator_type;
101 // concept requirements
102 typedef typename _Alloc::value_type _Alloc_value_type;
103 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
104 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
105 _BinaryFunctionConcept)
106 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
110 : public std::binary_function<value_type, value_type, bool>
112 friend class map<_Key, _Tp, _Compare, _Alloc>;
116 value_compare(_Compare __c)
120 bool operator()(const value_type& __x, const value_type& __y) const
121 { return comp(__x.first, __y.first); }
125 /// This turns a red-black tree into a [multi]map.
126 typedef typename _Alloc::template rebind<value_type>::other
129 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
130 key_compare, _Pair_alloc_type> _Rep_type;
132 /// The actual tree structure.
136 // many of these are specified differently in ISO, but the following are
137 // "functionally equivalent"
138 typedef typename _Pair_alloc_type::pointer pointer;
139 typedef typename _Pair_alloc_type::const_pointer const_pointer;
140 typedef typename _Pair_alloc_type::reference reference;
141 typedef typename _Pair_alloc_type::const_reference const_reference;
142 typedef typename _Rep_type::iterator iterator;
143 typedef typename _Rep_type::const_iterator const_iterator;
144 typedef typename _Rep_type::size_type size_type;
145 typedef typename _Rep_type::difference_type difference_type;
146 typedef typename _Rep_type::reverse_iterator reverse_iterator;
147 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
149 // [23.3.1.1] construct/copy/destroy
150 // (get_allocator() is normally listed in this section, but seems to have
151 // been accidentally omitted in the printed standard)
153 * @brief Default constructor creates no elements.
159 * @brief Creates a %map with no elements.
160 * @param comp A comparison object.
161 * @param a An allocator object.
164 map(const _Compare& __comp,
165 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
178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
180 * @brief %Map move constructor.
181 * @param x A %map of identical element and allocator types.
183 * The newly-created %map contains the exact contents of @a x.
184 * The contents of @a x are a valid, but unspecified %map.
187 : _M_t(std::forward<_Rep_type>(__x._M_t)) { }
191 * @brief Builds a %map from a range.
192 * @param first An input iterator.
193 * @param last An input iterator.
195 * Create a %map consisting of copies of the elements from [first,last).
196 * This is linear in N if the range is already sorted, and NlogN
197 * otherwise (where N is distance(first,last)).
199 template<typename _InputIterator>
200 map(_InputIterator __first, _InputIterator __last)
202 { _M_t._M_insert_unique(__first, __last); }
205 * @brief Builds a %map from a range.
206 * @param first An input iterator.
207 * @param last An input iterator.
208 * @param comp A comparison functor.
209 * @param a An allocator object.
211 * Create a %map consisting of copies of the elements from [first,last).
212 * This is linear in N if the range is already sorted, and NlogN
213 * otherwise (where N is distance(first,last)).
215 template<typename _InputIterator>
216 map(_InputIterator __first, _InputIterator __last,
217 const _Compare& __comp,
218 const allocator_type& __a = allocator_type())
220 { _M_t._M_insert_unique(__first, __last); }
222 // FIXME There is no dtor declared, but we should have something
223 // generated by Doxygen. I don't know what tags to add to this
224 // paragraph to make that happen:
226 * The dtor only erases the elements, and note that if the elements
227 * themselves are pointers, the pointed-to memory is not touched in any
228 * way. Managing the pointer is the user's responsibility.
232 * @brief %Map assignment operator.
233 * @param x A %map of identical element and allocator types.
235 * All the elements of @a x are copied, but unlike the copy constructor,
236 * the allocator object is not copied.
239 operator=(const map& __x)
245 #ifdef __GXX_EXPERIMENTAL_CXX0X__
247 * @brief %Map move assignment operator.
248 * @param x A %map of identical element and allocator types.
250 * The contents of @a x are moved into this map (without copying).
251 * @a x is a valid, but unspecified %map.
263 /// Get a copy of the memory allocation object.
265 get_allocator() const
266 { return _M_t.get_allocator(); }
270 * Returns a read/write iterator that points to the first pair in the
272 * Iteration is done in ascending order according to the keys.
276 { return _M_t.begin(); }
279 * Returns a read-only (constant) iterator that points to the first pair
280 * in the %map. Iteration is done in ascending order according to the
285 { return _M_t.begin(); }
288 * Returns a read/write iterator that points one past the last
289 * pair in the %map. Iteration is done in ascending order
290 * according to the keys.
294 { return _M_t.end(); }
297 * Returns a read-only (constant) iterator that points one past the last
298 * pair in the %map. Iteration is done in ascending order according to
303 { return _M_t.end(); }
306 * Returns a read/write reverse iterator that points to the last pair in
307 * the %map. Iteration is done in descending order according to the
312 { return _M_t.rbegin(); }
315 * Returns a read-only (constant) reverse iterator that points to the
316 * last pair in the %map. Iteration is done in descending order
317 * according to the keys.
319 const_reverse_iterator
321 { return _M_t.rbegin(); }
324 * Returns a read/write reverse iterator that points to one before the
325 * first pair in the %map. Iteration is done in descending order
326 * according to the keys.
330 { return _M_t.rend(); }
333 * Returns a read-only (constant) reverse iterator that points to one
334 * before the first pair in the %map. Iteration is done in descending
335 * order according to the keys.
337 const_reverse_iterator
339 { return _M_t.rend(); }
341 #ifdef __GXX_EXPERIMENTAL_CXX0X__
343 * Returns a read-only (constant) iterator that points to the first pair
344 * in the %map. Iteration is done in ascending order according to the
349 { return _M_t.begin(); }
352 * Returns a read-only (constant) iterator that points one past the last
353 * pair in the %map. Iteration is done in ascending order according to
358 { return _M_t.end(); }
361 * Returns a read-only (constant) reverse iterator that points to the
362 * last pair in the %map. Iteration is done in descending order
363 * according to the keys.
365 const_reverse_iterator
367 { return _M_t.rbegin(); }
370 * Returns a read-only (constant) reverse iterator that points to one
371 * before the first pair in the %map. Iteration is done in descending
372 * order according to the keys.
374 const_reverse_iterator
376 { return _M_t.rend(); }
380 /** Returns true if the %map is empty. (Thus begin() would equal
385 { return _M_t.empty(); }
387 /** Returns the size of the %map. */
390 { return _M_t.size(); }
392 /** Returns the maximum size of the %map. */
395 { return _M_t.max_size(); }
397 // [23.3.1.2] element access
399 * @brief Subscript ( @c [] ) access to %map data.
400 * @param k The key for which data should be retrieved.
401 * @return A reference to the data of the (key,data) %pair.
403 * Allows for easy lookup with the subscript ( @c [] )
404 * operator. Returns data associated with the key specified in
405 * subscript. If the key does not exist, a pair with that key
406 * is created using default values, which is then returned.
408 * Lookup requires logarithmic time.
411 operator[](const key_type& __k)
413 // concept requirements
414 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
416 iterator __i = lower_bound(__k);
417 // __i->first is greater than or equivalent to __k.
418 if (__i == end() || key_comp()(__k, (*__i).first))
419 __i = insert(__i, value_type(__k, mapped_type()));
420 return (*__i).second;
423 // _GLIBCXX_RESOLVE_LIB_DEFECTS
424 // DR 464. Suggestion for new member functions in standard containers.
426 * @brief Access to %map data.
427 * @param k The key for which data should be retrieved.
428 * @return A reference to the data whose key is equivalent to @a k, if
429 * such a data is present in the %map.
430 * @throw std::out_of_range If no such data is present.
433 at(const key_type& __k)
435 iterator __i = lower_bound(__k);
436 if (__i == end() || key_comp()(__k, (*__i).first))
437 __throw_out_of_range(__N("map::at"));
438 return (*__i).second;
442 at(const key_type& __k) const
444 const_iterator __i = lower_bound(__k);
445 if (__i == end() || key_comp()(__k, (*__i).first))
446 __throw_out_of_range(__N("map::at"));
447 return (*__i).second;
452 * @brief Attempts to insert a std::pair into the %map.
454 * @param x Pair to be inserted (see std::make_pair for easy creation
457 * @return A pair, of which the first element is an iterator that
458 * points to the possibly inserted pair, and the second is
459 * a bool that is true if the pair was actually inserted.
461 * This function attempts to insert a (key, value) %pair into the %map.
462 * A %map relies on unique keys and thus a %pair is only inserted if its
463 * first element (the key) is not already present in the %map.
465 * Insertion requires logarithmic time.
467 std::pair<iterator, bool>
468 insert(const value_type& __x)
469 { return _M_t._M_insert_unique(__x); }
472 * @brief Attempts to insert a std::pair into the %map.
473 * @param position An iterator that serves as a hint as to where the
474 * pair should be inserted.
475 * @param x Pair to be inserted (see std::make_pair for easy creation
477 * @return An iterator that points to the element with key of @a x (may
478 * or may not be the %pair passed in).
481 * This function is not concerned about whether the insertion
482 * took place, and thus does not return a boolean like the
483 * single-argument insert() does. Note that the first
484 * parameter is only a hint and can potentially improve the
485 * performance of the insertion process. A bad hint would
486 * cause no gains in efficiency.
489 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
490 * for more on "hinting".
492 * Insertion requires logarithmic time (if the hint is not taken).
495 insert(iterator __position, const value_type& __x)
496 { return _M_t._M_insert_unique_(__position, __x); }
499 * @brief Template function that attempts to insert a range of elements.
500 * @param first Iterator pointing to the start of the range to be
502 * @param last Iterator pointing to the end of the range.
504 * Complexity similar to that of the range constructor.
506 template<typename _InputIterator>
508 insert(_InputIterator __first, _InputIterator __last)
509 { _M_t._M_insert_unique(__first, __last); }
512 * @brief Erases an element from a %map.
513 * @param position An iterator pointing to the element to be erased.
515 * This function erases an element, pointed to by the given
516 * iterator, from a %map. Note that this function only erases
517 * the element, and that if the element is itself a pointer,
518 * the pointed-to memory is not touched in any way. Managing
519 * the pointer is the user's responsibility.
522 erase(iterator __position)
523 { _M_t.erase(__position); }
526 * @brief Erases elements according to the provided key.
527 * @param x Key of element to be erased.
528 * @return The number of elements erased.
530 * This function erases all the elements located by the given key from
532 * Note that this function only erases the element, and that if
533 * the element is itself a pointer, the pointed-to memory is not touched
534 * in any way. Managing the pointer is the user's responsibility.
537 erase(const key_type& __x)
538 { return _M_t.erase(__x); }
541 * @brief Erases a [first,last) range of elements from a %map.
542 * @param first Iterator pointing to the start of the range to be
544 * @param last Iterator pointing to the end of the range to be erased.
546 * This function erases a sequence of elements from a %map.
547 * Note that this function only erases the element, and that if
548 * the element is itself a pointer, the pointed-to memory is not touched
549 * in any way. Managing the pointer is the user's responsibility.
552 erase(iterator __first, iterator __last)
553 { _M_t.erase(__first, __last); }
556 * @brief Swaps data with another %map.
557 * @param x A %map of the same element and allocator types.
559 * This exchanges the elements between two maps in constant
560 * time. (It is only swapping a pointer, an integer, and an
561 * instance of the @c Compare type (which itself is often
562 * stateless and empty), so it should be quite fast.) Note
563 * that the global std::swap() function is specialized such
564 * that std::swap(m1,m2) will feed to this function.
567 #ifdef __GXX_EXPERIMENTAL_CXX0X__
572 { _M_t.swap(__x._M_t); }
575 * Erases all elements in a %map. Note that this function only
576 * erases the elements, and that if the elements themselves are
577 * pointers, the pointed-to memory is not touched in any way.
578 * Managing the pointer is the user's responsibility.
586 * Returns the key comparison object out of which the %map was
591 { return _M_t.key_comp(); }
594 * Returns a value comparison object, built from the key comparison
595 * object out of which the %map was constructed.
599 { return value_compare(_M_t.key_comp()); }
601 // [23.3.1.3] map operations
603 * @brief Tries to locate an element in a %map.
604 * @param x Key of (key, value) %pair to be located.
605 * @return Iterator pointing to sought-after element, or end() if not
608 * This function takes a key and tries to locate the element with which
609 * the key matches. If successful the function returns an iterator
610 * pointing to the sought after %pair. If unsuccessful it returns the
611 * past-the-end ( @c end() ) iterator.
614 find(const key_type& __x)
615 { return _M_t.find(__x); }
618 * @brief Tries to locate an element in a %map.
619 * @param x Key of (key, value) %pair to be located.
620 * @return Read-only (constant) iterator pointing to sought-after
621 * element, or end() if not found.
623 * This function takes a key and tries to locate the element with which
624 * the key matches. If successful the function returns a constant
625 * iterator pointing to the sought after %pair. If unsuccessful it
626 * returns the past-the-end ( @c end() ) iterator.
629 find(const key_type& __x) const
630 { return _M_t.find(__x); }
633 * @brief Finds the number of elements with given key.
634 * @param x Key of (key, value) pairs to be located.
635 * @return Number of elements with specified key.
637 * This function only makes sense for multimaps; for map the result will
638 * either be 0 (not present) or 1 (present).
641 count(const key_type& __x) const
642 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
645 * @brief Finds the beginning of a subsequence matching given key.
646 * @param x Key of (key, value) pair to be located.
647 * @return Iterator pointing to first element equal to or greater
648 * than key, or end().
650 * This function returns the first element of a subsequence of elements
651 * that matches the given key. If unsuccessful it returns an iterator
652 * pointing to the first element that has a greater value than given key
653 * or end() if no such element exists.
656 lower_bound(const key_type& __x)
657 { return _M_t.lower_bound(__x); }
660 * @brief Finds the beginning of a subsequence matching given key.
661 * @param x Key of (key, value) pair to be located.
662 * @return Read-only (constant) iterator pointing to first element
663 * equal to or greater than key, or end().
665 * This function returns the first element of a subsequence of elements
666 * that matches the given key. If unsuccessful it returns an iterator
667 * pointing to the first element that has a greater value than given key
668 * or end() if no such element exists.
671 lower_bound(const key_type& __x) const
672 { return _M_t.lower_bound(__x); }
675 * @brief Finds the end of a subsequence matching given key.
676 * @param x Key of (key, value) pair to be located.
677 * @return Iterator pointing to the first element
678 * greater than key, or end().
681 upper_bound(const key_type& __x)
682 { return _M_t.upper_bound(__x); }
685 * @brief Finds the end of a subsequence matching given key.
686 * @param x Key of (key, value) pair to be located.
687 * @return Read-only (constant) iterator pointing to first iterator
688 * greater than key, or end().
691 upper_bound(const key_type& __x) const
692 { return _M_t.upper_bound(__x); }
695 * @brief Finds a subsequence matching given key.
696 * @param x Key of (key, value) pairs to be located.
697 * @return Pair of iterators that possibly points to the subsequence
698 * matching given key.
700 * This function is equivalent to
702 * std::make_pair(c.lower_bound(val),
703 * c.upper_bound(val))
705 * (but is faster than making the calls separately).
707 * This function probably only makes sense for multimaps.
709 std::pair<iterator, iterator>
710 equal_range(const key_type& __x)
711 { return _M_t.equal_range(__x); }
714 * @brief Finds a subsequence matching given key.
715 * @param x Key of (key, value) pairs to be located.
716 * @return Pair of read-only (constant) iterators that possibly points
717 * to the subsequence matching given key.
719 * This function is equivalent to
721 * std::make_pair(c.lower_bound(val),
722 * c.upper_bound(val))
724 * (but is faster than making the calls separately).
726 * This function probably only makes sense for multimaps.
728 std::pair<const_iterator, const_iterator>
729 equal_range(const key_type& __x) const
730 { return _M_t.equal_range(__x); }
732 template<typename _K1, typename _T1, typename _C1, typename _A1>
734 operator==(const map<_K1, _T1, _C1, _A1>&,
735 const map<_K1, _T1, _C1, _A1>&);
737 template<typename _K1, typename _T1, typename _C1, typename _A1>
739 operator<(const map<_K1, _T1, _C1, _A1>&,
740 const map<_K1, _T1, _C1, _A1>&);
744 * @brief Map equality comparison.
746 * @param y A %map of the same type as @a x.
747 * @return True iff the size and elements of the maps are equal.
749 * This is an equivalence relation. It is linear in the size of the
750 * maps. Maps are considered equivalent if their sizes are equal,
751 * and if corresponding elements compare equal.
753 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
755 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
756 const map<_Key, _Tp, _Compare, _Alloc>& __y)
757 { return __x._M_t == __y._M_t; }
760 * @brief Map ordering relation.
762 * @param y A %map of the same type as @a x.
763 * @return True iff @a x is lexicographically less than @a y.
765 * This is a total ordering relation. It is linear in the size of the
766 * maps. The elements must be comparable with @c <.
768 * See std::lexicographical_compare() for how the determination is made.
770 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
772 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
773 const map<_Key, _Tp, _Compare, _Alloc>& __y)
774 { return __x._M_t < __y._M_t; }
776 /// Based on operator==
777 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
779 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
780 const map<_Key, _Tp, _Compare, _Alloc>& __y)
781 { return !(__x == __y); }
783 /// Based on operator<
784 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
786 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
787 const map<_Key, _Tp, _Compare, _Alloc>& __y)
788 { return __y < __x; }
790 /// Based on operator<
791 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
793 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
794 const map<_Key, _Tp, _Compare, _Alloc>& __y)
795 { return !(__y < __x); }
797 /// Based on operator<
798 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
800 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
801 const map<_Key, _Tp, _Compare, _Alloc>& __y)
802 { return !(__x < __y); }
804 /// See std::map::swap().
805 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
807 swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
808 map<_Key, _Tp, _Compare, _Alloc>& __y)
811 #ifdef __GXX_EXPERIMENTAL_CXX0X__
812 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
814 swap(map<_Key, _Tp, _Compare, _Alloc>&& __x,
815 map<_Key, _Tp, _Compare, _Alloc>& __y)
818 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
820 swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
821 map<_Key, _Tp, _Compare, _Alloc>&& __y)
825 _GLIBCXX_END_NESTED_NAMESPACE
827 #endif /* _STL_MAP_H */