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>
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
70 * @brief A standard container made up of (key,value) pairs, which can be
71 * retrieved based on a key, in logarithmic time.
74 * @ingroup Assoc_containers
76 * Meets the requirements of a <a href="tables.html#65">container</a>, a
77 * <a href="tables.html#66">reversible container</a>, and an
78 * <a href="tables.html#69">associative container</a> (using 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
)) { }
189 * @brief Builds a %multimap from a range.
190 * @param first An input iterator.
191 * @param last An input iterator.
193 * Create a %multimap consisting of copies of the elements from
194 * [first,last). This is linear in N if the range is already sorted,
195 * and NlogN otherwise (where N is distance(first,last)).
197 template<typename _InputIterator
>
198 multimap(_InputIterator __first
, _InputIterator __last
)
200 { _M_t
._M_insert_unique(__first
, __last
); }
203 * @brief Builds a %multimap from a range.
204 * @param first An input iterator.
205 * @param last An input iterator.
206 * @param comp A comparison functor.
207 * @param a An allocator object.
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
,
215 const _Compare
& __comp
,
216 const allocator_type
& __a
= allocator_type())
218 { _M_t
._M_insert_equal(__first
, __last
); }
220 // FIXME There is no dtor declared, but we should have something generated
221 // by Doxygen. I don't know what tags to add to this paragraph to make
224 * The dtor only erases the elements, and note that if the elements
225 * themselves are pointers, the pointed-to memory is not touched in any
226 * way. Managing the pointer is the user's responsibility.
230 * @brief %Multimap assignment operator.
231 * @param x A %multimap of identical element and allocator types.
233 * All the elements of @a x are copied, but unlike the copy constructor,
234 * the allocator object is not copied.
237 operator=(const multimap
& __x
)
243 #ifdef __GXX_EXPERIMENTAL_CXX0X__
245 * @brief %Multimap move assignment operator.
246 * @param x A %multimap of identical element and allocator types.
248 * The contents of @a x are moved into this multimap (without copying).
249 * @a x is a valid, but unspecified multimap.
252 operator=(multimap
&& __x
)
261 /// Get a copy of the memory allocation object.
263 get_allocator() const
264 { return _M_t
.get_allocator(); }
268 * Returns a read/write iterator that points to the first pair in the
269 * %multimap. Iteration is done in ascending order according to the
274 { return _M_t
.begin(); }
277 * Returns a read-only (constant) iterator that points to the first pair
278 * in the %multimap. Iteration is done in ascending order according to
283 { return _M_t
.begin(); }
286 * Returns a read/write iterator that points one past the last pair in
287 * the %multimap. Iteration is done in ascending order according to the
292 { return _M_t
.end(); }
295 * Returns a read-only (constant) iterator that points one past the last
296 * pair in the %multimap. Iteration is done in ascending order according
301 { return _M_t
.end(); }
304 * Returns a read/write reverse iterator that points to the last pair in
305 * the %multimap. Iteration is done in descending order according to the
310 { return _M_t
.rbegin(); }
313 * Returns a read-only (constant) reverse iterator that points to the
314 * last pair in the %multimap. Iteration is done in descending order
315 * according to the keys.
317 const_reverse_iterator
319 { return _M_t
.rbegin(); }
322 * Returns a read/write reverse iterator that points to one before the
323 * first pair in the %multimap. Iteration is done in descending order
324 * according to the keys.
328 { return _M_t
.rend(); }
331 * Returns a read-only (constant) reverse iterator that points to one
332 * before the first pair in the %multimap. Iteration is done in
333 * descending order according to the keys.
335 const_reverse_iterator
337 { return _M_t
.rend(); }
339 #ifdef __GXX_EXPERIMENTAL_CXX0X__
341 * Returns a read-only (constant) iterator that points to the first pair
342 * in the %multimap. Iteration is done in ascending order according to
347 { return _M_t
.begin(); }
350 * Returns a read-only (constant) iterator that points one past the last
351 * pair in the %multimap. Iteration is done in ascending order according
356 { return _M_t
.end(); }
359 * Returns a read-only (constant) reverse iterator that points to the
360 * last pair in the %multimap. Iteration is done in descending order
361 * according to the keys.
363 const_reverse_iterator
365 { return _M_t
.rbegin(); }
368 * Returns a read-only (constant) reverse iterator that points to one
369 * before the first pair in the %multimap. Iteration is done in
370 * descending order according to the keys.
372 const_reverse_iterator
374 { return _M_t
.rend(); }
378 /** Returns true if the %multimap is empty. */
381 { return _M_t
.empty(); }
383 /** Returns the size of the %multimap. */
386 { return _M_t
.size(); }
388 /** Returns the maximum size of the %multimap. */
391 { return _M_t
.max_size(); }
395 * @brief Inserts a std::pair into the %multimap.
396 * @param x Pair to be inserted (see std::make_pair for easy creation
398 * @return An iterator that points to the inserted (key,value) pair.
400 * This function inserts a (key, value) pair into the %multimap.
401 * Contrary to a std::map the %multimap does not rely on unique keys and
402 * thus multiple pairs with the same key can be inserted.
404 * Insertion requires logarithmic time.
407 insert(const value_type
& __x
)
408 { return _M_t
._M_insert_equal(__x
); }
411 * @brief Inserts a std::pair into the %multimap.
412 * @param position An iterator that serves as a hint as to where the
413 * pair should be inserted.
414 * @param x Pair to be inserted (see std::make_pair for easy creation
416 * @return An iterator that points to the inserted (key,value) pair.
418 * This function inserts a (key, value) pair into the %multimap.
419 * Contrary to a std::map the %multimap does not rely on unique keys and
420 * thus multiple pairs with the same key can be inserted.
421 * Note that the first parameter is only a hint and can potentially
422 * improve the performance of the insertion process. A bad hint would
423 * cause no gains in efficiency.
425 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
426 * for more on "hinting".
428 * Insertion requires logarithmic time (if the hint is not taken).
431 insert(iterator __position
, const value_type
& __x
)
432 { return _M_t
._M_insert_equal_(__position
, __x
); }
435 * @brief A template function that attempts to insert a range of elements.
436 * @param first Iterator pointing to the start of the range to be
438 * @param last Iterator pointing to the end of the range.
440 * Complexity similar to that of the range constructor.
442 template<typename _InputIterator
>
444 insert(_InputIterator __first
, _InputIterator __last
)
445 { _M_t
._M_insert_equal(__first
, __last
); }
448 * @brief Erases an element from a %multimap.
449 * @param position An iterator pointing to the element to be erased.
451 * This function erases an element, pointed to by the given iterator,
452 * from a %multimap. Note that this function only erases the element,
453 * and that if the element is itself a pointer, the pointed-to memory is
454 * not touched in any way. Managing the pointer is the user's
458 erase(iterator __position
)
459 { _M_t
.erase(__position
); }
462 * @brief Erases elements according to the provided key.
463 * @param x Key of element to be erased.
464 * @return The number of elements erased.
466 * This function erases all elements located by the given key from a
468 * Note that this function only erases the element, and that if
469 * the element is itself a pointer, the pointed-to memory is not touched
470 * in any way. Managing the pointer is the user's responsibility.
473 erase(const key_type
& __x
)
474 { return _M_t
.erase(__x
); }
477 * @brief Erases a [first,last) range of elements from a %multimap.
478 * @param first Iterator pointing to the start of the range to be
480 * @param last Iterator pointing to the end of the range to be erased.
482 * This function erases a sequence of elements from a %multimap.
483 * Note that this function only erases the elements, and that if
484 * the elements themselves are pointers, the pointed-to memory is not
485 * touched in any way. Managing the pointer is the user's responsibility.
488 erase(iterator __first
, iterator __last
)
489 { _M_t
.erase(__first
, __last
); }
492 * @brief Swaps data with another %multimap.
493 * @param x A %multimap of the same element and allocator types.
495 * This exchanges the elements between two multimaps in constant time.
496 * (It is only swapping a pointer, an integer, and an instance of
497 * the @c Compare type (which itself is often stateless and empty), so it
498 * should be quite fast.)
499 * Note that the global std::swap() function is specialized such that
500 * std::swap(m1,m2) will feed to this function.
503 #ifdef __GXX_EXPERIMENTAL_CXX0X__
508 { _M_t
.swap(__x
._M_t
); }
511 * Erases all elements in a %multimap. Note that this function only
512 * erases the elements, and that if the elements themselves are pointers,
513 * the pointed-to memory is not touched in any way. Managing the pointer
514 * is the user's responsibility.
522 * Returns the key comparison object out of which the %multimap
527 { return _M_t
.key_comp(); }
530 * Returns a value comparison object, built from the key comparison
531 * object out of which the %multimap was constructed.
535 { return value_compare(_M_t
.key_comp()); }
537 // multimap operations
539 * @brief Tries to locate an element in a %multimap.
540 * @param x Key of (key, value) pair to be located.
541 * @return Iterator pointing to sought-after element,
542 * or end() if not found.
544 * This function takes a key and tries to locate the element with which
545 * the key matches. If successful the function returns an iterator
546 * pointing to the sought after %pair. If unsuccessful it returns the
547 * past-the-end ( @c end() ) iterator.
550 find(const key_type
& __x
)
551 { return _M_t
.find(__x
); }
554 * @brief Tries to locate an element in a %multimap.
555 * @param x Key of (key, value) pair to be located.
556 * @return Read-only (constant) iterator pointing to sought-after
557 * element, or end() if not found.
559 * This function takes a key and tries to locate the element with which
560 * the key matches. If successful the function returns a constant
561 * iterator pointing to the sought after %pair. If unsuccessful it
562 * returns the past-the-end ( @c end() ) iterator.
565 find(const key_type
& __x
) const
566 { return _M_t
.find(__x
); }
569 * @brief Finds the number of elements with given key.
570 * @param x Key of (key, value) pairs to be located.
571 * @return Number of elements with specified key.
574 count(const key_type
& __x
) const
575 { return _M_t
.count(__x
); }
578 * @brief Finds the beginning of a subsequence matching given key.
579 * @param x Key of (key, value) pair to be located.
580 * @return Iterator pointing to first element equal to or greater
581 * than key, or end().
583 * This function returns the first element of a subsequence of elements
584 * that matches the given key. If unsuccessful it returns an iterator
585 * pointing to the first element that has a greater value than given key
586 * or end() if no such element exists.
589 lower_bound(const key_type
& __x
)
590 { return _M_t
.lower_bound(__x
); }
593 * @brief Finds the beginning of a subsequence matching given key.
594 * @param x Key of (key, value) pair to be located.
595 * @return Read-only (constant) iterator pointing to first element
596 * equal to or greater than key, or end().
598 * This function returns the first element of a subsequence of elements
599 * that matches the given key. If unsuccessful the iterator will point
600 * to the next greatest element or, if no such greater element exists, to
604 lower_bound(const key_type
& __x
) const
605 { return _M_t
.lower_bound(__x
); }
608 * @brief Finds the end of a subsequence matching given key.
609 * @param x Key of (key, value) pair to be located.
610 * @return Iterator pointing to the first element
611 * greater than key, or end().
614 upper_bound(const key_type
& __x
)
615 { return _M_t
.upper_bound(__x
); }
618 * @brief Finds the end of a subsequence matching given key.
619 * @param x Key of (key, value) pair to be located.
620 * @return Read-only (constant) iterator pointing to first iterator
621 * greater than key, or end().
624 upper_bound(const key_type
& __x
) const
625 { return _M_t
.upper_bound(__x
); }
628 * @brief Finds a subsequence matching given key.
629 * @param x Key of (key, value) pairs to be located.
630 * @return Pair of iterators that possibly points to the subsequence
631 * matching given key.
633 * This function is equivalent to
635 * std::make_pair(c.lower_bound(val),
636 * c.upper_bound(val))
638 * (but is faster than making the calls separately).
640 std::pair
<iterator
, iterator
>
641 equal_range(const key_type
& __x
)
642 { return _M_t
.equal_range(__x
); }
645 * @brief Finds a subsequence matching given key.
646 * @param x Key of (key, value) pairs to be located.
647 * @return Pair of read-only (constant) iterators that possibly points
648 * to the subsequence matching given key.
650 * This function is equivalent to
652 * std::make_pair(c.lower_bound(val),
653 * c.upper_bound(val))
655 * (but is faster than making the calls separately).
657 std::pair
<const_iterator
, const_iterator
>
658 equal_range(const key_type
& __x
) const
659 { return _M_t
.equal_range(__x
); }
661 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
663 operator==(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
664 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
666 template<typename _K1
, typename _T1
, typename _C1
, typename _A1
>
668 operator<(const multimap
<_K1
, _T1
, _C1
, _A1
>&,
669 const multimap
<_K1
, _T1
, _C1
, _A1
>&);
673 * @brief Multimap equality comparison.
674 * @param x A %multimap.
675 * @param y A %multimap of the same type as @a x.
676 * @return True iff the size and elements of the maps are equal.
678 * This is an equivalence relation. It is linear in the size of the
679 * multimaps. Multimaps are considered equivalent if their sizes are equal,
680 * and if corresponding elements compare equal.
682 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
684 operator==(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
685 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
686 { return __x
._M_t
== __y
._M_t
; }
689 * @brief Multimap ordering relation.
690 * @param x A %multimap.
691 * @param y A %multimap of the same type as @a x.
692 * @return True iff @a x is lexicographically less than @a y.
694 * This is a total ordering relation. It is linear in the size of the
695 * multimaps. The elements must be comparable with @c <.
697 * See std::lexicographical_compare() for how the determination is made.
699 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
701 operator<(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
702 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
703 { return __x
._M_t
< __y
._M_t
; }
705 /// Based on operator==
706 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
708 operator!=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
709 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
710 { return !(__x
== __y
); }
712 /// Based on operator<
713 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
715 operator>(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
716 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
717 { return __y
< __x
; }
719 /// Based on operator<
720 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
722 operator<=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
723 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
724 { return !(__y
< __x
); }
726 /// Based on operator<
727 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
729 operator>=(const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
730 const multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
731 { return !(__x
< __y
); }
733 /// See std::multimap::swap().
734 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
736 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
737 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
740 #ifdef __GXX_EXPERIMENTAL_CXX0X__
741 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
743 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>&& __x
,
744 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __y
)
747 template<typename _Key
, typename _Tp
, typename _Compare
, typename _Alloc
>
749 swap(multimap
<_Key
, _Tp
, _Compare
, _Alloc
>& __x
,
750 multimap
<_Key
, _Tp
, _Compare
, _Alloc
>&& __y
)
754 _GLIBCXX_END_NESTED_NAMESPACE
756 #endif /* _STL_MULTIMAP_H */