libstdc++
stl_set.h
Go to the documentation of this file.
1 // Set implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2016 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 
51 /** @file bits/stl_set.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{set}
54  */
55 
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 
68  template<typename _Key, typename _Compare, typename _Alloc>
69  class multiset;
70 
71  /**
72  * @brief A standard container made up of unique keys, which can be
73  * retrieved in logarithmic time.
74  *
75  * @ingroup associative_containers
76  *
77  * @tparam _Key Type of key objects.
78  * @tparam _Compare Comparison function object type, defaults to less<_Key>.
79  * @tparam _Alloc Allocator type, defaults to allocator<_Key>.
80  *
81  * Meets the requirements of a <a href="tables.html#65">container</a>, a
82  * <a href="tables.html#66">reversible container</a>, and an
83  * <a href="tables.html#69">associative container</a> (using unique keys).
84  *
85  * Sets support bidirectional iterators.
86  *
87  * The private tree data is declared exactly the same way for set and
88  * multiset; the distinction is made entirely in how the tree functions are
89  * called (*_unique versus *_equal, same as the standard).
90  */
91  template<typename _Key, typename _Compare = std::less<_Key>,
92  typename _Alloc = std::allocator<_Key> >
93  class set
94  {
95  // concept requirements
96  typedef typename _Alloc::value_type _Alloc_value_type;
97  __glibcxx_class_requires(_Key, _SGIAssignableConcept)
98  __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
99  _BinaryFunctionConcept)
100  __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
101 
102  public:
103  // typedefs:
104  //@{
105  /// Public typedefs.
106  typedef _Key key_type;
107  typedef _Key value_type;
108  typedef _Compare key_compare;
109  typedef _Compare value_compare;
110  typedef _Alloc allocator_type;
111  //@}
112 
113  private:
114  typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
115  rebind<_Key>::other _Key_alloc_type;
116 
117  typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
118  key_compare, _Key_alloc_type> _Rep_type;
119  _Rep_type _M_t; // Red-black tree representing set.
120 
121  typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits;
122 
123  public:
124  //@{
125  /// Iterator-related typedefs.
126  typedef typename _Alloc_traits::pointer pointer;
127  typedef typename _Alloc_traits::const_pointer const_pointer;
128  typedef typename _Alloc_traits::reference reference;
129  typedef typename _Alloc_traits::const_reference const_reference;
130  // _GLIBCXX_RESOLVE_LIB_DEFECTS
131  // DR 103. set::iterator is required to be modifiable,
132  // but this allows modification of keys.
133  typedef typename _Rep_type::const_iterator iterator;
134  typedef typename _Rep_type::const_iterator const_iterator;
135  typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
136  typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
137  typedef typename _Rep_type::size_type size_type;
138  typedef typename _Rep_type::difference_type difference_type;
139  //@}
140 
141 #if __cplusplus > 201402L
142  using node_type = typename _Rep_type::node_type;
143  using insert_return_type = typename _Rep_type::insert_return_type;
144 #endif
145 
146  // allocation/deallocation
147  /**
148  * @brief Default constructor creates no elements.
149  */
150 #if __cplusplus < 201103L
151  set() : _M_t() { }
152 #else
153  set() = default;
154 #endif
155 
156  /**
157  * @brief Creates a %set with no elements.
158  * @param __comp Comparator to use.
159  * @param __a An allocator object.
160  */
161  explicit
162  set(const _Compare& __comp,
163  const allocator_type& __a = allocator_type())
164  : _M_t(__comp, _Key_alloc_type(__a)) { }
165 
166  /**
167  * @brief Builds a %set from a range.
168  * @param __first An input iterator.
169  * @param __last An input iterator.
170  *
171  * Create a %set consisting of copies of the elements from
172  * [__first,__last). This is linear in N if the range is
173  * already sorted, and NlogN otherwise (where N is
174  * distance(__first,__last)).
175  */
176  template<typename _InputIterator>
177  set(_InputIterator __first, _InputIterator __last)
178  : _M_t()
179  { _M_t._M_insert_unique(__first, __last); }
180 
181  /**
182  * @brief Builds a %set from a range.
183  * @param __first An input iterator.
184  * @param __last An input iterator.
185  * @param __comp A comparison functor.
186  * @param __a An allocator object.
187  *
188  * Create a %set consisting of copies of the elements from
189  * [__first,__last). This is linear in N if the range is
190  * already sorted, and NlogN otherwise (where N is
191  * distance(__first,__last)).
192  */
193  template<typename _InputIterator>
194  set(_InputIterator __first, _InputIterator __last,
195  const _Compare& __comp,
196  const allocator_type& __a = allocator_type())
197  : _M_t(__comp, _Key_alloc_type(__a))
198  { _M_t._M_insert_unique(__first, __last); }
199 
200  /**
201  * @brief %Set copy constructor.
202  * @param __x A %set of identical element and allocator types.
203  *
204  * The newly-created %set uses a copy of the allocator object used
205  * by @a __x (unless the allocator traits dictate a different object).
206  */
207  set(const set& __x)
208  : _M_t(__x._M_t) { }
209 
210 #if __cplusplus >= 201103L
211  /**
212  * @brief %Set move constructor
213  * @param __x A %set of identical element and allocator types.
214  *
215  * The newly-created %set contains the exact contents of @a x.
216  * The contents of @a x are a valid, but unspecified %set.
217  */
218  set(set&& __x)
219  noexcept(is_nothrow_copy_constructible<_Compare>::value)
220  : _M_t(std::move(__x._M_t)) { }
221 
222  /**
223  * @brief Builds a %set from an initializer_list.
224  * @param __l An initializer_list.
225  * @param __comp A comparison functor.
226  * @param __a An allocator object.
227  *
228  * Create a %set consisting of copies of the elements in the list.
229  * This is linear in N if the list is already sorted, and NlogN
230  * otherwise (where N is @a __l.size()).
231  */
233  const _Compare& __comp = _Compare(),
234  const allocator_type& __a = allocator_type())
235  : _M_t(__comp, _Key_alloc_type(__a))
236  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
237 
238  /// Allocator-extended default constructor.
239  explicit
240  set(const allocator_type& __a)
241  : _M_t(_Compare(), _Key_alloc_type(__a)) { }
242 
243  /// Allocator-extended copy constructor.
244  set(const set& __x, const allocator_type& __a)
245  : _M_t(__x._M_t, _Key_alloc_type(__a)) { }
246 
247  /// Allocator-extended move constructor.
248  set(set&& __x, const allocator_type& __a)
249  noexcept(is_nothrow_copy_constructible<_Compare>::value
250  && _Alloc_traits::_S_always_equal())
251  : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { }
252 
253  /// Allocator-extended initialier-list constructor.
254  set(initializer_list<value_type> __l, const allocator_type& __a)
255  : _M_t(_Compare(), _Key_alloc_type(__a))
256  { _M_t._M_insert_unique(__l.begin(), __l.end()); }
257 
258  /// Allocator-extended range constructor.
259  template<typename _InputIterator>
260  set(_InputIterator __first, _InputIterator __last,
261  const allocator_type& __a)
262  : _M_t(_Compare(), _Key_alloc_type(__a))
263  { _M_t._M_insert_unique(__first, __last); }
264 #endif
265 
266  /**
267  * @brief %Set assignment operator.
268  * @param __x A %set of identical element and allocator types.
269  *
270  * All the elements of @a __x are copied.
271  *
272  * Whether the allocator is copied depends on the allocator traits.
273  */
274  set&
275  operator=(const set& __x)
276  {
277  _M_t = __x._M_t;
278  return *this;
279  }
280 
281 #if __cplusplus >= 201103L
282  /// Move assignment operator.
283  set&
284  operator=(set&&) = default;
285 
286  /**
287  * @brief %Set list assignment operator.
288  * @param __l An initializer_list.
289  *
290  * This function fills a %set with copies of the elements in the
291  * initializer list @a __l.
292  *
293  * Note that the assignment completely changes the %set and
294  * that the resulting %set's size is the same as the number
295  * of elements assigned.
296  */
297  set&
299  {
300  _M_t._M_assign_unique(__l.begin(), __l.end());
301  return *this;
302  }
303 #endif
304 
305  // accessors:
306 
307  /// Returns the comparison object with which the %set was constructed.
308  key_compare
309  key_comp() const
310  { return _M_t.key_comp(); }
311  /// Returns the comparison object with which the %set was constructed.
312  value_compare
313  value_comp() const
314  { return _M_t.key_comp(); }
315  /// Returns the allocator object with which the %set was constructed.
316  allocator_type
317  get_allocator() const _GLIBCXX_NOEXCEPT
318  { return allocator_type(_M_t.get_allocator()); }
319 
320  /**
321  * Returns a read-only (constant) iterator that points to the first
322  * element in the %set. Iteration is done in ascending order according
323  * to the keys.
324  */
325  iterator
326  begin() const _GLIBCXX_NOEXCEPT
327  { return _M_t.begin(); }
328 
329  /**
330  * Returns a read-only (constant) iterator that points one past the last
331  * element in the %set. Iteration is done in ascending order according
332  * to the keys.
333  */
334  iterator
335  end() const _GLIBCXX_NOEXCEPT
336  { return _M_t.end(); }
337 
338  /**
339  * Returns a read-only (constant) iterator that points to the last
340  * element in the %set. Iteration is done in descending order according
341  * to the keys.
342  */
343  reverse_iterator
344  rbegin() const _GLIBCXX_NOEXCEPT
345  { return _M_t.rbegin(); }
346 
347  /**
348  * Returns a read-only (constant) reverse iterator that points to the
349  * last pair in the %set. Iteration is done in descending order
350  * according to the keys.
351  */
352  reverse_iterator
353  rend() const _GLIBCXX_NOEXCEPT
354  { return _M_t.rend(); }
355 
356 #if __cplusplus >= 201103L
357  /**
358  * Returns a read-only (constant) iterator that points to the first
359  * element in the %set. Iteration is done in ascending order according
360  * to the keys.
361  */
362  iterator
363  cbegin() const noexcept
364  { return _M_t.begin(); }
365 
366  /**
367  * Returns a read-only (constant) iterator that points one past the last
368  * element in the %set. Iteration is done in ascending order according
369  * to the keys.
370  */
371  iterator
372  cend() const noexcept
373  { return _M_t.end(); }
374 
375  /**
376  * Returns a read-only (constant) iterator that points to the last
377  * element in the %set. Iteration is done in descending order according
378  * to the keys.
379  */
380  reverse_iterator
381  crbegin() const noexcept
382  { return _M_t.rbegin(); }
383 
384  /**
385  * Returns a read-only (constant) reverse iterator that points to the
386  * last pair in the %set. Iteration is done in descending order
387  * according to the keys.
388  */
389  reverse_iterator
390  crend() const noexcept
391  { return _M_t.rend(); }
392 #endif
393 
394  /// Returns true if the %set is empty.
395  bool
396  empty() const _GLIBCXX_NOEXCEPT
397  { return _M_t.empty(); }
398 
399  /// Returns the size of the %set.
400  size_type
401  size() const _GLIBCXX_NOEXCEPT
402  { return _M_t.size(); }
403 
404  /// Returns the maximum size of the %set.
405  size_type
406  max_size() const _GLIBCXX_NOEXCEPT
407  { return _M_t.max_size(); }
408 
409  /**
410  * @brief Swaps data with another %set.
411  * @param __x A %set of the same element and allocator types.
412  *
413  * This exchanges the elements between two sets in constant
414  * time. (It is only swapping a pointer, an integer, and an
415  * instance of the @c Compare type (which itself is often
416  * stateless and empty), so it should be quite fast.) Note
417  * that the global std::swap() function is specialized such
418  * that std::swap(s1,s2) will feed to this function.
419  *
420  * Whether the allocators are swapped depends on the allocator traits.
421  */
422  void
423  swap(set& __x)
424  _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
425  { _M_t.swap(__x._M_t); }
426 
427  // insert/erase
428 #if __cplusplus >= 201103L
429  /**
430  * @brief Attempts to build and insert an element into the %set.
431  * @param __args Arguments used to generate an element.
432  * @return A pair, of which the first element is an iterator that points
433  * to the possibly inserted element, and the second is a bool
434  * that is true if the element was actually inserted.
435  *
436  * This function attempts to build and insert an element into the %set.
437  * A %set relies on unique keys and thus an element is only inserted if
438  * it is not already present in the %set.
439  *
440  * Insertion requires logarithmic time.
441  */
442  template<typename... _Args>
444  emplace(_Args&&... __args)
445  { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
446 
447  /**
448  * @brief Attempts to insert an element into the %set.
449  * @param __pos An iterator that serves as a hint as to where the
450  * element should be inserted.
451  * @param __args Arguments used to generate the element to be
452  * inserted.
453  * @return An iterator that points to the element with key equivalent to
454  * the one generated from @a __args (may or may not be the
455  * element itself).
456  *
457  * This function is not concerned about whether the insertion took place,
458  * and thus does not return a boolean like the single-argument emplace()
459  * does. Note that the first parameter is only a hint and can
460  * potentially improve the performance of the insertion process. A bad
461  * hint would cause no gains in efficiency.
462  *
463  * For more on @a hinting, see:
464  * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
465  *
466  * Insertion requires logarithmic time (if the hint is not taken).
467  */
468  template<typename... _Args>
469  iterator
470  emplace_hint(const_iterator __pos, _Args&&... __args)
471  {
472  return _M_t._M_emplace_hint_unique(__pos,
473  std::forward<_Args>(__args)...);
474  }
475 #endif
476 
477  /**
478  * @brief Attempts to insert an element into the %set.
479  * @param __x Element to be inserted.
480  * @return A pair, of which the first element is an iterator that points
481  * to the possibly inserted element, and the second is a bool
482  * that is true if the element was actually inserted.
483  *
484  * This function attempts to insert an element into the %set. A %set
485  * relies on unique keys and thus an element is only inserted if it is
486  * not already present in the %set.
487  *
488  * Insertion requires logarithmic time.
489  */
491  insert(const value_type& __x)
492  {
494  _M_t._M_insert_unique(__x);
495  return std::pair<iterator, bool>(__p.first, __p.second);
496  }
497 
498 #if __cplusplus >= 201103L
500  insert(value_type&& __x)
501  {
503  _M_t._M_insert_unique(std::move(__x));
504  return std::pair<iterator, bool>(__p.first, __p.second);
505  }
506 #endif
507 
508  /**
509  * @brief Attempts to insert an element into the %set.
510  * @param __position An iterator that serves as a hint as to where the
511  * element should be inserted.
512  * @param __x Element to be inserted.
513  * @return An iterator that points to the element with key of
514  * @a __x (may or may not be the element passed in).
515  *
516  * This function is not concerned about whether the insertion took place,
517  * and thus does not return a boolean like the single-argument insert()
518  * does. Note that the first parameter is only a hint and can
519  * potentially improve the performance of the insertion process. A bad
520  * hint would cause no gains in efficiency.
521  *
522  * For more on @a hinting, see:
523  * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
524  *
525  * Insertion requires logarithmic time (if the hint is not taken).
526  */
527  iterator
528  insert(const_iterator __position, const value_type& __x)
529  { return _M_t._M_insert_unique_(__position, __x); }
530 
531 #if __cplusplus >= 201103L
532  iterator
533  insert(const_iterator __position, value_type&& __x)
534  { return _M_t._M_insert_unique_(__position, std::move(__x)); }
535 #endif
536 
537  /**
538  * @brief A template function that attempts to insert a range
539  * of elements.
540  * @param __first Iterator pointing to the start of the range to be
541  * inserted.
542  * @param __last Iterator pointing to the end of the range.
543  *
544  * Complexity similar to that of the range constructor.
545  */
546  template<typename _InputIterator>
547  void
548  insert(_InputIterator __first, _InputIterator __last)
549  { _M_t._M_insert_unique(__first, __last); }
550 
551 #if __cplusplus >= 201103L
552  /**
553  * @brief Attempts to insert a list of elements into the %set.
554  * @param __l A std::initializer_list<value_type> of elements
555  * to be inserted.
556  *
557  * Complexity similar to that of the range constructor.
558  */
559  void
561  { this->insert(__l.begin(), __l.end()); }
562 #endif
563 
564 #if __cplusplus > 201402L
565  /// Extract a node.
566  node_type
567  extract(const_iterator __pos)
568  {
569  __glibcxx_assert(__pos != end());
570  return _M_t.extract(__pos);
571  }
572 
573  /// Extract a node.
574  node_type
575  extract(const key_type& __x)
576  { return _M_t.extract(__x); }
577 
578  /// Re-insert an extracted node.
579  insert_return_type
580  insert(node_type&& __nh)
581  { return _M_t._M_reinsert_node_unique(std::move(__nh)); }
582 
583  /// Re-insert an extracted node.
584  iterator
585  insert(const_iterator __hint, node_type&& __nh)
586  { return _M_t._M_reinsert_node_hint_unique(__hint, std::move(__nh)); }
587 
588  template<typename, typename>
589  friend class _Rb_tree_merge_helper;
590 
591  template<typename _Compare1>
592  void
593  merge(set<_Key, _Compare1, _Alloc>& __source)
594  {
595  using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
596  _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
597  }
598 
599  template<typename _Compare1>
600  void
601  merge(set<_Key, _Compare1, _Alloc>&& __source)
602  { merge(__source); }
603 
604  template<typename _Compare1>
605  void
606  merge(multiset<_Key, _Compare1, _Alloc>& __source)
607  {
608  using _Merge_helper = _Rb_tree_merge_helper<set, _Compare1>;
609  _M_t._M_merge_unique(_Merge_helper::_S_get_tree(__source));
610  }
611 
612  template<typename _Compare1>
613  void
614  merge(multiset<_Key, _Compare1, _Alloc>&& __source)
615  { merge(__source); }
616 #endif // C++17
617 
618 #if __cplusplus >= 201103L
619  // _GLIBCXX_RESOLVE_LIB_DEFECTS
620  // DR 130. Associative erase should return an iterator.
621  /**
622  * @brief Erases an element from a %set.
623  * @param __position An iterator pointing to the element to be erased.
624  * @return An iterator pointing to the element immediately following
625  * @a __position prior to the element being erased. If no such
626  * element exists, end() is returned.
627  *
628  * This function erases an element, pointed to by the given iterator,
629  * from a %set. Note that this function only erases the element, and
630  * that if the element is itself a pointer, the pointed-to memory is not
631  * touched in any way. Managing the pointer is the user's
632  * responsibility.
633  */
634  _GLIBCXX_ABI_TAG_CXX11
635  iterator
636  erase(const_iterator __position)
637  { return _M_t.erase(__position); }
638 #else
639  /**
640  * @brief Erases an element from a %set.
641  * @param position An iterator pointing to the element to be erased.
642  *
643  * This function erases an element, pointed to by the given iterator,
644  * from a %set. Note that this function only erases the element, and
645  * that if the element is itself a pointer, the pointed-to memory is not
646  * touched in any way. Managing the pointer is the user's
647  * responsibility.
648  */
649  void
650  erase(iterator __position)
651  { _M_t.erase(__position); }
652 #endif
653 
654  /**
655  * @brief Erases elements according to the provided key.
656  * @param __x Key of element to be erased.
657  * @return The number of elements erased.
658  *
659  * This function erases all the elements located by the given key from
660  * a %set.
661  * Note that this function only erases the element, and that if
662  * the element is itself a pointer, the pointed-to memory is not touched
663  * in any way. Managing the pointer is the user's responsibility.
664  */
665  size_type
666  erase(const key_type& __x)
667  { return _M_t.erase(__x); }
668 
669 #if __cplusplus >= 201103L
670  // _GLIBCXX_RESOLVE_LIB_DEFECTS
671  // DR 130. Associative erase should return an iterator.
672  /**
673  * @brief Erases a [__first,__last) range of elements from a %set.
674  * @param __first Iterator pointing to the start of the range to be
675  * erased.
676 
677  * @param __last Iterator pointing to the end of the range to
678  * be erased.
679  * @return The iterator @a __last.
680  *
681  * This function erases a sequence of elements from a %set.
682  * Note that this function only erases the element, and that if
683  * the element is itself a pointer, the pointed-to memory is not touched
684  * in any way. Managing the pointer is the user's responsibility.
685  */
686  _GLIBCXX_ABI_TAG_CXX11
687  iterator
688  erase(const_iterator __first, const_iterator __last)
689  { return _M_t.erase(__first, __last); }
690 #else
691  /**
692  * @brief Erases a [first,last) range of elements from a %set.
693  * @param __first Iterator pointing to the start of the range to be
694  * erased.
695  * @param __last Iterator pointing to the end of the range to
696  * be erased.
697  *
698  * This function erases a sequence of elements from a %set.
699  * Note that this function only erases the element, and that if
700  * the element is itself a pointer, the pointed-to memory is not touched
701  * in any way. Managing the pointer is the user's responsibility.
702  */
703  void
704  erase(iterator __first, iterator __last)
705  { _M_t.erase(__first, __last); }
706 #endif
707 
708  /**
709  * Erases all elements in a %set. Note that this function only erases
710  * the elements, and that if the elements themselves are pointers, the
711  * pointed-to memory is not touched in any way. Managing the pointer is
712  * the user's responsibility.
713  */
714  void
715  clear() _GLIBCXX_NOEXCEPT
716  { _M_t.clear(); }
717 
718  // set operations:
719 
720  //@{
721  /**
722  * @brief Finds the number of elements.
723  * @param __x Element to located.
724  * @return Number of elements with specified key.
725  *
726  * This function only makes sense for multisets; for set the result will
727  * either be 0 (not present) or 1 (present).
728  */
729  size_type
730  count(const key_type& __x) const
731  { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
732 
733 #if __cplusplus > 201103L
734  template<typename _Kt>
735  auto
736  count(const _Kt& __x) const
737  -> decltype(_M_t._M_count_tr(__x))
738  { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; }
739 #endif
740  //@}
741 
742  // _GLIBCXX_RESOLVE_LIB_DEFECTS
743  // 214. set::find() missing const overload
744  //@{
745  /**
746  * @brief Tries to locate an element in a %set.
747  * @param __x Element to be located.
748  * @return Iterator pointing to sought-after element, or end() if not
749  * found.
750  *
751  * This function takes a key and tries to locate the element with which
752  * the key matches. If successful the function returns an iterator
753  * pointing to the sought after element. If unsuccessful it returns the
754  * past-the-end ( @c end() ) iterator.
755  */
756  iterator
757  find(const key_type& __x)
758  { return _M_t.find(__x); }
759 
760  const_iterator
761  find(const key_type& __x) const
762  { return _M_t.find(__x); }
763 
764 #if __cplusplus > 201103L
765  template<typename _Kt>
766  auto
767  find(const _Kt& __x)
768  -> decltype(iterator{_M_t._M_find_tr(__x)})
769  { return iterator{_M_t._M_find_tr(__x)}; }
770 
771  template<typename _Kt>
772  auto
773  find(const _Kt& __x) const
774  -> decltype(const_iterator{_M_t._M_find_tr(__x)})
775  { return const_iterator{_M_t._M_find_tr(__x)}; }
776 #endif
777  //@}
778 
779  //@{
780  /**
781  * @brief Finds the beginning of a subsequence matching given key.
782  * @param __x Key to be located.
783  * @return Iterator pointing to first element equal to or greater
784  * than key, or end().
785  *
786  * This function returns the first element of a subsequence of elements
787  * that matches the given key. If unsuccessful it returns an iterator
788  * pointing to the first element that has a greater value than given key
789  * or end() if no such element exists.
790  */
791  iterator
792  lower_bound(const key_type& __x)
793  { return _M_t.lower_bound(__x); }
794 
795  const_iterator
796  lower_bound(const key_type& __x) const
797  { return _M_t.lower_bound(__x); }
798 
799 #if __cplusplus > 201103L
800  template<typename _Kt>
801  auto
802  lower_bound(const _Kt& __x)
803  -> decltype(_M_t._M_lower_bound_tr(__x))
804  { return _M_t._M_lower_bound_tr(__x); }
805 
806  template<typename _Kt>
807  auto
808  lower_bound(const _Kt& __x) const
809  -> decltype(_M_t._M_lower_bound_tr(__x))
810  { return _M_t._M_lower_bound_tr(__x); }
811 #endif
812  //@}
813 
814  //@{
815  /**
816  * @brief Finds the end of a subsequence matching given key.
817  * @param __x Key to be located.
818  * @return Iterator pointing to the first element
819  * greater than key, or end().
820  */
821  iterator
822  upper_bound(const key_type& __x)
823  { return _M_t.upper_bound(__x); }
824 
825  const_iterator
826  upper_bound(const key_type& __x) const
827  { return _M_t.upper_bound(__x); }
828 
829 #if __cplusplus > 201103L
830  template<typename _Kt>
831  auto
832  upper_bound(const _Kt& __x)
833  -> decltype(_M_t._M_upper_bound_tr(__x))
834  { return _M_t._M_upper_bound_tr(__x); }
835 
836  template<typename _Kt>
837  auto
838  upper_bound(const _Kt& __x) const
839  -> decltype(_M_t._M_upper_bound_tr(__x))
840  { return _M_t._M_upper_bound_tr(__x); }
841 #endif
842  //@}
843 
844  //@{
845  /**
846  * @brief Finds a subsequence matching given key.
847  * @param __x Key to be located.
848  * @return Pair of iterators that possibly points to the subsequence
849  * matching given key.
850  *
851  * This function is equivalent to
852  * @code
853  * std::make_pair(c.lower_bound(val),
854  * c.upper_bound(val))
855  * @endcode
856  * (but is faster than making the calls separately).
857  *
858  * This function probably only makes sense for multisets.
859  */
861  equal_range(const key_type& __x)
862  { return _M_t.equal_range(__x); }
863 
865  equal_range(const key_type& __x) const
866  { return _M_t.equal_range(__x); }
867 
868 #if __cplusplus > 201103L
869  template<typename _Kt>
870  auto
871  equal_range(const _Kt& __x)
872  -> decltype(_M_t._M_equal_range_tr(__x))
873  { return _M_t._M_equal_range_tr(__x); }
874 
875  template<typename _Kt>
876  auto
877  equal_range(const _Kt& __x) const
878  -> decltype(_M_t._M_equal_range_tr(__x))
879  { return _M_t._M_equal_range_tr(__x); }
880 #endif
881  //@}
882 
883  template<typename _K1, typename _C1, typename _A1>
884  friend bool
886 
887  template<typename _K1, typename _C1, typename _A1>
888  friend bool
889  operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&);
890  };
891 
892 
893  /**
894  * @brief Set equality comparison.
895  * @param __x A %set.
896  * @param __y A %set of the same type as @a x.
897  * @return True iff the size and elements of the sets are equal.
898  *
899  * This is an equivalence relation. It is linear in the size of the sets.
900  * Sets are considered equivalent if their sizes are equal, and if
901  * corresponding elements compare equal.
902  */
903  template<typename _Key, typename _Compare, typename _Alloc>
904  inline bool
906  const set<_Key, _Compare, _Alloc>& __y)
907  { return __x._M_t == __y._M_t; }
908 
909  /**
910  * @brief Set ordering relation.
911  * @param __x A %set.
912  * @param __y A %set of the same type as @a x.
913  * @return True iff @a __x is lexicographically less than @a __y.
914  *
915  * This is a total ordering relation. It is linear in the size of the
916  * sets. The elements must be comparable with @c <.
917  *
918  * See std::lexicographical_compare() for how the determination is made.
919  */
920  template<typename _Key, typename _Compare, typename _Alloc>
921  inline bool
922  operator<(const set<_Key, _Compare, _Alloc>& __x,
923  const set<_Key, _Compare, _Alloc>& __y)
924  { return __x._M_t < __y._M_t; }
925 
926  /// Returns !(x == y).
927  template<typename _Key, typename _Compare, typename _Alloc>
928  inline bool
930  const set<_Key, _Compare, _Alloc>& __y)
931  { return !(__x == __y); }
932 
933  /// Returns y < x.
934  template<typename _Key, typename _Compare, typename _Alloc>
935  inline bool
937  const set<_Key, _Compare, _Alloc>& __y)
938  { return __y < __x; }
939 
940  /// Returns !(y < x)
941  template<typename _Key, typename _Compare, typename _Alloc>
942  inline bool
943  operator<=(const set<_Key, _Compare, _Alloc>& __x,
944  const set<_Key, _Compare, _Alloc>& __y)
945  { return !(__y < __x); }
946 
947  /// Returns !(x < y)
948  template<typename _Key, typename _Compare, typename _Alloc>
949  inline bool
951  const set<_Key, _Compare, _Alloc>& __y)
952  { return !(__x < __y); }
953 
954  /// See std::set::swap().
955  template<typename _Key, typename _Compare, typename _Alloc>
956  inline void
958  _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
959  { __x.swap(__y); }
960 
961 _GLIBCXX_END_NAMESPACE_CONTAINER
962 
963 #if __cplusplus > 201402L
964 _GLIBCXX_BEGIN_NAMESPACE_VERSION
965  // Allow std::set access to internals of compatible sets.
966  template<typename _Val, typename _Cmp1, typename _Alloc, typename _Cmp2>
967  struct
968  _Rb_tree_merge_helper<_GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>, _Cmp2>
969  {
970  private:
971  friend class _GLIBCXX_STD_C::set<_Val, _Cmp1, _Alloc>;
972 
973  static auto&
974  _S_get_tree(_GLIBCXX_STD_C::set<_Val, _Cmp2, _Alloc>& __set)
975  { return __set._M_t; }
976 
977  static auto&
978  _S_get_tree(_GLIBCXX_STD_C::multiset<_Val, _Cmp2, _Alloc>& __set)
979  { return __set._M_t; }
980  };
981 _GLIBCXX_END_NAMESPACE_VERSION
982 #endif // C++17
983 
984 } //namespace std
985 #endif /* _STL_SET_H */
std::pair< iterator, bool > emplace(_Args &&... __args)
Attempts to build and insert an element into the set.
Definition: stl_set.h:444
value_compare value_comp() const
Returns the comparison object with which the set was constructed.
Definition: stl_set.h:313
A standard container made up of unique keys, which can be retrieved in logarithmic time...
Definition: stl_multiset.h:69
_Alloc allocator_type
Public typedefs.
Definition: stl_set.h:110
_Rep_type::const_iterator const_iterator
Iterator-related typedefs.
Definition: stl_set.h:134
auto lower_bound(const _Kt &__x) const -> decltype(_M_t._M_lower_bound_tr(__x))
Finds the beginning of a subsequence matching given key.
Definition: stl_set.h:808
_T2 second
first is a copy of the first object
Definition: stl_pair.h:196
bool operator>(const set< _Key, _Compare, _Alloc > &__x, const set< _Key, _Compare, _Alloc > &__y)
Returns y < x.
Definition: stl_set.h:936
reverse_iterator rbegin() const noexcept
Definition: stl_set.h:344
_GLIBCXX_ABI_TAG_CXX11 iterator erase(const_iterator __position)
Erases an element from a set.
Definition: stl_set.h:636
auto upper_bound(const _Kt &__x) const -> decltype(_M_t._M_upper_bound_tr(__x))
Finds the end of a subsequence matching given key.
Definition: stl_set.h:838
auto equal_range(const _Kt &__x) -> decltype(_M_t._M_equal_range_tr(__x))
Finds a subsequence matching given key.
Definition: stl_set.h:871
_Alloc_traits::const_reference const_reference
Iterator-related typedefs.
Definition: stl_set.h:129
auto find(const _Kt &__x) const -> decltype(const_iterator
Tries to locate an element in a set.
Definition: stl_set.h:773
Uniform interface to C++98 and C++11 allocators.
auto count(const _Kt &__x) const -> decltype(_M_t._M_count_tr(__x))
Finds the number of elements.
Definition: stl_set.h:736
std::pair< iterator, bool > insert(const value_type &__x)
Attempts to insert an element into the set.
Definition: stl_set.h:491
_Key value_type
Public typedefs.
Definition: stl_set.h:107
iterator upper_bound(const key_type &__x)
Finds the end of a subsequence matching given key.
Definition: stl_set.h:822
initializer_list
std::pair< iterator, iterator > equal_range(const key_type &__x)
Finds a subsequence matching given key.
Definition: stl_set.h:861
_Alloc_traits::reference reference
Iterator-related typedefs.
Definition: stl_set.h:128
ISO C++ entities toplevel namespace is std.
const_iterator find(const key_type &__x) const
Tries to locate an element in a set.
Definition: stl_set.h:761
auto find(const _Kt &__x) -> decltype(iterator
Tries to locate an element in a set.
Definition: stl_set.h:767
iterator end() const noexcept
Definition: stl_set.h:335
_GLIBCXX_ABI_TAG_CXX11 iterator erase(const_iterator __first, const_iterator __last)
Erases a [__first,__last) range of elements from a set.
Definition: stl_set.h:688
void swap(set &__x) noexcept(/*conditional */)
Swaps data with another set.
Definition: stl_set.h:423
auto equal_range(const _Kt &__x) const -> decltype(_M_t._M_equal_range_tr(__x))
Finds a subsequence matching given key.
Definition: stl_set.h:877
size_type count(const key_type &__x) const
Finds the number of elements.
Definition: stl_set.h:730
size_type erase(const key_type &__x)
Erases elements according to the provided key.
Definition: stl_set.h:666
GNU extensions for public use.
bool operator==(const set< _Key, _Compare, _Alloc > &__x, const set< _Key, _Compare, _Alloc > &__y)
Set equality comparison.
Definition: stl_set.h:905
key_compare key_comp() const
Returns the comparison object with which the set was constructed.
Definition: stl_set.h:309
auto upper_bound(const _Kt &__x) -> decltype(_M_t._M_upper_bound_tr(__x))
Finds the end of a subsequence matching given key.
Definition: stl_set.h:832
_Rep_type::size_type size_type
Iterator-related typedefs.
Definition: stl_set.h:137
iterator cbegin() const noexcept
Definition: stl_set.h:363
set & operator=(initializer_list< value_type > __l)
Set list assignment operator.
Definition: stl_set.h:298
void insert(_InputIterator __first, _InputIterator __last)
A template function that attempts to insert a range of elements.
Definition: stl_set.h:548
iterator begin() const noexcept
Definition: stl_set.h:326
_Compare key_compare
Public typedefs.
Definition: stl_set.h:108
reverse_iterator rend() const noexcept
Definition: stl_set.h:353
allocator_type get_allocator() const noexcept
Returns the allocator object with which the set was constructed.
Definition: stl_set.h:317
bool operator!=(const set< _Key, _Compare, _Alloc > &__x, const set< _Key, _Compare, _Alloc > &__y)
Returns !(x == y).
Definition: stl_set.h:929
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:190
size_type size() const noexcept
Returns the size of the set.
Definition: stl_set.h:401
const_iterator upper_bound(const key_type &__x) const
Finds the end of a subsequence matching given key.
Definition: stl_set.h:826
void clear() noexcept
Definition: stl_set.h:715
iterator find(const key_type &__x)
Tries to locate an element in a set.
Definition: stl_set.h:757
_Alloc_traits::pointer pointer
Iterator-related typedefs.
Definition: stl_set.h:126
const_iterator lower_bound(const key_type &__x) const
Finds the beginning of a subsequence matching given key.
Definition: stl_set.h:796
A standard container made up of elements, which can be retrieved in logarithmic time.
Definition: stl_multiset.h:95
reverse_iterator crbegin() const noexcept
Definition: stl_set.h:381
iterator lower_bound(const key_type &__x)
Finds the beginning of a subsequence matching given key.
Definition: stl_set.h:792
iterator cend() const noexcept
Definition: stl_set.h:372
iterator emplace_hint(const_iterator __pos, _Args &&... __args)
Attempts to insert an element into the set.
Definition: stl_set.h:470
_Rep_type::difference_type difference_type
Iterator-related typedefs.
Definition: stl_set.h:138
_Alloc_traits::const_pointer const_pointer
Iterator-related typedefs.
Definition: stl_set.h:127
auto lower_bound(const _Kt &__x) -> decltype(_M_t._M_lower_bound_tr(__x))
Finds the beginning of a subsequence matching given key.
Definition: stl_set.h:802
iterator insert(const_iterator __position, const value_type &__x)
Attempts to insert an element into the set.
Definition: stl_set.h:528
size_type max_size() const noexcept
Returns the maximum size of the set.
Definition: stl_set.h:406
_Rep_type::const_iterator iterator
Iterator-related typedefs.
Definition: stl_set.h:133
_Compare value_compare
Public typedefs.
Definition: stl_set.h:109
std::pair< const_iterator, const_iterator > equal_range(const key_type &__x) const
Finds a subsequence matching given key.
Definition: stl_set.h:865
bool empty() const noexcept
Returns true if the set is empty.
Definition: stl_set.h:396
set & operator=(const set &__x)
Set assignment operator.
Definition: stl_set.h:275
void insert(initializer_list< value_type > __l)
Attempts to insert a list of elements into the set.
Definition: stl_set.h:560
reverse_iterator crend() const noexcept
Definition: stl_set.h:390
bool operator>=(const set< _Key, _Compare, _Alloc > &__x, const set< _Key, _Compare, _Alloc > &__y)
Returns !(x < y)
Definition: stl_set.h:950
_Key key_type
Public typedefs.
Definition: stl_set.h:106
_T1 first
second_type is the second bound type
Definition: stl_pair.h:195