OSDN Git Service

2004-04-16 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_multimap.h
1 // Multimap implementation -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
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.
42  *
43  *
44  * Copyright (c) 1996,1997
45  * Silicon Graphics Computer Systems, Inc.
46  *
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.
54  */
55
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.
59  */
60
61 #ifndef _MULTIMAP_H
62 #define _MULTIMAP_H 1
63
64 #include <bits/concept_check.h>
65
66 namespace _GLIBCXX_STD
67 {
68   // Forward declaration of operators < and ==, needed for friend declaration.
69
70   template <typename _Key, typename _Tp,
71             typename _Compare = less<_Key>,
72             typename _Alloc = allocator<pair<const _Key, _Tp> > >
73     class multimap;
74
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);
79
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);
84
85   /**
86    *  @brief A standard container made up of (key,value) pairs, which can be
87    *  retrieved based on a key, in logarithmic time.
88    *
89    *  @ingroup Containers
90    *  @ingroup Assoc_containers
91    *
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>.
97    *
98    *  Multimaps support bidirectional iterators.
99    *
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
105   */
106   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
107     class multimap
108     {
109       // concept requirements
110       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
111       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
112                                 _BinaryFunctionConcept)
113
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;
119
120       class value_compare
121       : public binary_function<value_type, value_type, bool>
122       {
123         friend class multimap<_Key,_Tp,_Compare,_Alloc>;
124       protected:
125         _Compare comp;
126
127         value_compare(_Compare __c)
128         : comp(__c) { }
129
130       public:
131         bool operator()(const value_type& __x, const value_type& __y) const
132         { return comp(__x.first, __y.first); }
133       };
134
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;
141
142     public:
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;
156
157
158       // [23.3.2] construct/copy/destroy
159       // (get_allocator() is also listed in this section)
160       /**
161        *  @brief  Default constructor creates no elements.
162        */
163       multimap()
164       : _M_t(_Compare(), allocator_type()) { }
165
166       // for some reason this was made a separate function
167       /**
168        *  @brief  Default constructor creates no elements.
169        */
170       explicit
171       multimap(const _Compare& __comp,
172                const allocator_type& __a = allocator_type())
173       : _M_t(__comp, __a) { }
174
175       /**
176        *  @brief  %Multimap copy constructor.
177        *  @param  x  A %multimap of identical element and allocator types.
178        *
179        *  The newly-created %multimap uses a copy of the allocation object used
180        *  by @a x.
181        */
182       multimap(const multimap& __x)
183       : _M_t(__x._M_t) { }
184
185       /**
186        *  @brief  Builds a %multimap from a range.
187        *  @param  first  An input iterator.
188        *  @param  last  An input iterator.
189        *
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)).
193        */
194       template <typename _InputIterator>
195         multimap(_InputIterator __first, _InputIterator __last)
196         : _M_t(_Compare(), allocator_type())
197         { _M_t.insert_equal(__first, __last); }
198
199       /**
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.
205        *
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)).
209        */
210       template <typename _InputIterator>
211         multimap(_InputIterator __first, _InputIterator __last,
212                  const _Compare& __comp,
213                  const allocator_type& __a = allocator_type())
214         : _M_t(__comp, __a)
215         { _M_t.insert_equal(__first, __last); }
216
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
219       // that happen:
220       /**
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.
224        */
225
226       /**
227        *  @brief  %Multimap assignment operator.
228        *  @param  x  A %multimap of identical element and allocator types.
229        *
230        *  All the elements of @a x are copied, but unlike the copy constructor,
231        *  the allocator object is not copied.
232        */
233       multimap&
234       operator=(const multimap& __x)
235       {
236         _M_t = __x._M_t;
237         return *this;
238       }
239
240       /// Get a copy of the memory allocation object.
241       allocator_type
242       get_allocator() const
243       { return _M_t.get_allocator(); }
244
245       // iterators
246       /**
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
249        *  keys.
250        */
251       iterator
252       begin()
253       { return _M_t.begin(); }
254
255       /**
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
258        *  the keys.
259        */
260       const_iterator
261       begin() const
262       { return _M_t.begin(); }
263
264       /**
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
267        *  keys.
268        */
269       iterator
270       end()
271       { return _M_t.end(); }
272
273       /**
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
276        *  to the keys.
277        */
278       const_iterator
279       end() const
280       { return _M_t.end(); }
281
282       /**
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
285        *  keys.
286        */
287       reverse_iterator
288       rbegin()
289       { return _M_t.rbegin(); }
290
291       /**
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.
295        */
296       const_reverse_iterator
297       rbegin() const
298       { return _M_t.rbegin(); }
299
300       /**
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.
304        */
305       reverse_iterator
306       rend()
307       { return _M_t.rend(); }
308
309       /**
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.
313        */
314       const_reverse_iterator
315       rend() const
316       { return _M_t.rend(); }
317
318       // capacity
319       /** Returns true if the %multimap is empty.  */
320       bool
321       empty() const
322       { return _M_t.empty(); }
323
324       /** Returns the size of the %multimap.  */
325       size_type
326       size() const
327       { return _M_t.size(); }
328
329       /** Returns the maximum size of the %multimap.  */
330       size_type
331       max_size() const
332       { return _M_t.max_size(); }
333
334       // modifiers
335       /**
336        *  @brief Inserts a std::pair into the %multimap.
337        *  @param  x  Pair to be inserted (see std::make_pair for easy creation
338        *             of pairs).
339        *  @return An iterator that points to the inserted (key,value) pair.
340        *
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.
344        *
345        *  Insertion requires logarithmic time.
346        */
347       iterator
348       insert(const value_type& __x)
349       { return _M_t.insert_equal(__x); }
350
351       /**
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
356        *             of pairs).
357        *  @return An iterator that points to the inserted (key,value) pair.
358        *
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.
365        *
366        *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
367        *  for more on "hinting".
368        *
369        *  Insertion requires logarithmic time (if the hint is not taken).
370        */
371       iterator
372       insert(iterator __position, const value_type& __x)
373       { return _M_t.insert_equal(__position, __x); }
374
375       /**
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
378        *                 inserted.
379        *  @param  last  Iterator pointing to the end of the range.
380        *
381        *  Complexity similar to that of the range constructor.
382        */
383       template <typename _InputIterator>
384         void
385         insert(_InputIterator __first, _InputIterator __last)
386         { _M_t.insert_equal(__first, __last); }
387
388       /**
389        *  @brief Erases an element from a %multimap.
390        *  @param  position  An iterator pointing to the element to be erased.
391        *
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
396        *  responsibilty.
397        */
398       void
399       erase(iterator __position)
400       { _M_t.erase(__position); }
401
402       /**
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.
406        *
407        *  This function erases all elements located by the given key from a
408        *  %multimap.
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.
412        */
413       size_type
414       erase(const key_type& __x)
415       { return _M_t.erase(__x); }
416
417       /**
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
420        *                 erased.
421        *  @param  last  Iterator pointing to the end of the range to be erased.
422        *
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.
427        */
428       void
429       erase(iterator __first, iterator __last)
430       { _M_t.erase(__first, __last); }
431
432       /**
433        *  @brief  Swaps data with another %multimap.
434        *  @param  x  A %multimap of the same element and allocator types.
435        *
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.
442        */
443       void
444       swap(multimap& __x)
445       { _M_t.swap(__x._M_t); }
446
447       /**
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.
452        */
453       void
454       clear()
455       { _M_t.clear(); }
456
457       // observers
458       /**
459        *  Returns the key comparison object out of which the %multimap
460        *  was constructed.
461        */
462       key_compare
463       key_comp() const
464       { return _M_t.key_comp(); }
465
466       /**
467        *  Returns a value comparison object, built from the key comparison
468        *  object out of which the %multimap was constructed.
469        */
470       value_compare
471       value_comp() const
472       { return value_compare(_M_t.key_comp()); }
473
474       // multimap operations
475       /**
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.
480        *
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.
485        */
486       iterator
487       find(const key_type& __x)
488       { return _M_t.find(__x); }
489
490       /**
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.
495        *
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.
500        */
501       const_iterator
502       find(const key_type& __x) const
503       { return _M_t.find(__x); }
504
505       /**
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.
509        */
510       size_type
511       count(const key_type& __x) const
512       { return _M_t.count(__x); }
513
514       /**
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().
519        *
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.
524        */
525       iterator
526       lower_bound(const key_type& __x)
527       { return _M_t.lower_bound(__x); }
528
529       /**
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().
534        *
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
538        *  end().
539        */
540       const_iterator
541       lower_bound(const key_type& __x) const
542       { return _M_t.lower_bound(__x); }
543
544       /**
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().
549        */
550       iterator
551       upper_bound(const key_type& __x)
552       { return _M_t.upper_bound(__x); }
553
554       /**
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().
559        */
560       const_iterator
561       upper_bound(const key_type& __x) const
562       { return _M_t.upper_bound(__x); }
563
564       /**
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.
569        *
570        *  This function is equivalent to
571        *  @code
572        *    std::make_pair(c.lower_bound(val),
573        *                   c.upper_bound(val))
574        *  @endcode
575        *  (but is faster than making the calls separately).
576        */
577       pair<iterator,iterator>
578       equal_range(const key_type& __x)
579       { return _M_t.equal_range(__x); }
580
581       /**
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.
586        *
587        *  This function is equivalent to
588        *  @code
589        *    std::make_pair(c.lower_bound(val),
590        *                   c.upper_bound(val))
591        *  @endcode
592        *  (but is faster than making the calls separately).
593        */
594       pair<const_iterator,const_iterator>
595       equal_range(const key_type& __x) const
596       { return _M_t.equal_range(__x); }
597
598       template <typename _K1, typename _T1, typename _C1, typename _A1>
599         friend bool
600         operator== (const multimap<_K1,_T1,_C1,_A1>&,
601                     const multimap<_K1,_T1,_C1,_A1>&);
602
603       template <typename _K1, typename _T1, typename _C1, typename _A1>
604         friend bool
605         operator< (const multimap<_K1,_T1,_C1,_A1>&,
606                    const multimap<_K1,_T1,_C1,_A1>&);
607   };
608
609   /**
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.
614    *
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.
618   */
619   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
620     inline bool
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; }
624
625   /**
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.
630    *
631    *  This is a total ordering relation.  It is linear in the size of the
632    *  multimaps.  The elements must be comparable with @c <.
633    *
634    *  See std::lexicographical_compare() for how the determination is made.
635   */
636   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
637     inline bool
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; }
641
642   /// Based on operator==
643   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
644     inline bool
645     operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
646                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
647     { return !(__x == __y); }
648
649   /// Based on operator<
650   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
651     inline bool
652     operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
653               const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
654     { return __y < __x; }
655
656   /// Based on operator<
657   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
658     inline bool
659     operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
660                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
661     { return !(__y < __x); }
662
663   /// Based on operator<
664   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
665     inline bool
666     operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
667                const multimap<_Key,_Tp,_Compare,_Alloc>& __y)
668     { return !(__x < __y); }
669
670   /// See std::multimap::swap().
671   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
672     inline void
673     swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
674          multimap<_Key,_Tp,_Compare,_Alloc>& __y)
675     { __x.swap(__y); }
676 } // namespace std
677
678 #endif /* _MULTIMAP_H */