Mark as release
[official-gcc.git] / libstdc++-v3 / include / bits / stl_multimap.h
blobb11b6e409d57204842fa2044341ce9e7716d9b58
1 // Multimap implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
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.
32 * Copyright (c) 1994
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.
61 #ifndef _MULTIMAP_H
62 #define _MULTIMAP_H 1
64 #include <bits/concept_check.h>
66 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
68 /**
69 * @brief A standard container made up of (key,value) pairs, which can be
70 * retrieved based on a key, in logarithmic time.
72 * @ingroup Containers
73 * @ingroup Assoc_containers
75 * Meets the requirements of a <a href="tables.html#65">container</a>, a
76 * <a href="tables.html#66">reversible container</a>, and an
77 * <a href="tables.html#69">associative container</a> (using equivalent
78 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
79 * is T, and the value_type is std::pair<const Key,T>.
81 * Multimaps support bidirectional iterators.
83 * @if maint
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).
87 * @endif
89 template <typename _Key, typename _Tp,
90 typename _Compare = std::less<_Key>,
91 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
92 class multimap
94 public:
95 typedef _Key key_type;
96 typedef _Tp mapped_type;
97 typedef std::pair<const _Key, _Tp> value_type;
98 typedef _Compare key_compare;
99 typedef _Alloc allocator_type;
101 private:
102 // concept requirements
103 typedef typename _Alloc::value_type _Alloc_value_type;
104 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
105 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
106 _BinaryFunctionConcept)
107 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
109 public:
110 class value_compare
111 : public std::binary_function<value_type, value_type, bool>
113 friend class multimap<_Key, _Tp, _Compare, _Alloc>;
114 protected:
115 _Compare comp;
117 value_compare(_Compare __c)
118 : comp(__c) { }
120 public:
121 bool operator()(const value_type& __x, const value_type& __y) const
122 { return comp(__x.first, __y.first); }
125 private:
126 /// @if maint This turns a red-black tree into a [multi]map. @endif
127 typedef typename _Alloc::template rebind<value_type>::other
128 _Pair_alloc_type;
130 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
131 key_compare, _Pair_alloc_type> _Rep_type;
132 /// @if maint The actual tree structure. @endif
133 _Rep_type _M_t;
135 public:
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.2] construct/copy/destroy
150 // (get_allocator() is also listed in this section)
152 * @brief Default constructor creates no elements.
154 multimap()
155 : _M_t(_Compare(), allocator_type()) { }
157 // for some reason this was made a separate function
159 * @brief Default constructor creates no elements.
161 explicit
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 used
171 * by @a x.
173 multimap(const multimap& __x)
174 : _M_t(__x._M_t) { }
177 * @brief Builds a %multimap from a range.
178 * @param first An input iterator.
179 * @param last An input iterator.
181 * Create a %multimap consisting of copies of the elements from
182 * [first,last). This is linear in N if the range is already sorted,
183 * and NlogN otherwise (where N is distance(first,last)).
185 template <typename _InputIterator>
186 multimap(_InputIterator __first, _InputIterator __last)
187 : _M_t(_Compare(), allocator_type())
188 { _M_t._M_insert_equal(__first, __last); }
191 * @brief Builds a %multimap from a range.
192 * @param first An input iterator.
193 * @param last An input iterator.
194 * @param comp A comparison functor.
195 * @param a An allocator object.
197 * Create a %multimap consisting of copies of the elements from
198 * [first,last). This is linear in N if the range is already sorted,
199 * and NlogN otherwise (where N is distance(first,last)).
201 template <typename _InputIterator>
202 multimap(_InputIterator __first, _InputIterator __last,
203 const _Compare& __comp,
204 const allocator_type& __a = allocator_type())
205 : _M_t(__comp, __a)
206 { _M_t._M_insert_equal(__first, __last); }
208 // FIXME There is no dtor declared, but we should have something generated
209 // by Doxygen. I don't know what tags to add to this paragraph to make
210 // that happen:
212 * The dtor only erases the elements, and note that if the elements
213 * themselves are pointers, the pointed-to memory is not touched in any
214 * way. Managing the pointer is the user's responsibilty.
218 * @brief %Multimap assignment operator.
219 * @param x A %multimap of identical element and allocator types.
221 * All the elements of @a x are copied, but unlike the copy constructor,
222 * the allocator object is not copied.
224 multimap&
225 operator=(const multimap& __x)
227 _M_t = __x._M_t;
228 return *this;
231 /// Get a copy of the memory allocation object.
232 allocator_type
233 get_allocator() const
234 { return _M_t.get_allocator(); }
236 // iterators
238 * Returns a read/write iterator that points to the first pair in the
239 * %multimap. Iteration is done in ascending order according to the
240 * keys.
242 iterator
243 begin()
244 { return _M_t.begin(); }
247 * Returns a read-only (constant) iterator that points to the first pair
248 * in the %multimap. Iteration is done in ascending order according to
249 * the keys.
251 const_iterator
252 begin() const
253 { return _M_t.begin(); }
256 * Returns a read/write iterator that points one past the last pair in
257 * the %multimap. Iteration is done in ascending order according to the
258 * keys.
260 iterator
261 end()
262 { return _M_t.end(); }
265 * Returns a read-only (constant) iterator that points one past the last
266 * pair in the %multimap. Iteration is done in ascending order according
267 * to the keys.
269 const_iterator
270 end() const
271 { return _M_t.end(); }
274 * Returns a read/write reverse iterator that points to the last pair in
275 * the %multimap. Iteration is done in descending order according to the
276 * keys.
278 reverse_iterator
279 rbegin()
280 { return _M_t.rbegin(); }
283 * Returns a read-only (constant) reverse iterator that points to the
284 * last pair in the %multimap. Iteration is done in descending order
285 * according to the keys.
287 const_reverse_iterator
288 rbegin() const
289 { return _M_t.rbegin(); }
292 * Returns a read/write reverse iterator that points to one before the
293 * first pair in the %multimap. Iteration is done in descending order
294 * according to the keys.
296 reverse_iterator
297 rend()
298 { return _M_t.rend(); }
301 * Returns a read-only (constant) reverse iterator that points to one
302 * before the first pair in the %multimap. Iteration is done in
303 * descending order according to the keys.
305 const_reverse_iterator
306 rend() const
307 { return _M_t.rend(); }
309 // capacity
310 /** Returns true if the %multimap is empty. */
311 bool
312 empty() const
313 { return _M_t.empty(); }
315 /** Returns the size of the %multimap. */
316 size_type
317 size() const
318 { return _M_t.size(); }
320 /** Returns the maximum size of the %multimap. */
321 size_type
322 max_size() const
323 { return _M_t.max_size(); }
325 // modifiers
327 * @brief Inserts a std::pair into the %multimap.
328 * @param x Pair to be inserted (see std::make_pair for easy creation
329 * of pairs).
330 * @return An iterator that points to the inserted (key,value) pair.
332 * This function inserts a (key, value) pair into the %multimap.
333 * Contrary to a std::map the %multimap does not rely on unique keys and
334 * thus multiple pairs with the same key can be inserted.
336 * Insertion requires logarithmic time.
338 iterator
339 insert(const value_type& __x)
340 { return _M_t._M_insert_equal(__x); }
343 * @brief Inserts a std::pair into the %multimap.
344 * @param position An iterator that serves as a hint as to where the
345 * pair should be inserted.
346 * @param x Pair to be inserted (see std::make_pair for easy creation
347 * of pairs).
348 * @return An iterator that points to the inserted (key,value) pair.
350 * This function inserts a (key, value) pair into the %multimap.
351 * Contrary to a std::map the %multimap does not rely on unique keys and
352 * thus multiple pairs with the same key can be inserted.
353 * Note that the first parameter is only a hint and can potentially
354 * improve the performance of the insertion process. A bad hint would
355 * cause no gains in efficiency.
357 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
358 * for more on "hinting".
360 * Insertion requires logarithmic time (if the hint is not taken).
362 iterator
363 insert(iterator __position, const value_type& __x)
364 { return _M_t._M_insert_equal(__position, __x); }
367 * @brief A template function that attemps to insert a range of elements.
368 * @param first Iterator pointing to the start of the range to be
369 * inserted.
370 * @param last Iterator pointing to the end of the range.
372 * Complexity similar to that of the range constructor.
374 template <typename _InputIterator>
375 void
376 insert(_InputIterator __first, _InputIterator __last)
377 { _M_t._M_insert_equal(__first, __last); }
380 * @brief Erases an element from a %multimap.
381 * @param position An iterator pointing to the element to be erased.
383 * This function erases an element, pointed to by the given iterator,
384 * from a %multimap. Note that this function only erases the element,
385 * and that if the element is itself a pointer, the pointed-to memory is
386 * not touched in any way. Managing the pointer is the user's
387 * responsibilty.
389 void
390 erase(iterator __position)
391 { _M_t.erase(__position); }
394 * @brief Erases elements according to the provided key.
395 * @param x Key of element to be erased.
396 * @return The number of elements erased.
398 * This function erases all elements located by the given key from a
399 * %multimap.
400 * Note that this function only erases the element, and that if
401 * the element is itself a pointer, the pointed-to memory is not touched
402 * in any way. Managing the pointer is the user's responsibilty.
404 size_type
405 erase(const key_type& __x)
406 { return _M_t.erase(__x); }
409 * @brief Erases a [first,last) range of elements from a %multimap.
410 * @param first Iterator pointing to the start of the range to be
411 * erased.
412 * @param last Iterator pointing to the end of the range to be erased.
414 * This function erases a sequence of elements from a %multimap.
415 * Note that this function only erases the elements, and that if
416 * the elements themselves are pointers, the pointed-to memory is not
417 * touched in any way. Managing the pointer is the user's responsibilty.
419 void
420 erase(iterator __first, iterator __last)
421 { _M_t.erase(__first, __last); }
424 * @brief Swaps data with another %multimap.
425 * @param x A %multimap of the same element and allocator types.
427 * This exchanges the elements between two multimaps in constant time.
428 * (It is only swapping a pointer, an integer, and an instance of
429 * the @c Compare type (which itself is often stateless and empty), so it
430 * should be quite fast.)
431 * Note that the global std::swap() function is specialized such that
432 * std::swap(m1,m2) will feed to this function.
434 void
435 swap(multimap& __x)
436 { _M_t.swap(__x._M_t); }
439 * Erases all elements in a %multimap. Note that this function only
440 * erases the elements, and that if the elements themselves are pointers,
441 * the pointed-to memory is not touched in any way. Managing the pointer
442 * is the user's responsibilty.
444 void
445 clear()
446 { _M_t.clear(); }
448 // observers
450 * Returns the key comparison object out of which the %multimap
451 * was constructed.
453 key_compare
454 key_comp() const
455 { return _M_t.key_comp(); }
458 * Returns a value comparison object, built from the key comparison
459 * object out of which the %multimap was constructed.
461 value_compare
462 value_comp() const
463 { return value_compare(_M_t.key_comp()); }
465 // multimap operations
467 * @brief Tries to locate an element in a %multimap.
468 * @param x Key of (key, value) pair to be located.
469 * @return Iterator pointing to sought-after element,
470 * or end() if not found.
472 * This function takes a key and tries to locate the element with which
473 * the key matches. If successful the function returns an iterator
474 * pointing to the sought after %pair. If unsuccessful it returns the
475 * past-the-end ( @c end() ) iterator.
477 iterator
478 find(const key_type& __x)
479 { return _M_t.find(__x); }
482 * @brief Tries to locate an element in a %multimap.
483 * @param x Key of (key, value) pair to be located.
484 * @return Read-only (constant) iterator pointing to sought-after
485 * element, or end() if not found.
487 * This function takes a key and tries to locate the element with which
488 * the key matches. If successful the function returns a constant
489 * iterator pointing to the sought after %pair. If unsuccessful it
490 * returns the past-the-end ( @c end() ) iterator.
492 const_iterator
493 find(const key_type& __x) const
494 { return _M_t.find(__x); }
497 * @brief Finds the number of elements with given key.
498 * @param x Key of (key, value) pairs to be located.
499 * @return Number of elements with specified key.
501 size_type
502 count(const key_type& __x) const
503 { return _M_t.count(__x); }
506 * @brief Finds the beginning of a subsequence matching given key.
507 * @param x Key of (key, value) pair to be located.
508 * @return Iterator pointing to first element equal to or greater
509 * than key, or end().
511 * This function returns the first element of a subsequence of elements
512 * that matches the given key. If unsuccessful it returns an iterator
513 * pointing to the first element that has a greater value than given key
514 * or end() if no such element exists.
516 iterator
517 lower_bound(const key_type& __x)
518 { return _M_t.lower_bound(__x); }
521 * @brief Finds the beginning of a subsequence matching given key.
522 * @param x Key of (key, value) pair to be located.
523 * @return Read-only (constant) iterator pointing to first element
524 * equal to or greater than key, or end().
526 * This function returns the first element of a subsequence of elements
527 * that matches the given key. If unsuccessful the iterator will point
528 * to the next greatest element or, if no such greater element exists, to
529 * end().
531 const_iterator
532 lower_bound(const key_type& __x) const
533 { return _M_t.lower_bound(__x); }
536 * @brief Finds the end of a subsequence matching given key.
537 * @param x Key of (key, value) pair to be located.
538 * @return Iterator pointing to the first element
539 * greater than key, or end().
541 iterator
542 upper_bound(const key_type& __x)
543 { return _M_t.upper_bound(__x); }
546 * @brief Finds the end of a subsequence matching given key.
547 * @param x Key of (key, value) pair to be located.
548 * @return Read-only (constant) iterator pointing to first iterator
549 * greater than key, or end().
551 const_iterator
552 upper_bound(const key_type& __x) const
553 { return _M_t.upper_bound(__x); }
556 * @brief Finds a subsequence matching given key.
557 * @param x Key of (key, value) pairs to be located.
558 * @return Pair of iterators that possibly points to the subsequence
559 * matching given key.
561 * This function is equivalent to
562 * @code
563 * std::make_pair(c.lower_bound(val),
564 * c.upper_bound(val))
565 * @endcode
566 * (but is faster than making the calls separately).
568 std::pair<iterator, iterator>
569 equal_range(const key_type& __x)
570 { return _M_t.equal_range(__x); }
573 * @brief Finds a subsequence matching given key.
574 * @param x Key of (key, value) pairs to be located.
575 * @return Pair of read-only (constant) iterators that possibly points
576 * to the subsequence matching given key.
578 * This function is equivalent to
579 * @code
580 * std::make_pair(c.lower_bound(val),
581 * c.upper_bound(val))
582 * @endcode
583 * (but is faster than making the calls separately).
585 std::pair<const_iterator, const_iterator>
586 equal_range(const key_type& __x) const
587 { return _M_t.equal_range(__x); }
589 template <typename _K1, typename _T1, typename _C1, typename _A1>
590 friend bool
591 operator== (const multimap<_K1, _T1, _C1, _A1>&,
592 const multimap<_K1, _T1, _C1, _A1>&);
594 template <typename _K1, typename _T1, typename _C1, typename _A1>
595 friend bool
596 operator< (const multimap<_K1, _T1, _C1, _A1>&,
597 const multimap<_K1, _T1, _C1, _A1>&);
601 * @brief Multimap equality comparison.
602 * @param x A %multimap.
603 * @param y A %multimap of the same type as @a x.
604 * @return True iff the size and elements of the maps are equal.
606 * This is an equivalence relation. It is linear in the size of the
607 * multimaps. Multimaps are considered equivalent if their sizes are equal,
608 * and if corresponding elements compare equal.
610 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
611 inline bool
612 operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
613 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
614 { return __x._M_t == __y._M_t; }
617 * @brief Multimap ordering relation.
618 * @param x A %multimap.
619 * @param y A %multimap of the same type as @a x.
620 * @return True iff @a x is lexicographically less than @a y.
622 * This is a total ordering relation. It is linear in the size of the
623 * multimaps. The elements must be comparable with @c <.
625 * See std::lexicographical_compare() for how the determination is made.
627 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
628 inline bool
629 operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
630 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
631 { return __x._M_t < __y._M_t; }
633 /// Based on operator==
634 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
635 inline bool
636 operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
637 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
638 { return !(__x == __y); }
640 /// Based on operator<
641 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
642 inline bool
643 operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
644 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
645 { return __y < __x; }
647 /// Based on operator<
648 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
649 inline bool
650 operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
651 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
652 { return !(__y < __x); }
654 /// Based on operator<
655 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
656 inline bool
657 operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
658 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
659 { return !(__x < __y); }
661 /// See std::multimap::swap().
662 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
663 inline void
664 swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
665 multimap<_Key, _Tp, _Compare, _Alloc>& __y)
666 { __x.swap(__y); }
668 _GLIBCXX_END_NESTED_NAMESPACE
670 #endif /* _MULTIMAP_H */