1 // Profiling vector implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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)
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.
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.
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/>.
24 /** @file profile/vector
25 * This file is a GNU profile extension to the Standard C++ Library.
28 #ifndef _GLIBCXX_PROFILE_VECTOR
29 #define _GLIBCXX_PROFILE_VECTOR 1
33 #include <profile/base.h>
34 #include <profile/iterator_tracker.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
40 template<typename _Tp,
41 typename _Allocator = std::allocator<_Tp> >
43 : public _GLIBCXX_STD_C::vector<_Tp, _Allocator>
45 typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
48 typedef typename _Base::reference reference;
49 typedef typename _Base::const_reference const_reference;
51 typedef __iterator_tracker<typename _Base::iterator, vector>
53 typedef __iterator_tracker<typename _Base::const_iterator, vector>
56 typedef typename _Base::size_type size_type;
57 typedef typename _Base::difference_type difference_type;
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;
67 _M_base() { return *this; }
70 _M_base() const { return *this; }
72 // 23.2.4.1 construct/copy/destroy:
74 vector(const _Allocator& __a = _Allocator())
77 __profcxx_vector_construct(this, this->capacity());
78 __profcxx_vector_construct2(this);
81 #ifdef __GXX_EXPERIMENTAL_CXX0X__
86 __profcxx_vector_construct(this, this->capacity());
87 __profcxx_vector_construct2(this);
90 vector(size_type __n, const _Tp& __value,
91 const _Allocator& __a = _Allocator())
92 : _Base(__n, __value, __a)
94 __profcxx_vector_construct(this, this->capacity());
95 __profcxx_vector_construct2(this);
99 vector(size_type __n, const _Tp& __value = _Tp(),
100 const _Allocator& __a = _Allocator())
101 : _Base(__n, __value, __a)
103 __profcxx_vector_construct(this, this->capacity());
104 __profcxx_vector_construct2(this);
108 template<class _InputIterator>
109 vector(_InputIterator __first, _InputIterator __last,
110 const _Allocator& __a = _Allocator())
111 : _Base(__first, __last, __a)
113 __profcxx_vector_construct(this, this->capacity());
114 __profcxx_vector_construct2(this);
117 vector(const vector& __x)
120 __profcxx_vector_construct(this, this->capacity());
121 __profcxx_vector_construct2(this);
124 /// Construction from a release-mode vector
125 vector(const _Base& __x)
128 __profcxx_vector_construct(this, this->capacity());
129 __profcxx_vector_construct2(this);
132 #ifdef __GXX_EXPERIMENTAL_CXX0X__
134 : _Base(std::move(__x))
136 __profcxx_vector_construct(this, this->capacity());
137 __profcxx_vector_construct2(this);
140 vector(initializer_list<value_type> __l,
141 const allocator_type& __a = allocator_type())
142 : _Base(__l, __a) { }
146 __profcxx_vector_destruct(this, this->capacity(), this->size());
147 __profcxx_vector_destruct2(this);
151 operator=(const vector& __x)
153 static_cast<_Base&>(*this) = __x;
157 #ifdef __GXX_EXPERIMENTAL_CXX0X__
159 operator=(vector&& __x)
169 operator=(initializer_list<value_type> __l)
171 static_cast<_Base&>(*this) = __l;
177 using _Base::get_allocator;
183 { return iterator(_Base::begin(), this); }
187 { return const_iterator(_Base::begin(), this); }
191 { return iterator(_Base::end(), this); }
195 { return const_iterator(_Base::end(), this); }
199 { return reverse_iterator(end()); }
201 const_reverse_iterator
203 { return const_reverse_iterator(end()); }
207 { return reverse_iterator(begin()); }
209 const_reverse_iterator
211 { return const_reverse_iterator(begin()); }
213 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 { return const_iterator(_Base::begin(), this); }
220 { return const_iterator(_Base::end(), this); }
222 const_reverse_iterator
224 { return const_reverse_iterator(end()); }
226 const_reverse_iterator
228 { return const_reverse_iterator(begin()); }
231 // 23.2.4.2 capacity:
233 using _Base::max_size;
235 #ifdef __GXX_EXPERIMENTAL_CXX0X__
237 resize(size_type __sz)
239 __profcxx_vector_invalid_operator(this);
240 _M_profile_resize(this, this->capacity(), __sz);
245 resize(size_type __sz, const _Tp& __c)
247 __profcxx_vector_invalid_operator(this);
248 _M_profile_resize(this, this->capacity(), __sz);
249 _Base::resize(__sz, __c);
253 resize(size_type __sz, _Tp __c = _Tp())
255 __profcxx_vector_invalid_operator(this);
256 _M_profile_resize(this, this->capacity(), __sz);
257 _Base::resize(__sz, __c);
261 #ifdef __GXX_EXPERIMENTAL_CXX0X__
262 using _Base::shrink_to_fit;
269 operator[](size_type __n)
271 __profcxx_vector_invalid_operator(this);
272 return _M_base()[__n];
275 operator[](size_type __n) const
277 __profcxx_vector_invalid_operator(this);
278 return _M_base()[__n];
286 return _Base::front();
292 return _Base::front();
298 return _Base::back();
304 return _Base::back();
307 // _GLIBCXX_RESOLVE_LIB_DEFECTS
308 // DR 464. Suggestion for new member functions in standard containers.
311 // 23.2.4.3 modifiers:
313 push_back(const _Tp& __x)
315 size_type __old_size = this->capacity();
316 _Base::push_back(__x);
317 _M_profile_resize(this, __old_size, this->capacity());
320 #ifdef __GXX_EXPERIMENTAL_CXX0X__
324 size_type __old_size = this->capacity();
325 _Base::push_back(__x);
326 _M_profile_resize(this, __old_size, this->capacity());
332 insert(iterator __position, const _Tp& __x)
334 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
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);
342 #ifdef __GXX_EXPERIMENTAL_CXX0X__
344 insert(iterator __position, _Tp&& __x)
346 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
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);
355 insert(iterator __position, initializer_list<value_type> __l)
356 { this->insert(__position, __l.begin(), __l.end()); }
359 #ifdef __GXX_EXPERIMENTAL_CXX0X__
374 insert(iterator __position, size_type __n, const _Tp& __x)
376 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
378 size_type __old_size = this->capacity();
379 _Base::insert(__position, __n, __x);
380 _M_profile_resize(this, __old_size, this->capacity());
383 template<class _InputIterator>
385 insert(iterator __position,
386 _InputIterator __first, _InputIterator __last)
388 __profcxx_vector_insert(this, __position.base()-_Base::begin(),
390 size_type __old_size = this->capacity();
391 _Base::insert(__position, __first, __last);
392 _M_profile_resize(this, __old_size, this->capacity());
397 erase(iterator __position)
399 typename _Base::iterator __res = _Base::erase(__position.base());
400 return iterator(__res, this);
404 erase(iterator __first, iterator __last)
406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
407 // 151. can't currently clear() empty container
408 typename _Base::iterator __res = _Base::erase(__first.base(),
410 return iterator(__res, this);
416 __profcxx_vector_destruct(this, this->capacity(), this->size());
417 __profcxx_vector_destruct2(this);
421 inline void _M_profile_find() const
423 __profcxx_vector_find(this, size());
426 inline void _M_profile_iterate(int __rewind = 0) const
428 __profcxx_vector_iterate(this);
432 void _M_profile_resize(void* obj, size_type __old_size,
433 size_type __new_size)
435 if (__old_size < __new_size) {
436 __profcxx_vector_resize(this, this->size(), __new_size);
437 __profcxx_vector_resize2(this, this->size(), __new_size);
442 template<typename _Tp, typename _Alloc>
444 operator==(const vector<_Tp, _Alloc>& __lhs,
445 const vector<_Tp, _Alloc>& __rhs)
446 { return __lhs._M_base() == __rhs._M_base(); }
448 template<typename _Tp, typename _Alloc>
450 operator!=(const vector<_Tp, _Alloc>& __lhs,
451 const vector<_Tp, _Alloc>& __rhs)
452 { return __lhs._M_base() != __rhs._M_base(); }
454 template<typename _Tp, typename _Alloc>
456 operator<(const vector<_Tp, _Alloc>& __lhs,
457 const vector<_Tp, _Alloc>& __rhs)
458 { return __lhs._M_base() < __rhs._M_base(); }
460 template<typename _Tp, typename _Alloc>
462 operator<=(const vector<_Tp, _Alloc>& __lhs,
463 const vector<_Tp, _Alloc>& __rhs)
464 { return __lhs._M_base() <= __rhs._M_base(); }
466 template<typename _Tp, typename _Alloc>
468 operator>=(const vector<_Tp, _Alloc>& __lhs,
469 const vector<_Tp, _Alloc>& __rhs)
470 { return __lhs._M_base() >= __rhs._M_base(); }
472 template<typename _Tp, typename _Alloc>
474 operator>(const vector<_Tp, _Alloc>& __lhs,
475 const vector<_Tp, _Alloc>& __rhs)
476 { return __lhs._M_base() > __rhs._M_base(); }
478 template<typename _Tp, typename _Alloc>
480 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
481 { __lhs.swap(__rhs); }
483 #ifdef __GXX_EXPERIMENTAL_CXX0X__
484 template<typename _Tp, typename _Alloc>
486 swap(vector<_Tp, _Alloc>&& __lhs, vector<_Tp, _Alloc>& __rhs)
487 { __lhs.swap(__rhs); }
489 template<typename _Tp, typename _Alloc>
491 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>&& __rhs)
492 { __lhs.swap(__rhs); }
495 } // namespace __profile
497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
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>>
505 operator()(const __profile::vector<bool, _Alloc>& __b) const
506 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()