1 // Profiling vector implementation -*- C++ -*-
3 // Copyright (C) 2009 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>
45 /// Class std::vector wrapper with performance instrumentation.
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 typename _Base::iterator iterator;
58 typedef typename _Base::const_iterator const_iterator;
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;
69 _M_base() { return *this; }
72 _M_base() const { return *this; }
74 // 23.2.4.1 construct/copy/destroy:
75 explicit vector(const _Allocator& __a = _Allocator())
78 __profcxx_vector_construct(this, this->capacity());
79 __profcxx_vector_construct2(this);
82 explicit vector(size_type __n, const _Tp& __value = _Tp(),
83 const _Allocator& __a = _Allocator())
84 : _Base(__n, __value, __a)
86 __profcxx_vector_construct(this, this->capacity());
87 __profcxx_vector_construct2(this);
90 template<class _InputIterator>
91 vector(_InputIterator __first, _InputIterator __last,
92 const _Allocator& __a = _Allocator())
93 : _Base(__first, __last, __a)
95 __profcxx_vector_construct(this, this->capacity());
96 __profcxx_vector_construct2(this);
99 vector(const vector& __x)
102 __profcxx_vector_construct(this, this->capacity());
103 __profcxx_vector_construct2(this);
106 /// Construction from a release-mode vector
107 vector(const _Base& __x)
110 __profcxx_vector_construct(this, this->capacity());
111 __profcxx_vector_construct2(this);
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
116 : _Base(std::forward<vector>(__x))
118 __profcxx_vector_construct(this, this->capacity());
119 __profcxx_vector_construct2(this);
122 vector(initializer_list<value_type> __l,
123 const allocator_type& __a = allocator_type())
124 : _Base(__l, __a) { }
128 __profcxx_vector_destruct(this, this->capacity(), this->size());
129 __profcxx_vector_destruct2(this);
133 operator=(const vector& __x)
135 static_cast<_Base&>(*this) = __x;
139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
141 operator=(vector&& __x)
150 operator=(initializer_list<value_type> __l)
152 static_cast<_Base&>(*this) = __l;
158 using _Base::get_allocator;
160 // 23.2.4.2 capacity:
162 using _Base::max_size;
165 resize(size_type __sz, _Tp __c = _Tp())
167 __profcxx_vector_invalid_operator(this);
168 _M_profile_resize(this, this->capacity(), __sz);
169 _Base::resize(__sz, __c);
176 operator[](size_type __n)
178 __profcxx_vector_invalid_operator(this);
179 return _M_base()[__n];
182 operator[](size_type __n) const
184 __profcxx_vector_invalid_operator(this);
185 return _M_base()[__n];
193 return _Base::front();
199 return _Base::front();
205 return _Base::back();
211 return _Base::back();
214 // _GLIBCXX_RESOLVE_LIB_DEFECTS
215 // DR 464. Suggestion for new member functions in standard containers.
218 // 23.2.4.3 modifiers:
220 push_back(const _Tp& __x)
222 size_type __old_size = this->capacity();
223 _Base::push_back(__x);
224 _M_profile_resize(this, __old_size, this->capacity());
227 #ifdef __GXX_EXPERIMENTAL_CXX0X__
231 size_type __old_size = this->capacity();
232 _Base::push_back(__x);
233 _M_profile_resize(this, __old_size, this->capacity());
239 insert(iterator __position, const _Tp& __x)
241 __profcxx_vector_insert(this, __position-_Base::begin(), this->size());
242 size_type __old_size = this->capacity();
243 typename _Base::iterator __res = _Base::insert(__position,__x);
244 _M_profile_resize(this, __old_size, this->capacity());
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
250 insert(iterator __position, _Tp&& __x)
252 __profcxx_vector_insert(this, __position-_Base::begin(), this->size());
253 size_type __old_size = this->capacity();
254 typename _Base::iterator __res = _Base::insert(__position,__x);
255 _M_profile_resize(this, __old_size, this->capacity());
260 insert(iterator __position, initializer_list<value_type> __l)
261 { this->insert(__position, __l.begin(), __l.end()); }
273 insert(iterator __position, size_type __n, const _Tp& __x)
275 __profcxx_vector_insert(this, __position-_Base::begin(), this->size());
276 size_type __old_size = this->capacity();
277 _Base::insert(__position, __n, __x);
278 _M_profile_resize(this, __old_size, this->capacity());
281 template<class _InputIterator>
283 insert(iterator __position,
284 _InputIterator __first, _InputIterator __last)
286 __profcxx_vector_insert(this, __position-_Base::begin(), this->size());
287 size_type __old_size = this->capacity();
288 _Base::insert(__position, __first, __last);
289 _M_profile_resize(this, __old_size, this->capacity());
295 __profcxx_vector_destruct(this, this->capacity(), this->size());
296 __profcxx_vector_destruct2(this);
304 return _Base::begin();
310 return _Base::begin();
314 void _M_profile_resize(void* obj, size_type __old_size,
315 size_type __new_size)
317 if (__old_size < __new_size) {
318 __profcxx_vector_resize(this, this->size(), __new_size);
319 __profcxx_vector_resize2(this, this->size(), __new_size);
324 template<typename _Tp, typename _Alloc>
326 operator==(const vector<_Tp, _Alloc>& __lhs,
327 const vector<_Tp, _Alloc>& __rhs)
328 { return __lhs._M_base() == __rhs._M_base(); }
330 template<typename _Tp, typename _Alloc>
332 operator!=(const vector<_Tp, _Alloc>& __lhs,
333 const vector<_Tp, _Alloc>& __rhs)
334 { return __lhs._M_base() != __rhs._M_base(); }
336 template<typename _Tp, typename _Alloc>
338 operator<(const vector<_Tp, _Alloc>& __lhs,
339 const vector<_Tp, _Alloc>& __rhs)
340 { return __lhs._M_base() < __rhs._M_base(); }
342 template<typename _Tp, typename _Alloc>
344 operator<=(const vector<_Tp, _Alloc>& __lhs,
345 const vector<_Tp, _Alloc>& __rhs)
346 { return __lhs._M_base() <= __rhs._M_base(); }
348 template<typename _Tp, typename _Alloc>
350 operator>=(const vector<_Tp, _Alloc>& __lhs,
351 const vector<_Tp, _Alloc>& __rhs)
352 { return __lhs._M_base() >= __rhs._M_base(); }
354 template<typename _Tp, typename _Alloc>
356 operator>(const vector<_Tp, _Alloc>& __lhs,
357 const vector<_Tp, _Alloc>& __rhs)
358 { return __lhs._M_base() > __rhs._M_base(); }
360 template<typename _Tp, typename _Alloc>
362 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
363 { __lhs.swap(__rhs); }
365 } // namespace __profile
366 using _GLIBCXX_STD_D::_S_word_bit;