STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stl_multiset.h
Go to the documentation of this file.
1 // Multiset implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
56 #ifndef _STL_MULTISET_H
57 #define _STL_MULTISET_H 1
58 
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
63 
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67 
90  template <typename _Key, typename _Compare = std::less<_Key>,
91  typename _Alloc = std::allocator<_Key> >
92  class multiset
93  {
94  // concept requirements
95  typedef typename _Alloc::value_type _Alloc_value_type;
96  __glibcxx_class_requires(_Key, _SGIAssignableConcept)
97  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
98  _BinaryFunctionConcept)
99  __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
100 
101  public:
102  // typedefs:
103  typedef _Key key_type;
104  typedef _Key value_type;
105  typedef _Compare key_compare;
106  typedef _Compare value_compare;
107  typedef _Alloc allocator_type;
108 
109  private:
111  typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
112 
113  typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
114  key_compare, _Key_alloc_type> _Rep_type;
116  _Rep_type _M_t;
117 
118  public:
119  typedef typename _Key_alloc_type::pointer pointer;
120  typedef typename _Key_alloc_type::const_pointer const_pointer;
121  typedef typename _Key_alloc_type::reference reference;
122  typedef typename _Key_alloc_type::const_reference const_reference;
123  // _GLIBCXX_RESOLVE_LIB_DEFECTS
124  // DR 103. set::iterator is required to be modifiable,
125  // but this allows modification of keys.
126  typedef typename _Rep_type::const_iterator iterator;
127  typedef typename _Rep_type::const_iterator const_iterator;
128  typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
129  typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
130  typedef typename _Rep_type::size_type size_type;
131  typedef typename _Rep_type::difference_type difference_type;
132 
133  // allocation/deallocation
137  multiset()
138  : _M_t() { }
139 
145  explicit
146  multiset(const _Compare& __comp,
147  const allocator_type& __a = allocator_type())
148  : _M_t(__comp, _Key_alloc_type(__a)) { }
149 
159  template<typename _InputIterator>
160  multiset(_InputIterator __first, _InputIterator __last)
161  : _M_t()
162  { _M_t._M_insert_equal(__first, __last); }
163 
175  template<typename _InputIterator>
176  multiset(_InputIterator __first, _InputIterator __last,
177  const _Compare& __comp,
178  const allocator_type& __a = allocator_type())
179  : _M_t(__comp, _Key_alloc_type(__a))
180  { _M_t._M_insert_equal(__first, __last); }
181 
189  multiset(const multiset& __x)
190  : _M_t(__x._M_t) { }
191 
192 #if __cplusplus >= 201103L
193 
200  multiset(multiset&& __x)
201  noexcept(is_nothrow_copy_constructible<_Compare>::value)
202  : _M_t(std::move(__x._M_t)) { }
203 
214  multiset(initializer_list<value_type> __l,
215  const _Compare& __comp = _Compare(),
216  const allocator_type& __a = allocator_type())
217  : _M_t(__comp, _Key_alloc_type(__a))
218  { _M_t._M_insert_equal(__l.begin(), __l.end()); }
219 #endif
220 
228  multiset&
229  operator=(const multiset& __x)
230  {
231  _M_t = __x._M_t;
232  return *this;
233  }
234 
235 #if __cplusplus >= 201103L
236 
244  multiset&
245  operator=(multiset&& __x)
246  {
247  // NB: DR 1204.
248  // NB: DR 675.
249  this->clear();
250  this->swap(__x);
251  return *this;
252  }
253 
265  multiset&
266  operator=(initializer_list<value_type> __l)
267  {
268  this->clear();
269  this->insert(__l.begin(), __l.end());
270  return *this;
271  }
272 #endif
273 
274  // accessors:
275 
277  key_compare
278  key_comp() const
279  { return _M_t.key_comp(); }
281  value_compare
282  value_comp() const
283  { return _M_t.key_comp(); }
285  allocator_type
286  get_allocator() const _GLIBCXX_NOEXCEPT
287  { return allocator_type(_M_t.get_allocator()); }
288 
294  iterator
295  begin() const _GLIBCXX_NOEXCEPT
296  { return _M_t.begin(); }
297 
303  iterator
304  end() const _GLIBCXX_NOEXCEPT
305  { return _M_t.end(); }
306 
312  reverse_iterator
313  rbegin() const _GLIBCXX_NOEXCEPT
314  { return _M_t.rbegin(); }
315 
321  reverse_iterator
322  rend() const _GLIBCXX_NOEXCEPT
323  { return _M_t.rend(); }
324 
325 #if __cplusplus >= 201103L
326 
331  iterator
332  cbegin() const noexcept
333  { return _M_t.begin(); }
334 
340  iterator
341  cend() const noexcept
342  { return _M_t.end(); }
343 
349  reverse_iterator
350  crbegin() const noexcept
351  { return _M_t.rbegin(); }
352 
358  reverse_iterator
359  crend() const noexcept
360  { return _M_t.rend(); }
361 #endif
362 
364  bool
365  empty() const _GLIBCXX_NOEXCEPT
366  { return _M_t.empty(); }
367 
369  size_type
370  size() const _GLIBCXX_NOEXCEPT
371  { return _M_t.size(); }
372 
374  size_type
375  max_size() const _GLIBCXX_NOEXCEPT
376  { return _M_t.max_size(); }
377 
389  void
390  swap(multiset& __x)
391  { _M_t.swap(__x._M_t); }
392 
393  // insert/erase
394 #if __cplusplus >= 201103L
395 
407  template<typename... _Args>
408  iterator
409  emplace(_Args&&... __args)
410  { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
411 
433  template<typename... _Args>
434  iterator
435  emplace_hint(const_iterator __pos, _Args&&... __args)
436  {
437  return _M_t._M_emplace_hint_equal(__pos,
438  std::forward<_Args>(__args)...);
439  }
440 #endif
441 
453  iterator
454  insert(const value_type& __x)
455  { return _M_t._M_insert_equal(__x); }
456 
457 #if __cplusplus >= 201103L
458  iterator
459  insert(value_type&& __x)
460  { return _M_t._M_insert_equal(std::move(__x)); }
461 #endif
462 
483  iterator
484  insert(const_iterator __position, const value_type& __x)
485  { return _M_t._M_insert_equal_(__position, __x); }
486 
487 #if __cplusplus >= 201103L
488  iterator
489  insert(const_iterator __position, value_type&& __x)
490  { return _M_t._M_insert_equal_(__position, std::move(__x)); }
491 #endif
492 
501  template<typename _InputIterator>
502  void
503  insert(_InputIterator __first, _InputIterator __last)
504  { _M_t._M_insert_equal(__first, __last); }
505 
506 #if __cplusplus >= 201103L
507 
514  void
515  insert(initializer_list<value_type> __l)
516  { this->insert(__l.begin(), __l.end()); }
517 #endif
518 
519 #if __cplusplus >= 201103L
520  // _GLIBCXX_RESOLVE_LIB_DEFECTS
521  // DR 130. Associative erase should return an iterator.
535  _GLIBCXX_ABI_TAG_CXX11
536  iterator
537  erase(const_iterator __position)
538  { return _M_t.erase(__position); }
539 #else
540 
550  void
551  erase(iterator __position)
552  { _M_t.erase(__position); }
553 #endif
554 
566  size_type
567  erase(const key_type& __x)
568  { return _M_t.erase(__x); }
569 
570 #if __cplusplus >= 201103L
571  // _GLIBCXX_RESOLVE_LIB_DEFECTS
572  // DR 130. Associative erase should return an iterator.
587  _GLIBCXX_ABI_TAG_CXX11
588  iterator
589  erase(const_iterator __first, const_iterator __last)
590  { return _M_t.erase(__first, __last); }
591 #else
592 
604  void
605  erase(iterator __first, iterator __last)
606  { _M_t.erase(__first, __last); }
607 #endif
608 
615  void
616  clear() _GLIBCXX_NOEXCEPT
617  { _M_t.clear(); }
618 
619  // multiset operations:
620 
626  size_type
627  count(const key_type& __x) const
628  { return _M_t.count(__x); }
629 
630  // _GLIBCXX_RESOLVE_LIB_DEFECTS
631  // 214. set::find() missing const overload
633 
644  iterator
645  find(const key_type& __x)
646  { return _M_t.find(__x); }
647 
648  const_iterator
649  find(const key_type& __x) const
650  { return _M_t.find(__x); }
652 
654 
665  iterator
666  lower_bound(const key_type& __x)
667  { return _M_t.lower_bound(__x); }
668 
669  const_iterator
670  lower_bound(const key_type& __x) const
671  { return _M_t.lower_bound(__x); }
673 
675 
681  iterator
682  upper_bound(const key_type& __x)
683  { return _M_t.upper_bound(__x); }
684 
685  const_iterator
686  upper_bound(const key_type& __x) const
687  { return _M_t.upper_bound(__x); }
689 
691 
706  std::pair<iterator, iterator>
707  equal_range(const key_type& __x)
708  { return _M_t.equal_range(__x); }
709 
710  std::pair<const_iterator, const_iterator>
711  equal_range(const key_type& __x) const
712  { return _M_t.equal_range(__x); }
714 
715  template<typename _K1, typename _C1, typename _A1>
716  friend bool
717  operator==(const multiset<_K1, _C1, _A1>&,
718  const multiset<_K1, _C1, _A1>&);
719 
720  template<typename _K1, typename _C1, typename _A1>
721  friend bool
722  operator< (const multiset<_K1, _C1, _A1>&,
723  const multiset<_K1, _C1, _A1>&);
724  };
725 
737  template<typename _Key, typename _Compare, typename _Alloc>
738  inline bool
739  operator==(const multiset<_Key, _Compare, _Alloc>& __x,
740  const multiset<_Key, _Compare, _Alloc>& __y)
741  { return __x._M_t == __y._M_t; }
742 
754  template<typename _Key, typename _Compare, typename _Alloc>
755  inline bool
756  operator<(const multiset<_Key, _Compare, _Alloc>& __x,
757  const multiset<_Key, _Compare, _Alloc>& __y)
758  { return __x._M_t < __y._M_t; }
759 
761  template<typename _Key, typename _Compare, typename _Alloc>
762  inline bool
763  operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
764  const multiset<_Key, _Compare, _Alloc>& __y)
765  { return !(__x == __y); }
766 
768  template<typename _Key, typename _Compare, typename _Alloc>
769  inline bool
770  operator>(const multiset<_Key,_Compare,_Alloc>& __x,
771  const multiset<_Key,_Compare,_Alloc>& __y)
772  { return __y < __x; }
773 
775  template<typename _Key, typename _Compare, typename _Alloc>
776  inline bool
777  operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
778  const multiset<_Key, _Compare, _Alloc>& __y)
779  { return !(__y < __x); }
780 
782  template<typename _Key, typename _Compare, typename _Alloc>
783  inline bool
784  operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
785  const multiset<_Key, _Compare, _Alloc>& __y)
786  { return !(__x < __y); }
787 
789  template<typename _Key, typename _Compare, typename _Alloc>
790  inline void
791  swap(multiset<_Key, _Compare, _Alloc>& __x,
792  multiset<_Key, _Compare, _Alloc>& __y)
793  { __x.swap(__y); }
794 
795 _GLIBCXX_END_NAMESPACE_CONTAINER
796 } // namespace std
797 
798 #endif /* _STL_MULTISET_H */
namespace std _GLIBCXX_VISIBILITY(default)
Definition: stl_multiset.h:64
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define __glibcxx_class_requires(_a, _b)
Definition: concept_check.h:48
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
#define __glibcxx_class_requires2(_a, _b, _c)
Definition: concept_check.h:49
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160
#define __glibcxx_class_requires4(_a, _b, _c, _d, _e)
Definition: concept_check.h:51