libstdc++
profile/vector
Go to the documentation of this file.
1 // Profiling vector implementation -*- C++ -*-
2 
3 // Copyright (C) 2009, 2010 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 along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
23 
24 /** @file profile/vector
25  * This file is a GNU profile extension to the Standard C++ Library.
26  */
27 
28 #ifndef _GLIBCXX_PROFILE_VECTOR
29 #define _GLIBCXX_PROFILE_VECTOR 1
30 
31 #include <vector>
32 #include <utility>
33 #include <profile/base.h>
35 
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 namespace __profile
39 {
40  template<typename _Tp,
41  typename _Allocator = std::allocator<_Tp> >
42  class vector
43  : public _GLIBCXX_STD_C::vector<_Tp, _Allocator>
44  {
45  typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
46 
47  public:
48  typedef typename _Base::reference reference;
49  typedef typename _Base::const_reference const_reference;
50 
51  typedef __iterator_tracker<typename _Base::iterator, vector>
52  iterator;
53  typedef __iterator_tracker<typename _Base::const_iterator, vector>
54  const_iterator;
55 
56  typedef typename _Base::size_type size_type;
57  typedef typename _Base::difference_type difference_type;
58 
59  typedef _Tp value_type;
60  typedef _Allocator allocator_type;
61  typedef typename _Base::pointer pointer;
62  typedef typename _Base::const_pointer const_pointer;
63  typedef std::reverse_iterator<iterator> reverse_iterator;
64  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
65 
66  _Base&
67  _M_base() { return *this; }
68 
69  const _Base&
70  _M_base() const { return *this; }
71 
72  // 23.2.4.1 construct/copy/destroy:
73  explicit
74  vector(const _Allocator& __a = _Allocator())
75  : _Base(__a)
76  {
77  __profcxx_vector_construct(this, this->capacity());
78  __profcxx_vector_construct2(this);
79  }
80 
81 #ifdef __GXX_EXPERIMENTAL_CXX0X__
82  explicit
83  vector(size_type __n)
84  : _Base(__n)
85  {
86  __profcxx_vector_construct(this, this->capacity());
87  __profcxx_vector_construct2(this);
88  }
89 
90  vector(size_type __n, const _Tp& __value,
91  const _Allocator& __a = _Allocator())
92  : _Base(__n, __value, __a)
93  {
94  __profcxx_vector_construct(this, this->capacity());
95  __profcxx_vector_construct2(this);
96  }
97 #else
98  explicit
99  vector(size_type __n, const _Tp& __value = _Tp(),
100  const _Allocator& __a = _Allocator())
101  : _Base(__n, __value, __a)
102  {
103  __profcxx_vector_construct(this, this->capacity());
104  __profcxx_vector_construct2(this);
105  }
106 #endif
107 
108  template<class _InputIterator>
109  vector(_InputIterator __first, _InputIterator __last,
110  const _Allocator& __a = _Allocator())
111  : _Base(__first, __last, __a)
112  {
113  __profcxx_vector_construct(this, this->capacity());
114  __profcxx_vector_construct2(this);
115  }
116 
117  vector(const vector& __x)
118  : _Base(__x)
119  {
120  __profcxx_vector_construct(this, this->capacity());
121  __profcxx_vector_construct2(this);
122  }
123 
124  /// Construction from a release-mode vector
125  vector(const _Base& __x)
126  : _Base(__x)
127  {
128  __profcxx_vector_construct(this, this->capacity());
129  __profcxx_vector_construct2(this);
130  }
131 
132 #ifdef __GXX_EXPERIMENTAL_CXX0X__
133  vector(vector&& __x)
134  : _Base(std::move(__x))
135  {
136  __profcxx_vector_construct(this, this->capacity());
137  __profcxx_vector_construct2(this);
138  }
139 
140  vector(initializer_list<value_type> __l,
141  const allocator_type& __a = allocator_type())
142  : _Base(__l, __a) { }
143 #endif
144 
145  ~vector() {
146  __profcxx_vector_destruct(this, this->capacity(), this->size());
147  __profcxx_vector_destruct2(this);
148  }
149 
150  vector&
151  operator=(const vector& __x)
152  {
153  static_cast<_Base&>(*this) = __x;
154  return *this;
155  }
156 
157 #ifdef __GXX_EXPERIMENTAL_CXX0X__
158  vector&
159  operator=(vector&& __x)
160  {
161  // NB: DR 1204.
162  // NB: DR 675.
163  this->clear();
164  this->swap(__x);
165  return *this;
166  }
167 
168  vector&
169  operator=(initializer_list<value_type> __l)
170  {
171  static_cast<_Base&>(*this) = __l;
172  return *this;
173  }
174 #endif
175 
176  using _Base::assign;
177  using _Base::get_allocator;
178 
179 
180  // iterators:
181  iterator
182  begin()
183  { return iterator(_Base::begin(), this); }
184 
185  const_iterator
186  begin() const
187  { return const_iterator(_Base::begin(), this); }
188 
189  iterator
190  end()
191  { return iterator(_Base::end(), this); }
192 
193  const_iterator
194  end() const
195  { return const_iterator(_Base::end(), this); }
196 
197  reverse_iterator
198  rbegin()
199  { return reverse_iterator(end()); }
200 
201  const_reverse_iterator
202  rbegin() const
203  { return const_reverse_iterator(end()); }
204 
205  reverse_iterator
206  rend()
207  { return reverse_iterator(begin()); }
208 
209  const_reverse_iterator
210  rend() const
211  { return const_reverse_iterator(begin()); }
212 
213 #ifdef __GXX_EXPERIMENTAL_CXX0X__
214  const_iterator
215  cbegin() const
216  { return const_iterator(_Base::begin(), this); }
217 
218  const_iterator
219  cend() const
220  { return const_iterator(_Base::end(), this); }
221 
222  const_reverse_iterator
223  crbegin() const
224  { return const_reverse_iterator(end()); }
225 
226  const_reverse_iterator
227  crend() const
228  { return const_reverse_iterator(begin()); }
229 #endif
230 
231  // 23.2.4.2 capacity:
232  using _Base::size;
233  using _Base::max_size;
234 
235 #ifdef __GXX_EXPERIMENTAL_CXX0X__
236  void
237  resize(size_type __sz)
238  {
239  __profcxx_vector_invalid_operator(this);
240  _M_profile_resize(this, this->capacity(), __sz);
241  _Base::resize(__sz);
242  }
243 
244  void
245  resize(size_type __sz, const _Tp& __c)
246  {
247  __profcxx_vector_invalid_operator(this);
248  _M_profile_resize(this, this->capacity(), __sz);
249  _Base::resize(__sz, __c);
250  }
251 #else
252  void
253  resize(size_type __sz, _Tp __c = _Tp())
254  {
255  __profcxx_vector_invalid_operator(this);
256  _M_profile_resize(this, this->capacity(), __sz);
257  _Base::resize(__sz, __c);
258  }
259 #endif
260 
261 #ifdef __GXX_EXPERIMENTAL_CXX0X__
262  using _Base::shrink_to_fit;
263 #endif
264 
265  using _Base::empty;
266 
267  // element access:
268  reference
269  operator[](size_type __n)
270  {
271  __profcxx_vector_invalid_operator(this);
272  return _M_base()[__n];
273  }
274  const_reference
275  operator[](size_type __n) const
276  {
277  __profcxx_vector_invalid_operator(this);
278  return _M_base()[__n];
279  }
280 
281  using _Base::at;
282 
283  reference
284  front()
285  {
286  return _Base::front();
287  }
288 
289  const_reference
290  front() const
291  {
292  return _Base::front();
293  }
294 
295  reference
296  back()
297  {
298  return _Base::back();
299  }
300 
301  const_reference
302  back() const
303  {
304  return _Base::back();
305  }
306 
307  // _GLIBCXX_RESOLVE_LIB_DEFECTS
308  // DR 464. Suggestion for new member functions in standard containers.
309  using _Base::data;
310 
311  // 23.2.4.3 modifiers:
312  void
313  push_back(const _Tp& __x)
314  {
315  size_type __old_size = this->capacity();
316  _Base::push_back(__x);
317  _M_profile_resize(this, __old_size, this->capacity());
318  }
319 
320 #ifdef __GXX_EXPERIMENTAL_CXX0X__
321  void
322  push_back(_Tp&& __x)
323  {
324  size_type __old_size = this->capacity();
325  _Base::push_back(__x);
326  _M_profile_resize(this, __old_size, this->capacity());
327  }
328 
329 #endif
330 
331  iterator
332  insert(iterator __position, const _Tp& __x)
333  {
334  __profcxx_vector_insert(this, __position.base() - _Base::begin(),
335  this->size());
336  size_type __old_size = this->capacity();
337  typename _Base::iterator __res = _Base::insert(__position.base(), __x);
338  _M_profile_resize(this, __old_size, this->capacity());
339  return iterator(__res, this);
340  }
341 
342 #ifdef __GXX_EXPERIMENTAL_CXX0X__
343  iterator
344  insert(iterator __position, _Tp&& __x)
345  {
346  __profcxx_vector_insert(this, __position.base() - _Base::begin(),
347  this->size());
348  size_type __old_size = this->capacity();
349  typename _Base::iterator __res = _Base::insert(__position.base(), __x);
350  _M_profile_resize(this, __old_size, this->capacity());
351  return iterator(__res, this);
352  }
353 
354  void
355  insert(iterator __position, initializer_list<value_type> __l)
356  { this->insert(__position, __l.begin(), __l.end()); }
357 #endif
358 
359 #ifdef __GXX_EXPERIMENTAL_CXX0X__
360  void
361  swap(vector&& __x)
362  {
363  _Base::swap(__x);
364  }
365 #endif
366 
367  void
368  swap(vector& __x)
369  {
370  _Base::swap(__x);
371  }
372 
373  void
374  insert(iterator __position, size_type __n, const _Tp& __x)
375  {
376  __profcxx_vector_insert(this, __position.base() - _Base::begin(),
377  this->size());
378  size_type __old_size = this->capacity();
379  _Base::insert(__position, __n, __x);
380  _M_profile_resize(this, __old_size, this->capacity());
381  }
382 
383  template<class _InputIterator>
384  void
385  insert(iterator __position,
386  _InputIterator __first, _InputIterator __last)
387  {
388  __profcxx_vector_insert(this, __position.base()-_Base::begin(),
389  this->size());
390  size_type __old_size = this->capacity();
391  _Base::insert(__position, __first, __last);
392  _M_profile_resize(this, __old_size, this->capacity());
393  }
394 
395 
396  iterator
397  erase(iterator __position)
398  {
399  typename _Base::iterator __res = _Base::erase(__position.base());
400  return iterator(__res, this);
401  }
402 
403  iterator
404  erase(iterator __first, iterator __last)
405  {
406  // _GLIBCXX_RESOLVE_LIB_DEFECTS
407  // 151. can't currently clear() empty container
408  typename _Base::iterator __res = _Base::erase(__first.base(),
409  __last.base());
410  return iterator(__res, this);
411  }
412 
413  void
414  clear()
415  {
416  __profcxx_vector_destruct(this, this->capacity(), this->size());
417  __profcxx_vector_destruct2(this);
418  _Base::clear();
419  }
420 
421  inline void _M_profile_find() const
422  {
423  __profcxx_vector_find(this, size());
424  }
425 
426  inline void _M_profile_iterate(int __rewind = 0) const
427  {
428  __profcxx_vector_iterate(this);
429  }
430 
431  private:
432  void _M_profile_resize(void* obj, size_type __old_size,
433  size_type __new_size)
434  {
435  if (__old_size < __new_size) {
436  __profcxx_vector_resize(this, this->size(), __new_size);
437  __profcxx_vector_resize2(this, this->size(), __new_size);
438  }
439  }
440  };
441 
442  template<typename _Tp, typename _Alloc>
443  inline bool
444  operator==(const vector<_Tp, _Alloc>& __lhs,
445  const vector<_Tp, _Alloc>& __rhs)
446  { return __lhs._M_base() == __rhs._M_base(); }
447 
448  template<typename _Tp, typename _Alloc>
449  inline bool
450  operator!=(const vector<_Tp, _Alloc>& __lhs,
451  const vector<_Tp, _Alloc>& __rhs)
452  { return __lhs._M_base() != __rhs._M_base(); }
453 
454  template<typename _Tp, typename _Alloc>
455  inline bool
456  operator<(const vector<_Tp, _Alloc>& __lhs,
457  const vector<_Tp, _Alloc>& __rhs)
458  { return __lhs._M_base() < __rhs._M_base(); }
459 
460  template<typename _Tp, typename _Alloc>
461  inline bool
462  operator<=(const vector<_Tp, _Alloc>& __lhs,
463  const vector<_Tp, _Alloc>& __rhs)
464  { return __lhs._M_base() <= __rhs._M_base(); }
465 
466  template<typename _Tp, typename _Alloc>
467  inline bool
468  operator>=(const vector<_Tp, _Alloc>& __lhs,
469  const vector<_Tp, _Alloc>& __rhs)
470  { return __lhs._M_base() >= __rhs._M_base(); }
471 
472  template<typename _Tp, typename _Alloc>
473  inline bool
474  operator>(const vector<_Tp, _Alloc>& __lhs,
475  const vector<_Tp, _Alloc>& __rhs)
476  { return __lhs._M_base() > __rhs._M_base(); }
477 
478  template<typename _Tp, typename _Alloc>
479  inline void
480  swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
481  { __lhs.swap(__rhs); }
482 
483 #ifdef __GXX_EXPERIMENTAL_CXX0X__
484  template<typename _Tp, typename _Alloc>
485  inline void
486  swap(vector<_Tp, _Alloc>&& __lhs, vector<_Tp, _Alloc>& __rhs)
487  { __lhs.swap(__rhs); }
488 
489  template<typename _Tp, typename _Alloc>
490  inline void
491  swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>&& __rhs)
492  { __lhs.swap(__rhs); }
493 #endif
494 
495 } // namespace __profile
496 
497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
498  // DR 1182.
499  /// std::hash specialization for vector<bool>.
500  template<typename _Alloc>
501  struct hash<__profile::vector<bool, _Alloc>>
502  : public __hash_base<size_t, __profile::vector<bool, _Alloc>>
503  {
504  size_t
505  operator()(const __profile::vector<bool, _Alloc>& __b) const
507  (__b._M_base()); }
508  };
509 #endif
510 
511 } // namespace std
512 
513 #endif
reference operator[](size_t __position)
Array-indexing support.
Definition: bitset:1138
constexpr const _Tp * begin(initializer_list< _Tp > __ils)
Return an iterator pointing to the first element of the initilizer_list.
Primary class template hash.
Definition: system_error:112
constexpr size_t size() const
Returns the total number of bits.
Definition: bitset:1275
Sequential helper functions. This file is a GNU profile extension to the Standard C++ Library...
constexpr const _Tp * end(initializer_list< _Tp > __ils)
Return an iterator pointing to one past the last element of the initilizer_list.
A standard container which offers fixed time access to individual elements in any order...
Definition: stl_vector.h:180
The standard allocator, as per [20.4].Further details: http://gcc.gnu.org/onlinedocs/libstdc++/manual...
Definition: allocator.h:66