OSDN Git Service

2009-11-17 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / profile / vector
1 // Profiling vector implementation -*- C++ -*-
2
3 // Copyright (C) 2009 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 2, 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 // 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,
19 // USA.
20
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.
29
30 /** @file profile/vector
31  *  This file is a GNU profile extension to the Standard C++ Library.
32  */
33
34 #ifndef _GLIBCXX_PROFILE_VECTOR
35 #define _GLIBCXX_PROFILE_VECTOR 1
36
37 #include <vector>
38 #include <utility>
39 #include <profile/base.h>
40
41 namespace std
42 {
43 namespace __profile
44 {
45   /// Class std::vector wrapper with performance instrumentation.
46   template<typename _Tp,
47            typename _Allocator = std::allocator<_Tp> >
48     class vector
49     : public _GLIBCXX_STD_PR::vector<_Tp, _Allocator>
50     {
51       typedef _GLIBCXX_STD_PR::vector<_Tp, _Allocator> _Base;
52
53     public:
54       typedef typename _Base::reference             reference;
55       typedef typename _Base::const_reference       const_reference;
56
57       typedef typename _Base::iterator iterator;
58       typedef typename _Base::const_iterator const_iterator;
59
60       typedef typename _Base::size_type             size_type;
61       typedef typename _Base::difference_type       difference_type;
62
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
68       _Base&
69       _M_base()       { return *this; }
70
71       const _Base&
72       _M_base() const { return *this; }
73
74       // 23.2.4.1 construct/copy/destroy:
75       explicit vector(const _Allocator& __a = _Allocator())
76       : _Base(__a)
77       { 
78         __profcxx_vector_construct(this, this->capacity());
79         __profcxx_vector_construct2(this);
80       }
81
82       explicit vector(size_type __n, const _Tp& __value = _Tp(),
83                       const _Allocator& __a = _Allocator())
84       :  _Base(__n, __value, __a)
85       { 
86         __profcxx_vector_construct(this, this->capacity());
87         __profcxx_vector_construct2(this);
88       }
89
90       template<class _InputIterator>
91         vector(_InputIterator __first, _InputIterator __last,
92                const _Allocator& __a = _Allocator())
93         : _Base(__first, __last, __a)
94       { 
95         __profcxx_vector_construct(this, this->capacity());
96         __profcxx_vector_construct2(this);
97       }
98
99       vector(const vector& __x)
100       : _Base(__x) 
101       { 
102         __profcxx_vector_construct(this, this->capacity());
103         __profcxx_vector_construct2(this);
104       }
105
106       /// Construction from a release-mode vector
107       vector(const _Base& __x)
108       : _Base(__x) 
109       { 
110         __profcxx_vector_construct(this, this->capacity());
111         __profcxx_vector_construct2(this);
112       }
113
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
115       vector(vector&& __x)
116       : _Base(std::forward<vector>(__x))
117       {
118         __profcxx_vector_construct(this, this->capacity());
119         __profcxx_vector_construct2(this);
120       }
121
122       vector(initializer_list<value_type> __l,
123              const allocator_type& __a = allocator_type())
124       : _Base(__l, __a) { }
125 #endif
126
127       ~vector() {
128         __profcxx_vector_destruct(this, this->capacity(), this->size());
129         __profcxx_vector_destruct2(this);
130       }
131
132       vector&
133       operator=(const vector& __x)
134       {
135         static_cast<_Base&>(*this) = __x;
136         return *this;
137       }
138
139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
140       vector&
141       operator=(vector&& __x)
142       {
143         // NB: DR 675.
144         this->clear();
145         this->swap(__x);
146         return *this;
147       }
148
149       vector&
150       operator=(initializer_list<value_type> __l)
151       {
152         static_cast<_Base&>(*this) = __l;
153         return *this;
154       }
155 #endif
156
157       using _Base::assign;
158       using _Base::get_allocator;
159
160       // 23.2.4.2 capacity:
161       using _Base::size;
162       using _Base::max_size;
163
164       void
165       resize(size_type __sz, _Tp __c = _Tp())
166       {
167         __profcxx_vector_invalid_operator(this);
168         _M_profile_resize(this, this->capacity(), __sz);
169         _Base::resize(__sz, __c);
170       }
171
172       using _Base::empty;
173
174       // element access:
175       reference
176       operator[](size_type __n)
177       {
178         __profcxx_vector_invalid_operator(this);
179         return _M_base()[__n];
180       }
181       const_reference
182       operator[](size_type __n) const
183       {
184         __profcxx_vector_invalid_operator(this);
185         return _M_base()[__n];
186       }
187
188       using _Base::at;
189
190       reference
191       front()
192       { 
193         return _Base::front();
194       }
195
196       const_reference
197       front() const
198       {
199         return _Base::front();
200       }
201
202       reference
203       back()
204       {
205         return _Base::back();
206       }
207
208       const_reference
209       back() const
210       {
211         return _Base::back();
212       }
213
214       // _GLIBCXX_RESOLVE_LIB_DEFECTS
215       // DR 464. Suggestion for new member functions in standard containers.
216       using _Base::data;
217
218       // 23.2.4.3 modifiers:
219       void
220       push_back(const _Tp& __x)
221       {
222         size_type __old_size = this->capacity();
223         _Base::push_back(__x);
224         _M_profile_resize(this, __old_size, this->capacity());
225       }
226
227 #ifdef __GXX_EXPERIMENTAL_CXX0X__
228       void
229       push_back(_Tp&& __x)
230       {
231         size_type __old_size = this->capacity();
232         _Base::push_back(__x);
233         _M_profile_resize(this, __old_size, this->capacity());
234       }
235
236 #endif
237
238       iterator
239       insert(iterator __position, const _Tp& __x)
240       {
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());
245         return __res;
246       }
247
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
249       iterator
250       insert(iterator __position, _Tp&& __x)
251       {
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());
256         return __res;
257       }
258
259       void
260       insert(iterator __position, initializer_list<value_type> __l)
261       { this->insert(__position, __l.begin(), __l.end()); }
262 #endif
263
264       void
265
266       swap(vector& __x)
267       {
268         _Base::swap(__x);
269       }
270
271
272       void
273       insert(iterator __position, size_type __n, const _Tp& __x)
274       {
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());
279       }
280
281       template<class _InputIterator>
282       void
283       insert(iterator __position,
284              _InputIterator __first, _InputIterator __last)
285       {
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());
290       }
291
292       void
293       clear()
294       {
295         __profcxx_vector_destruct(this, this->capacity(), this->size());
296         __profcxx_vector_destruct2(this);
297         _Base::clear();
298       }
299
300       // iterators:
301       iterator
302       begin()
303       { 
304         return _Base::begin(); 
305       }
306
307       const_iterator
308       begin() const
309       { 
310         return _Base::begin(); 
311       }
312
313     private:
314       void _M_profile_resize(void* obj, size_type __old_size, 
315                              size_type __new_size)
316       {
317         if (__old_size < __new_size) {
318           __profcxx_vector_resize(this, this->size(), __new_size);
319           __profcxx_vector_resize2(this, this->size(), __new_size);
320         }
321       }
322     };
323
324   template<typename _Tp, typename _Alloc>
325     inline bool
326     operator==(const vector<_Tp, _Alloc>& __lhs,
327            const vector<_Tp, _Alloc>& __rhs)
328     { return __lhs._M_base() == __rhs._M_base(); }
329
330   template<typename _Tp, typename _Alloc>
331     inline bool
332     operator!=(const vector<_Tp, _Alloc>& __lhs,
333            const vector<_Tp, _Alloc>& __rhs)
334     { return __lhs._M_base() != __rhs._M_base(); }
335
336   template<typename _Tp, typename _Alloc>
337     inline bool
338     operator<(const vector<_Tp, _Alloc>& __lhs,
339           const vector<_Tp, _Alloc>& __rhs)
340     { return __lhs._M_base() < __rhs._M_base(); }
341
342   template<typename _Tp, typename _Alloc>
343     inline bool
344     operator<=(const vector<_Tp, _Alloc>& __lhs,
345            const vector<_Tp, _Alloc>& __rhs)
346     { return __lhs._M_base() <= __rhs._M_base(); }
347
348   template<typename _Tp, typename _Alloc>
349     inline bool
350     operator>=(const vector<_Tp, _Alloc>& __lhs,
351            const vector<_Tp, _Alloc>& __rhs)
352     { return __lhs._M_base() >= __rhs._M_base(); }
353
354   template<typename _Tp, typename _Alloc>
355     inline bool
356     operator>(const vector<_Tp, _Alloc>& __lhs,
357           const vector<_Tp, _Alloc>& __rhs)
358     { return __lhs._M_base() > __rhs._M_base(); }
359
360   template<typename _Tp, typename _Alloc>
361     inline void
362     swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
363     { __lhs.swap(__rhs); }
364
365 } // namespace __profile
366   using _GLIBCXX_STD_D::_S_word_bit;
367 } // namespace std
368
369 #endif