]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/_vector.c
Add STLport 5.1.4
[polintos/scott/priv.git] / include / c++ / stl / stl / _vector.c
1 /*
2  *
3  *
4  * Copyright (c) 1994
5  * Hewlett-Packard Company
6  *
7  * Copyright (c) 1996,1997
8  * Silicon Graphics Computer Systems, Inc.
9  *
10  * Copyright (c) 1997
11  * Moscow Center for SPARC Technology
12  *
13  * Copyright (c) 1999
14  * Boris Fomitchev
15  *
16  * This material is provided "as is", with absolutely no warranty expressed
17  * or implied. Any use is at your own risk.
18  *
19  * Permission to use or copy this software for any purpose is hereby granted
20  * without fee, provided the above notices are retained on all copies.
21  * Permission to modify the code and to distribute modified code is granted,
22  * provided the above notices are retained, and a notice that the code was
23  * modified is included with the above copyright notice.
24  *
25  */
26 #ifndef _STLP_VECTOR_C
27 #define _STLP_VECTOR_C
28
29 #if !defined (_STLP_INTERNAL_VECTOR_H)
30 #  include <stl/_vector.h>
31 #endif
32
33 #include <stl/_range_errors.h>
34
35 _STLP_BEGIN_NAMESPACE
36
37 _STLP_MOVE_TO_PRIV_NAMESPACE
38
39 template <class _Tp, class _Alloc>
40 void _Vector_base<_Tp,_Alloc>::_M_throw_length_error() const {
41   __stl_throw_length_error("vector");
42 }
43
44 template <class _Tp, class _Alloc>
45 void _Vector_base<_Tp, _Alloc>::_M_throw_out_of_range() const {
46   __stl_throw_out_of_range("vector");
47 }
48
49 #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
50 #  define vector _STLP_PTR_IMPL_NAME(vector)
51 #elif defined (_STLP_DEBUG)
52 #  define vector _STLP_NON_DBG_NAME(vector)
53 #else
54 _STLP_MOVE_TO_STD_NAMESPACE
55 #endif
56
57 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
58 #  define __iterator__  _Tp*
59 #else
60 #  define __iterator__  _STLP_TYPENAME_ON_RETURN_TYPE vector<_Tp, _Alloc>::iterator
61 #endif
62
63 template <class _Tp, class _Alloc>
64 void vector<_Tp, _Alloc>::reserve(size_type __n) {
65   if (capacity() < __n) {
66     if (max_size() < __n) {
67       this->_M_throw_length_error();
68     }
69
70     const size_type __old_size = size();
71     pointer __tmp;
72     if (this->_M_start) {
73       __tmp = _M_allocate_and_copy(__n, this->_M_start, this->_M_finish);
74       _M_clear();
75     } else {
76       __tmp = this->_M_end_of_storage.allocate(__n, __n);
77     }
78     _M_set(__tmp, __tmp + __old_size, __tmp + __n);
79   }
80 }
81
82 template <class _Tp, class _Alloc>
83 void vector<_Tp, _Alloc>::_M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __false_type& /*DO NOT USE!!*/,
84                                                  size_type __fill_len, bool __atend ) {
85   const size_type __old_size = size();
86   size_type __len = __old_size + (max)(__old_size, __fill_len);
87
88   pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
89   pointer __new_finish = __new_start;
90   _STLP_TRY {
91     __new_finish = _STLP_PRIV __uninitialized_move(this->_M_start, __pos, __new_start, _TrivialUCopy(), _Movable());
92     // handle insertion
93     if (__fill_len == 1) {
94       _Copy_Construct(__new_finish, __x);
95       ++__new_finish;
96     } else
97       __new_finish = _STLP_PRIV __uninitialized_fill_n(__new_finish, __fill_len, __x);
98     if (!__atend)
99       __new_finish = _STLP_PRIV __uninitialized_move(__pos, this->_M_finish, __new_finish, _TrivialUCopy(), _Movable()); // copy remainder
100   }
101   _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
102                this->_M_end_of_storage.deallocate(__new_start,__len)))
103   _M_clear_after_move();
104   _M_set(__new_start, __new_finish, __new_start + __len);
105 }
106
107 template <class _Tp, class _Alloc>
108 void vector<_Tp, _Alloc>::_M_insert_overflow(pointer __pos, const _Tp& __x, const __true_type& /*_TrivialCopy*/,
109                                              size_type __fill_len, bool __atend ) {
110   const size_type __old_size = size();
111   size_type __len = __old_size + (max)(__old_size, __fill_len);
112
113   pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
114   pointer __new_finish = __STATIC_CAST(pointer, _STLP_PRIV __copy_trivial(this->_M_start, __pos, __new_start));
115   // handle insertion
116   __new_finish = _STLP_PRIV __fill_n(__new_finish, __fill_len, __x);
117   if (!__atend)
118     __new_finish = __STATIC_CAST(pointer, _STLP_PRIV __copy_trivial(__pos, this->_M_finish, __new_finish)); // copy remainder
119   _M_clear();
120   _M_set(__new_start, __new_finish, __new_start + __len);
121 }
122
123 template <class _Tp, class _Alloc>
124 void vector<_Tp, _Alloc>::_M_fill_insert_aux(iterator __pos, size_type __n,
125                                              const _Tp& __x, const __true_type& /*_Movable*/) {
126   if (_M_is_inside(__x)) {
127     _Tp __x_copy = __x;
128     _M_fill_insert_aux(__pos, __n, __x_copy, __true_type());
129     return;
130   }
131   iterator __src = this->_M_finish - 1;
132   iterator __dst = __src + __n;
133   for (; __src >= __pos; --__dst, --__src) {
134     _STLP_STD::_Move_Construct(__dst, *__src);
135     _STLP_STD::_Destroy_Moved(__src);
136   }
137   _STLP_PRIV __uninitialized_fill_n(__pos, __n, __x);
138   this->_M_finish += __n;
139 }
140
141 template <class _Tp, class _Alloc>
142 void vector<_Tp, _Alloc>::_M_fill_insert_aux (iterator __pos, size_type __n,
143                                               const _Tp& __x, const __false_type& /*_Movable*/) {
144   //Here self referencing needs to be checked even for non movable types.
145   if (_M_is_inside(__x)) {
146     _Tp __x_copy = __x;
147     _M_fill_insert_aux(__pos, __n, __x_copy, __false_type());
148     return;
149   }
150   const size_type __elems_after = this->_M_finish - __pos;
151   pointer __old_finish = this->_M_finish;
152   if (__elems_after > __n) {
153     _STLP_PRIV __ucopy_ptrs(this->_M_finish - __n, this->_M_finish, this->_M_finish, _TrivialUCopy());
154     this->_M_finish += __n;
155     _STLP_PRIV __copy_backward_ptrs(__pos, __old_finish - __n, __old_finish, _TrivialCopy());
156     _STLP_STD::fill(__pos, __pos + __n, __x);
157   } else {
158     this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_finish, __n - __elems_after, __x);
159     _STLP_PRIV __ucopy_ptrs(__pos, __old_finish, this->_M_finish, _TrivialUCopy());
160     this->_M_finish += __elems_after;
161     _STLP_STD::fill(__pos, __old_finish, __x);
162   }
163 }
164
165 template <class _Tp, class _Alloc>
166 void vector<_Tp, _Alloc>::_M_fill_insert(iterator __pos,
167                                          size_type __n, const _Tp& __x) {
168   if (__n != 0) {
169     if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n) {
170       _M_fill_insert_aux(__pos, __n, __x, _Movable());
171     } else
172       _M_insert_overflow(__pos, __x, _TrivialCopy(), __n);
173   }
174 }
175
176 template <class _Tp, class _Alloc>
177 vector<_Tp, _Alloc>& vector<_Tp, _Alloc>::operator = (const vector<_Tp, _Alloc>& __x) {
178   if (&__x != this) {
179     const size_type __xlen = __x.size();
180     if (__xlen > capacity()) {
181       size_type __len = __xlen;
182       pointer __tmp = _M_allocate_and_copy(__len, __CONST_CAST(const_pointer, __x._M_start) + 0,
183                                                   __CONST_CAST(const_pointer, __x._M_finish) + 0);
184       _M_clear();
185       this->_M_start = __tmp;
186       this->_M_end_of_storage._M_data = this->_M_start + __len;
187     } else if (size() >= __xlen) {
188       pointer __i = _STLP_PRIV __copy_ptrs(__CONST_CAST(const_pointer, __x._M_start) + 0,
189                                            __CONST_CAST(const_pointer, __x._M_finish) + 0, this->_M_start, _TrivialCopy());
190       _STLP_STD::_Destroy_Range(__i, this->_M_finish);
191     } else {
192       _STLP_PRIV __copy_ptrs(__CONST_CAST(const_pointer, __x._M_start),
193                              __CONST_CAST(const_pointer, __x._M_start) + size(), this->_M_start, _TrivialCopy());
194       _STLP_PRIV __ucopy_ptrs(__CONST_CAST(const_pointer, __x._M_start) + size(),
195                               __CONST_CAST(const_pointer, __x._M_finish) + 0, this->_M_finish, _TrivialUCopy());
196     }
197     this->_M_finish = this->_M_start + __xlen;
198   }
199   return *this;
200 }
201
202 template <class _Tp, class _Alloc>
203 void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const _Tp& __val) {
204   if (__n > capacity()) {
205     vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator());
206     __tmp.swap(*this);
207   } else if (__n > size()) {
208     fill(begin(), end(), __val);
209     this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_finish, __n - size(), __val);
210   } else
211     erase(_STLP_PRIV __fill_n(begin(), __n, __val), end());
212 }
213
214 template <class _Tp, class _Alloc>
215 __iterator__
216 vector<_Tp, _Alloc>::insert(iterator __pos, const _Tp& __x) {
217   size_type __n = __pos - begin();
218   _M_fill_insert(__pos, 1, __x);
219   return begin() + __n;
220 }
221
222 #undef __iterator__
223
224 #if defined (vector)
225 #  undef vector
226 _STLP_MOVE_TO_STD_NAMESPACE
227 #endif
228
229 _STLP_END_NAMESPACE
230
231 #endif /*  _STLP_VECTOR_C */
232
233 // Local Variables:
234 // mode:C++
235 // End: