STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stl_set.h
Go to the documentation of this file.
1 // Set 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,1997
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_SET_H
57 #define _STL_SET_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 
88  template<typename _Key, typename _Compare = std::less<_Key>,
89  typename _Alloc = std::allocator<_Key> >
90  class set
91  {
92  // concept requirements
93  typedef typename _Alloc::value_type _Alloc_value_type;
94  __glibcxx_class_requires(_Key, _SGIAssignableConcept)
95  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
96  _BinaryFunctionConcept)
97  __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
98 
99  public:
100  // typedefs:
102  typedef _Key key_type;
104  typedef _Key value_type;
105  typedef _Compare key_compare;
106  typedef _Compare value_compare;
107  typedef _Alloc allocator_type;
109 
110  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;
115  _Rep_type _M_t; // Red-black tree representing set.
116 
117  public:
119  typedef typename _Key_alloc_type::pointer pointer;
121  typedef typename _Key_alloc_type::const_pointer const_pointer;
122  typedef typename _Key_alloc_type::reference reference;
123  typedef typename _Key_alloc_type::const_reference const_reference;
124  // _GLIBCXX_RESOLVE_LIB_DEFECTS
125  // DR 103. set::iterator is required to be modifiable,
126  // but this allows modification of keys.
127  typedef typename _Rep_type::const_iterator iterator;
128  typedef typename _Rep_type::const_iterator const_iterator;
129  typedef typename _Rep_type::const_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;
134 
135  // allocation/deallocation
139  set()
140  : _M_t() { }
141 
147  explicit
148  set(const _Compare& __comp,
149  const allocator_type& __a = allocator_type())
150  : _M_t(__comp, _Key_alloc_type(__a)) { }
151 
162  template<typename _InputIterator>
163  set(_InputIterator __first, _InputIterator __last)
164  : _M_t()
165  { _M_t._M_insert_unique(__first, __last); }
166 
179  template<typename _InputIterator>
180  set(_InputIterator __first, _InputIterator __last,
181  const _Compare& __comp,
182  const allocator_type& __a = allocator_type())
183  : _M_t(__comp, _Key_alloc_type(__a))
184  { _M_t._M_insert_unique(__first, __last); }
185 
193  set(const set& __x)
194  : _M_t(__x._M_t) { }
195 
196 #if __cplusplus >= 201103L
197 
204  set(set&& __x)
205  noexcept(is_nothrow_copy_constructible<_Compare>::value)
206  : _M_t(std::move(__x._M_t)) { }
207 
218  set(initializer_list<value_type> __l,
219  const _Compare& __comp = _Compare(),
220  const allocator_type& __a = allocator_type())
221  : _M_t(__comp, _Key_alloc_type(__a))
222  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
223 #endif
224 
232  set&
233  operator=(const set& __x)
234  {
235  _M_t = __x._M_t;
236  return *this;
237  }
238 
239 #if __cplusplus >= 201103L
240 
247  set&
248  operator=(set&& __x)
249  {
250  // NB: DR 1204.
251  // NB: DR 675.
252  this->clear();
253  this->swap(__x);
254  return *this;
255  }
256 
268  set&
269  operator=(initializer_list<value_type> __l)
270  {
271  this->clear();
272  this->insert(__l.begin(), __l.end());
273  return *this;
274  }
275 #endif
276 
277  // accessors:
278 
280  key_compare
281  key_comp() const
282  { return _M_t.key_comp(); }
284  value_compare
285  value_comp() const
286  { return _M_t.key_comp(); }
288  allocator_type
289  get_allocator() const _GLIBCXX_NOEXCEPT
290  { return allocator_type(_M_t.get_allocator()); }
291 
297  iterator
298  begin() const _GLIBCXX_NOEXCEPT
299  { return _M_t.begin(); }
300 
306  iterator
307  end() const _GLIBCXX_NOEXCEPT
308  { return _M_t.end(); }
309 
315  reverse_iterator
316  rbegin() const _GLIBCXX_NOEXCEPT
317  { return _M_t.rbegin(); }
318 
324  reverse_iterator
325  rend() const _GLIBCXX_NOEXCEPT
326  { return _M_t.rend(); }
327 
328 #if __cplusplus >= 201103L
329 
334  iterator
335  cbegin() const noexcept
336  { return _M_t.begin(); }
337 
343  iterator
344  cend() const noexcept
345  { return _M_t.end(); }
346 
352  reverse_iterator
353  crbegin() const noexcept
354  { return _M_t.rbegin(); }
355 
361  reverse_iterator
362  crend() const noexcept
363  { return _M_t.rend(); }
364 #endif
365 
367  bool
368  empty() const _GLIBCXX_NOEXCEPT
369  { return _M_t.empty(); }
370 
372  size_type
373  size() const _GLIBCXX_NOEXCEPT
374  { return _M_t.size(); }
375 
377  size_type
378  max_size() const _GLIBCXX_NOEXCEPT
379  { return _M_t.max_size(); }
380 
392  void
393  swap(set& __x)
394  { _M_t.swap(__x._M_t); }
395 
396  // insert/erase
397 #if __cplusplus >= 201103L
398 
411  template<typename... _Args>
412  std::pair<iterator, bool>
413  emplace(_Args&&... __args)
414  { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
415 
437  template<typename... _Args>
438  iterator
439  emplace_hint(const_iterator __pos, _Args&&... __args)
440  {
441  return _M_t._M_emplace_hint_unique(__pos,
442  std::forward<_Args>(__args)...);
443  }
444 #endif
445 
459  std::pair<iterator, bool>
460  insert(const value_type& __x)
461  {
462  std::pair<typename _Rep_type::iterator, bool> __p =
463  _M_t._M_insert_unique(__x);
464  return std::pair<iterator, bool>(__p.first, __p.second);
465  }
466 
467 #if __cplusplus >= 201103L
468  std::pair<iterator, bool>
469  insert(value_type&& __x)
470  {
471  std::pair<typename _Rep_type::iterator, bool> __p =
472  _M_t._M_insert_unique(std::move(__x));
473  return std::pair<iterator, bool>(__p.first, __p.second);
474  }
475 #endif
476 
496  iterator
497  insert(const_iterator __position, const value_type& __x)
498  { return _M_t._M_insert_unique_(__position, __x); }
499 
500 #if __cplusplus >= 201103L
501  iterator
502  insert(const_iterator __position, value_type&& __x)
503  { return _M_t._M_insert_unique_(__position, std::move(__x)); }
504 #endif
505 
515  template<typename _InputIterator>
516  void
517  insert(_InputIterator __first, _InputIterator __last)
518  { _M_t._M_insert_unique(__first, __last); }
519 
520 #if __cplusplus >= 201103L
521 
528  void
529  insert(initializer_list<value_type> __l)
530  { this->insert(__l.begin(), __l.end()); }
531 #endif
532 
533 #if __cplusplus >= 201103L
534  // _GLIBCXX_RESOLVE_LIB_DEFECTS
535  // DR 130. Associative erase should return an iterator.
549  _GLIBCXX_ABI_TAG_CXX11
550  iterator
551  erase(const_iterator __position)
552  { return _M_t.erase(__position); }
553 #else
554 
564  void
565  erase(iterator __position)
566  { _M_t.erase(__position); }
567 #endif
568 
580  size_type
581  erase(const key_type& __x)
582  { return _M_t.erase(__x); }
583 
584 #if __cplusplus >= 201103L
585  // _GLIBCXX_RESOLVE_LIB_DEFECTS
586  // DR 130. Associative erase should return an iterator.
601  _GLIBCXX_ABI_TAG_CXX11
602  iterator
603  erase(const_iterator __first, const_iterator __last)
604  { return _M_t.erase(__first, __last); }
605 #else
606 
618  void
619  erase(iterator __first, iterator __last)
620  { _M_t.erase(__first, __last); }
621 #endif
622 
629  void
630  clear() _GLIBCXX_NOEXCEPT
631  { _M_t.clear(); }
632 
633  // set operations:
634 
643  size_type
644  count(const key_type& __x) const
645  { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
646 
647  // _GLIBCXX_RESOLVE_LIB_DEFECTS
648  // 214. set::find() missing const overload
650 
661  iterator
662  find(const key_type& __x)
663  { return _M_t.find(__x); }
664 
665  const_iterator
666  find(const key_type& __x) const
667  { return _M_t.find(__x); }
669 
671 
682  iterator
683  lower_bound(const key_type& __x)
684  { return _M_t.lower_bound(__x); }
685 
686  const_iterator
687  lower_bound(const key_type& __x) const
688  { return _M_t.lower_bound(__x); }
690 
692 
698  iterator
699  upper_bound(const key_type& __x)
700  { return _M_t.upper_bound(__x); }
701 
702  const_iterator
703  upper_bound(const key_type& __x) const
704  { return _M_t.upper_bound(__x); }
706 
708 
723  std::pair<iterator, iterator>
724  equal_range(const key_type& __x)
725  { return _M_t.equal_range(__x); }
726 
727  std::pair<const_iterator, const_iterator>
728  equal_range(const key_type& __x) const
729  { return _M_t.equal_range(__x); }
731 
732  template<typename _K1, typename _C1, typename _A1>
733  friend bool
734  operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
735 
736  template<typename _K1, typename _C1, typename _A1>
737  friend bool
738  operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
739  };
740 
741 
752  template<typename _Key, typename _Compare, typename _Alloc>
753  inline bool
754  operator==(const set<_Key, _Compare, _Alloc>& __x,
755  const set<_Key, _Compare, _Alloc>& __y)
756  { return __x._M_t == __y._M_t; }
757 
769  template<typename _Key, typename _Compare, typename _Alloc>
770  inline bool
771  operator<(const set<_Key, _Compare, _Alloc>& __x,
772  const set<_Key, _Compare, _Alloc>& __y)
773  { return __x._M_t < __y._M_t; }
774 
776  template<typename _Key, typename _Compare, typename _Alloc>
777  inline bool
778  operator!=(const set<_Key, _Compare, _Alloc>& __x,
779  const set<_Key, _Compare, _Alloc>& __y)
780  { return !(__x == __y); }
781 
783  template<typename _Key, typename _Compare, typename _Alloc>
784  inline bool
785  operator>(const set<_Key, _Compare, _Alloc>& __x,
786  const set<_Key, _Compare, _Alloc>& __y)
787  { return __y < __x; }
788 
790  template<typename _Key, typename _Compare, typename _Alloc>
791  inline bool
792  operator<=(const set<_Key, _Compare, _Alloc>& __x,
793  const set<_Key, _Compare, _Alloc>& __y)
794  { return !(__y < __x); }
795 
797  template<typename _Key, typename _Compare, typename _Alloc>
798  inline bool
799  operator>=(const set<_Key, _Compare, _Alloc>& __x,
800  const set<_Key, _Compare, _Alloc>& __y)
801  { return !(__x < __y); }
802 
804  template<typename _Key, typename _Compare, typename _Alloc>
805  inline void
806  swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y)
807  { __x.swap(__y); }
808 
809 _GLIBCXX_END_NAMESPACE_CONTAINER
810 } //namespace std
811 #endif /* _STL_SET_H */
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
namespace std _GLIBCXX_VISIBILITY(default)
Definition: stl_set.h:64
#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