1 // Multimap implementation -*- C++ -*-
2 
3 // Copyright (C) 2001 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 __GLIBCPP_INTERNAL_MULTIMAP_H
62 #define __GLIBCPP_INTERNAL_MULTIMAP_H
63 
64 #include <bits/concept_check.h>
65 
66 namespace std
67 {
68 // Forward declaration of operators < and ==, needed for friend declaration.
69 template <class _Key, class _Tp,
70           class _Compare = less<_Key>,
71           class _Alloc = allocator<pair<const _Key, _Tp> > >
72 class multimap;
73 
74 template <class _Key, class _Tp, class _Compare, class _Alloc>
75 inline bool operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
76                        const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
77 
78 template <class _Key, class _Tp, class _Compare, class _Alloc>
79 inline bool operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
80                       const multimap<_Key,_Tp,_Compare,_Alloc>& __y);
81 
82 /**
83  *  @brief A standard container made up of pairs (see std::pair in <utility>)
84  *         which can be retrieved based on a key.
85  *
86  *  This is an associative container.  Values contained within it can be
87  *  quickly retrieved through a key element. In contrast with a map a
88  *  multimap can have multiple duplicate keys.
89 */
90 template <class _Key, class _Tp, class _Compare, class _Alloc>
91 class multimap
92 {
93   // concept requirements
94   __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
95   __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept);
96 
97 public:
98 
99 // typedefs:
100 
101   typedef _Key                  key_type;
102   typedef _Tp                   data_type;
103   typedef _Tp                   mapped_type;
104   typedef pair<const _Key, _Tp> value_type;
105   typedef _Compare              key_compare;
106 
107   class value_compare : public binary_function<value_type, value_type, bool> {
108   friend class multimap<_Key,_Tp,_Compare,_Alloc>;
109   protected:
110     _Compare comp;
111     value_compare(_Compare __c) : comp(__c) {}
112   public:
113     bool operator()(const value_type& __x, const value_type& __y) const {
114       return comp(__x.first, __y.first);
115     }
116   };
117 
118 private:
119   typedef _Rb_tree<key_type, value_type,
120                   _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
121   _Rep_type _M_t;  // red-black tree representing multimap
122 public:
123   typedef typename _Rep_type::pointer pointer;
124   typedef typename _Rep_type::const_pointer const_pointer;
125   typedef typename _Rep_type::reference reference;
126   typedef typename _Rep_type::const_reference const_reference;
127   typedef typename _Rep_type::iterator iterator;
128   typedef typename _Rep_type::const_iterator const_iterator;
129   typedef typename _Rep_type::reverse_iterator reverse_iterator;
130   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
131   typedef typename _Rep_type::size_type size_type;
132   typedef typename _Rep_type::difference_type difference_type;
133   typedef typename _Rep_type::allocator_type allocator_type;
134 
135 // allocation/deallocation
136 
137   multimap() : _M_t(_Compare(), allocator_type()) { }
138   explicit multimap(const _Compare& __comp,
139                     const allocator_type& __a = allocator_type())
140     : _M_t(__comp, __a) { }
141 
142   template <class _InputIterator>
143   multimap(_InputIterator __first, _InputIterator __last)
144     : _M_t(_Compare(), allocator_type())
145     { _M_t.insert_equal(__first, __last); }
146 
147   template <class _InputIterator>
148   multimap(_InputIterator __first, _InputIterator __last,
149            const _Compare& __comp,
150            const allocator_type& __a = allocator_type())
151     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
152   multimap(const multimap<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) { }
153 
154   multimap<_Key,_Tp,_Compare,_Alloc>&
155   operator=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x) {
156     _M_t = __x._M_t;
157     return *this;
158   }
159 
160   // accessors:
161 
162   key_compare key_comp() const { return _M_t.key_comp(); }
163   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
164   allocator_type get_allocator() const { return _M_t.get_allocator(); }
165 
166   /**
167    *  Returns a read/write iterator that points to the first pair in the
168    *  multimap.  Iteration is done in ascending order according to the keys.
169   */
170   iterator begin() { return _M_t.begin(); }
171 
172   /**
173    *  Returns a read-only (constant) iterator that points to the first pair
174    *  in the multimap.  Iteration is done in ascending order according to the
175    *  keys.
176   */
177   const_iterator begin() const { return _M_t.begin(); }
178 
179   /**
180    *  Returns a read/write iterator that points one past the last pair in the
181    *  multimap.  Iteration is done in ascending order according to the keys.
182   */
183   iterator end() { return _M_t.end(); }
184 
185   /**
186    *  Returns a read-only (constant) iterator that points one past the last
187    *  pair in the multimap.  Iteration is done in ascending order according
188    *  to the keys.
189   */
190   const_iterator end() const { return _M_t.end(); }
191 
192   /**
193    *  Returns a read/write reverse iterator that points to the last pair in
194    *  the multimap.  Iteration is done in descending order according to the
195    *  keys.
196   */
197   reverse_iterator rbegin() { return _M_t.rbegin(); }
198 
199   /**
200    *  Returns a read-only (constant) reverse iterator that points to the last
201    *  pair in the multimap.  Iteration is done in descending order according
202    *  to the keys.
203   */
204   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
205 
206   /**
207    *  Returns a read/write reverse iterator that points to one before the
208    *  first pair in the multimap.  Iteration is done in descending order
209    *  according to the keys.
210   */
211   reverse_iterator rend() { return _M_t.rend(); }
212 
213   /**
214    *  Returns a read-only (constant) reverse iterator that points to one
215    *  before the first pair in the multimap.  Iteration is done in descending
216    *  order according to the keys.
217   */
218   const_reverse_iterator rend() const { return _M_t.rend(); }
219 
220   /** Returns true if the map is empty.  (Thus begin() would equal end().)  */
221   bool empty() const { return _M_t.empty(); }
222 
223   /** Returns the size of the map.  */
224   size_type size() const { return _M_t.size(); }
225 
226   /** Returns the maximum size of the map.  */
227   size_type max_size() const { return _M_t.max_size(); }
228 
229   void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
230 
231   // insert/erase
232   /**
233    *  @brief Inserts a std::pair into the multimap.
234    *  @param  x  Pair to be inserted (see std::make_pair for easy creation of
235    *             pairs).
236    *  @return An iterator that points to the inserted (key,value) pair.
237    *
238    *  This function inserts a (key, value) pair into the multimap.  Contrary
239    *  to a std::map the multimap does not rely on unique keys and thus a
240    *  multiple pairs with the same key can be inserted.
241   */
242   iterator insert(const value_type& __x) { return _M_t.insert_equal(__x); }
243 
244   /**
245    *  @brief Inserts a std::pair into the multimap.
246    *  @param  position  An iterator that serves as a hint as to where the
247    *                    pair should be inserted.
248    *  @param  x  Pair to be inserted (see std::make_pair for easy creation of
249    *             pairs).
250    *  @return An iterator that points to the inserted (key,value) pair.
251    *
252    *  This function inserts a (key, value) pair into the multimap.  Contrary
253    *  to a std::map the multimap does not rely on unique keys and thus a
254    *  multiple pairs with the same key can be inserted.
255    *  Note that the first parameter is only a hint and can potentially
256    *  improve the performance of the insertion process.  A bad hint would
257    *  cause no gains in efficiency.
258   */
259   iterator insert(iterator __position, const value_type& __x) {
260     return _M_t.insert_equal(__position, __x);
261   }
262 
263   /**
264    *  @brief A template function that attemps to insert elements from
265    *         another range (possibly another multimap or standard container).
266    *  @param  first  Iterator pointing to the start of the range to be
267    *                 inserted.
268    *  @param  last  Iterator pointing to the end of the range to be inserted.
269   */
270   template <class _InputIterator>
271   void insert(_InputIterator __first, _InputIterator __last) {
272     _M_t.insert_equal(__first, __last);
273   }
274 
275   /**
276    *  @brief Erases an element from a multimap.
277    *  @param  position  An iterator pointing to the element to be erased.
278    *
279    *  This function erases an element, pointed to by the given iterator, from
280    *  a mutlimap.  Note that this function only erases the element, and that
281    *  if the element is itself a pointer, the pointed-to memory is not
282    *  touched in any way.  Managing the pointer is the user's responsibilty.
283   */
284   void erase(iterator __position) { _M_t.erase(__position); }
285 
286   /**
287    *  @brief Erases an element according to the provided key.
288    *  @param  x  Key of element to be erased.
289    *  @return  Doc me! (Number of elements erased?)
290    *
291    *  This function erases all elements, located by the given key, from a
292    *  multimap.
293    *  Note that this function only erases the element, and that if
294    *  the element is itself a pointer, the pointed-to memory is not touched
295    *  in any way.  Managing the pointer is the user's responsibilty.
296   */
297   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
298 
299   /**
300    *  @brief Erases a [first,last) range of elements from a multimap.
301    *  @param  first  Iterator pointing to the start of the range to be erased.
302    *  @param  last  Iterator pointing to the end of the range to be erased.
303    *
304    *  This function erases a sequence of elements from a multimap.
305    *  Note that this function only erases the elements, and that if
306    *  the elements themselves are pointers, the pointed-to memory is not
307    *  touched in any way.  Managing the pointer is the user's responsibilty.
308   */
309   void erase(iterator __first, iterator __last)
310     { _M_t.erase(__first, __last); }
311 
312   /** Erases all elements in a multimap.  Note that this function only erases
313    *  the elements, and that if the elements themselves are pointers, the
314    *  pointed-to memory is not touched in any way.  Managing the pointer is
315    *  the user's responsibilty.
316   */
317   void clear() { _M_t.clear(); }
318 
319   // multimap operations:
320 
321   /**
322    *  @brief Tries to locate an element in a multimap.
323    *  @param  x  Key of (key, value) pair to be located.
324    *  @return  Iterator pointing to sought-after (first matching?) element,
325    *           or end() if not found.
326    *
327    *  This function takes a key and tries to locate the element with which
328    *  the key matches.  If successful the function returns an iterator
329    *  pointing to the sought after pair. If unsuccessful it returns the
330    *  one past the end ( end() ) iterator.
331   */
332   iterator find(const key_type& __x) { return _M_t.find(__x); }
333 
334   /**
335    *  @brief Tries to locate an element in a multimap.
336    *  @param  x  Key of (key, value) pair to be located.
337    *  @return  Read-only (constant) iterator pointing to sought-after (first
338    *           matching?) element, or end() if not found.
339    *
340    *  This function takes a key and tries to locate the element with which
341    *  the key matches.  If successful the function returns a constant iterator
342    *  pointing to the sought after pair. If unsuccessful it returns the
343    *  one past the end ( end() ) iterator.
344   */
345   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
346 
347   /**
348    *  @brief Finds the number of elements with given key.
349    *  @param  x  Key of (key, value) pairs to be located.
350    *  @return Number of elements with specified key.
351   */
352   size_type count(const key_type& __x) const { return _M_t.count(__x); }
353 
354   /**
355    *  @brief Finds the beginning of a subsequence matching given key.
356    *  @param  x  Key of (key, value) pair to be located.
357    *  @return  Iterator pointing to first element matching given key, or
358    *           end() if not found.
359    *
360    *  This function returns the first element of a subsequence of elements
361    *  that matches the given key.  If unsuccessful it returns an iterator
362    *  pointing to the first element that has a greater value than given key
363    *  or end() if no such element exists.
364   */
365   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
366 
367   /**
368    *  @brief Finds the beginning of a subsequence matching given key.
369    *  @param  x  Key of (key, value) pair to be located.
370    *  @return  Read-only (constant) iterator pointing to first element
371    *           matching given key, or end() if not found.
372    *
373    *  This function returns the first element of a subsequence of elements
374    *  that matches the given key.  If unsuccessful the iterator will point
375    *  to the next greatest element or, if no such greater element exists, to
376    *  end().
377   */
378   const_iterator lower_bound(const key_type& __x) const {
379     return _M_t.lower_bound(__x);
380   }
381 
382   /**
383    *  @brief Finds the end of a subsequence matching given key.
384    *  @param  x  Key of (key, value) pair to be located.
385    *  @return Iterator pointing to last element matching given key.
386   */
387   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
388 
389   /**
390    *  @brief Finds the end of a subsequence matching given key.
391    *  @param  x  Key of (key, value) pair to be located.
392    *  @return  Read-only (constant) iterator pointing to last element matching
393    *           given key.
394   */
395   const_iterator upper_bound(const key_type& __x) const {
396     return _M_t.upper_bound(__x);
397   }
398 
399   /**
400    *  @brief Finds a subsequence matching given key.
401    *  @param  x  Key of (key, value) pairs to be located.
402    *  @return  Pair of iterators that possibly points to the subsequence
403    *           matching given key.
404    *
405    *  This function improves on lower_bound() and upper_bound() by giving a more
406    *  elegant and efficient solution.  It returns a pair of which the first
407    *  element possibly points to the first element matching the given key
408    *  and the second element possibly points to the last element matching the
409    *  given key.  If unsuccessful the first element of the returned pair will
410    *  contain an iterator pointing to the next greatest element or, if no such
411    *  greater element exists, to end().
412   */
413   pair<iterator,iterator> equal_range(const key_type& __x) {
414     return _M_t.equal_range(__x);
415   }
416 
417   /**
418    *  @brief Finds a subsequence matching given key.
419    *  @param  x  Key of (key, value) pairs to be located.
420    *  @return  Pair of read-only (constant) iterators that possibly points to
421    *           the subsequence matching given key.
422    *
423    *  This function improves on lower_bound() and upper_bound() by giving a more
424    *  elegant and efficient solution.  It returns a pair of which the first
425    *  element possibly points to the first element matching the given key
426    *  and the second element possibly points to the last element matching the
427    *  given key.  If unsuccessful the first element of the returned pair will
428    *  contain an iterator pointing to the next greatest element or, if no such
429    *  a greater element exists, to end().
430   */
431   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
432     return _M_t.equal_range(__x);
433   }
434 
435   template <class _K1, class _T1, class _C1, class _A1>
436   friend bool operator== (const multimap<_K1, _T1, _C1, _A1>&,
437                           const multimap<_K1, _T1, _C1, _A1>&);
438   template <class _K1, class _T1, class _C1, class _A1>
439   friend bool operator< (const multimap<_K1, _T1, _C1, _A1>&,
440                          const multimap<_K1, _T1, _C1, _A1>&);
441 };
442 
443 template <class _Key, class _Tp, class _Compare, class _Alloc>
444 inline bool operator==(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
445                        const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
446   return __x._M_t == __y._M_t;
447 }
448 
449 template <class _Key, class _Tp, class _Compare, class _Alloc>
450 inline bool operator<(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
451                       const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
452   return __x._M_t < __y._M_t;
453 }
454 
455 template <class _Key, class _Tp, class _Compare, class _Alloc>
456 inline bool operator!=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
457                        const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
458   return !(__x == __y);
459 }
460 
461 template <class _Key, class _Tp, class _Compare, class _Alloc>
462 inline bool operator>(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
463                       const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
464   return __y < __x;
465 }
466 
467 template <class _Key, class _Tp, class _Compare, class _Alloc>
468 inline bool operator<=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
469                        const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
470   return !(__y < __x);
471 }
472 
473 template <class _Key, class _Tp, class _Compare, class _Alloc>
474 inline bool operator>=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x,
475                        const multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
476   return !(__x < __y);
477 }
478 
479 template <class _Key, class _Tp, class _Compare, class _Alloc>
480 inline void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
481                  multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
482   __x.swap(__y);
483 }
484 
485 } // namespace std
486 
487 #endif /* __GLIBCPP_INTERNAL_MULTIMAP_H */
488 
489 // Local Variables:
490 // mode:C++
491 // End:
492