📄 vector.cc
字号:
template<class _TypeT, class _Allocator>
template<class _InputIter>
void vector<_TypeT,_Allocator>::_C_insert_aux2 (iterator __it,
_InputIter __first,
_InputIter __last)
#else
template<class _TypeT, class _Allocator>
void vector<_TypeT,_Allocator>::_C_insert_aux2 (iterator __it,
const_iterator __first,
const_iterator __last)
#endif // _RWSTD_NO_MEMBER_TEMPLATES
{
if (__first == __last) return;
size_type __n = _DISTANCE(__first, __last, size_type);
if ((size () + __n) <= capacity ()) {
iterator __old_end= end();
// avoid dereferencing end () by incrementing _C_finish first
_C_finish += __n;
if (__it + __n < __old_end) { //insertion entirely before old end
_TRY {
uninitialized_copy (__old_end - __n, __old_end, __old_end,
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_C_destroy (__old_end, end ());
_C_finish -= __n;
_RETHROW;
}
copy_backward(__it, __old_end - __n, __old_end);
copy(__first, __last, __it);
}
else {
size_type __first_part = __old_end - __it;
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
_InputIter __iter(__first);
#else
const_iterator __iter(__first);
#endif
advance(__iter, __first_part);
_TRY {
uninitialized_copy(__iter, __last, __old_end,
_RWSTD_VALUE_ALLOC_CAST (*this));
uninitialized_copy(__it, __old_end, __it + __n,
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_C_destroy (__old_end, end());
_C_finish -= __n;
_RETHROW;
}
copy(__first, __iter, __it);
}
}
else {
size_t __new_capacity =
max ((size_t)(size() + __n), (size_t)_RW::__rw_new_capacity(size(),this));
pointer __start =
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
allocate(__new_capacity,_C_start));
_TRY {
uninitialized_copy(begin(), __it, __start,
_RWSTD_VALUE_ALLOC_CAST (*this));
uninitialized_copy(__first, __last,
__start + (__it - begin()),
_RWSTD_VALUE_ALLOC_CAST (*this));
uninitialized_copy(__it, end(),
__start + (__it - begin() + __n),
_RWSTD_VALUE_ALLOC_CAST (*this));
}
_CATCH (...) {
_C_destroy (_C_make_iter (__start),
_C_make_iter (__start + __new_capacity));
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
deallocate(__start,__new_capacity));
_RETHROW;
}
// compute size before deallocating
size_type __size = size ();
_C_destroy (begin (), end ());
// invalidates all iterators into *this
_RWSTD_VALUE_ALLOC(_C_value_alloc_type,
deallocate(_C_start, capacity()));
_C_start = __start;
_C_finish = __start + __size + __n;
_C_end_of_storage = __start + __new_capacity;
}
}
#ifndef _RWSTD_NO_VECTOR_BOOL
#ifndef _RWSTD_NO_MEMBER_TEMPLATES
// The body of this function is duplicated in src/vecbool.cpp and
// further down in this file as well.
#if !defined (_RWSTD_NO_CLASS_PARTIAL_SPEC)
template <class _Allocator>
template<class _InputIter>
void vector<bool, _Allocator >::insert
#else
template<class _InputIter>
void vector<bool, allocator<bool> >::insert
#endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
(iterator __it, _InputIter __first, _InputIter __last)
{
if (__first == __last) return;
size_type __n = _DISTANCE (__first, __last, size_type);
if (capacity() - size() >= __n)
{
copy_backward(__it, end(), _C_finish + __n);
copy(__first, __last, __it);
_C_finish += __n;
}
else
{
size_type __len = size() + max(size(), __n);
unsigned int* __q = _C_bit_alloc(__len);
iterator __i = copy(begin(), __it, iterator(__q, 0));
__i = copy(__first, __last, __i);
_C_finish = copy(__it, end(), __i);
_C_value_alloc_type (*this).
deallocate((pointer)_C_start._C_p,_C_end_of_storage - _C_start._C_p);
_C_end_of_storage = __q + (__len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
_C_start = iterator(__q, 0);
}
}
#endif // _RWSTD_NO_MEMBER_TEMPLATES
#ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
// Duplicates of the followign functions exist in src/stl/vecbool.cpp.
// Which set is used depends on the availability of partial specialization.
template <class _Allocator>
void vector<bool,_Allocator >::flip ()
{
for (iterator __i = begin(); !(__i == end()); __i++)
*__i = !(*__i);
}
template <class _Allocator>
void vector<bool,_Allocator >::swap (reference __x, reference __y)
{
bool __tmp = __x; __x = __y; __y = __tmp;
}
template <class _Allocator>
void vector<bool,_Allocator >::_C_insert_aux (iterator __it, bool __x)
{
if (_C_finish._C_p != _C_end_of_storage)
{
_C_copy_backward(__it, _C_finish - difference_type (1), _C_finish);
*__it = __x;
++_C_finish;
}
else
{
size_type __len = size() ? 2 * size() : _RWSTD_WORD_BIT;
unsigned int* __q = _C_bit_alloc(__len);
iterator __i = _C_copy(begin(), __it, iterator(__q, 0));
*__i++ = __x;
_C_finish = _C_copy(__it, end(), __i);
_C_value_alloc_type (*this).
deallocate((pointer)_C_start._C_p,_C_end_of_storage - _C_start._C_p);
_C_end_of_storage = __q + (__len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
_C_start = iterator(__q, 0);
}
}
template <class _Allocator>
void vector<bool,_Allocator >::insert (iterator __it, size_type __n,
const bool& __x)
{
if (__n == 0) return;
if (capacity() - size() >= __n)
{
_C_copy_backward(__it, end(), _C_finish + __n);
_C_fill(__it, __it + __n, __x);
_C_finish += __n;
}
else
{
size_type __len = size() + max(size(), __n);
unsigned int* __q = _C_bit_alloc(__len);
iterator __i = _C_copy(begin(), __it, iterator(__q, 0));
_C_fill_n(__i, __n, __x);
_C_finish = _C_copy(__it, end(), __i + __n);
_C_value_alloc_type (*this).
deallocate((pointer)_C_start._C_p,_C_end_of_storage - _C_start._C_p);
_C_end_of_storage = __q + (__len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
_C_start = iterator(__q, 0);
}
}
#ifdef _RWSTD_NO_MEMBER_TEMPLATES
template <class _Allocator>
void vector<bool,_Allocator >::insert (iterator __it,
const_iterator __first,
const_iterator __last)
{
if (__first == __last) return;
size_type __n = _DISTANCE(__first, __last, size_type);
if (capacity() - size() >= __n)
{
_C_copy_backward(__it, end(), _C_finish + __n);
_C_copy(__first, __last, __it);
_C_finish += __n;
}
else
{
size_type __len = size() + max(size(), __n);
unsigned int* __q = _C_bit_alloc(__len);
iterator __i = _C_copy(begin(), __it, iterator(__q, 0));
__i = _C_copy(__first, __last, __i);
_C_finish = _C_copy(__it, end(), __i);
_C_value_alloc_type (*this).
deallocate((pointer)_C_start._C_p,_C_end_of_storage - _C_start._C_p);
_C_end_of_storage = __q + (__len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
_C_start = iterator(__q, 0);
}
}
#endif // _RWSTD_NO_MEMBER_TEMPLATES
template <class _Allocator>
void vector<bool,_Allocator >::resize (size_type __new_size, bool __c)
{
if (__new_size > size())
insert(end(), __new_size - size(), __c);
else if (__new_size < size())
erase(begin() + __new_size, end());
}
#endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
#endif // _RWSTD_NO_VECTOR_BOOL
_RWSTD_NAMESPACE_END // std
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -