1 // Profiling vector implementation -*- C++ -*-
3 // Copyright (C) 2009, 2010, 2011 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;
47 #ifdef __GXX_EXPERIMENTAL_CXX0X__
48 typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits;
52 typedef typename _Base::reference reference;
53 typedef typename _Base::const_reference const_reference;
55 typedef __iterator_tracker<typename _Base::iterator, vector>
57 typedef __iterator_tracker<typename _Base::const_iterator, vector>
60 typedef typename _Base::size_type size_type;
61 typedef typename _Base::difference_type difference_type;
63 typedef _Tp value_type;
64 typedef _Allocator allocator_type;
65 typedef typename _Base::pointer pointer;
66 typedef typename _Base::const_pointer const_pointer;
67 typedef std::reverse_iterator<iterator> reverse_iterator;
68 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
71 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
74 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
76 // 23.2.4.1 construct/copy/destroy:
78 vector(const _Allocator& __a = _Allocator())
81 __profcxx_vector_construct(this, this->capacity());
82 __profcxx_vector_construct2(this);
85 #ifdef __GXX_EXPERIMENTAL_CXX0X__
90 __profcxx_vector_construct(this, this->capacity());
91 __profcxx_vector_construct2(this);
94 vector(size_type __n, const _Tp& __value,
95 const _Allocator& __a = _Allocator())
96 : _Base(__n, __value, __a)
98 __profcxx_vector_construct(this, this->capacity());
99 __profcxx_vector_construct2(this);
103 vector(size_type __n, const _Tp& __value = _Tp(),
104 const _Allocator& __a = _Allocator())
105 : _Base(__n, __value, __a)
107 __profcxx_vector_construct(this, this->capacity());
108 __profcxx_vector_construct2(this);
112 template<class _InputIterator>
113 vector(_InputIterator __first, _InputIterator __last,
114 const _Allocator& __a = _Allocator())
115 : _Base(__first, __last, __a)
117 __profcxx_vector_construct(this, this->capacity());
118 __profcxx_vector_construct2(this);
121 vector(const vector& __x)
124 __profcxx_vector_construct(this, this->capacity());
125 __profcxx_vector_construct2(this);
128 /// Construction from a release-mode vector
129 vector(const _Base& __x)
132 __profcxx_vector_construct(this, this->capacity());
133 __profcxx_vector_construct2(this);
136 #ifdef __GXX_EXPERIMENTAL_CXX0X__
137 vector(vector&& __x) noexcept
138 : _Base(std::move(__x))
140 __profcxx_vector_construct(this, this->capacity());
141 __profcxx_vector_construct2(this);
144 vector(const _Base& __x, const _Allocator& __a)
147 __profcxx_vector_construct(this, this->capacity());
148 __profcxx_vector_construct2(this);
151 vector(vector&& __x, const _Allocator& __a) noexcept
152 : _Base(std::move(__x), __a)
154 __profcxx_vector_construct(this, this->capacity());
155 __profcxx_vector_construct2(this);
158 vector(initializer_list<value_type> __l,
159 const allocator_type& __a = allocator_type())
160 : _Base(__l, __a) { }
163 ~vector() _GLIBCXX_NOEXCEPT
165 __profcxx_vector_destruct(this, this->capacity(), this->size());
166 __profcxx_vector_destruct2(this);
170 operator=(const vector& __x)
172 static_cast<_Base&>(*this) = __x;
176 #ifdef __GXX_EXPERIMENTAL_CXX0X__
178 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
180 __profcxx_vector_destruct(this, this->capacity(), this->size());
181 __profcxx_vector_destruct2(this);
182 static_cast<_Base&>(*this) = std::move(__x);
187 operator=(initializer_list<value_type> __l)
189 static_cast<_Base&>(*this) = __l;
195 using _Base::get_allocator;
200 begin() _GLIBCXX_NOEXCEPT
201 { return iterator(_Base::begin(), this); }
204 begin() const _GLIBCXX_NOEXCEPT
205 { return const_iterator(_Base::begin(), this); }
208 end() _GLIBCXX_NOEXCEPT
209 { return iterator(_Base::end(), this); }
212 end() const _GLIBCXX_NOEXCEPT
213 { return const_iterator(_Base::end(), this); }
216 rbegin() _GLIBCXX_NOEXCEPT
217 { return reverse_iterator(end()); }
219 const_reverse_iterator
220 rbegin() const _GLIBCXX_NOEXCEPT
221 { return const_reverse_iterator(end()); }
224 rend() _GLIBCXX_NOEXCEPT
225 { return reverse_iterator(begin()); }
227 const_reverse_iterator
228 rend() const _GLIBCXX_NOEXCEPT
229 { return const_reverse_iterator(begin()); }
231 #ifdef __GXX_EXPERIMENTAL_CXX0X__
233 cbegin() const noexcept
234 { return const_iterator(_Base::begin(), this); }
237 cend() const noexcept
238 { return const_iterator(_Base::end(), this); }
240 const_reverse_iterator
241 crbegin() const noexcept
242 { return const_reverse_iterator(end()); }
244 const_reverse_iterator
245 crend() const noexcept
246 { return const_reverse_iterator(begin()); }
249 // 23.2.4.2 capacity:
251 using _Base::max_size;
253 #ifdef __GXX_EXPERIMENTAL_CXX0X__
255 resize(size_type __sz)
257 __profcxx_vector_invalid_operator(this);
258 _M_profile_resize(this, this->capacity(), __sz);
263 resize(size_type __sz, const _Tp& __c)
265 __profcxx_vector_invalid_operator(this);
266 _M_profile_resize(this, this->capacity(), __sz);
267 _Base::resize(__sz, __c);
271 resize(size_type __sz, _Tp __c = _Tp())
273 __profcxx_vector_invalid_operator(this);
274 _M_profile_resize(this, this->capacity(), __sz);
275 _Base::resize(__sz, __c);
279 #ifdef __GXX_EXPERIMENTAL_CXX0X__
280 using _Base::shrink_to_fit;
287 operator[](size_type __n)
289 __profcxx_vector_invalid_operator(this);
290 return _M_base()[__n];
293 operator[](size_type __n) const
295 __profcxx_vector_invalid_operator(this);
296 return _M_base()[__n];
304 return _Base::front();
310 return _Base::front();
316 return _Base::back();
322 return _Base::back();
325 // _GLIBCXX_RESOLVE_LIB_DEFECTS
326 // DR 464. Suggestion for new member functions in standard containers.
329 // 23.2.4.3 modifiers:
331 push_back(const _Tp& __x)
333 size_type __old_size = this->capacity();
334 _Base::push_back(__x);
335 _M_profile_resize(this, __old_size, this->capacity());
338 #ifdef __GXX_EXPERIMENTAL_CXX0X__
342 size_type __old_size = this->capacity();
343 _Base::push_back(std::move(__x));
344 _M_profile_resize(this, __old_size, this->capacity());
350 insert(iterator __position, const _Tp& __x)
352 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
354 size_type __old_size = this->capacity();
355 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
356 _M_profile_resize(this, __old_size, this->capacity());
357 return iterator(__res, this);
360 #ifdef __GXX_EXPERIMENTAL_CXX0X__
362 insert(iterator __position, _Tp&& __x)
364 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
366 size_type __old_size = this->capacity();
367 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
368 _M_profile_resize(this, __old_size, this->capacity());
369 return iterator(__res, this);
373 insert(iterator __position, initializer_list<value_type> __l)
374 { this->insert(__position, __l.begin(), __l.end()); }
377 #ifdef __GXX_EXPERIMENTAL_CXX0X__
387 #ifdef __GXX_EXPERIMENTAL_CXX0X__
388 noexcept(_Alloc_traits::_S_nothrow_swap())
395 insert(iterator __position, size_type __n, const _Tp& __x)
397 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
399 size_type __old_size = this->capacity();
400 _Base::insert(__position, __n, __x);
401 _M_profile_resize(this, __old_size, this->capacity());
404 template<class _InputIterator>
406 insert(iterator __position,
407 _InputIterator __first, _InputIterator __last)
409 __profcxx_vector_insert(this, __position.base()-_Base::begin(),
411 size_type __old_size = this->capacity();
412 _Base::insert(__position, __first, __last);
413 _M_profile_resize(this, __old_size, this->capacity());
418 erase(iterator __position)
420 typename _Base::iterator __res = _Base::erase(__position.base());
421 return iterator(__res, this);
425 erase(iterator __first, iterator __last)
427 // _GLIBCXX_RESOLVE_LIB_DEFECTS
428 // 151. can't currently clear() empty container
429 typename _Base::iterator __res = _Base::erase(__first.base(),
431 return iterator(__res, this);
435 clear() _GLIBCXX_NOEXCEPT
437 __profcxx_vector_destruct(this, this->capacity(), this->size());
438 __profcxx_vector_destruct2(this);
442 inline void _M_profile_find() const
444 __profcxx_vector_find(this, size());
447 inline void _M_profile_iterate(int __rewind = 0) const
449 __profcxx_vector_iterate(this);
453 void _M_profile_resize(void* obj, size_type __old_size,
454 size_type __new_size)
456 if (__old_size < __new_size) {
457 __profcxx_vector_resize(this, this->size(), __new_size);
458 __profcxx_vector_resize2(this, this->size(), __new_size);
463 template<typename _Tp, typename _Alloc>
465 operator==(const vector<_Tp, _Alloc>& __lhs,
466 const vector<_Tp, _Alloc>& __rhs)
467 { return __lhs._M_base() == __rhs._M_base(); }
469 template<typename _Tp, typename _Alloc>
471 operator!=(const vector<_Tp, _Alloc>& __lhs,
472 const vector<_Tp, _Alloc>& __rhs)
473 { return __lhs._M_base() != __rhs._M_base(); }
475 template<typename _Tp, typename _Alloc>
477 operator<(const vector<_Tp, _Alloc>& __lhs,
478 const vector<_Tp, _Alloc>& __rhs)
479 { return __lhs._M_base() < __rhs._M_base(); }
481 template<typename _Tp, typename _Alloc>
483 operator<=(const vector<_Tp, _Alloc>& __lhs,
484 const vector<_Tp, _Alloc>& __rhs)
485 { return __lhs._M_base() <= __rhs._M_base(); }
487 template<typename _Tp, typename _Alloc>
489 operator>=(const vector<_Tp, _Alloc>& __lhs,
490 const vector<_Tp, _Alloc>& __rhs)
491 { return __lhs._M_base() >= __rhs._M_base(); }
493 template<typename _Tp, typename _Alloc>
495 operator>(const vector<_Tp, _Alloc>& __lhs,
496 const vector<_Tp, _Alloc>& __rhs)
497 { return __lhs._M_base() > __rhs._M_base(); }
499 template<typename _Tp, typename _Alloc>
501 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
502 { __lhs.swap(__rhs); }
504 #ifdef __GXX_EXPERIMENTAL_CXX0X__
505 template<typename _Tp, typename _Alloc>
507 swap(vector<_Tp, _Alloc>&& __lhs, vector<_Tp, _Alloc>& __rhs)
508 { __lhs.swap(__rhs); }
510 template<typename _Tp, typename _Alloc>
512 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>&& __rhs)
513 { __lhs.swap(__rhs); }
516 } // namespace __profile
518 #ifdef __GXX_EXPERIMENTAL_CXX0X__
520 /// std::hash specialization for vector<bool>.
521 template<typename _Alloc>
522 struct hash<__profile::vector<bool, _Alloc>>
523 : public __hash_base<size_t, __profile::vector<bool, _Alloc>>
526 operator()(const __profile::vector<bool, _Alloc>& __b) const noexcept
527 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()