?? stl_bvector.h
字號:
void _Init_bounds() {
_M_iter_list._Orphan(_M_start);
_M_iter_list._Orphan(_M_finish);
}
void _Invalidate_iterator(const _Bit_iterator& __it) {
__invalidate_iterator(&_M_iter_list,__it);
}
void _Invalidate_all() {
_M_iter_list._Invalidate_all();
}
# endif
};
// The next few lines are confusing. What we're doing is declaring a
// partial specialization of vector<T, Alloc> if we have the necessary
// compiler support. Otherwise, we define a class bit_vector which uses
// the default allocator.
#if defined(__STL_CLASS_PARTIAL_SPECIALIZATION) \
&& !defined(__STL_PARTIAL_SPECIALIZATION_BUG) && ! defined(__STL_NO_BOOL)
# define __SGI_STL_VECBOOL_TEMPLATE
# define __BVEC_TMPL_HEADER template <class _Alloc>
#else
# undef __SGI_STL_VECBOOL_TEMPLATE
# ifdef __STL_NO_BOOL
# define __BVEC_TMPL_HEADER
# else
# define __BVEC_TMPL_HEADER __STL_TEMPLATE_NULL
# endif
# define _Alloc __STL_DEFAULT_ALLOCATOR(bool)
#endif
#ifdef __STL_NO_BOOL
# define __BVECTOR_QUALIFIED bit_vector
# define __BVECTOR bit_vector
#else
# ifdef __SGI_STL_VECBOOL_TEMPLATE
# define __BVECTOR_QUALIFIED vector<bool, _Alloc>
# else
# define __BVECTOR_QUALIFIED vector<bool>
# endif
#ifdef __STL_PARTIAL_SPEC_NEEDS_TEMPLATE_ARGS
# define __BVECTOR __BVECTOR_QUALIFIED
#else
# define __BVECTOR vector
#endif
#endif
__BVEC_TMPL_HEADER
class __BVECTOR_QUALIFIED : public _Bvector_base<_Alloc >
{
typedef _Bvector_base<_Alloc > _Base;
typedef __BVECTOR_QUALIFIED _Self;
public:
typedef bool value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Bit_reference reference;
typedef bool const_reference;
typedef _Bit_reference* pointer;
typedef const bool* const_pointer;
typedef _Bit_iterator iterator;
typedef _Bit_const_iterator const_iterator;
#if defined ( __STL_CLASS_PARTIAL_SPECIALIZATION ) && \
! defined (__STL_PARTIAL_SPECIALIZATION_BUG)
typedef __STLPORT_STD::reverse_iterator<const_iterator> const_reverse_iterator;
typedef __STLPORT_STD::reverse_iterator<iterator> reverse_iterator;
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
# if defined (__STL_MSVC50_COMPATIBILITY)
typedef __STLPORT_STD::reverse_iterator<const_iterator, value_type, const_reference,
const_pointer, difference_type> const_reverse_iterator;
typedef __STLPORT_STD::reverse_iterator<iterator, value_type, reference, reference*,
difference_type> reverse_iterator;
# else
typedef __STLPORT_STD::reverse_iterator<const_iterator, value_type, const_reference,
difference_type> const_reverse_iterator;
typedef __STLPORT_STD::reverse_iterator<iterator, value_type, reference, difference_type>
reverse_iterator;
# endif
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
# ifdef __SGI_STL_VECBOOL_TEMPLATE
typedef typename _Base::allocator_type allocator_type;
# else
typedef _Base::allocator_type allocator_type;
# endif
#if defined( __STL_HAS_NAMESPACES )
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::get_allocator;
protected:
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::_M_bit_alloc;
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::_M_deallocate;
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::_M_start;
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::_M_finish;
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::_M_end_of_storage;
# ifdef __STL_DEBUG
__STL_USING_BASE_MEMBER _Bvector_base<_Alloc >::_M_iter_list;
# endif
#endif /* __STL_HAS_NAMESPACES */
protected:
# if defined (__STL_DEBUG)
# define _Make_iterator(__x,__y,__z) iterator(&_M_iter_list,__x,__y,__z)
# define _Make_const_iterator(__x,__y,__z) const_iterator(&_M_iter_list,__x,__y,__z)
# else
# define _Make_iterator(__x,__y,__z) iterator(__x,__y)
# define _Make_const_iterator(__x,__y,__z) const_iterator(__x,__y)
# endif
void _M_initialize(size_type __n) {
unsigned int* __q = _M_bit_alloc(__n);
_M_end_of_storage._M_data = __q + (__n + __WORD_BIT - 1)/__WORD_BIT;
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
_M_start = _Make_iterator(__q, 0, false);
__stl_debug_do(_M_start._Set_overrun(true));
_M_finish = _M_start + difference_type(__n);
__stl_debug_do(_Init_bounds());
}
void _M_insert_aux(iterator __position, bool __x) {
__stl_debug_do(_M_finish._Set_overrun(true));
if (_M_finish._M_p != _M_end_of_storage._M_data) {
copy_backward(__position, _M_finish, _M_finish + 1);
*__position = __x;
++_M_finish;
}
else {
size_type __len = size() ? 2 * size() : __WORD_BIT;
unsigned int* __q = _M_bit_alloc(__len);
iterator __i = copy(begin(), __position, _Make_iterator(__q, 0, true));
*__i++ = __x;
_M_finish = copy(__position, end(), __i);
__stl_debug_do(_M_iter_list._Orphan(_M_finish));
_M_deallocate();
_M_end_of_storage._M_data = __q + (__len + __WORD_BIT - 1)/__WORD_BIT;
_M_start = _Make_iterator(__q, 0, false);
__stl_debug_do(_M_iter_list._Orphan(_M_start));
}
__stl_debug_do(_M_finish._Set_overrun(false));
}
#ifdef __STL_MEMBER_TEMPLATES
template <class _InputIterator>
void _M_initialize_range(_InputIterator __first, _InputIterator __last,
input_iterator_tag) {
__stl_debug_check(__check_range(__first,__last));
__stl_debug_do(_Safe_init(&_M_start));
_M_start = iterator();
_M_finish = iterator();
__stl_debug_do(_Init_bounds());
_M_end_of_storage._M_data = 0;
# if defined (__STL_DEBUG) && defined (__STL_USE_EXCEPTIONS)
try {
# endif
for ( ; __first != __last; ++__first)
push_back(*__first);
# if defined (__STL_DEBUG) && defined (__STL_USE_EXCEPTIONS)
}
catch (...) {
__stl_debug_do(_M_start._Invalidate());
__stl_debug_do(_M_finish._Invalidate());
__stl_debug_do(_M_iter_list._Invalidate());
}
# endif
}
template <class _ForwardIterator>
void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag) {
size_type __n = 0;
distance(__first, __last, __n);
_M_initialize(__n);
copy(__first, __last, _M_start);
}
template <class _InputIterator>
void _M_insert_range(iterator __pos,
_InputIterator __first, _InputIterator __last,
input_iterator_tag) {
for ( ; __first != __last; ++__first) {
__pos = insert(__pos, *__first);
++__pos;
}
}
template <class _ForwardIterator>
void _M_insert_range(iterator __position,
_ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag) {
__stl_debug_check(__check_range(__first,__last));
if (__first != __last) {
size_type __n = 0;
distance(__first, __last, __n);
__stl_debug_do(_M_finish._Set_overrun(true));
__stl_debug_do(__position._Set_overrun(true));
if (capacity() - size() >= __n) {
copy_backward(__position, end(), _M_finish + difference_type(__n));
copy(__first, __last, __position);
_M_finish += difference_type(__n);
}
else {
size_type __len = size() + max(size(), __n);
unsigned int* __q = _M_bit_alloc(__len);
iterator __i = copy(begin(), __position, _Make_iterator(__q, 0, true));
__i = copy(__first, __last, __i);
_M_finish = copy(__position, end(), __i);
__stl_debug_do(_M_iter_list._Orphan(_M_finish));
_M_deallocate();
_M_end_of_storage._M_data = __q + (__len + __WORD_BIT - 1)/__WORD_BIT;
_M_start = _Make_iterator(__q, 0, false);
__stl_debug_do(_M_iter_list._Orphan(_M_start));
}
}
__stl_debug_do(_M_finish._Set_overrun(false));
}
#endif /* __STL_MEMBER_TEMPLATES */
public:
iterator begin() { return _M_start; }
const_iterator begin() const { return *(const const_iterator*)&_M_start; }
iterator end() { return _M_finish; }
const_iterator end() const { return *(const const_iterator*)&_M_finish; }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
size_type size() const { return size_type(end() - begin()); }
size_type max_size() const { return size_type(-1); }
size_type capacity() const {
return size_type(_Make_const_iterator(_M_end_of_storage._M_data, 0, false) - begin());
}
bool empty() const { return begin() == end(); }
reference operator[](size_type __n)
{ return *(begin() + difference_type(__n)); }
const_reference operator[](size_type __n) const
{ return *(begin() + difference_type(__n)); }
#ifdef __STL_THROW_RANGE_ERRORS
void _M_range_check(size_type __n) const {
if (__n >= this->size())
__stl_throw_range_error("vector<bool>");
}
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 __BVECTOR(const allocator_type& __a =
__STL_ALLOC_INSTANCE(allocator_type))
: _Bvector_base<_Alloc >(__a) {}
__BVECTOR(size_type __n, bool __value,
const allocator_type& __a =
__STL_ALLOC_INSTANCE(allocator_type))
: _Bvector_base<_Alloc >(__a)
{
_M_initialize(__n);
fill(_M_start._M_p, (__chunk_type*)_M_end_of_storage._M_data, __value ? ~0 : 0);
}
explicit __BVECTOR(size_type __n)
: _Bvector_base<_Alloc >(allocator_type())
{
_M_initialize(__n);
fill(_M_start._M_p, (__chunk_type*)_M_end_of_storage._M_data, 0);
}
__BVECTOR(const _Self& __x) : _Bvector_base<_Alloc >(__x.get_allocator()) {
_M_initialize(__x.size());
copy(__x.begin(), __x.end(), _M_start);
}
#if defined (__STL_MEMBER_TEMPLATES) && !(defined (__GNUC__) && __GNUC_MINOR__ < 90)
// Check whether it's an integral type. If so, it's not an iterator.
template <class _InputIterator>
__BVECTOR(_InputIterator __first, _InputIterator __last,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _Base(__a)
{
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_initialize_dispatch(__first, __last, _Integral());
}
template <class _Integer>
void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
_M_initialize(__n);
fill(_M_start._M_p, _M_end_of_storage._M_data, __x ? ~0 : 0);
}
template <class _InputIterator>
void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
__false_type) {
_M_initialize_range(__first, __last, __ITERATOR_CATEGORY(__first));
}
#else /* __STL_MEMBER_TEMPLATES */
__BVECTOR(const_iterator __first, const_iterator __last,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _Bvector_base<_Alloc >(__a)
{
size_type __n = 0;
distance(__first, __last, __n);
_M_initialize(__n);
copy(__first, __last, _M_start);
}
__BVECTOR(const bool* __first, const bool* __last,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _Bvector_base<_Alloc >(__a)
{
size_type __n = 0;
distance(__first, __last, __n);
_M_initialize(__n);
copy(__first, __last, _M_start);
}
#endif /* __STL_MEMBER_TEMPLATES */
~__BVECTOR() { }
__BVECTOR_QUALIFIED& operator=(const __BVECTOR_QUALIFIED& __x) {
if (&__x == this) return *this;
__stl_debug_do(_Invalidate_all());
if (__x.size() > capacity()) {
_M_deallocate();
_M_initialize(__x.size());
}
copy(__x.begin(), __x.end(), begin());
_M_finish = begin() + difference_type(__x.size());
__stl_debug_do(_M_iter_list._Orphan(_M_finish));
return *this;
}
// assign(), a generalized assignment member function. Two
// versions: one that takes a count, and one that takes a range.
// The range version is a member template, so we dispatch on whether
// or not the type is an integer.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -