Commit 62e67651 by Paolo Carlini Committed by Paolo Carlini

stl_bvector.h: Wrap overlong lines...

2004-02-01  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/stl_bvector.h: Wrap overlong lines, constify
	a few variables, reformat according to the coding standards.
	* include/bits/stl_tree.h: Likewise.

From-SVN: r77065
parent 2efe2033
2004-02-01 Paolo Carlini <pcarlini@suse.de>
* include/bits/stl_bvector.h: Wrap overlong lines, constify
a few variables, reformat according to the coding standards.
* include/bits/stl_tree.h: Likewise.
2004-01-31 Paolo Carlini <pcarlini@suse.de> 2004-01-31 Paolo Carlini <pcarlini@suse.de>
* include/bits/stl_algo.h: Minor additional reformat, add * include/bits/stl_algo.h: Minor additional reformat, add
......
...@@ -154,8 +154,8 @@ namespace __gnu_norm ...@@ -154,8 +154,8 @@ namespace __gnu_norm
bool bool
operator<(const _Bit_iterator_base& __i) const operator<(const _Bit_iterator_base& __i) const
{ {
return _M_p < __i._M_p return _M_p < __i._M_p
|| (_M_p == __i._M_p && _M_offset < __i._M_offset); || (_M_p == __i._M_p && _M_offset < __i._M_offset);
} }
bool bool
...@@ -181,7 +181,6 @@ namespace __gnu_norm ...@@ -181,7 +181,6 @@ namespace __gnu_norm
return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset; return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;
} }
struct _Bit_iterator : public _Bit_iterator_base struct _Bit_iterator : public _Bit_iterator_base
{ {
typedef _Bit_reference reference; typedef _Bit_reference reference;
...@@ -254,7 +253,8 @@ namespace __gnu_norm ...@@ -254,7 +253,8 @@ namespace __gnu_norm
} }
reference reference
operator[](difference_type __i) { return *(*this + __i); } operator[](difference_type __i)
{ return *(*this + __i); }
}; };
inline _Bit_iterator inline _Bit_iterator
...@@ -347,12 +347,13 @@ namespace __gnu_norm ...@@ -347,12 +347,13 @@ namespace __gnu_norm
class _Bvector_base class _Bvector_base
: public _Alloc::template rebind<_Bit_type>::other : public _Alloc::template rebind<_Bit_type>::other
{ {
typedef typename _Alloc::template rebind<_Bit_type>::other _Bit_alloc_type; typedef typename _Alloc::template rebind<_Bit_type>::other
_Bit_alloc_type;
public: public:
typedef _Alloc allocator_type; typedef _Alloc allocator_type;
allocator_type allocator_type
get_allocator() const get_allocator() const
{ return *static_cast<const _Bit_alloc_type*>(this); } { return *static_cast<const _Bit_alloc_type*>(this); }
...@@ -364,7 +365,8 @@ namespace __gnu_norm ...@@ -364,7 +365,8 @@ namespace __gnu_norm
protected: protected:
_Bit_type* _Bit_type*
_M_bit_alloc(size_t __n) _M_bit_alloc(size_t __n)
{ return _Bit_alloc_type::allocate((__n + _S_word_bit - 1)/_S_word_bit);} { return _Bit_alloc_type::allocate((__n + _S_word_bit - 1)
/ _S_word_bit); }
void void
_M_deallocate() _M_deallocate()
...@@ -422,9 +424,9 @@ template<typename _Alloc> ...@@ -422,9 +424,9 @@ template<typename _Alloc>
typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator;
typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type; typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
allocator_type get_allocator() const {
return _Bvector_base<_Alloc>::get_allocator(); allocator_type get_allocator() const
} { return _Bvector_base<_Alloc>::get_allocator(); }
protected: protected:
using _Bvector_base<_Alloc>::_M_bit_alloc; using _Bvector_base<_Alloc>::_M_bit_alloc;
...@@ -434,34 +436,41 @@ template<typename _Alloc> ...@@ -434,34 +436,41 @@ template<typename _Alloc>
using _Bvector_base<_Alloc>::_M_end_of_storage; using _Bvector_base<_Alloc>::_M_end_of_storage;
protected: protected:
void _M_initialize(size_type __n) { void _M_initialize(size_type __n)
_Bit_type * __q = this->_M_bit_alloc(__n); {
this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit; _Bit_type* __q = this->_M_bit_alloc(__n);
this->_M_end_of_storage = __q + (__n + _S_word_bit - 1) / _S_word_bit;
this->_M_start = iterator(__q, 0); this->_M_start = iterator(__q, 0);
this->_M_finish = this->_M_start + difference_type(__n); this->_M_finish = this->_M_start + difference_type(__n);
} }
void _M_insert_aux(iterator __position, bool __x) {
if (this->_M_finish._M_p != this->_M_end_of_storage) { void _M_insert_aux(iterator __position, bool __x)
std::copy_backward(__position, this->_M_finish, this->_M_finish + 1); {
*__position = __x; if (this->_M_finish._M_p != this->_M_end_of_storage)
++this->_M_finish; {
} std::copy_backward(__position, this->_M_finish, this->_M_finish + 1);
else { *__position = __x;
size_type __len = size() ++this->_M_finish;
? 2 * size() : static_cast<size_type>(_S_word_bit); }
_Bit_type * __q = this->_M_bit_alloc(__len); else
iterator __i = std::copy(begin(), __position, iterator(__q, 0)); {
*__i++ = __x; const size_type __len = size() ? 2 * size()
this->_M_finish = std::copy(__position, end(), __i); : static_cast<size_type>(_S_word_bit);
this->_M_deallocate(); _Bit_type * __q = this->_M_bit_alloc(__len);
this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit; iterator __i = std::copy(begin(), __position, iterator(__q, 0));
this->_M_start = iterator(__q, 0); *__i++ = __x;
} this->_M_finish = std::copy(__position, end(), __i);
this->_M_deallocate();
this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)
/ _S_word_bit;
this->_M_start = iterator(__q, 0);
}
} }
template<class _InputIterator> template<class _InputIterator>
void _M_initialize_range(_InputIterator __first, _InputIterator __last, void _M_initialize_range(_InputIterator __first, _InputIterator __last,
input_iterator_tag) { input_iterator_tag)
{
this->_M_start = iterator(); this->_M_start = iterator();
this->_M_finish = iterator(); this->_M_finish = iterator();
this->_M_end_of_storage = 0; this->_M_end_of_storage = 0;
...@@ -471,8 +480,9 @@ template<typename _Alloc> ...@@ -471,8 +480,9 @@ template<typename _Alloc>
template<class _ForwardIterator> template<class _ForwardIterator>
void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last, void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag) { forward_iterator_tag)
size_type __n = std::distance(__first, __last); {
const size_type __n = std::distance(__first, __last);
_M_initialize(__n); _M_initialize(__n);
std::copy(__first, __last, this->_M_start); std::copy(__first, __last, this->_M_start);
} }
...@@ -480,81 +490,105 @@ template<typename _Alloc> ...@@ -480,81 +490,105 @@ template<typename _Alloc>
template<class _InputIterator> template<class _InputIterator>
void _M_insert_range(iterator __pos, void _M_insert_range(iterator __pos,
_InputIterator __first, _InputIterator __last, _InputIterator __first, _InputIterator __last,
input_iterator_tag) { input_iterator_tag)
for ( ; __first != __last; ++__first) { {
__pos = insert(__pos, *__first); for ( ; __first != __last; ++__first)
++__pos; {
} __pos = insert(__pos, *__first);
++__pos;
}
} }
template<class _ForwardIterator> template<class _ForwardIterator>
void _M_insert_range(iterator __position, void _M_insert_range(iterator __position,
_ForwardIterator __first, _ForwardIterator __last, _ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag) { forward_iterator_tag)
if (__first != __last) { {
size_type __n = std::distance(__first, __last); if (__first != __last)
if (capacity() - size() >= __n) { {
std::copy_backward(__position, end(), size_type __n = std::distance(__first, __last);
this->_M_finish + difference_type(__n)); if (capacity() - size() >= __n)
std::copy(__first, __last, __position); {
this->_M_finish += difference_type(__n); std::copy_backward(__position, end(),
} this->_M_finish + difference_type(__n));
else { std::copy(__first, __last, __position);
size_type __len = size() + std::max(size(), __n); this->_M_finish += difference_type(__n);
_Bit_type * __q = this->_M_bit_alloc(__len); }
iterator __i = std::copy(begin(), __position, iterator(__q, 0)); else
__i = std::copy(__first, __last, __i); {
this->_M_finish = std::copy(__position, end(), __i); const size_type __len = size() + std::max(size(), __n);
this->_M_deallocate(); _Bit_type * __q = this->_M_bit_alloc(__len);
this->_M_end_of_storage iterator __i = std::copy(begin(), __position, iterator(__q, 0));
= __q + (__len + _S_word_bit - 1)/_S_word_bit; __i = std::copy(__first, __last, __i);
this->_M_start = iterator(__q, 0); this->_M_finish = std::copy(__position, end(), __i);
} this->_M_deallocate();
} this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)
} / _S_word_bit;
this->_M_start = iterator(__q, 0);
}
}
}
public: public:
iterator begin() { return this->_M_start; } iterator begin()
const_iterator begin() const { return this->_M_start; } { return this->_M_start; }
iterator end() { return this->_M_finish; }
const_iterator end() const { return this->_M_finish; } const_iterator begin() const
{ return this->_M_start; }
iterator end()
{ return this->_M_finish; }
const_iterator end() const
{ return this->_M_finish; }
reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rbegin()
const_reverse_iterator rbegin() const { { return reverse_iterator(end()); }
return const_reverse_iterator(end());
} const_reverse_iterator rbegin() const
reverse_iterator rend() { return reverse_iterator(begin()); } { return const_reverse_iterator(end()); }
const_reverse_iterator rend() const {
return const_reverse_iterator(begin()); 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 size() const
size_type max_size() const { return size_type(-1); } { return size_type(end() - begin()); }
size_type capacity() const {
return size_type(const_iterator(this->_M_end_of_storage, 0) - begin()); size_type max_size() const
} { return size_type(-1); }
bool empty() const { return begin() == end(); }
size_type capacity() const
{ return size_type(const_iterator(this->_M_end_of_storage, 0)
- begin()); }
bool empty() const
{ return begin() == end(); }
reference operator[](size_type __n) reference operator[](size_type __n)
{ return *(begin() + difference_type(__n)); } { return *(begin() + difference_type(__n)); }
const_reference operator[](size_type __n) const const_reference operator[](size_type __n) const
{ return *(begin() + difference_type(__n)); } { return *(begin() + difference_type(__n)); }
void _M_range_check(size_type __n) const { void _M_range_check(size_type __n) const
{
if (__n >= this->size()) if (__n >= this->size())
__throw_out_of_range(__N("vector<bool>::_M_range_check")); __throw_out_of_range(__N("vector<bool>::_M_range_check"));
} }
reference at(size_type __n) reference at(size_type __n)
{ _M_range_check(__n); return (*this)[__n]; } { _M_range_check(__n); return (*this)[__n]; }
const_reference at(size_type __n) const const_reference at(size_type __n) const
{ _M_range_check(__n); return (*this)[__n]; } { _M_range_check(__n); return (*this)[__n]; }
explicit vector(const allocator_type& __a = allocator_type()) explicit vector(const allocator_type& __a = allocator_type())
: _Bvector_base<_Alloc>(__a) { } : _Bvector_base<_Alloc>(__a) { }
vector(size_type __n, bool __value, vector(size_type __n, bool __value,
const allocator_type& __a = allocator_type()) const allocator_type& __a = allocator_type())
: _Bvector_base<_Alloc>(__a) : _Bvector_base<_Alloc>(__a)
{ {
_M_initialize(__n); _M_initialize(__n);
...@@ -568,7 +602,8 @@ template<typename _Alloc> ...@@ -568,7 +602,8 @@ template<typename _Alloc>
std::fill(this->_M_start._M_p, this->_M_end_of_storage, 0); std::fill(this->_M_start._M_p, this->_M_end_of_storage, 0);
} }
vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) { vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator())
{
_M_initialize(__x.size()); _M_initialize(__x.size());
std::copy(__x.begin(), __x.end(), this->_M_start); std::copy(__x.begin(), __x.end(), this->_M_start);
} }
...@@ -576,16 +611,16 @@ template<typename _Alloc> ...@@ -576,16 +611,16 @@ template<typename _Alloc>
// Check whether it's an integral type. If so, it's not an iterator. // Check whether it's an integral type. If so, it's not an iterator.
template<class _Integer> template<class _Integer>
void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) { void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
{
_M_initialize(__n); _M_initialize(__n);
std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0); std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
} }
template<class _InputIterator> template<class _InputIterator>
void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
__false_type) { __false_type)
_M_initialize_range(__first, __last, std::__iterator_category(__first)); { _M_initialize_range(__first, __last, std::__iterator_category(__first)); }
}
template<class _InputIterator> template<class _InputIterator>
vector(_InputIterator __first, _InputIterator __last, vector(_InputIterator __first, _InputIterator __last,
...@@ -598,12 +633,15 @@ template<typename _Alloc> ...@@ -598,12 +633,15 @@ template<typename _Alloc>
~vector() { } ~vector() { }
vector& operator=(const vector& __x) { vector& operator=(const vector& __x)
if (&__x == this) return *this; {
if (__x.size() > capacity()) { if (&__x == this)
this->_M_deallocate(); return *this;
_M_initialize(__x.size()); if (__x.size() > capacity())
} {
this->_M_deallocate();
_M_initialize(__x.size());
}
std::copy(__x.begin(), __x.end(), begin()); std::copy(__x.begin(), __x.end(), begin());
this->_M_finish = begin() + difference_type(__x.size()); this->_M_finish = begin() + difference_type(__x.size());
return *this; return *this;
...@@ -614,36 +652,43 @@ template<typename _Alloc> ...@@ -614,36 +652,43 @@ template<typename _Alloc>
// The range version is a member template, so we dispatch on whether // The range version is a member template, so we dispatch on whether
// or not the type is an integer. // or not the type is an integer.
void _M_fill_assign(size_t __n, bool __x) { void _M_fill_assign(size_t __n, bool __x)
if (__n > size()) { {
std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0); if (__n > size())
insert(end(), __n - size(), __x); {
} std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
else { insert(end(), __n - size(), __x);
erase(begin() + __n, end()); }
std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0); else
} {
erase(begin() + __n, end());
std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
}
} }
void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); } void assign(size_t __n, bool __x)
{ _M_fill_assign(__n, __x); }
template<class _InputIterator> template<class _InputIterator>
void assign(_InputIterator __first, _InputIterator __last) { void assign(_InputIterator __first, _InputIterator __last)
{
typedef typename _Is_integer<_InputIterator>::_Integral _Integral; typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_assign_dispatch(__first, __last, _Integral()); _M_assign_dispatch(__first, __last, _Integral());
} }
template<class _Integer> template<class _Integer>
void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
{ _M_fill_assign((size_t) __n, (bool) __val); } { _M_fill_assign((size_t) __n, (bool) __val); }
template<class _InputIterator> template<class _InputIterator>
void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type) void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
{ _M_assign_aux(__first, __last, std::__iterator_category(__first)); } __false_type)
{ _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
template<class _InputIterator> template<class _InputIterator>
void _M_assign_aux(_InputIterator __first, _InputIterator __last, void _M_assign_aux(_InputIterator __first, _InputIterator __last,
input_iterator_tag) { input_iterator_tag)
{
iterator __cur = begin(); iterator __cur = begin();
for ( ; __first != __last && __cur != end(); ++__cur, ++__first) for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
*__cur = *__first; *__cur = *__first;
...@@ -655,55 +700,72 @@ template<typename _Alloc> ...@@ -655,55 +700,72 @@ template<typename _Alloc>
template<class _ForwardIterator> template<class _ForwardIterator>
void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
forward_iterator_tag) { forward_iterator_tag)
size_type __len = std::distance(__first, __last); {
const size_type __len = std::distance(__first, __last);
if (__len < size()) if (__len < size())
erase(std::copy(__first, __last, begin()), end()); erase(std::copy(__first, __last, begin()), end());
else { else
_ForwardIterator __mid = __first; {
std::advance(__mid, size()); _ForwardIterator __mid = __first;
std::copy(__first, __mid, begin()); std::advance(__mid, size());
insert(end(), __mid, __last); std::copy(__first, __mid, begin());
} insert(end(), __mid, __last);
}
} }
void reserve(size_type __n) { void reserve(size_type __n)
{
if (__n > this->max_size()) if (__n > this->max_size())
__throw_length_error(__N("vector::reserve")); __throw_length_error(__N("vector::reserve"));
if (this->capacity() < __n) { if (this->capacity() < __n)
_Bit_type * __q = this->_M_bit_alloc(__n); {
this->_M_finish = std::copy(begin(), end(), iterator(__q, 0)); _Bit_type* __q = this->_M_bit_alloc(__n);
this->_M_deallocate(); this->_M_finish = std::copy(begin(), end(), iterator(__q, 0));
this->_M_start = iterator(__q, 0); this->_M_deallocate();
this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit; this->_M_start = iterator(__q, 0);
} this->_M_end_of_storage = __q + (__n + _S_word_bit - 1) / _S_word_bit;
} }
}
reference front() { return *begin(); }
const_reference front() const { return *begin(); } reference front()
reference back() { return *(end() - 1); } { return *begin(); }
const_reference back() const { return *(end() - 1); }
void push_back(bool __x) { const_reference front() const
{ return *begin(); }
reference back()
{ return *(end() - 1); }
const_reference back() const
{ return *(end() - 1); }
void push_back(bool __x)
{
if (this->_M_finish._M_p != this->_M_end_of_storage) if (this->_M_finish._M_p != this->_M_end_of_storage)
*this->_M_finish++ = __x; *this->_M_finish++ = __x;
else else
_M_insert_aux(end(), __x); _M_insert_aux(end(), __x);
} }
void swap(vector<bool, _Alloc>& __x) {
void swap(vector<bool, _Alloc>& __x)
{
std::swap(this->_M_start, __x._M_start); std::swap(this->_M_start, __x._M_start);
std::swap(this->_M_finish, __x._M_finish); std::swap(this->_M_finish, __x._M_finish);
std::swap(this->_M_end_of_storage, __x._M_end_of_storage); std::swap(this->_M_end_of_storage, __x._M_end_of_storage);
} }
// [23.2.5]/1, third-to-last entry in synopsis listing // [23.2.5]/1, third-to-last entry in synopsis listing
static void swap(reference __x, reference __y) { static void swap(reference __x, reference __y)
{
bool __tmp = __x; bool __tmp = __x;
__x = __y; __x = __y;
__y = __tmp; __y = __tmp;
} }
iterator insert(iterator __position, bool __x = bool()) { iterator insert(iterator __position, bool __x = bool())
difference_type __n = __position - begin(); {
const difference_type __n = __position - begin();
if (this->_M_finish._M_p != this->_M_end_of_storage if (this->_M_finish._M_p != this->_M_end_of_storage
&& __position == end()) && __position == end())
*this->_M_finish++ = __x; *this->_M_finish++ = __x;
...@@ -716,74 +778,88 @@ template<typename _Alloc> ...@@ -716,74 +778,88 @@ template<typename _Alloc>
template<class _Integer> template<class _Integer>
void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
__true_type) { __true_type)
_M_fill_insert(__pos, __n, __x); { _M_fill_insert(__pos, __n, __x); }
}
template<class _InputIterator> template<class _InputIterator>
void _M_insert_dispatch(iterator __pos, void _M_insert_dispatch(iterator __pos,
_InputIterator __first, _InputIterator __last, _InputIterator __first, _InputIterator __last,
__false_type) { __false_type)
_M_insert_range(__pos, __first, __last, std::__iterator_category(__first)); { _M_insert_range(__pos, __first, __last,
} std::__iterator_category(__first)); }
template<class _InputIterator> template<class _InputIterator>
void insert(iterator __position, void insert(iterator __position,
_InputIterator __first, _InputIterator __last) { _InputIterator __first, _InputIterator __last)
{
typedef typename _Is_integer<_InputIterator>::_Integral _Integral; typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_insert_dispatch(__position, __first, __last, _Integral()); _M_insert_dispatch(__position, __first, __last, _Integral());
} }
void _M_fill_insert(iterator __position, size_type __n, bool __x) { void _M_fill_insert(iterator __position, size_type __n, bool __x)
if (__n == 0) return; {
if (capacity() - size() >= __n) { if (__n == 0)
std::copy_backward(__position, end(), return;
this->_M_finish + difference_type(__n)); if (capacity() - size() >= __n)
std::fill(__position, __position + difference_type(__n), __x); {
this->_M_finish += difference_type(__n); std::copy_backward(__position, end(),
} this->_M_finish + difference_type(__n));
else { std::fill(__position, __position + difference_type(__n), __x);
size_type __len = size() + std::max(size(), __n); this->_M_finish += difference_type(__n);
_Bit_type * __q = this->_M_bit_alloc(__len); }
iterator __i = std::copy(begin(), __position, iterator(__q, 0)); else
std::fill_n(__i, __n, __x); {
this->_M_finish = std::copy(__position, end(), __i + difference_type(__n)); const size_type __len = size() + std::max(size(), __n);
this->_M_deallocate(); _Bit_type * __q = this->_M_bit_alloc(__len);
this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit; iterator __i = std::copy(begin(), __position, iterator(__q, 0));
this->_M_start = iterator(__q, 0); std::fill_n(__i, __n, __x);
} this->_M_finish = std::copy(__position, end(),
} __i + difference_type(__n));
this->_M_deallocate();
void insert(iterator __position, size_type __n, bool __x) { this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)
_M_fill_insert(__position, __n, __x); / _S_word_bit;
} this->_M_start = iterator(__q, 0);
}
void pop_back() { --this->_M_finish; } }
iterator erase(iterator __position) {
void insert(iterator __position, size_type __n, bool __x)
{ _M_fill_insert(__position, __n, __x); }
void pop_back()
{ --this->_M_finish; }
iterator erase(iterator __position)
{
if (__position + 1 != end()) if (__position + 1 != end())
std::copy(__position + 1, end(), __position); std::copy(__position + 1, end(), __position);
--this->_M_finish; --this->_M_finish;
return __position; return __position;
} }
iterator erase(iterator __first, iterator __last) {
iterator erase(iterator __first, iterator __last)
{
this->_M_finish = std::copy(__last, end(), __first); this->_M_finish = std::copy(__last, end(), __first);
return __first; return __first;
} }
void resize(size_type __new_size, bool __x = bool()) {
void resize(size_type __new_size, bool __x = bool())
{
if (__new_size < size()) if (__new_size < size())
erase(begin() + difference_type(__new_size), end()); erase(begin() + difference_type(__new_size), end());
else else
insert(end(), __new_size - size(), __x); insert(end(), __new_size - size(), __x);
} }
void flip() {
void flip()
{
for (_Bit_type * __p = this->_M_start._M_p; for (_Bit_type * __p = this->_M_start._M_p;
__p != this->_M_end_of_storage; __p != this->_M_end_of_storage; ++__p)
++__p)
*__p = ~*__p; *__p = ~*__p;
} }
void clear() { erase(begin(), end()); } void clear()
{ erase(begin(), end()); }
}; };
} // namespace __gnu_norm } // namespace __gnu_norm
#endif #endif
// RB tree implementation -*- C++ -*- // RB tree implementation -*- C++ -*-
// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -348,16 +348,17 @@ namespace std ...@@ -348,16 +348,17 @@ namespace std
typedef ptrdiff_t difference_type; typedef ptrdiff_t difference_type;
typedef _Alloc allocator_type; typedef _Alloc allocator_type;
allocator_type get_allocator() const { allocator_type get_allocator() const
return *static_cast<const _Node_allocator*>(this); { return *static_cast<const _Node_allocator*>(this); }
}
protected: protected:
_Rb_tree_node* _Rb_tree_node*
_M_get_node() { return _Node_allocator::allocate(1); } _M_get_node()
{ return _Node_allocator::allocate(1); }
void void
_M_put_node(_Rb_tree_node* __p) { _Node_allocator::deallocate(__p, 1); } _M_put_node(_Rb_tree_node* __p)
{ _Node_allocator::deallocate(__p, 1); }
_Link_type _Link_type
_M_create_node(const value_type& __x) _M_create_node(const value_type& __x)
...@@ -367,8 +368,8 @@ namespace std ...@@ -367,8 +368,8 @@ namespace std
{ std::_Construct(&__tmp->_M_value_field, __x); } { std::_Construct(&__tmp->_M_value_field, __x); }
catch(...) catch(...)
{ {
_M_put_node(__tmp); _M_put_node(__tmp);
__throw_exception_again; __throw_exception_again;
} }
return __tmp; return __tmp;
} }
...@@ -397,58 +398,76 @@ namespace std ...@@ -397,58 +398,76 @@ namespace std
protected: protected:
_Base_ptr& _Base_ptr&
_M_root() { return this->_M_header._M_parent; } _M_root()
{ return this->_M_header._M_parent; }
_Const_Base_ptr _Const_Base_ptr
_M_root() const { return this->_M_header._M_parent; } _M_root() const
{ return this->_M_header._M_parent; }
_Base_ptr& _Base_ptr&
_M_leftmost() { return this->_M_header._M_left; } _M_leftmost()
{ return this->_M_header._M_left; }
_Const_Base_ptr _Const_Base_ptr
_M_leftmost() const { return this->_M_header._M_left; } _M_leftmost() const
{ return this->_M_header._M_left; }
_Base_ptr& _Base_ptr&
_M_rightmost() { return this->_M_header._M_right; } _M_rightmost()
{ return this->_M_header._M_right; }
_Const_Base_ptr _Const_Base_ptr
_M_rightmost() const { return this->_M_header._M_right; } _M_rightmost() const
{ return this->_M_header._M_right; }
_Link_type _Link_type
_M_begin() { return static_cast<_Link_type>(this->_M_header._M_parent); } _M_begin()
{ return static_cast<_Link_type>(this->_M_header._M_parent); }
_Const_Link_type _Const_Link_type
_M_begin() const { return static_cast<_Const_Link_type>(this->_M_header._M_parent); } _M_begin() const
{ return static_cast<_Const_Link_type>(this->_M_header._M_parent); }
_Link_type _Link_type
_M_end() { return static_cast<_Link_type>(&this->_M_header); } _M_end()
{ return static_cast<_Link_type>(&this->_M_header); }
_Const_Link_type _Const_Link_type
_M_end() const { return static_cast<_Const_Link_type>(&this->_M_header); } _M_end() const
{ return static_cast<_Const_Link_type>(&this->_M_header); }
static const_reference static const_reference
_S_value(_Const_Link_type __x) { return __x->_M_value_field; } _S_value(_Const_Link_type __x)
{ return __x->_M_value_field; }
static const _Key& static const _Key&
_S_key(_Const_Link_type __x) { return _KeyOfValue()(_S_value(__x)); } _S_key(_Const_Link_type __x)
{ return _KeyOfValue()(_S_value(__x)); }
static _Link_type static _Link_type
_S_left(_Base_ptr __x) { return static_cast<_Link_type>(__x->_M_left); } _S_left(_Base_ptr __x)
{ return static_cast<_Link_type>(__x->_M_left); }
static _Const_Link_type static _Const_Link_type
_S_left(_Const_Base_ptr __x) { return static_cast<_Const_Link_type>(__x->_M_left); } _S_left(_Const_Base_ptr __x)
{ return static_cast<_Const_Link_type>(__x->_M_left); }
static _Link_type static _Link_type
_S_right(_Base_ptr __x) { return static_cast<_Link_type>(__x->_M_right); } _S_right(_Base_ptr __x)
{ return static_cast<_Link_type>(__x->_M_right); }
static _Const_Link_type static _Const_Link_type
_S_right(_Const_Base_ptr __x) { return static_cast<_Const_Link_type>(__x->_M_right); } _S_right(_Const_Base_ptr __x)
{ return static_cast<_Const_Link_type>(__x->_M_right); }
static const_reference static const_reference
_S_value(_Const_Base_ptr __x) { return static_cast<_Const_Link_type>(__x)->_M_value_field; } _S_value(_Const_Base_ptr __x)
{ return static_cast<_Const_Link_type>(__x)->_M_value_field; }
static const _Key& static const _Key&
_S_key(_Const_Base_ptr __x) { return _KeyOfValue()(_S_value(__x)); } _S_key(_Const_Base_ptr __x)
{ return _KeyOfValue()(_S_value(__x)); }
static _Base_ptr static _Base_ptr
_S_minimum(_Base_ptr __x) _S_minimum(_Base_ptr __x)
...@@ -538,40 +557,52 @@ namespace std ...@@ -538,40 +557,52 @@ namespace std
public: public:
// Accessors. // Accessors.
_Compare _Compare
key_comp() const { return _M_key_compare; } key_comp() const
{ return _M_key_compare; }
iterator iterator
begin() { return static_cast<_Link_type>(this->_M_header._M_left); } begin()
{ return static_cast<_Link_type>(this->_M_header._M_left); }
const_iterator const_iterator
begin() const { return static_cast<_Const_Link_type>(this->_M_header._M_left); } begin() const
{ return static_cast<_Const_Link_type>(this->_M_header._M_left); }
iterator iterator
end() { return static_cast<_Link_type>(&this->_M_header); } end()
{ return static_cast<_Link_type>(&this->_M_header); }
const_iterator const_iterator
end() const { return static_cast<_Const_Link_type>(&this->_M_header); } end() const
{ return static_cast<_Const_Link_type>(&this->_M_header); }
reverse_iterator reverse_iterator
rbegin() { return reverse_iterator(end()); } rbegin()
{ return reverse_iterator(end()); }
const_reverse_iterator const_reverse_iterator
rbegin() const { return const_reverse_iterator(end()); } rbegin() const
{ return const_reverse_iterator(end()); }
reverse_iterator reverse_iterator
rend() { return reverse_iterator(begin()); } rend()
{ return reverse_iterator(begin()); }
const_reverse_iterator const_reverse_iterator
rend() const { return const_reverse_iterator(begin()); } rend() const
{ return const_reverse_iterator(begin()); }
bool bool
empty() const { return _M_node_count == 0; } empty() const
{ return _M_node_count == 0; }
size_type size_type
size() const { return _M_node_count; } size() const
{ return _M_node_count; }
size_type size_type
max_size() const { return size_type(-1); } max_size() const
{ return size_type(-1); }
void void
swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t); swap(_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __t);
...@@ -658,8 +689,8 @@ namespace std ...@@ -658,8 +689,8 @@ namespace std
operator==(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x, operator==(const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __x,
const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y) const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>& __y)
{ {
return __x.size() == __y.size() && return __x.size() == __y.size()
equal(__x.begin(), __x.end(), __y.begin()); && equal(__x.begin(), __x.end(), __y.begin());
} }
template<typename _Key, typename _Val, typename _KeyOfValue, template<typename _Key, typename _Val, typename _KeyOfValue,
...@@ -738,8 +769,8 @@ namespace std ...@@ -738,8 +769,8 @@ namespace std
_Link_type __z = _M_create_node(__v); _Link_type __z = _M_create_node(__v);
bool __insert_left; bool __insert_left;
__insert_left = __x != 0 || __p == _M_end() || __insert_left = __x != 0 || __p == _M_end()
_M_key_compare(_KeyOfValue()(__v), _S_key(__p)); || _M_key_compare(_KeyOfValue()(__v), _S_key(__p));
_Rb_tree_insert_and_rebalance(__insert_left, __z, __p, this->_M_header); _Rb_tree_insert_and_rebalance(__insert_left, __z, __p, this->_M_header);
++_M_node_count; ++_M_node_count;
...@@ -757,8 +788,8 @@ namespace std ...@@ -757,8 +788,8 @@ namespace std
while (__x != 0) while (__x != 0)
{ {
__y = __x; __y = __x;
__x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ? __x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
_S_left(__x) : _S_right(__x); _S_left(__x) : _S_right(__x);
} }
return _M_insert(__x, __y, __v); return _M_insert(__x, __y, __v);
} }
...@@ -818,15 +849,15 @@ namespace std ...@@ -818,15 +849,15 @@ namespace std
_Link_type __x = _M_begin(); _Link_type __x = _M_begin();
_Link_type __y = _M_end(); _Link_type __y = _M_end();
bool __comp = true; bool __comp = true;
while (__x != 0) while (__x != 0)
{ {
__y = __x; __y = __x;
__comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)); __comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
__x = __comp ? _S_left(__x) : _S_right(__x); __x = __comp ? _S_left(__x) : _S_right(__x);
} }
iterator __j = iterator(__y); iterator __j = iterator(__y);
if (__comp) if (__comp)
if (__j == begin()) if (__j == begin())
return pair<iterator,bool>(_M_insert(__x, __y, __v), true); return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
else else
--__j; --__j;
...@@ -835,7 +866,6 @@ namespace std ...@@ -835,7 +866,6 @@ namespace std
return pair<iterator,bool>(__j, false); return pair<iterator,bool>(__j, false);
} }
template<typename _Key, typename _Val, typename _KeyOfValue, template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc> typename _Compare, typename _Alloc>
typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
...@@ -845,8 +875,8 @@ namespace std ...@@ -845,8 +875,8 @@ namespace std
if (__position._M_node == _M_leftmost()) if (__position._M_node == _M_leftmost())
{ {
// begin() // begin()
if (size() > 0 && if (size() > 0
_M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node))) && _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
return _M_insert(__position._M_node, __position._M_node, __v); return _M_insert(__position._M_node, __position._M_node, __v);
// first argument just needs to be non-null // first argument just needs to be non-null
else else
...@@ -887,8 +917,9 @@ namespace std ...@@ -887,8 +917,9 @@ namespace std
if (__position._M_node == _M_leftmost()) if (__position._M_node == _M_leftmost())
{ {
// begin() // begin()
if (size() > 0 && if (size() > 0
!_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v))) && !_M_key_compare(_S_key(__position._M_node),
_KeyOfValue()(__v)))
return _M_insert(__position._M_node, __position._M_node, __v); return _M_insert(__position._M_node, __position._M_node, __v);
// first argument just needs to be non-null // first argument just needs to be non-null
else else
...@@ -1060,7 +1091,7 @@ namespace std ...@@ -1060,7 +1091,7 @@ namespace std
template<typename _Key, typename _Val, typename _KeyOfValue, template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc> typename _Compare, typename _Alloc>
typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::const_iterator
_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>:: _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
find(const _Key& __k) const find(const _Key& __k) const
{ {
...@@ -1086,7 +1117,7 @@ namespace std ...@@ -1086,7 +1117,7 @@ namespace std
count(const _Key& __k) const count(const _Key& __k) const
{ {
pair<const_iterator, const_iterator> __p = equal_range(__k); pair<const_iterator, const_iterator> __p = equal_range(__k);
size_type __n = std::distance(__p.first, __p.second); const size_type __n = std::distance(__p.first, __p.second);
return __n; return __n;
} }
...@@ -1165,8 +1196,9 @@ namespace std ...@@ -1165,8 +1196,9 @@ namespace std
template<typename _Key, typename _Val, typename _KeyOfValue, template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc> typename _Compare, typename _Alloc>
inline inline
pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator, pair<typename _Rb_tree<_Key,_Val,_KeyOfValue,
typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator> _Compare,_Alloc>::iterator,
typename _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::iterator>
_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>:: _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::
equal_range(const _Key& __k) equal_range(const _Key& __k)
{ return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); } { return pair<iterator, iterator>(lower_bound(__k), upper_bound(__k)); }
...@@ -1174,14 +1206,13 @@ namespace std ...@@ -1174,14 +1206,13 @@ namespace std
template<typename _Key, typename _Val, typename _KoV, template<typename _Key, typename _Val, typename _KoV,
typename _Compare, typename _Alloc> typename _Compare, typename _Alloc>
inline inline
pair<typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator, pair<typename _Rb_tree<_Key, _Val, _KoV,
typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator> _Compare, _Alloc>::const_iterator,
typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::const_iterator>
_Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc> _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>
::equal_range(const _Key& __k) const ::equal_range(const _Key& __k) const
{ { return pair<const_iterator, const_iterator>(lower_bound(__k),
return pair<const_iterator,const_iterator>(lower_bound(__k), upper_bound(__k)); }
upper_bound(__k));
}
unsigned int unsigned int
_Rb_tree_black_count(const _Rb_tree_node_base* __node, _Rb_tree_black_count(const _Rb_tree_node_base* __node,
...@@ -1193,9 +1224,9 @@ namespace std ...@@ -1193,9 +1224,9 @@ namespace std
_Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
{ {
if (_M_node_count == 0 || begin() == end()) if (_M_node_count == 0 || begin() == end())
return _M_node_count == 0 && begin() == end() && return _M_node_count == 0 && begin() == end()
this->_M_header._M_left == _M_end() && && this->_M_header._M_left == _M_end()
this->_M_header._M_right == _M_end(); && this->_M_header._M_right == _M_end();
unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root()); unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
for (const_iterator __it = begin(); __it != end(); ++__it) for (const_iterator __it = begin(); __it != end(); ++__it)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment