1 // Multimap implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_multimap.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/concept_check.h>
66 namespace _GLIBCXX_STD
68 // Forward declaration of operators < and ==, needed for friend declaration.
70 template <typename _Key, typename _Tp,
71 typename _Compare = less<_Key>,
72 typename _Alloc = allocator<pair<const _Key, _Tp> > >
75 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
77 operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
78 const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
80 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
82 operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
83 const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
86 * @brief A standard container made up of (key,value) pairs, which can be
87 * retrieved based on a key, in logarithmic time.
90 * @ingroup Assoc_containers
92 * Meets the requirements of a <a href="tables.html#65">container</a>, a
93 * <a href="tables.html#66">reversible container</a>, and an
94 * <a href="tables.html#69">associative container</a> (using equivalent
95 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
96 * is T, and the value_type is std::pair<const Key,T>.
98 * Multimaps support bidirectional iterators.
101 * The private tree data is declared exactly the same way for map and
102 * multimap; the distinction is made entirely in how the tree functions are
103 * called (*_unique versus *_equal, same as the standard).
106 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
109 // concept requirements
110 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
111 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
112 _BinaryFunctionConcept)
115 typedef _Key key_type;
116 typedef _Tp mapped_type;
117 typedef pair<const _Key, _Tp> value_type;
118 typedef _Compare key_compare;
121 : public binary_function<value_type, value_type, bool>
123 friend class multimap<_Key,_Tp,_Compare,_Alloc>;
127 value_compare(_Compare __c)
131 bool operator()(const value_type& __x, const value_type& __y) const
132 { return comp(__x.first, __y.first); }
136 /// @if maint This turns a red-black tree into a [multi]map. @endif
137 typedef _Rb_tree<key_type, value_type,
138 _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
139 /// @if maint The actual tree structure. @endif
143 // many of these are specified differently in ISO, but the following are
144 // "functionally equivalent"
145 typedef typename _Rep_type::allocator_type allocator_type;
146 typedef typename _Rep_type::reference reference;
147 typedef typename _Rep_type::const_reference const_reference;
148 typedef typename _Rep_type::iterator iterator;
149 typedef typename _Rep_type::const_iterator const_iterator;
150 typedef typename _Rep_type::size_type size_type;
151 typedef typename _Rep_type::difference_type difference_type;
152 typedef typename _Rep_type::pointer pointer;
153 typedef typename _Rep_type::const_pointer const_pointer;
154 typedef typename _Rep_type::reverse_iterator reverse_iterator;
155 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
158 // [23.3.2] construct/copy/destroy
159 // (get_allocator() is also listed in this section)
161 * @brief Default constructor creates no elements.
164 : _M_t(_Compare(), allocator_type()) { }
166 // for some reason this was made a separate function
168 * @brief Default constructor creates no elements.
171 multimap(const _Compare& __comp,
172 const allocator_type& __a = allocator_type())
173 : _M_t(__comp, __a) { }
176 * @brief %Multimap copy constructor.
177 * @param x A %multimap of identical element and allocator types.
179 * The newly-created %multimap uses a copy of the allocation object used
182 multimap(const multimap& __x)
186 * @brief Builds a %multimap from a range.
187 * @param first An input iterator.
188 * @param last An input iterator.
190 * Create a %multimap consisting of copies of the elements from
191 * [first,last). This is linear in N if the range is already sorted,
192 * and NlogN otherwise (where N is distance(first,last)).
194 template <typename _InputIterator>
195 multimap(_InputIterator __first, _InputIterator __last)
196 : _M_t(_Compare(), allocator_type())
197 { _M_t.insert_equal(__first, __last); }
200 * @brief Builds a %multimap from a range.
201 * @param first An input iterator.
202 * @param last An input iterator.
203 * @param comp A comparison functor.
204 * @param a An allocator object.
206 * Create a %multimap consisting of copies of the elements from
207 * [first,last). This is linear in N if the range is already sorted,
208 * and NlogN otherwise (where N is distance(first,last)).
210 template <typename _InputIterator>
211 multimap(_InputIterator __first, _InputIterator __last,
212 const _Compare& __comp,
213 const allocator_type& __a = allocator_type())
215 { _M_t.insert_equal(__first, __last); }
217 // FIXME There is no dtor declared, but we should have something generated
218 // by Doxygen. I don't know what tags to add to this paragraph to make
221 * The dtor only erases the elements, and note that if the elements
222 * themselves are pointers, the pointed-to memory is not touched in any
223 * way. Managing the pointer is the user's responsibilty.
227 * @brief %Multimap assignment operator.
228 * @param x A %multimap of identical element and allocator types.
230 * All the elements of @a x are copied, but unlike the copy constructor,
231 * the allocator object is not copied.
234 operator=(const multimap& __x)
240 /// Get a copy of the memory allocation object.
242 get_allocator() const
243 { return _M_t.get_allocator(); }
247 * Returns a read/write iterator that points to the first pair in the
248 * %multimap. Iteration is done in ascending order according to the
253 { return _M_t.begin(); }
256 * Returns a read-only (constant) iterator that points to the first pair
257 * in the %multimap. Iteration is done in ascending order according to
262 { return _M_t.begin(); }
265 * Returns a read/write iterator that points one past the last pair in
266 * the %multimap. Iteration is done in ascending order according to the
271 { return _M_t.end(); }
274 * Returns a read-only (constant) iterator that points one past the last
275 * pair in the %multimap. Iteration is done in ascending order according
280 { return _M_t.end(); }
283 * Returns a read/write reverse iterator that points to the last pair in
284 * the %multimap. Iteration is done in descending order according to the
289 { return _M_t.rbegin(); }
292 * Returns a read-only (constant) reverse iterator that points to the
293 * last pair in the %multimap. Iteration is done in descending order
294 * according to the keys.
296 const_reverse_iterator
298 { return _M_t.rbegin(); }
301 * Returns a read/write reverse iterator that points to one before the
302 * first pair in the %multimap. Iteration is done in descending order
303 * according to the keys.
307 { return _M_t.rend(); }
310 * Returns a read-only (constant) reverse iterator that points to one
311 * before the first pair in the %multimap. Iteration is done in
312 * descending order according to the keys.
314 const_reverse_iterator
316 { return _M_t.rend(); }
319 /** Returns true if the %multimap is empty. */
322 { return _M_t.empty(); }
324 /** Returns the size of the %multimap. */
327 { return _M_t.size(); }
329 /** Returns the maximum size of the %multimap. */
332 { return _M_t.max_size(); }
336 * @brief Inserts a std::pair into the %multimap.
337 * @param x Pair to be inserted (see std::make_pair for easy creation
339 * @return An iterator that points to the inserted (key,value) pair.
341 * This function inserts a (key, value) pair into the %multimap.
342 * Contrary to a std::map the %multimap does not rely on unique keys and
343 * thus multiple pairs with the same key can be inserted.
345 * Insertion requires logarithmic time.
348 insert(const value_type& __x)
349 { return _M_t.insert_equal(__x); }
352 * @brief Inserts a std::pair into the %multimap.
353 * @param position An iterator that serves as a hint as to where the
354 * pair should be inserted.
355 * @param x Pair to be inserted (see std::make_pair for easy creation
357 * @return An iterator that points to the inserted (key,value) pair.
359 * This function inserts a (key, value) pair into the %multimap.
360 * Contrary to a std::map the %multimap does not rely on unique keys and
361 * thus multiple pairs with the same key can be inserted.
362 * Note that the first parameter is only a hint and can potentially
363 * improve the performance of the insertion process. A bad hint would
364 * cause no gains in efficiency.
366 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
367 * for more on "hinting".
369 * Insertion requires logarithmic time (if the hint is not taken).
372 insert(iterator __position, const value_type& __x)
373 { return _M_t.insert_equal(__position, __x); }
376 * @brief A template function that attemps to insert a range of elements.
377 * @param first Iterator pointing to the start of the range to be
379 * @param last Iterator pointing to the end of the range.
381 * Complexity similar to that of the range constructor.
383 template <typename _InputIterator>
385 insert(_InputIterator __first, _InputIterator __last)
386 { _M_t.insert_equal(__first, __last); }
389 * @brief Erases an element from a %multimap.
390 * @param position An iterator pointing to the element to be erased.
392 * This function erases an element, pointed to by the given iterator,
393 * from a %multimap. Note that this function only erases the element,
394 * and that if the element is itself a pointer, the pointed-to memory is
395 * not touched in any way. Managing the pointer is the user's
399 erase(iterator __position)
400 { _M_t.erase(__position); }
403 * @brief Erases elements according to the provided key.
404 * @param x Key of element to be erased.
405 * @return The number of elements erased.
407 * This function erases all elements located by the given key from a
409 * Note that this function only erases the element, and that if
410 * the element is itself a pointer, the pointed-to memory is not touched
411 * in any way. Managing the pointer is the user's responsibilty.
414 erase(const key_type& __x)
415 { return _M_t.erase(__x); }
418 * @brief Erases a [first,last) range of elements from a %multimap.
419 * @param first Iterator pointing to the start of the range to be
421 * @param last Iterator pointing to the end of the range to be erased.
423 * This function erases a sequence of elements from a %multimap.
424 * Note that this function only erases the elements, and that if
425 * the elements themselves are pointers, the pointed-to memory is not
426 * touched in any way. Managing the pointer is the user's responsibilty.
429 erase(iterator __first, iterator __last)
430 { _M_t.erase(__first, __last); }
433 * @brief Swaps data with another %multimap.
434 * @param x A %multimap of the same element and allocator types.
436 * This exchanges the elements between two multimaps in constant time.
437 * (It is only swapping a pointer, an integer, and an instance of
438 * the @c Compare type (which itself is often stateless and empty), so it
439 * should be quite fast.)
440 * Note that the global std::swap() function is specialized such that
441 * std::swap(m1,m2) will feed to this function.
445 { _M_t.swap(__x._M_t); }
448 * Erases all elements in a %multimap. Note that this function only
449 * erases the elements, and that if the elements themselves are pointers,
450 * the pointed-to memory is not touched in any way. Managing the pointer
451 * is the user's responsibilty.
459 * Returns the key comparison object out of which the %multimap
464 { return _M_t.key_comp(); }
467 * Returns a value comparison object, built from the key comparison
468 * object out of which the %multimap was constructed.
472 { return value_compare(_M_t.key_comp()); }
474 // multimap operations
476 * @brief Tries to locate an element in a %multimap.
477 * @param x Key of (key, value) pair to be located.
478 * @return Iterator pointing to sought-after element,
479 * or end() if not found.
481 * This function takes a key and tries to locate the element with which
482 * the key matches. If successful the function returns an iterator
483 * pointing to the sought after %pair. If unsuccessful it returns the
484 * past-the-end ( @c end() ) iterator.
487 find(const key_type& __x)
488 { return _M_t.find(__x); }
491 * @brief Tries to locate an element in a %multimap.
492 * @param x Key of (key, value) pair to be located.
493 * @return Read-only (constant) iterator pointing to sought-after
494 * element, or end() if not found.
496 * This function takes a key and tries to locate the element with which
497 * the key matches. If successful the function returns a constant
498 * iterator pointing to the sought after %pair. If unsuccessful it
499 * returns the past-the-end ( @c end() ) iterator.
502 find(const key_type& __x) const
503 { return _M_t.find(__x); }
506 * @brief Finds the number of elements with given key.
507 * @param x Key of (key, value) pairs to be located.
508 * @return Number of elements with specified key.
511 count(const key_type& __x) const
512 { return _M_t.count(__x); }
515 * @brief Finds the beginning of a subsequence matching given key.
516 * @param x Key of (key, value) pair to be located.
517 * @return Iterator pointing to first element equal to or greater
518 * than key, or end().
520 * This function returns the first element of a subsequence of elements
521 * that matches the given key. If unsuccessful it returns an iterator
522 * pointing to the first element that has a greater value than given key
523 * or end() if no such element exists.
526 lower_bound(const key_type& __x)
527 { return _M_t.lower_bound(__x); }
530 * @brief Finds the beginning of a subsequence matching given key.
531 * @param x Key of (key, value) pair to be located.
532 * @return Read-only (constant) iterator pointing to first element
533 * equal to or greater than key, or end().
535 * This function returns the first element of a subsequence of elements
536 * that matches the given key. If unsuccessful the iterator will point
537 * to the next greatest element or, if no such greater element exists, to
541 lower_bound(const key_type& __x) const
542 { return _M_t.lower_bound(__x); }
545 * @brief Finds the end of a subsequence matching given key.
546 * @param x Key of (key, value) pair to be located.
547 * @return Iterator pointing to the first element
548 * greater than key, or end().
551 upper_bound(const key_type& __x)
552 { return _M_t.upper_bound(__x); }
555 * @brief Finds the end of a subsequence matching given key.
556 * @param x Key of (key, value) pair to be located.
557 * @return Read-only (constant) iterator pointing to first iterator
558 * greater than key, or end().
561 upper_bound(const key_type& __x) const
562 { return _M_t.upper_bound(__x); }
565 * @brief Finds a subsequence matching given key.
566 * @param x Key of (key, value) pairs to be located.
567 * @return Pair of iterators that possibly points to the subsequence
568 * matching given key.
570 * This function is equivalent to
572 * std::make_pair(c.lower_bound(val),
573 * c.upper_bound(val))
575 * (but is faster than making the calls separately).
577 pair<iterator,iterator>
578 equal_range(const key_type& __x)
579 { return _M_t.equal_range(__x); }
582 * @brief Finds a subsequence matching given key.
583 * @param x Key of (key, value) pairs to be located.
584 * @return Pair of read-only (constant) iterators that possibly points
585 * to the subsequence matching given key.
587 * This function is equivalent to
589 * std::make_pair(c.lower_bound(val),
590 * c.upper_bound(val))
592 * (but is faster than making the calls separately).
594 pair<const_iterator,const_iterator>
595 equal_range(const key_type& __x) const
596 { return _M_t.equal_range(__x); }
598 template <typename _K1, typename _T1, typename _C1, typename _A1>
600 operator== (const multimap<_K1,_T1,_C1,_A1>&,
601 const multimap<_K1,_T1,_C1,_A1>&);
603 template <typename _K1, typename _T1, typename _C1, typename _A1>
605 operator< (const multimap<_K1,_T1,_C1,_A1>&,
606 const multimap<_K1,_T1,_C1,_A1>&);
610 * @brief Multimap equality comparison.
611 * @param x A %multimap.
612 * @param y A %multimap of the same type as @a x.
613 * @return True iff the size and elements of the maps are equal.
615 * This is an equivalence relation. It is linear in the size of the
616 * multimaps. Multimaps are considered equivalent if their sizes are equal,
617 * and if corresponding elements compare equal.
619 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
621 operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
622 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
623 { return __x._M_t == __y._M_t; }
626 * @brief Multimap ordering relation.
627 * @param x A %multimap.
628 * @param y A %multimap of the same type as @a x.
629 * @return True iff @a x is lexicographically less than @a y.
631 * This is a total ordering relation. It is linear in the size of the
632 * multimaps. The elements must be comparable with @c <.
634 * See std::lexicographical_compare() for how the determination is made.
636 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
638 operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
639 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
640 { return __x._M_t < __y._M_t; }
642 /// Based on operator==
643 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
645 operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
646 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
647 { return !(__x == __y); }
649 /// Based on operator<
650 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
652 operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
653 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
654 { return __y < __x; }
656 /// Based on operator<
657 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
659 operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
660 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
661 { return !(__y < __x); }
663 /// Based on operator<
664 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
666 operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
667 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
668 { return !(__x < __y); }
670 /// See std::multimap::swap().
671 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
673 swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
674 multimap<_Key,_Tp,_Compare,_Alloc>& __y)
678 #endif /* _MULTIMAP_H */