diff options
Diffstat (limited to 'libstdc++-v3/include/bits/stl_vector.h')
-rw-r--r-- | libstdc++-v3/include/bits/stl_vector.h | 213 |
1 files changed, 45 insertions, 168 deletions
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 0727df6fac1..485503c0acb 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1,3 +1,32 @@ +// Vector implementation -*- C++ -*- + +// Copyright (C) 2001 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. + /* * * Copyright (c) 1994 @@ -31,16 +60,12 @@ #ifndef __SGI_STL_INTERNAL_VECTOR_H #define __SGI_STL_INTERNAL_VECTOR_H -#include <bits/exception_support.h> - -#include <bits/concept_checks.h> - -__STL_BEGIN_NAMESPACE +#include <bits/stl_iterator_base_funcs.h> +#include <bits/functexcept.h> +#include <bits/concept_check.h> -#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) -#pragma set woff 1174 -#pragma set woff 1375 -#endif +namespace std +{ // The vector base class serves two purposes. First, its constructor // and destructor allocate (but don't initialize) storage. This makes @@ -48,8 +73,6 @@ __STL_BEGIN_NAMESPACE // the differences between SGI-style allocators and standard-conforming // allocators. -#ifdef __STL_USE_STD_ALLOCATORS - // Base class for ordinary allocators. template <class _Tp, class _Allocator, bool _IsStatic> class _Vector_alloc_base { @@ -119,46 +142,12 @@ struct _Vector_base ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); } }; -#else /* __STL_USE_STD_ALLOCATORS */ - -template <class _Tp, class _Alloc> -class _Vector_base { -public: - typedef _Alloc allocator_type; - allocator_type get_allocator() const { return allocator_type(); } - - _Vector_base(const _Alloc&) - : _M_start(0), _M_finish(0), _M_end_of_storage(0) {} - _Vector_base(size_t __n, const _Alloc&) - : _M_start(0), _M_finish(0), _M_end_of_storage(0) - { - _M_start = _M_allocate(__n); - _M_finish = _M_start; - _M_end_of_storage = _M_start + __n; - } - - ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); } - -protected: - _Tp* _M_start; - _Tp* _M_finish; - _Tp* _M_end_of_storage; - - typedef simple_alloc<_Tp, _Alloc> _M_data_allocator; - _Tp* _M_allocate(size_t __n) - { return _M_data_allocator::allocate(__n); } - void _M_deallocate(_Tp* __p, size_t __n) - { _M_data_allocator::deallocate(__p, __n); } -}; - -#endif /* __STL_USE_STD_ALLOCATORS */ template <class _Tp, class _Alloc = allocator<_Tp> > class vector : protected _Vector_base<_Tp, _Alloc> { - // requirements: - - __STL_CLASS_REQUIRES(_Tp, _Assignable); + // concept requirements + __glibcpp_class_requires(_Tp, _SGIAssignableConcept); private: typedef _Vector_base<_Tp, _Alloc> _Base; @@ -177,24 +166,15 @@ public: typedef typename _Base::allocator_type allocator_type; allocator_type get_allocator() const { return _Base::get_allocator(); } -#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION typedef reverse_iterator<const_iterator> const_reverse_iterator; typedef reverse_iterator<iterator> reverse_iterator; -#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ - typedef reverse_iterator<const_iterator, value_type, const_reference, - difference_type> const_reverse_iterator; - typedef reverse_iterator<iterator, value_type, reference, difference_type> - reverse_iterator; -#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ protected: -#ifdef __STL_HAS_NAMESPACES using _Base::_M_allocate; using _Base::_M_deallocate; using _Base::_M_start; using _Base::_M_finish; using _Base::_M_end_of_storage; -#endif /* __STL_HAS_NAMESPACES */ protected: void _M_insert_aux(iterator __position, const _Tp& __x); @@ -228,17 +208,15 @@ public: reference operator[](size_type __n) { return *(begin() + __n); } const_reference operator[](size_type __n) const { return *(begin() + __n); } -#ifdef __STL_THROW_RANGE_ERRORS void _M_range_check(size_type __n) const { if (__n >= this->size()) - __out_of_range("vector"); + __throw_out_of_range("vector"); } reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; } const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; } -#endif /* __STL_THROW_RANGE_ERRORS */ explicit vector(const allocator_type& __a = allocator_type()) : _Base(__a) {} @@ -256,7 +234,6 @@ public: : _Base(__x.size(), __x.get_allocator()) { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); } -#ifdef __STL_MEMBER_TEMPLATES // Check whether it's an integral type. If so, it's not an iterator. template <class _InputIterator> vector(_InputIterator __first, _InputIterator __last, @@ -275,16 +252,9 @@ public: template <class _InputIterator> void _M_initialize_aux(_InputIterator __first, _InputIterator __last, __false_type) { - _M_range_initialize(__first, __last, __ITERATOR_CATEGORY(__first)); + _M_range_initialize(__first, __last, __iterator_category(__first)); } -#else - vector(const _Tp* __first, const _Tp* __last, - const allocator_type& __a = allocator_type()) - : _Base(__last - __first, __a) - { _M_finish = uninitialized_copy(__first, __last, _M_start); } -#endif /* __STL_MEMBER_TEMPLATES */ - ~vector() { destroy(_M_start, _M_finish); } vector<_Tp, _Alloc>& operator=(const vector<_Tp, _Alloc>& __x); @@ -308,8 +278,6 @@ public: void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); } void _M_fill_assign(size_type __n, const _Tp& __val); -#ifdef __STL_MEMBER_TEMPLATES - template <class _InputIterator> void assign(_InputIterator __first, _InputIterator __last) { typedef typename _Is_integer<_InputIterator>::_Integral _Integral; @@ -322,7 +290,7 @@ public: template <class _InputIter> void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type) - { _M_assign_aux(__first, __last, __ITERATOR_CATEGORY(__first)); } + { _M_assign_aux(__first, __last, __iterator_category(__first)); } template <class _InputIterator> void _M_assign_aux(_InputIterator __first, _InputIterator __last, @@ -332,8 +300,6 @@ public: void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag); -#endif /* __STL_MEMBER_TEMPLATES */ - reference front() { return *begin(); } const_reference front() const { return *begin(); } reference back() { return *(end() - 1); } @@ -356,9 +322,9 @@ public: _M_insert_aux(end()); } void swap(vector<_Tp, _Alloc>& __x) { - __STD::swap(_M_start, __x._M_start); - __STD::swap(_M_finish, __x._M_finish); - __STD::swap(_M_end_of_storage, __x._M_end_of_storage); + std::swap(_M_start, __x._M_start); + std::swap(_M_finish, __x._M_finish); + std::swap(_M_end_of_storage, __x._M_end_of_storage); } iterator insert(iterator __position, const _Tp& __x) { @@ -381,7 +347,6 @@ public: _M_insert_aux(iterator(__position)); return begin() + __n; } -#ifdef __STL_MEMBER_TEMPLATES // Check whether it's an integral type. If so, it's not an iterator. template <class _InputIterator> void insert(iterator __pos, _InputIterator __first, _InputIterator __last) { @@ -398,12 +363,8 @@ public: void _M_insert_dispatch(iterator __pos, _InputIterator __first, _InputIterator __last, __false_type) { - _M_range_insert(__pos, __first, __last, __ITERATOR_CATEGORY(__first)); + _M_range_insert(__pos, __first, __last, __iterator_category(__first)); } -#else /* __STL_MEMBER_TEMPLATES */ - void insert(iterator __position, - const_iterator __first, const_iterator __last); -#endif /* __STL_MEMBER_TEMPLATES */ void insert (iterator __pos, size_type __n, const _Tp& __x) { _M_fill_insert(__pos, __n, __x); } @@ -439,33 +400,18 @@ public: protected: -#ifdef __STL_MEMBER_TEMPLATES template <class _ForwardIterator> pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last) -{ - pointer __result = _M_allocate(__n); - __STL_TRY { - uninitialized_copy(__first, __last, __result); - return __result; - } - __STL_UNWIND(_M_deallocate(__result, __n)); - } -#else /* __STL_MEMBER_TEMPLATES */ - pointer _M_allocate_and_copy(size_type __n, const_iterator __first, - const_iterator __last) { - iterator __result(_M_allocate(__n)); + pointer __result = _M_allocate(__n); __STL_TRY { uninitialized_copy(__first, __last, __result); return __result; } __STL_UNWIND(_M_deallocate(__result, __n)); } -#endif /* __STL_MEMBER_TEMPLATES */ - -#ifdef __STL_MEMBER_TEMPLATES template <class _InputIterator> void _M_range_initialize(_InputIterator __first, _InputIterator __last, input_iterator_tag) @@ -495,8 +441,6 @@ protected: void _M_range_insert(iterator __pos, _ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag); - -#endif /* __STL_MEMBER_TEMPLATES */ }; template <class _Tp, class _Alloc> @@ -515,8 +459,6 @@ operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) __y.begin(), __y.end()); } -#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER - template <class _Tp, class _Alloc> inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y) { @@ -547,8 +489,6 @@ operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__x < __y); } -#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */ - template <class _Tp, class _Alloc> vector<_Tp,_Alloc>& vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x) @@ -590,8 +530,6 @@ void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val) erase(fill_n(begin(), __n, __val), end()); } -#ifdef __STL_MEMBER_TEMPLATES - template <class _Tp, class _Alloc> template <class _InputIter> void vector<_Tp, _Alloc>::_M_assign_aux(_InputIter __first, _InputIter __last, input_iterator_tag) { @@ -631,8 +569,6 @@ vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last, } } -#endif /* __STL_MEMBER_TEMPLATES */ - template <class _Tp, class _Alloc> void vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x) @@ -746,8 +682,6 @@ void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n, } } -#ifdef __STL_MEMBER_TEMPLATES - template <class _Tp, class _Alloc> template <class _InputIterator> void vector<_Tp, _Alloc>::_M_range_insert(iterator __pos, @@ -813,64 +747,7 @@ vector<_Tp, _Alloc>::_M_range_insert(iterator __position, } } -#else /* __STL_MEMBER_TEMPLATES */ - -template <class _Tp, class _Alloc> -void -vector<_Tp, _Alloc>::insert(iterator __position, - const_iterator __first, - const_iterator __last) -{ - if (__first != __last) { - size_type __n = 0; - distance(__first, __last, __n); - if (size_type(_M_end_of_storage - _M_finish) >= __n) { - const size_type __elems_after = _M_finish - __position; - iterator __old_finish(_M_finish); - if (__elems_after > __n) { - uninitialized_copy(_M_finish - __n, _M_finish, _M_finish); - _M_finish += __n; - copy_backward(__position, __old_finish - __n, __old_finish); - copy(__first, __last, __position); - } - else { - uninitialized_copy(__first + __elems_after, __last, _M_finish); - _M_finish += __n - __elems_after; - uninitialized_copy(__position, __old_finish, _M_finish); - _M_finish += __elems_after; - copy(__first, __first + __elems_after, __position); - } - } - else { - const size_type __old_size = size(); - const size_type __len = __old_size + max(__old_size, __n); - iterator __new_start(_M_allocate(__len)); - iterator __new_finish(__new_start); - __STL_TRY { - __new_finish = uninitialized_copy(_M_start, __position, __new_start); - __new_finish = uninitialized_copy(__first, __last, __new_finish); - __new_finish - = uninitialized_copy(__position, _M_finish, __new_finish); - } - __STL_UNWIND((destroy(__new_start,__new_finish), - _M_deallocate(__new_start,__len))); - destroy(_M_start, _M_finish); - _M_deallocate(_M_start, _M_end_of_storage - _M_start); - _M_start = __new_start; - _M_finish = __new_finish; - _M_end_of_storage = __new_start + __len; - } - } -} - -#endif /* __STL_MEMBER_TEMPLATES */ - -#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) -#pragma reset woff 1174 -#pragma reset woff 1375 -#endif - -__STL_END_NAMESPACE +} // namespace std #endif /* __SGI_STL_INTERNAL_VECTOR_H */ |