libstdc++
profile/set.h
Go to the documentation of this file.
1 // Profiling set implementation -*- C++ -*-
2 
3 // Copyright (C) 2009, 2010, 2011 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 /** @file profile/set.h
26  * This file is a GNU profile extension to the Standard C++ Library.
27  */
28 
29 #ifndef _GLIBCXX_PROFILE_SET_H
30 #define _GLIBCXX_PROFILE_SET_H 1
31 
32 #include <utility>
33 
34 namespace std _GLIBCXX_VISIBILITY(default)
35 {
36 namespace __profile
37 {
38  /// Class std::set wrapper with performance instrumentation.
39  template<typename _Key, typename _Compare = std::less<_Key>,
40  typename _Allocator = std::allocator<_Key> >
41  class set
42  : public _GLIBCXX_STD_C::set<_Key,_Compare,_Allocator>
43  {
44  typedef _GLIBCXX_STD_C::set<_Key, _Compare, _Allocator> _Base;
45 
46  public:
47  // types:
48  typedef _Key key_type;
49  typedef _Key value_type;
50  typedef _Compare key_compare;
51  typedef _Compare value_compare;
52  typedef _Allocator allocator_type;
53  typedef typename _Base::reference reference;
54  typedef typename _Base::const_reference const_reference;
55 
56  typedef typename _Base::iterator iterator;
57  typedef typename _Base::const_iterator const_iterator;
58  typedef typename _Base::reverse_iterator reverse_iterator;
59  typedef typename _Base::const_reverse_iterator const_reverse_iterator;
60 
61  typedef typename _Base::size_type size_type;
62  typedef typename _Base::difference_type difference_type;
63  typedef typename _Base::pointer pointer;
64  typedef typename _Base::const_pointer const_pointer;
65 
66  // 23.3.3.1 construct/copy/destroy:
67  explicit set(const _Compare& __comp = _Compare(),
68  const _Allocator& __a = _Allocator())
69  : _Base(__comp, __a) { }
70 
71  template<typename _InputIterator>
72  set(_InputIterator __first, _InputIterator __last,
73  const _Compare& __comp = _Compare(),
74  const _Allocator& __a = _Allocator())
75  : _Base(__first, __last, __comp, __a) { }
76 
77  set(const set& __x)
78  : _Base(__x) { }
79 
80  set(const _Base& __x)
81  : _Base(__x) { }
82 
83 #ifdef __GXX_EXPERIMENTAL_CXX0X__
84  set(set&& __x)
86  : _Base(std::move(__x))
87  { }
88 
90  const _Compare& __comp = _Compare(),
91  const allocator_type& __a = allocator_type())
92  : _Base(__l, __comp, __a) { }
93 #endif
94 
95  ~set() _GLIBCXX_NOEXCEPT { }
96 
97  set&
98  operator=(const set& __x)
99  {
100  *static_cast<_Base*>(this) = __x;
101  return *this;
102  }
103 
104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
105  set&
106  operator=(set&& __x)
107  {
108  // NB: DR 1204.
109  // NB: DR 675.
110  this->clear();
111  this->swap(__x);
112  return *this;
113  }
114 
115  set&
116  operator=(initializer_list<value_type> __l)
117  {
118  this->clear();
119  this->insert(__l);
120  return *this;
121  }
122 #endif
123 
124  using _Base::get_allocator;
125 
126  // iterators:
127  iterator
128  begin() _GLIBCXX_NOEXCEPT
129  { return iterator(_Base::begin()); }
130 
131  const_iterator
132  begin() const _GLIBCXX_NOEXCEPT
133  { return const_iterator(_Base::begin()); }
134 
135  iterator
136  end() _GLIBCXX_NOEXCEPT
137  { return iterator(_Base::end()); }
138 
139  const_iterator
140  end() const _GLIBCXX_NOEXCEPT
141  { return const_iterator(_Base::end()); }
142 
143  reverse_iterator
144  rbegin() _GLIBCXX_NOEXCEPT
145  { return reverse_iterator(end()); }
146 
147  const_reverse_iterator
148  rbegin() const _GLIBCXX_NOEXCEPT
149  { return const_reverse_iterator(end()); }
150 
151  reverse_iterator
152  rend() _GLIBCXX_NOEXCEPT
153  { return reverse_iterator(begin()); }
154 
155  const_reverse_iterator
156  rend() const _GLIBCXX_NOEXCEPT
157  { return const_reverse_iterator(begin()); }
158 
159 #ifdef __GXX_EXPERIMENTAL_CXX0X__
160  const_iterator
161  cbegin() const noexcept
162  { return const_iterator(_Base::begin()); }
163 
164  const_iterator
165  cend() const noexcept
166  { return const_iterator(_Base::end()); }
167 
168  const_reverse_iterator
169  crbegin() const noexcept
170  { return const_reverse_iterator(end()); }
171 
172  const_reverse_iterator
173  crend() const noexcept
174  { return const_reverse_iterator(begin()); }
175 #endif
176 
177  // capacity:
178  using _Base::empty;
179  using _Base::size;
180  using _Base::max_size;
181 
182  // modifiers:
184  insert(const value_type& __x)
185  {
186  typedef typename _Base::iterator _Base_iterator;
187  std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
188  return std::pair<iterator, bool>(iterator(__res.first),
189  __res.second);
190  }
191 
192 #ifdef __GXX_EXPERIMENTAL_CXX0X__
194  insert(value_type&& __x)
195  {
196  typedef typename _Base::iterator _Base_iterator;
198  = _Base::insert(std::move(__x));
199  return std::pair<iterator, bool>(iterator(__res.first),
200  __res.second);
201  }
202 #endif
203 
204  iterator
205  insert(const_iterator __position, const value_type& __x)
206  { return iterator(_Base::insert(__position, __x)); }
207 
208 #ifdef __GXX_EXPERIMENTAL_CXX0X__
209  iterator
210  insert(const_iterator __position, value_type&& __x)
211  { return iterator(_Base::insert(__position, std::move(__x))); }
212 #endif
213 
214  template <typename _InputIterator>
215  void
216  insert(_InputIterator __first, _InputIterator __last)
217  { _Base::insert(__first, __last); }
218 
219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
220  void
221  insert(initializer_list<value_type> __l)
222  { _Base::insert(__l); }
223 #endif
224 
225 #ifdef __GXX_EXPERIMENTAL_CXX0X__
226  iterator
227  erase(const_iterator __position)
228  { return iterator(_Base::erase(__position)); }
229 #else
230  void
231  erase(iterator __position)
232  { _Base::erase(__position); }
233 #endif
234 
235  size_type
236  erase(const key_type& __x)
237  {
238  iterator __victim = find(__x);
239  if (__victim == end())
240  return 0;
241  else
242  {
243  _Base::erase(__victim);
244  return 1;
245  }
246  }
247 
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
249  iterator
250  erase(const_iterator __first, const_iterator __last)
251  { return iterator(_Base::erase(__first, __last)); }
252 #else
253  void
254  erase(iterator __first, iterator __last)
255  { _Base::erase(__first, __last); }
256 #endif
257 
258  void
259  swap(set& __x)
260  { _Base::swap(__x); }
261 
262  void
263  clear() _GLIBCXX_NOEXCEPT
264  { this->erase(begin(), end()); }
265 
266  // observers:
267  using _Base::key_comp;
268  using _Base::value_comp;
269 
270  // set operations:
271  iterator
272  find(const key_type& __x)
273  { return iterator(_Base::find(__x)); }
274 
275  // _GLIBCXX_RESOLVE_LIB_DEFECTS
276  // 214. set::find() missing const overload
277  const_iterator
278  find(const key_type& __x) const
279  { return const_iterator(_Base::find(__x)); }
280 
281  using _Base::count;
282 
283  iterator
284  lower_bound(const key_type& __x)
285  { return iterator(_Base::lower_bound(__x)); }
286 
287  // _GLIBCXX_RESOLVE_LIB_DEFECTS
288  // 214. set::find() missing const overload
289  const_iterator
290  lower_bound(const key_type& __x) const
291  { return const_iterator(_Base::lower_bound(__x)); }
292 
293  iterator
294  upper_bound(const key_type& __x)
295  { return iterator(_Base::upper_bound(__x)); }
296 
297  // _GLIBCXX_RESOLVE_LIB_DEFECTS
298  // 214. set::find() missing const overload
299  const_iterator
300  upper_bound(const key_type& __x) const
301  { return const_iterator(_Base::upper_bound(__x)); }
302 
304  equal_range(const key_type& __x)
305  {
306  typedef typename _Base::iterator _Base_iterator;
308  _Base::equal_range(__x);
309  return std::make_pair(iterator(__res.first),
310  iterator(__res.second));
311  }
312 
313  // _GLIBCXX_RESOLVE_LIB_DEFECTS
314  // 214. set::find() missing const overload
316  equal_range(const key_type& __x) const
317  {
318  typedef typename _Base::const_iterator _Base_iterator;
320  _Base::equal_range(__x);
321  return std::make_pair(const_iterator(__res.first),
322  const_iterator(__res.second));
323  }
324 
325  _Base&
326  _M_base() _GLIBCXX_NOEXCEPT { return *this; }
327 
328  const _Base&
329  _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
330 
331  };
332 
333  template<typename _Key, typename _Compare, typename _Allocator>
334  inline bool
335  operator==(const set<_Key, _Compare, _Allocator>& __lhs,
336  const set<_Key, _Compare, _Allocator>& __rhs)
337  { return __lhs._M_base() == __rhs._M_base(); }
338 
339  template<typename _Key, typename _Compare, typename _Allocator>
340  inline bool
341  operator!=(const set<_Key, _Compare, _Allocator>& __lhs,
342  const set<_Key, _Compare, _Allocator>& __rhs)
343  { return __lhs._M_base() != __rhs._M_base(); }
344 
345  template<typename _Key, typename _Compare, typename _Allocator>
346  inline bool
347  operator<(const set<_Key, _Compare, _Allocator>& __lhs,
348  const set<_Key, _Compare, _Allocator>& __rhs)
349  { return __lhs._M_base() < __rhs._M_base(); }
350 
351  template<typename _Key, typename _Compare, typename _Allocator>
352  inline bool
353  operator<=(const set<_Key, _Compare, _Allocator>& __lhs,
354  const set<_Key, _Compare, _Allocator>& __rhs)
355  { return __lhs._M_base() <= __rhs._M_base(); }
356 
357  template<typename _Key, typename _Compare, typename _Allocator>
358  inline bool
359  operator>=(const set<_Key, _Compare, _Allocator>& __lhs,
360  const set<_Key, _Compare, _Allocator>& __rhs)
361  { return __lhs._M_base() >= __rhs._M_base(); }
362 
363  template<typename _Key, typename _Compare, typename _Allocator>
364  inline bool
365  operator>(const set<_Key, _Compare, _Allocator>& __lhs,
366  const set<_Key, _Compare, _Allocator>& __rhs)
367  { return __lhs._M_base() > __rhs._M_base(); }
368 
369  template<typename _Key, typename _Compare, typename _Allocator>
370  void
371  swap(set<_Key, _Compare, _Allocator>& __x,
372  set<_Key, _Compare, _Allocator>& __y)
373  { return __x.swap(__y); }
374 
375 } // namespace __profile
376 } // namespace std
377 
378 #endif