Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
18 * Permission to use or copy this software for any purpose is hereby granted
19 * without fee, provided the above notices are retained on all copies.
20 * Permission to modify the code and to distribute modified code is granted,
21 * provided the above notices are retained, and a notice that the code was
22 * modified is included with the above copyright notice.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #ifndef _STLP_INTERNAL_VECTOR_H
31 #define _STLP_INTERNAL_VECTOR_H
35 # ifndef _STLP_INTERNAL_ALGOBASE_H
36 # include <stl/_algobase.h>
39 # ifndef _STLP_INTERNAL_ALLOC_H
40 # include <stl/_alloc.h>
43 # ifndef _STLP_INTERNAL_ITERATOR_H
44 # include <stl/_iterator.h>
47 # ifndef _STLP_INTERNAL_UNINITIALIZED_H
48 # include <stl/_uninitialized.h>
51 # ifndef _STLP_RANGE_ERRORS_H
52 # include <stl/_range_errors.h>
56 # define vector __WORKAROUND_DBG_RENAME(vector)
60 // The vector base class serves two purposes. First, its constructor
61 // and destructor allocate (but don't initialize) storage. This makes
62 // exception safety easier.
64 template <class _Tp, class _Alloc>
67 typedef _Vector_base<_Tp, _Alloc> _Base;
68 _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
69 typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
71 _Vector_base(const _Alloc& __a)
72 : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) {
73 // no push here as not needed
75 _Vector_base(size_t __n, const _Alloc& __a)
76 : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0)
78 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
79 _M_start = _M_end_of_storage.allocate(__n);
81 _M_end_of_storage._M_data = _M_start + __n;
82 _STLP_MPWFIX_TRY _STLP_MPWFIX_CATCH
87 _M_end_of_storage.deallocate(_M_start, _M_end_of_storage._M_data - _M_start);
93 _STLP_alloc_proxy<_Tp*, _Tp, allocator_type> _M_end_of_storage;
96 template <class _Tp, _STLP_DEFAULT_ALLOCATOR_SELECT(_Tp) >
97 class vector : public _Vector_base<_Tp, _Alloc>
100 typedef _Vector_base<_Tp, _Alloc> _Base;
102 typedef _Tp value_type;
103 typedef value_type* pointer;
104 typedef const value_type* const_pointer;
105 typedef value_type* iterator;
106 typedef const value_type* const_iterator;
109 typedef value_type& reference;
110 typedef const value_type& const_reference;
111 typedef size_t size_type;
112 typedef ptrdiff_t difference_type;
113 typedef random_access_iterator_tag _Iterator_category;
115 _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
116 _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
117 typedef typename _Vector_base<_Tp, _Alloc>::allocator_type allocator_type;
119 allocator_type get_allocator() const {
120 return _STLP_CONVERT_ALLOCATOR((const allocator_type&)this->_M_end_of_storage, _Tp);
123 #ifdef _STLP_USE_TRAP_LEAVE
125 static void* operator new (size_t __n, TLeave) { return _STLP_StackHelper<bool>::_NewLC(__n); }
126 static void* operator new (size_t __n) { return _STLP_StackHelper<bool>::_NewLC(__n); }
130 typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialAss;
131 typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _IsPODType;
133 // handles insertions on overflow
134 void _M_insert_overflow(pointer __position, const _Tp& __x, const __false_type&,
135 size_type __fill_len, bool __atend = false) {
136 const size_type __old_size = size();
137 const size_type __len = __old_size + (max)(__old_size, __fill_len);
139 _STLP_LEAVE_VOLATILE pointer __new_start = this->_M_end_of_storage.allocate(__len);
140 _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
142 __new_finish = __uninitialized_copy(this->_M_start, __position, __new_start, __false_type());
144 if (__fill_len == 1) {
145 _Construct(__new_finish, __x);
148 __new_finish = __uninitialized_fill_n(__new_finish, __fill_len, __x, __false_type());
151 __new_finish = __uninitialized_copy(__position, this->_M_finish, __new_finish, __false_type());
153 _STLP_UNWIND((_Destroy(__new_start,__new_finish),
154 this->_M_end_of_storage.deallocate(__new_start,__len)));
156 _M_set(__new_start, __new_finish, __new_start + __len);
159 void _M_insert_overflow(pointer __position, const _Tp& __x, const __true_type&,
160 size_type __fill_len, bool __atend = false) {
161 const size_type __old_size = size();
162 const size_type __len = __old_size + (max)(__old_size, __fill_len);
164 pointer __new_start = this->_M_end_of_storage.allocate(__len);
165 pointer __new_finish = (pointer)__copy_trivial(this->_M_start, __position, __new_start);
167 __new_finish = fill_n(__new_finish, __fill_len, __x);
170 __new_finish = (pointer)__copy_trivial(__position, this->_M_finish, __new_finish);
172 _M_set(__new_start, __new_finish, __new_start + __len);
175 void _M_range_check(size_type __n) const {
176 if (__n >= size_type(this->_M_finish-this->_M_start))
177 __stl_throw_out_of_range("vector");
181 iterator begin() { return this->_M_start; }
182 const_iterator begin() const { return this->_M_start; }
183 iterator end() { return this->_M_finish; }
184 const_iterator end() const { return this->_M_finish; }
186 reverse_iterator rbegin() { return reverse_iterator(end()); }
187 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
188 reverse_iterator rend() { return reverse_iterator(begin()); }
189 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
191 size_type size() const { return size_type(this->_M_finish - this->_M_start); }
192 size_type max_size() const { return size_type(-1) / sizeof(_Tp); }
193 size_type capacity() const { return size_type(this->_M_end_of_storage._M_data - this->_M_start); }
194 bool empty() const { return this->_M_start == this->_M_finish; }
196 reference operator[](size_type __n) { return *(begin() + __n); }
197 const_reference operator[](size_type __n) const { return *(begin() + __n); }
199 reference front() { return *begin(); }
200 const_reference front() const { return *begin(); }
201 reference back() { return *(end() - 1); }
202 const_reference back() const { return *(end() - 1); }
204 reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
205 const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
207 //explicit vector(const allocator_type& __a = allocator_type()) :
208 vector(const allocator_type& __a = allocator_type()) : //to useimplicitly constructor taking allocator
209 _Vector_base<_Tp, _Alloc>(__a) {
210 // needed for new() to work correctly
214 vector(size_type __n, const _Tp& __val,
215 const allocator_type& __a = allocator_type())
216 : _Vector_base<_Tp, _Alloc>(__n, __a) {
217 this->_M_finish = uninitialized_fill_n(this->_M_start, __n, __val);
218 _STLP_POP_CLEANUP_ITEM
221 explicit vector(size_type __n)
222 : _Vector_base<_Tp, _Alloc>(__n, allocator_type() ) {
223 # ifdef _STLP_USE_TRAP_LEAVE
225 _STLP_PUSH_CLEANUP_ITEM(_Tp, &__p)
226 this->_M_finish = uninitialized_fill_n(this->_M_start, __n, __p);
227 // unconditional for __p
229 _STLP_POP_CLEANUP_ITEM
231 this->_M_finish = uninitialized_fill_n(this->_M_start, __n, _Tp());
235 vector(const vector<_Tp, _Alloc>& __x)
236 : _Vector_base<_Tp, _Alloc>(__x.size(), __x.get_allocator()) {
237 this->_M_finish = __uninitialized_copy((const_pointer)__x._M_start,
238 (const_pointer)__x._M_finish, this->_M_start, _IsPODType());
239 _STLP_POP_CLEANUP_ITEM
242 #if defined (_STLP_MEMBER_TEMPLATES)
243 template <class _Integer>
244 void _M_initialize_aux(_Integer __n, _Integer __val, const __true_type&) {
245 this->_M_start = this->_M_end_of_storage.allocate(__n);
246 this->_M_end_of_storage._M_data = this->_M_start + __n;
247 this->_M_finish = uninitialized_fill_n(this->_M_start, __n, __val);
250 template <class _InputIterator>
251 void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
252 const __false_type&) {
253 _M_range_initialize(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator));
256 // Check whether it's an integral type. If so, it's not an iterator.
257 # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
258 template <class _InputIterator>
259 vector(_InputIterator __first, _InputIterator __last) :
260 _Vector_base<_Tp, _Alloc>(allocator_type()) {
261 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
262 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
263 _M_initialize_aux(__first, __last, _Integral());
264 _STLP_POP_CLEANUP_ITEM
267 template <class _InputIterator>
268 vector(_InputIterator __first, _InputIterator __last,
269 const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL ) :
270 _Vector_base<_Tp, _Alloc>(__a) {
271 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
272 _STLP_PUSH_CLEANUP_ITEM(_Base, this)
273 _M_initialize_aux(__first, __last, _Integral());
274 _STLP_POP_CLEANUP_ITEM
278 vector(const _Tp* __first, const _Tp* __last,
279 const allocator_type& __a = allocator_type())
280 : _Vector_base<_Tp, _Alloc>(__last - __first, __a) {
281 this->_M_finish = __uninitialized_copy(__first, __last, this->_M_start, _IsPODType());
282 _STLP_POP_CLEANUP_ITEM
284 #endif /* _STLP_MEMBER_TEMPLATES */
286 ~vector() { _Destroy(this->_M_start, this->_M_finish); }
288 vector<_Tp, _Alloc>& operator=(const vector<_Tp, _Alloc>& __x);
290 void reserve(size_type __n);
292 // assign(), a generalized assignment member function. Two
293 // versions: one that takes a count, and one that takes a range.
294 // The range version is a member template, so we dispatch on whether
295 // or not the type is an integer.
297 void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
298 void _M_fill_assign(size_type __n, const _Tp& __val);
300 #ifdef _STLP_MEMBER_TEMPLATES
301 template <class _ForwardIter>
302 void _M_assign_aux(_ForwardIter __first, _ForwardIter __last, const forward_iterator_tag &)
304 void assign(const_iterator __first, const_iterator __last)
307 size_type __len = distance(__first, __last);
309 if (__len > capacity()) {
310 iterator __tmp = _M_allocate_and_copy(__len, __first, __last);
312 _M_set(__tmp, __tmp + __len, __tmp + __len);
314 else if (size() >= __len) {
315 iterator __new_finish = copy(__first, __last, this->_M_start);
316 _Destroy(__new_finish, this->_M_finish);
317 this->_M_finish = __new_finish;
320 # if defined ( _STLP_MEMBER_TEMPLATES )
321 _ForwardIter __mid = __first;
322 advance(__mid, size());
324 const_iterator __mid = __first + size() ;
326 copy(__first, __mid, this->_M_start);
327 this->_M_finish = __uninitialized_copy(__mid, __last, this->_M_finish, _IsPODType());
331 #ifdef _STLP_MEMBER_TEMPLATES
332 template <class _InputIter>
333 void _M_assign_aux(_InputIter __first, _InputIter __last,
334 const input_iterator_tag &) {
335 iterator __cur = begin();
336 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
338 if (__first == __last)
341 insert(end(), __first, __last);
344 template <class _Integer>
345 void _M_assign_dispatch(_Integer __n, _Integer __val, const __true_type&)
346 { assign((size_type) __n, (_Tp) __val); }
348 template <class _InputIter>
349 void _M_assign_dispatch(_InputIter __first, _InputIter __last, const __false_type&)
350 { _M_assign_aux(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
352 template <class _InputIterator>
353 void assign(_InputIterator __first, _InputIterator __last) {
354 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
355 _M_assign_dispatch(__first, __last, _Integral());
357 #endif /* _STLP_MEMBER_TEMPLATES */
359 void push_back(const _Tp& __x) {
360 if (this->_M_finish != this->_M_end_of_storage._M_data) {
361 _Construct(this->_M_finish, __x);
365 _M_insert_overflow(this->_M_finish, __x, _IsPODType(), 1UL, true);
368 void swap(vector<_Tp, _Alloc>& __x) {
369 _STLP_STD::swap(this->_M_start, __x._M_start);
370 _STLP_STD::swap(this->_M_finish, __x._M_finish);
371 _STLP_STD::swap(this->_M_end_of_storage, __x._M_end_of_storage);
374 iterator insert(iterator __position, const _Tp& __x) {
375 size_type __n = __position - begin();
376 if (this->_M_finish != this->_M_end_of_storage._M_data) {
377 if (__position == end()) {
378 _Construct(this->_M_finish, __x);
381 _Construct(this->_M_finish, *(this->_M_finish - 1));
384 __copy_backward_ptrs(__position, this->_M_finish - 2, this->_M_finish - 1, _TrivialAss());
385 *__position = __x_copy;
389 _M_insert_overflow(__position, __x, _IsPODType(), 1UL);
390 return begin() + __n;
393 # ifndef _STLP_NO_ANACHRONISMS
394 void push_back() { push_back(_Tp()); }
395 iterator insert(iterator __position) { return insert(__position, _Tp()); }
398 void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x);
400 #if defined ( _STLP_MEMBER_TEMPLATES)
402 template <class _Integer>
403 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
404 const __true_type&) {
405 _M_fill_insert(__pos, (size_type) __n, (_Tp) __val);
408 template <class _InputIterator>
409 void _M_insert_dispatch(iterator __pos,
410 _InputIterator __first, _InputIterator __last,
411 const __false_type&) {
412 _M_range_insert(__pos, __first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator));
415 // Check whether it's an integral type. If so, it's not an iterator.
416 template <class _InputIterator>
417 void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
418 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
419 _M_insert_dispatch(__pos, __first, __last, _Integral());
422 template <class _InputIterator>
423 void _M_range_insert(iterator __pos,
424 _InputIterator __first,
425 _InputIterator __last,
426 const input_iterator_tag &) {
427 for ( ; __first != __last; ++__first) {
428 __pos = insert(__pos, *__first);
433 template <class _ForwardIterator>
434 void _M_range_insert(iterator __position,
435 _ForwardIterator __first,
436 _ForwardIterator __last,
437 const forward_iterator_tag &)
438 #else /* _STLP_MEMBER_TEMPLATES */
439 void insert(iterator __position,
440 const_iterator __first, const_iterator __last)
441 #endif /* _STLP_MEMBER_TEMPLATES */
444 if (__first != __last) {
445 size_type __n = distance(__first, __last);
447 if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n) {
448 const size_type __elems_after = this->_M_finish - __position;
449 pointer __old_finish = this->_M_finish;
450 if (__elems_after > __n) {
451 __uninitialized_copy(this->_M_finish - __n, this->_M_finish, this->_M_finish, _IsPODType());
452 this->_M_finish += __n;
453 __copy_backward_ptrs(__position, __old_finish - __n, __old_finish, _TrivialAss());
454 copy(__first, __last, __position);
457 # if defined ( _STLP_MEMBER_TEMPLATES )
458 _ForwardIterator __mid = __first;
459 advance(__mid, __elems_after);
461 const_pointer __mid = __first + __elems_after;
463 __uninitialized_copy(__mid, __last, this->_M_finish, _IsPODType());
464 this->_M_finish += __n - __elems_after;
465 __uninitialized_copy(__position, __old_finish, this->_M_finish, _IsPODType());
466 this->_M_finish += __elems_after;
467 copy(__first, __mid, __position);
471 const size_type __old_size = size();
472 const size_type __len = __old_size + (max)(__old_size, __n);
473 _STLP_LEAVE_VOLATILE pointer __new_start = this->_M_end_of_storage.allocate(__len);
474 _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
476 __new_finish = __uninitialized_copy(this->_M_start, __position, __new_start, _IsPODType());
477 __new_finish = __uninitialized_copy(__first, __last, __new_finish, _IsPODType());
478 __new_finish = __uninitialized_copy(__position, this->_M_finish, __new_finish, _IsPODType());
480 _STLP_UNWIND((_Destroy(__new_start,__new_finish),
481 this->_M_end_of_storage.deallocate(__new_start,__len)));
483 _M_set(__new_start, __new_finish, __new_start + __len);
487 void insert (iterator __pos, size_type __n, const _Tp& __x)
488 { _M_fill_insert(__pos, __n, __x); }
492 _Destroy(this->_M_finish);
494 iterator erase(iterator __position) {
495 if (__position + 1 != end())
496 __copy_ptrs(__position + 1, this->_M_finish, __position, _TrivialAss());
498 _Destroy(this->_M_finish);
501 iterator erase(iterator __first, iterator __last) {
502 pointer __i = __copy_ptrs(__last, this->_M_finish, __first, _TrivialAss());
503 _Destroy(__i, this->_M_finish);
504 this->_M_finish = __i;
508 void resize(size_type __new_size, _Tp __x) {
509 if (__new_size < size())
510 erase(begin() + __new_size, end());
512 insert(end(), __new_size - size(), __x);
515 void resize(size_type __new_size) {
516 resize(__new_size, _Tp());
520 erase(begin(), end());
526 // if (this->_M_start) {
527 _Destroy(this->_M_start, this->_M_finish);
528 this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start);
532 void _M_set(pointer __s, pointer __f, pointer __e) {
533 this->_M_start = __s;
534 this->_M_finish = __f;
535 this->_M_end_of_storage._M_data = __e;
538 #ifdef _STLP_MEMBER_TEMPLATES
539 template <class _ForwardIterator>
540 pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first,
541 _ForwardIterator __last)
542 #else /* _STLP_MEMBER_TEMPLATES */
543 pointer _M_allocate_and_copy(size_type __n, const_pointer __first,
544 const_pointer __last)
545 #endif /* _STLP_MEMBER_TEMPLATES */
547 _STLP_LEAVE_VOLATILE pointer __result = this->_M_end_of_storage.allocate(__n);
549 #if (!defined(__MRC__)) //*TY 12/17/2000 - added workaround for MrCpp. it confuses on nested try/catch block
550 __uninitialized_copy(__first, __last, __result, _IsPODType());
552 uninitialized_copy(__first, __last, __result);
556 _STLP_UNWIND(this->_M_end_of_storage.deallocate(__result, __n));
561 #ifdef _STLP_MEMBER_TEMPLATES
562 template <class _InputIterator>
563 void _M_range_initialize(_InputIterator __first,
564 _InputIterator __last, const input_iterator_tag &) {
565 for ( ; __first != __last; ++__first)
568 // This function is only called by the constructor.
569 template <class _ForwardIterator>
570 void _M_range_initialize(_ForwardIterator __first,
571 _ForwardIterator __last, const forward_iterator_tag &) {
572 size_type __n = distance(__first, __last);
573 this->_M_start = this->_M_end_of_storage.allocate(__n);
574 this->_M_end_of_storage._M_data = this->_M_start + __n;
575 this->_M_finish = __uninitialized_copy(__first, __last, this->_M_start, _IsPODType());
578 #endif /* _STLP_MEMBER_TEMPLATES */
581 # define _STLP_TEMPLATE_CONTAINER vector<_Tp, _Alloc>
582 # define _STLP_TEMPLATE_HEADER template <class _Tp, class _Alloc>
583 # include <stl/_relops_cont.h>
584 # undef _STLP_TEMPLATE_CONTAINER
585 # undef _STLP_TEMPLATE_HEADER
587 # if defined (_STLP_USE_TEMPLATE_EXPORT)
588 _STLP_EXPORT_TEMPLATE_CLASS allocator<void*>;
589 _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<void**, void*, allocator<void*> >;
590 _STLP_EXPORT_TEMPLATE_CLASS _Vector_base<void*,allocator<void*> >;
591 _STLP_EXPORT_TEMPLATE_CLASS vector<void*,allocator<void*> >;
596 # define __vector__ __WORKAROUND_RENAME(vector)
600 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
601 # include <stl/_vector.c>
604 #ifndef _STLP_INTERNAL_BVECTOR_H
605 # include <stl/_bvector.h>
608 # if defined (_STLP_DEBUG)
609 # include <stl/debug/_vector.h>
612 # if defined (_STLP_USE_WRAPPER_FOR_ALLOC_PARAM)
613 # include <stl/wrappers/_vector.h>
616 #endif /* _STLP_VECTOR_H */