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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file profile/vector
31 * This file is a GNU profile extension to the Standard C++ Library.
34 #ifndef _GLIBCXX_PROFILE_VECTOR
35 #define _GLIBCXX_PROFILE_VECTOR 1
39 #include <profile/base.h>
40 #include <profile/iterator_tracker.h>
46 template<typename _Tp,
47 typename _Allocator = std::allocator<_Tp> >
49 : public _GLIBCXX_STD_PR::vector<_Tp, _Allocator>
51 typedef _GLIBCXX_STD_PR::vector<_Tp, _Allocator> _Base;
54 typedef typename _Base::reference reference;
55 typedef typename _Base::const_reference const_reference;
57 typedef __iterator_tracker<typename _Base::iterator, vector>
59 typedef __iterator_tracker<typename _Base::const_iterator, vector>
62 typedef typename _Base::size_type size_type;
63 typedef typename _Base::difference_type difference_type;
65 typedef _Tp value_type;
66 typedef _Allocator allocator_type;
67 typedef typename _Base::pointer pointer;
68 typedef typename _Base::const_pointer const_pointer;
69 typedef std::reverse_iterator<iterator> reverse_iterator;
70 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 _M_base() { return *this; }
76 _M_base() const { return *this; }
78 // 23.2.4.1 construct/copy/destroy:
79 explicit vector(const _Allocator& __a = _Allocator())
82 __profcxx_vector_construct(this, this->capacity());
83 __profcxx_vector_construct2(this);
86 explicit vector(size_type __n, const _Tp& __value = _Tp(),
87 const _Allocator& __a = _Allocator())
88 : _Base(__n, __value, __a)
90 __profcxx_vector_construct(this, this->capacity());
91 __profcxx_vector_construct2(this);
94 template<class _InputIterator>
95 vector(_InputIterator __first, _InputIterator __last,
96 const _Allocator& __a = _Allocator())
97 : _Base(__first, __last, __a)
99 __profcxx_vector_construct(this, this->capacity());
100 __profcxx_vector_construct2(this);
103 vector(const vector& __x)
106 __profcxx_vector_construct(this, this->capacity());
107 __profcxx_vector_construct2(this);
110 /// Construction from a release-mode vector
111 vector(const _Base& __x)
114 __profcxx_vector_construct(this, this->capacity());
115 __profcxx_vector_construct2(this);
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
120 : _Base(std::forward<vector>(__x))
122 __profcxx_vector_construct(this, this->capacity());
123 __profcxx_vector_construct2(this);
126 vector(initializer_list<value_type> __l,
127 const allocator_type& __a = allocator_type())
128 : _Base(__l, __a) { }
132 __profcxx_vector_destruct(this, this->capacity(), this->size());
133 __profcxx_vector_destruct2(this);
137 operator=(const vector& __x)
139 static_cast<_Base&>(*this) = __x;
143 #ifdef __GXX_EXPERIMENTAL_CXX0X__
145 operator=(vector&& __x)
155 operator=(initializer_list<value_type> __l)
157 static_cast<_Base&>(*this) = __l;
163 using _Base::get_allocator;
169 { return iterator(_Base::begin(), this); }
173 { return const_iterator(_Base::begin(), this); }
177 { return iterator(_Base::end(), this); }
181 { return const_iterator(_Base::end(), this); }
185 { return reverse_iterator(end()); }
187 const_reverse_iterator
189 { return const_reverse_iterator(end()); }
193 { return reverse_iterator(begin()); }
195 const_reverse_iterator
197 { return const_reverse_iterator(begin()); }
199 #ifdef __GXX_EXPERIMENTAL_CXX0X__
202 { return const_iterator(_Base::begin(), this); }
206 { return const_iterator(_Base::end(), this); }
208 const_reverse_iterator
210 { return const_reverse_iterator(end()); }
212 const_reverse_iterator
214 { return const_reverse_iterator(begin()); }
217 // 23.2.4.2 capacity:
219 using _Base::max_size;
222 resize(size_type __sz, _Tp __c = _Tp())
224 __profcxx_vector_invalid_operator(this);
225 _M_profile_resize(this, this->capacity(), __sz);
226 _Base::resize(__sz, __c);
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
230 using _Base::shrink_to_fit;
237 operator[](size_type __n)
239 __profcxx_vector_invalid_operator(this);
240 return _M_base()[__n];
243 operator[](size_type __n) const
245 __profcxx_vector_invalid_operator(this);
246 return _M_base()[__n];
254 return _Base::front();
260 return _Base::front();
266 return _Base::back();
272 return _Base::back();
275 // _GLIBCXX_RESOLVE_LIB_DEFECTS
276 // DR 464. Suggestion for new member functions in standard containers.
279 // 23.2.4.3 modifiers:
281 push_back(const _Tp& __x)
283 size_type __old_size = this->capacity();
284 _Base::push_back(__x);
285 _M_profile_resize(this, __old_size, this->capacity());
288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
292 size_type __old_size = this->capacity();
293 _Base::push_back(__x);
294 _M_profile_resize(this, __old_size, this->capacity());
300 insert(iterator __position, const _Tp& __x)
302 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
304 size_type __old_size = this->capacity();
305 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
306 _M_profile_resize(this, __old_size, this->capacity());
307 return iterator(__res, this);
310 #ifdef __GXX_EXPERIMENTAL_CXX0X__
312 insert(iterator __position, _Tp&& __x)
314 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
316 size_type __old_size = this->capacity();
317 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
318 _M_profile_resize(this, __old_size, this->capacity());
319 return iterator(__res, this);
323 insert(iterator __position, initializer_list<value_type> __l)
324 { this->insert(__position, __l.begin(), __l.end()); }
327 #ifdef __GXX_EXPERIMENTAL_CXX0X__
342 insert(iterator __position, size_type __n, const _Tp& __x)
344 __profcxx_vector_insert(this, __position.base() - _Base::begin(),
346 size_type __old_size = this->capacity();
347 _Base::insert(__position, __n, __x);
348 _M_profile_resize(this, __old_size, this->capacity());
351 template<class _InputIterator>
353 insert(iterator __position,
354 _InputIterator __first, _InputIterator __last)
356 __profcxx_vector_insert(this, __position.base()-_Base::begin(),
358 size_type __old_size = this->capacity();
359 _Base::insert(__position, __first, __last);
360 _M_profile_resize(this, __old_size, this->capacity());
365 erase(iterator __position)
367 typename _Base::iterator __res = _Base::erase(__position.base());
368 return iterator(__res, this);
372 erase(iterator __first, iterator __last)
374 // _GLIBCXX_RESOLVE_LIB_DEFECTS
375 // 151. can't currently clear() empty container
376 typename _Base::iterator __res = _Base::erase(__first.base(),
378 return iterator(__res, this);
384 __profcxx_vector_destruct(this, this->capacity(), this->size());
385 __profcxx_vector_destruct2(this);
389 inline void _M_profile_find() const
391 __profcxx_vector_find(this, size());
394 inline void _M_profile_iterate(int __rewind = 0) const
396 __profcxx_vector_iterate(this);
400 void _M_profile_resize(void* obj, size_type __old_size,
401 size_type __new_size)
403 if (__old_size < __new_size) {
404 __profcxx_vector_resize(this, this->size(), __new_size);
405 __profcxx_vector_resize2(this, this->size(), __new_size);
410 template<typename _Tp, typename _Alloc>
412 operator==(const vector<_Tp, _Alloc>& __lhs,
413 const vector<_Tp, _Alloc>& __rhs)
414 { return __lhs._M_base() == __rhs._M_base(); }
416 template<typename _Tp, typename _Alloc>
418 operator!=(const vector<_Tp, _Alloc>& __lhs,
419 const vector<_Tp, _Alloc>& __rhs)
420 { return __lhs._M_base() != __rhs._M_base(); }
422 template<typename _Tp, typename _Alloc>
424 operator<(const vector<_Tp, _Alloc>& __lhs,
425 const vector<_Tp, _Alloc>& __rhs)
426 { return __lhs._M_base() < __rhs._M_base(); }
428 template<typename _Tp, typename _Alloc>
430 operator<=(const vector<_Tp, _Alloc>& __lhs,
431 const vector<_Tp, _Alloc>& __rhs)
432 { return __lhs._M_base() <= __rhs._M_base(); }
434 template<typename _Tp, typename _Alloc>
436 operator>=(const vector<_Tp, _Alloc>& __lhs,
437 const vector<_Tp, _Alloc>& __rhs)
438 { return __lhs._M_base() >= __rhs._M_base(); }
440 template<typename _Tp, typename _Alloc>
442 operator>(const vector<_Tp, _Alloc>& __lhs,
443 const vector<_Tp, _Alloc>& __rhs)
444 { return __lhs._M_base() > __rhs._M_base(); }
446 template<typename _Tp, typename _Alloc>
448 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
449 { __lhs.swap(__rhs); }
451 #ifdef __GXX_EXPERIMENTAL_CXX0X__
452 template<typename _Tp, typename _Alloc>
454 swap(vector<_Tp, _Alloc>&& __lhs, vector<_Tp, _Alloc>& __rhs)
455 { __lhs.swap(__rhs); }
457 template<typename _Tp, typename _Alloc>
459 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>&& __rhs)
460 { __lhs.swap(__rhs); }
463 } // namespace __profile
465 #ifdef __GXX_EXPERIMENTAL_CXX0X__
467 /// std::hash specialization for vector<bool>.
468 template<typename _Alloc>
469 struct hash<__profile::vector<bool, _Alloc>>
470 : public std::unary_function<__profile::vector<bool, _Alloc>, size_t>
473 operator()(const __profile::vector<bool, _Alloc>& __b) const
474 { return std::hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>()