1 // Multimap 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.
57 /** @file stl_multimap.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_MULTIMAP_H
63 #define _STL_MULTIMAP_H 1
65 #include <bits/concept_check.h>
66 #include <initializer_list>
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.
74 * @ingroup associative_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 equivalent
79 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
80 * is T, and the value_type is std::pair<const Key,T>.
82 * Multimaps support bidirectional iterators.
84 * The private tree data is declared exactly the same way for map and
85 * multimap; the distinction is made entirely in how the tree functions are
86 * called (*_unique versus *_equal, same as the standard).
88 template <typename _Key
, typename _Tp
,
89 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 multimap
<_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
;
131 /// The actual tree structure.
135 // many of these are specified differently in ISO, but the following are
136 // "functionally equivalent"
137 typedef typename
_Pair_alloc_type::pointer pointer
;
138 typedef typename
_Pair_alloc_type::const_pointer const_pointer
;
139 typedef typename
_Pair_alloc_type::reference reference
;
140 typedef typename
_Pair_alloc_type::const_reference const_reference
;
141 typedef typename
_Rep_type::iterator iterator
;
142 typedef typename
_Rep_type::const_iterator const_iterator
;
143 typedef typename
_Rep_type::size_type size_type
;
144 typedef typename
_Rep_type::difference_type difference_type
;
145 typedef typename
_Rep_type::reverse_iterator reverse_iterator
;
146 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
148 // [23.3.2] construct/copy/destroy
149 // (get_allocator() is also listed in this section)
151 * @brief Default constructor creates no elements.
157 * @brief Creates a %multimap with no elements.
158 * @param comp A comparison object.
159 * @param a An allocator object.
162 multimap(const _Compare
& __comp
,
163 const allocator_type
& __a
= allocator_type())
164 : _M_t(__comp
, __a
) { }
167 * @brief %Multimap copy constructor.
168 * @param x A %multimap of identical element and allocator types.
170 * The newly-created %multimap uses a copy of the allocation object
173 multimap(const multimap
& __x
)
176 #ifdef __GXX_EXPERIMENTAL_CXX0X__
178 * @brief %Multimap move constructor.
179 * @param x A %multimap of identical element and allocator types.
181 * The newly-created %multimap contains the exact contents of @a x.
182 * The contents of @a x are a valid, but unspecified %multimap.
184 multimap(multimap
&& __x
)
185 : _M_t(std::forward
<_Rep_type
>(__x
._M_t
)) { }
188 * @brief Builds a %multimap from an initializer_list.
189 * @param l An initializer_list.
190 * @param comp A comparison functor.
191 * @param a An allocator object.
193 * Create a %multimap consisting of copies of the elements from
194 * the initializer_list. This is linear in N if the list is already
195 * sorted, and NlogN otherwise (where N is @a __l.size()).
197 multimap(initializer_list
<value_type
> __l
,
198 const _Compare
& __comp
= _Compare(),
199 const allocator_type
& __a
= allocator_type())
201 { _M_t
._M_insert_equal(__l
.begin(), __l
.end()); }
205 * @brief Builds a %multimap from a range.
206 * @param first An input iterator.
207 * @param last An input iterator.
209 * Create a %multimap consisting of copies of the elements from
210 * [first,last). This is linear in N if the range is already sorted,
211 * and NlogN otherwise (where N is distance(first,last)).
213 template<typename _InputIterator
>
214 multimap(_InputIterator __first
, _InputIterator __last
)
216 { _M_t
._M_insert_equal(__first
, __last
); }
219 * @brief Builds a %multimap from a range.
220 * @param first An input iterator.
221 * @param last An input iterator.
222 * @param comp A comparison functor.
223 * @param a An allocator object.
225 * Create a %multimap consisting of copies of the elements from
226 * [first,last). This is linear in N if the range is already sorted,
227 * and NlogN otherwise (where N is distance(first,last)).
229 template<typename _InputIterator
>
230 multimap(_InputIterator __first
, _InputIterator __last
,
231 const _Compare
& __comp
,
232 const allocator_type
& __a
= allocator_type())
234 { _M_t
._M_insert_equal(__first
, __last
); }
236 // FIXME There is no dtor declared, but we should have something generated
237 // by Doxygen. I don't know what tags to add to this paragraph to make
240 * The dtor only erases the elements, and note that if the elements
241 * themselves are pointers, the pointed-to memory is not touched in any
242 * way. Managing the pointer is the user's responsibility.
246 * @brief %Multimap assignment operator.
247 * @param x A %multimap of identical element and allocator types.
249 * All the elements of @a x are copied, but unlike the copy constructor,
250 * the allocator object is not copied.
253 operator=(const multimap
& __x
)
259 #ifdef __GXX_EXPERIMENTAL_CXX0X__
261 * @brief %Multimap move assignment operator.
262 * @param x A %multimap of identical element and allocator types.
264 * The contents of @a x are moved into this multimap (without copying).
265 * @a x is a valid, but unspecified multimap.
268 operator=(multimap
&& __x
)
277 * @brief %Multimap list assignment operator.
278 * @param l An initializer_list.
280 * This function fills a %multimap with copies of the elements
281 * in the initializer list @a l.
283 * Note that the assignment completely changes the %multimap and
284 * that the resulting %multimap's size is the same as the number
285 * of elements assigned. Old data may be lost.
288 operator=(initializer_list
<value_type
> __l
)
291 this->insert(__l
.begin(), __l
.end());
296 /// Get a copy of the memory allocation object.
298 get_allocator() const
299 { return _M_t
.get_allocator(); }
303 * Returns a read/write iterator that points to the first pair in the
304 * %multimap. Iteration is done in ascending order according to the
309 { return _M_t
.begin(); }
312 * Returns a read-only (constant) iterator that points to the first pair
313 * in the %multimap. Iteration is done in ascending order according to
318 { return _M_t
.begin(); }
321 * Returns a read/write iterator that points one past the last pair in
322 * the %multimap. Iteration is done in ascending order according to the
327 { return _M_t
.end(); }
330 * Returns a read-only (constant) iterator that points one past the last
331 * pair in the %multimap. Iteration is done in ascending order according
336 { return _M_t
.end(); }
339 * Returns a read/write reverse iterator that points to the last pair in
340 * the %multimap. Iteration is done in descending order according to the
345 { return _M_t
.rbegin(); }
348 * Returns a read-only (constant) reverse iterator that points to the
349 * last pair in the %multimap. Iteration is done in descending order
350 * according to the keys.
352 const_reverse_iterator
354 { return _M_t
.rbegin(); }
357 * Returns a read/write reverse iterator that points to one before the
358 * first pair in the %multimap. Iteration is done in descending order
359 * according to the keys.
363 { return _M_t
.rend(); }
366 * Returns a read-only (constant) reverse iterator that points to one
367 * before the first pair in the %multimap. Iteration is done in
368 * descending order according to the keys.
370 const_reverse_iterator
372 { return _M_t
.rend(); }
374 #ifdef __GXX_EXPERIMENTAL_CXX0X__
376 * Returns a read-only (constant) iterator that points to the first pair
377 * in the %multimap. Iteration is done in ascending order according to
382 { return _M_t
.begin(); }
385 * Returns a read-only (constant) iterator that points one past the last
386 * pair in the %multimap. Iteration is done in ascending order according
391 { return _M_t
.end(); }
394 * Returns a read-only (constant) reverse iterator that points to the
395 * last pair in the %multimap. Iteration is done in descending order
396 * according to the keys.
398 const_reverse_iterator
400 { return _M_t
.rbegin(); }
403 * Returns a read-only (constant) reverse iterator that points to one
404 * before the first pair in the %multimap. Iteration is done in
405 * descending order according to the keys.
407 const_reverse_iterator
409 { return _M_t
.rend(); }
413 /** Returns true if the %multimap is empty. */
416 { return _M_t
.empty(); }
418 /** Returns the size of the %multimap. */
421 { return _M_t
.size(); }
423 /** Returns the maximum size of the %multimap. */
426 { return _M_t
.max_size(); }
430 * @brief Inserts a std::pair into the %multimap.
431 * @param x Pair to be inserted (see std::make_pair for easy creation
433 * @return An iterator that points to the inserted (key,value) pair.
435 * This function inserts a (key, value) pair into the %multimap.
436 * Contrary to a std::map the %multimap does not rely on unique keys and
437 * thus multiple pairs with the same key can be inserted.
439 * Insertion requires logarithmic time.
442 insert(const value_type
& __x
)
443 { return _M_t
._M_insert_equal(__x
); }
446 * @brief Inserts a std::pair into the %multimap.
447 * @param position An iterator that serves as a hint as to where the
448 * pair should be inserted.
449 * @param x Pair to be inserted (see std::make_pair for easy creation
451 * @return An iterator that points to the inserted (key,value) pair.
453 * This function inserts a (key, value) pair into the %multimap.
454 * Contrary to a std::map the %multimap does not rely on unique keys and
455 * thus multiple pairs with the same key can be inserted.
456 * Note that the first parameter is only a hint and can potentially
457 * improve the performance of the insertion process. A bad hint would
458 * cause no gains in efficiency.
460 * For more on "hinting," see:
461 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
463 * Insertion requires logarithmic time (if the hint is not taken).
466 insert(iterator __position
, const value_type
& __x
)
467 { return _M_t
._M_insert_equal_(__position
, __x
); }
470 * @brief A template function that attempts to insert a range
472 * @param first Iterator pointing to the start of the range to be
474 * @param last Iterator pointing to the end of the range.
476 * Complexity similar to that of the range constructor.
478 template<typename _InputIterator
>
480 insert(_InputIterator __first
, _InputIterator __last
)
481 { _M_t
._M_insert_equal(__first
, __last
); }
483 #ifdef __GXX_EXPERIMENTAL_CXX0X__
485 * @brief Attempts to insert a list of std::pairs into the %multimap.
486 * @param list A std::initializer_list<value_type> of pairs to be
489 * Complexity similar to that of the range constructor.
492 insert(initializer_list
<value_type
> __l
)
493 { this->insert(__l
.begin(), __l
.end()); }
497 * @brief Erases an element from a %multimap.
498 * @param position An iterator pointing to the element to be erased.
500 * This function erases an element, pointed to by the given iterator,
501 * from a %multimap. Note that this function only erases the element,
502 * and that if the element is itself a pointer, the pointed-to memory is
503 * not touched in any way. Managing the pointer is the user's
507 erase(iterator __position
)
508 { _M_t
.erase(__position
); }
511 * @brief Erases elements according to the provided key.
512 * @param x Key of element to be erased.
513 * @return The number of elements erased.
515 * This function erases all elements located by the given key from a
517 * Note that this function only erases the element, and that if
518 * the element is itself a pointer, the pointed-to memory is not touched
519 * in any way. Managing the pointer is the user's responsibility.
522 erase(const key_type
& __x
)
523 { return _M_t
.erase(__x
); }
526 * @brief Erases a [first,last) range of elements from a %multimap.
527 * @param first Iterator pointing to the start of the range to be
529 * @param last Iterator pointing to the end of the range to be erased.
531 * This function erases a sequence of elements from a %multimap.
532 * Note that this function only erases the elements, and that if
533 * the elements themselves are pointers, the pointed-to memory is not
534 * touched in any way. Managing the pointer is the user's responsibility.
537 erase(iterator __first
, iterator __last
)
538 { _M_t
.erase(__first
, __last
); }
541 * @brief Swaps data with another %multimap.
542 * @param x A %multimap of the same element and allocator types.
544 * This exchanges the elements between two multimaps in constant time.
545 * (It is only swapping a pointer, an integer, and an instance of
546 * the @c Compare type (which itself is often stateless and empty), so it
547 * should be quite fast.)
548 * Note that the global std::swap() function is specialized such that
549 * std::swap(m1,m2) will feed to this function.
552 #ifdef __GXX_EXPERIMENTAL_CXX0X__
557 { _M_t
.swap(__x
._M_t
); }
560 * Erases all elements in a %multimap. Note that this function only
561 * erases the elements, and that if the elements themselves are pointers,
562 * the pointed-to memory is not touched in any way. Managing the pointer
563 * is the user's responsibility.
571 * Returns the key comparison object out of which the %multimap
576 { return _M_t
.key_comp(); }
579 * Returns a value comparison object, built from the key comparison
580 * object out of which the %multimap was constructed.
584 { return value_compare(_M_t
.key_comp()); }
586 // multimap operations
588 * @brief Tries to locate an element in a %multimap.
589 * @param x Key of (key, value) pair to be located.
590 * @return Iterator pointing to sought-after element,
591 * or end() if not found.
593 * This function takes a key and tries to locate the element with which
594 * the key matches. If successful the function returns an iterator
595 * pointing to the sought after %pair. If unsuccessful it returns the
596 * past-the-end ( @c end() ) iterator.
599 find(const key_type
& __x
)
600 { return _M_t
.find(__x
); }
603 * @brief Tries to locate an element in a %multimap.
604 * @param x Key of (key, value) pair to be located.
605 * @return Read-only (constant) iterator pointing to sought-after
606 * element, or end() if not found.
608 * This function takes a key and tries to locate the element with which
609 * the key matches. If successful the function returns a constant
610 * iterator pointing to the sought after %pair. If unsuccessful it
611 * returns the past-the-end ( @c end() ) iterator.
614 find(const key_type
& __x
) const
615 { return _M_t
.find(__x
); }
618 * @brief Finds the number of elements with given key.
619 * @param x Key of (key, value) pairs to be located.
620 * @return Number of elements with specified key.
623 count(const key_type
& __x
) const
624 { return _M_t
.count(__x
); }
627 * @brief Finds the beginning of a subsequence matching given key.
628 * @param x Key of (key, value) pair to be located.
629 * @return Iterator pointing to first element equal to or greater
630 * than key, or end().
632 * This function returns the first element of a subsequence of elements
633 * that matches the given key. If unsuccessful it returns an iterator
634 * pointing to the first element that has a greater value than given key
635 * or end() if no such element exists.
638 lower_bound(const key_type
& __x
)
639 { return _M_t
.lower_bound(__x
); }
642 * @brief Finds the beginning of a subsequence matching given key.
643 * @param x Key of (key, value) pair to be located.
644 * @return Read-only (constant) iterator pointing to first element
645 * equal to or greater than key, or end().
647 * This function returns the first element of a subsequence of elements
648 * that matches the given key. If unsuccessful the iterator will point
649 * to the next greatest element or, if no such greater element exists, to
653 lower_bound(const key_type
& __x
) const
654 { return _M_t
.lower_bound(__x
); }
657 * @brief Finds the end of a subsequence matching given key.
658 * @param x Key of (key, value) pair to be located.
659 * @return Iterator pointing to the first element
660 * greater than key, or end().
663 upper_bound(const key_type
& __x
)
664 { return _M_t
.upper_bound(__x
); }
667 * @brief Finds the end of a subsequence matching given key.
668 * @param x Key of (key, value) pair to be located.
669 * @return Read-only (constant) iterator pointing to first iterator
670 * greater than key, or end().
673 upper_bound(const key_type
& __x
) const
674 { return _M_t
.upper_bound(__x
); }
677 * @brief Finds a subsequence matching given key.
678 * @param x Key of (key, value) pairs to be located.
679 * @return Pair of iterators that possibly points to the subsequence
680 * matching given key.
682 * This function is equivalent to
684 * std::make_pair(c.lower_bound(val),
685 * c.upper_bound(val))
687 * (but is faster than making the calls separately).
689 std::pair
<iterator
, iterator
>
690 equal_range(const key_type
& __x
)
691 { return _M_t
.equal_range(__x
); }
694 * @brief Finds a subsequence matching given key.
695 * @param x Key of (key, value) pairs to be located.
696 * @return Pair of read-only (constant) iterators that possibly points
697 * to the subsequence matching given key.
699 * This function is equivalent to
701 * std::make_pair(c.lower_bound(val),
702 * c.upper_bound(val))
704 * (but is faster than making the calls separately).
706 std::pair
<const_iterator
, const_iterator
>
707 equal_range(const key_type
& __x
) const
708 { return _M_t
.equal_range(__x
); }
710 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
712 operator==(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
713 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
715 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
717 operator<(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
718 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
722 * @brief Multimap equality comparison.
723 * @param x A %multimap.
724 * @param y A %multimap of the same type as @a x.
725 * @return True iff the size and elements of the maps are equal.
727 * This is an equivalence relation. It is linear in the size of the
728 * multimaps. Multimaps are considered equivalent if their sizes are equal,
729 * and if corresponding elements compare equal.
731 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
733 operator==(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
734 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
735 { return __x
._M_t
== __y
._M_t
; }
738 * @brief Multimap ordering relation.
739 * @param x A %multimap.
740 * @param y A %multimap of the same type as @a x.
741 * @return True iff @a x is lexicographically less than @a y.
743 * This is a total ordering relation. It is linear in the size of the
744 * multimaps. The elements must be comparable with @c <.
746 * See std::lexicographical_compare() for how the determination is made.
748 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
750 operator<(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
751 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
752 { return __x
._M_t
< __y
._M_t
; }
754 /// Based on operator==
755 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
757 operator!=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
758 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
759 { return !(__x
== __y
); }
761 /// Based on operator<
762 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
764 operator>(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
765 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
766 { return __y
< __x
; }
768 /// Based on operator<
769 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
771 operator<=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
772 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
773 { return !(__y
< __x
); }
775 /// Based on operator<
776 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
778 operator>=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
779 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
780 { return !(__x
< __y
); }
782 /// See std::multimap::swap().
783 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
785 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
786 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
789 #ifdef __GXX_EXPERIMENTAL_CXX0X__
790 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
792 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>&& __x
,
793 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
796 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
798 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
799 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>&& __y
)
803 _GLIBCXX_END_NESTED_NAMESPACE
805 #endif /* _STL_MULTIMAP_H */