Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / mingw / include / c++ / 3.4.2 / bits / stl_multimap.h
blobe080f9aaba7eb1e2dd932259f2f602e3ffa1d15a
1 // Multimap implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 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> > >
73 class multimap;
75 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
76 inline bool
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>
81 inline bool
82 operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
83 const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
85 /**
86 * @brief A standard container made up of (key,value) pairs, which can be
87 * retrieved based on a key, in logarithmic time.
89 * @ingroup Containers
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.
100 * @if maint
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).
104 * @endif
106 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
107 class multimap
109 // concept requirements
110 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
111 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
112 _BinaryFunctionConcept)
114 public:
115 typedef _Key key_type;
116 typedef _Tp mapped_type;
117 typedef pair<const _Key, _Tp> value_type;
118 typedef _Compare key_compare;
120 class value_compare
121 : public binary_function<value_type, value_type, bool>
123 friend class multimap<_Key,_Tp,_Compare,_Alloc>;
124 protected:
125 _Compare comp;
127 value_compare(_Compare __c)
128 : comp(__c) { }
130 public:
131 bool operator()(const value_type& __x, const value_type& __y) const
132 { return comp(__x.first, __y.first); }
135 private:
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
140 _Rep_type _M_t;
142 public:
143 // many of these are specified differently in ISO, but the following are
144 // "functionally equivalent"
145 typedef typename _Alloc::pointer pointer;
146 typedef typename _Alloc::const_pointer const_pointer;
147 typedef typename _Alloc::reference reference;
148 typedef typename _Alloc::const_reference const_reference;
149 typedef typename _Rep_type::allocator_type allocator_type;
150 typedef typename _Rep_type::iterator iterator;
151 typedef typename _Rep_type::const_iterator const_iterator;
152 typedef typename _Rep_type::size_type size_type;
153 typedef typename _Rep_type::difference_type difference_type;
154 typedef typename _Rep_type::reverse_iterator reverse_iterator;
155 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
157 // [23.3.2] construct/copy/destroy
158 // (get_allocator() is also listed in this section)
160 * @brief Default constructor creates no elements.
162 multimap()
163 : _M_t(_Compare(), allocator_type()) { }
165 // for some reason this was made a separate function
167 * @brief Default constructor creates no elements.
169 explicit
170 multimap(const _Compare& __comp,
171 const allocator_type& __a = allocator_type())
172 : _M_t(__comp, __a) { }
175 * @brief %Multimap copy constructor.
176 * @param x A %multimap of identical element and allocator types.
178 * The newly-created %multimap uses a copy of the allocation object used
179 * by @a x.
181 multimap(const multimap& __x)
182 : _M_t(__x._M_t) { }
185 * @brief Builds a %multimap from a range.
186 * @param first An input iterator.
187 * @param last An input iterator.
189 * Create a %multimap consisting of copies of the elements from
190 * [first,last). This is linear in N if the range is already sorted,
191 * and NlogN otherwise (where N is distance(first,last)).
193 template <typename _InputIterator>
194 multimap(_InputIterator __first, _InputIterator __last)
195 : _M_t(_Compare(), allocator_type())
196 { _M_t.insert_equal(__first, __last); }
199 * @brief Builds a %multimap from a range.
200 * @param first An input iterator.
201 * @param last An input iterator.
202 * @param comp A comparison functor.
203 * @param a An allocator object.
205 * Create a %multimap consisting of copies of the elements from
206 * [first,last). This is linear in N if the range is already sorted,
207 * and NlogN otherwise (where N is distance(first,last)).
209 template <typename _InputIterator>
210 multimap(_InputIterator __first, _InputIterator __last,
211 const _Compare& __comp,
212 const allocator_type& __a = allocator_type())
213 : _M_t(__comp, __a)
214 { _M_t.insert_equal(__first, __last); }
216 // FIXME There is no dtor declared, but we should have something generated
217 // by Doxygen. I don't know what tags to add to this paragraph to make
218 // that happen:
220 * The dtor only erases the elements, and note that if the elements
221 * themselves are pointers, the pointed-to memory is not touched in any
222 * way. Managing the pointer is the user's responsibilty.
226 * @brief %Multimap assignment operator.
227 * @param x A %multimap of identical element and allocator types.
229 * All the elements of @a x are copied, but unlike the copy constructor,
230 * the allocator object is not copied.
232 multimap&
233 operator=(const multimap& __x)
235 _M_t = __x._M_t;
236 return *this;
239 /// Get a copy of the memory allocation object.
240 allocator_type
241 get_allocator() const
242 { return _M_t.get_allocator(); }
244 // iterators
246 * Returns a read/write iterator that points to the first pair in the
247 * %multimap. Iteration is done in ascending order according to the
248 * keys.
250 iterator
251 begin()
252 { return _M_t.begin(); }
255 * Returns a read-only (constant) iterator that points to the first pair
256 * in the %multimap. Iteration is done in ascending order according to
257 * the keys.
259 const_iterator
260 begin() const
261 { return _M_t.begin(); }
264 * Returns a read/write iterator that points one past the last pair in
265 * the %multimap. Iteration is done in ascending order according to the
266 * keys.
268 iterator
269 end()
270 { return _M_t.end(); }
273 * Returns a read-only (constant) iterator that points one past the last
274 * pair in the %multimap. Iteration is done in ascending order according
275 * to the keys.
277 const_iterator
278 end() const
279 { return _M_t.end(); }
282 * Returns a read/write reverse iterator that points to the last pair in
283 * the %multimap. Iteration is done in descending order according to the
284 * keys.
286 reverse_iterator
287 rbegin()
288 { return _M_t.rbegin(); }
291 * Returns a read-only (constant) reverse iterator that points to the
292 * last pair in the %multimap. Iteration is done in descending order
293 * according to the keys.
295 const_reverse_iterator
296 rbegin() const
297 { return _M_t.rbegin(); }
300 * Returns a read/write reverse iterator that points to one before the
301 * first pair in the %multimap. Iteration is done in descending order
302 * according to the keys.
304 reverse_iterator
305 rend()
306 { return _M_t.rend(); }
309 * Returns a read-only (constant) reverse iterator that points to one
310 * before the first pair in the %multimap. Iteration is done in
311 * descending order according to the keys.
313 const_reverse_iterator
314 rend() const
315 { return _M_t.rend(); }
317 // capacity
318 /** Returns true if the %multimap is empty. */
319 bool
320 empty() const
321 { return _M_t.empty(); }
323 /** Returns the size of the %multimap. */
324 size_type
325 size() const
326 { return _M_t.size(); }
328 /** Returns the maximum size of the %multimap. */
329 size_type
330 max_size() const
331 { return _M_t.max_size(); }
333 // modifiers
335 * @brief Inserts a std::pair into the %multimap.
336 * @param x Pair to be inserted (see std::make_pair for easy creation
337 * of pairs).
338 * @return An iterator that points to the inserted (key,value) pair.
340 * This function inserts a (key, value) pair into the %multimap.
341 * Contrary to a std::map the %multimap does not rely on unique keys and
342 * thus multiple pairs with the same key can be inserted.
344 * Insertion requires logarithmic time.
346 iterator
347 insert(const value_type& __x)
348 { return _M_t.insert_equal(__x); }
351 * @brief Inserts a std::pair into the %multimap.
352 * @param position An iterator that serves as a hint as to where the
353 * pair should be inserted.
354 * @param x Pair to be inserted (see std::make_pair for easy creation
355 * of pairs).
356 * @return An iterator that points to the inserted (key,value) pair.
358 * This function inserts a (key, value) pair into the %multimap.
359 * Contrary to a std::map the %multimap does not rely on unique keys and
360 * thus multiple pairs with the same key can be inserted.
361 * Note that the first parameter is only a hint and can potentially
362 * improve the performance of the insertion process. A bad hint would
363 * cause no gains in efficiency.
365 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
366 * for more on "hinting".
368 * Insertion requires logarithmic time (if the hint is not taken).
370 iterator
371 insert(iterator __position, const value_type& __x)
372 { return _M_t.insert_equal(__position, __x); }
375 * @brief A template function that attemps to insert a range of elements.
376 * @param first Iterator pointing to the start of the range to be
377 * inserted.
378 * @param last Iterator pointing to the end of the range.
380 * Complexity similar to that of the range constructor.
382 template <typename _InputIterator>
383 void
384 insert(_InputIterator __first, _InputIterator __last)
385 { _M_t.insert_equal(__first, __last); }
388 * @brief Erases an element from a %multimap.
389 * @param position An iterator pointing to the element to be erased.
391 * This function erases an element, pointed to by the given iterator,
392 * from a %multimap. Note that this function only erases the element,
393 * and that if the element is itself a pointer, the pointed-to memory is
394 * not touched in any way. Managing the pointer is the user's
395 * responsibilty.
397 void
398 erase(iterator __position)
399 { _M_t.erase(__position); }
402 * @brief Erases elements according to the provided key.
403 * @param x Key of element to be erased.
404 * @return The number of elements erased.
406 * This function erases all elements located by the given key from a
407 * %multimap.
408 * Note that this function only erases the element, and that if
409 * the element is itself a pointer, the pointed-to memory is not touched
410 * in any way. Managing the pointer is the user's responsibilty.
412 size_type
413 erase(const key_type& __x)
414 { return _M_t.erase(__x); }
417 * @brief Erases a [first,last) range of elements from a %multimap.
418 * @param first Iterator pointing to the start of the range to be
419 * erased.
420 * @param last Iterator pointing to the end of the range to be erased.
422 * This function erases a sequence of elements from a %multimap.
423 * Note that this function only erases the elements, and that if
424 * the elements themselves are pointers, the pointed-to memory is not
425 * touched in any way. Managing the pointer is the user's responsibilty.
427 void
428 erase(iterator __first, iterator __last)
429 { _M_t.erase(__first, __last); }
432 * @brief Swaps data with another %multimap.
433 * @param x A %multimap of the same element and allocator types.
435 * This exchanges the elements between two multimaps in constant time.
436 * (It is only swapping a pointer, an integer, and an instance of
437 * the @c Compare type (which itself is often stateless and empty), so it
438 * should be quite fast.)
439 * Note that the global std::swap() function is specialized such that
440 * std::swap(m1,m2) will feed to this function.
442 void
443 swap(multimap& __x)
444 { _M_t.swap(__x._M_t); }
447 * Erases all elements in a %multimap. Note that this function only
448 * erases the elements, and that if the elements themselves are pointers,
449 * the pointed-to memory is not touched in any way. Managing the pointer
450 * is the user's responsibilty.
452 void
453 clear()
454 { _M_t.clear(); }
456 // observers
458 * Returns the key comparison object out of which the %multimap
459 * was constructed.
461 key_compare
462 key_comp() const
463 { return _M_t.key_comp(); }
466 * Returns a value comparison object, built from the key comparison
467 * object out of which the %multimap was constructed.
469 value_compare
470 value_comp() const
471 { return value_compare(_M_t.key_comp()); }
473 // multimap operations
475 * @brief Tries to locate an element in a %multimap.
476 * @param x Key of (key, value) pair to be located.
477 * @return Iterator pointing to sought-after element,
478 * or end() if not found.
480 * This function takes a key and tries to locate the element with which
481 * the key matches. If successful the function returns an iterator
482 * pointing to the sought after %pair. If unsuccessful it returns the
483 * past-the-end ( @c end() ) iterator.
485 iterator
486 find(const key_type& __x)
487 { return _M_t.find(__x); }
490 * @brief Tries to locate an element in a %multimap.
491 * @param x Key of (key, value) pair to be located.
492 * @return Read-only (constant) iterator pointing to sought-after
493 * element, or end() if not found.
495 * This function takes a key and tries to locate the element with which
496 * the key matches. If successful the function returns a constant
497 * iterator pointing to the sought after %pair. If unsuccessful it
498 * returns the past-the-end ( @c end() ) iterator.
500 const_iterator
501 find(const key_type& __x) const
502 { return _M_t.find(__x); }
505 * @brief Finds the number of elements with given key.
506 * @param x Key of (key, value) pairs to be located.
507 * @return Number of elements with specified key.
509 size_type
510 count(const key_type& __x) const
511 { return _M_t.count(__x); }
514 * @brief Finds the beginning of a subsequence matching given key.
515 * @param x Key of (key, value) pair to be located.
516 * @return Iterator pointing to first element equal to or greater
517 * than key, or end().
519 * This function returns the first element of a subsequence of elements
520 * that matches the given key. If unsuccessful it returns an iterator
521 * pointing to the first element that has a greater value than given key
522 * or end() if no such element exists.
524 iterator
525 lower_bound(const key_type& __x)
526 { return _M_t.lower_bound(__x); }
529 * @brief Finds the beginning of a subsequence matching given key.
530 * @param x Key of (key, value) pair to be located.
531 * @return Read-only (constant) iterator pointing to first element
532 * equal to or greater than key, or end().
534 * This function returns the first element of a subsequence of elements
535 * that matches the given key. If unsuccessful the iterator will point
536 * to the next greatest element or, if no such greater element exists, to
537 * end().
539 const_iterator
540 lower_bound(const key_type& __x) const
541 { return _M_t.lower_bound(__x); }
544 * @brief Finds the end of a subsequence matching given key.
545 * @param x Key of (key, value) pair to be located.
546 * @return Iterator pointing to the first element
547 * greater than key, or end().
549 iterator
550 upper_bound(const key_type& __x)
551 { return _M_t.upper_bound(__x); }
554 * @brief Finds the end of a subsequence matching given key.
555 * @param x Key of (key, value) pair to be located.
556 * @return Read-only (constant) iterator pointing to first iterator
557 * greater than key, or end().
559 const_iterator
560 upper_bound(const key_type& __x) const
561 { return _M_t.upper_bound(__x); }
564 * @brief Finds a subsequence matching given key.
565 * @param x Key of (key, value) pairs to be located.
566 * @return Pair of iterators that possibly points to the subsequence
567 * matching given key.
569 * This function is equivalent to
570 * @code
571 * std::make_pair(c.lower_bound(val),
572 * c.upper_bound(val))
573 * @endcode
574 * (but is faster than making the calls separately).
576 pair<iterator,iterator>
577 equal_range(const key_type& __x)
578 { return _M_t.equal_range(__x); }
581 * @brief Finds a subsequence matching given key.
582 * @param x Key of (key, value) pairs to be located.
583 * @return Pair of read-only (constant) iterators that possibly points
584 * to the subsequence matching given key.
586 * This function is equivalent to
587 * @code
588 * std::make_pair(c.lower_bound(val),
589 * c.upper_bound(val))
590 * @endcode
591 * (but is faster than making the calls separately).
593 pair<const_iterator,const_iterator>
594 equal_range(const key_type& __x) const
595 { return _M_t.equal_range(__x); }
597 template <typename _K1, typename _T1, typename _C1, typename _A1>
598 friend bool
599 operator== (const multimap<_K1,_T1,_C1,_A1>&,
600 const multimap<_K1,_T1,_C1,_A1>&);
602 template <typename _K1, typename _T1, typename _C1, typename _A1>
603 friend bool
604 operator< (const multimap<_K1,_T1,_C1,_A1>&,
605 const multimap<_K1,_T1,_C1,_A1>&);
609 * @brief Multimap equality comparison.
610 * @param x A %multimap.
611 * @param y A %multimap of the same type as @a x.
612 * @return True iff the size and elements of the maps are equal.
614 * This is an equivalence relation. It is linear in the size of the
615 * multimaps. Multimaps are considered equivalent if their sizes are equal,
616 * and if corresponding elements compare equal.
618 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
619 inline bool
620 operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
621 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
622 { return __x._M_t == __y._M_t; }
625 * @brief Multimap ordering relation.
626 * @param x A %multimap.
627 * @param y A %multimap of the same type as @a x.
628 * @return True iff @a x is lexicographically less than @a y.
630 * This is a total ordering relation. It is linear in the size of the
631 * multimaps. The elements must be comparable with @c <.
633 * See std::lexicographical_compare() for how the determination is made.
635 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
636 inline bool
637 operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
638 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
639 { return __x._M_t < __y._M_t; }
641 /// Based on operator==
642 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
643 inline bool
644 operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
645 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
646 { return !(__x == __y); }
648 /// Based on operator<
649 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
650 inline bool
651 operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
652 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
653 { return __y < __x; }
655 /// Based on operator<
656 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
657 inline bool
658 operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
659 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
660 { return !(__y < __x); }
662 /// Based on operator<
663 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
664 inline bool
665 operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
666 const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
667 { return !(__x < __y); }
669 /// See std::multimap::swap().
670 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
671 inline void
672 swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
673 multimap<_Key,_Tp,_Compare,_Alloc>& __y)
674 { __x.swap(__y); }
675 } // namespace std
677 #endif /* _MULTIMAP_H */