Improved type safety and strict aliasing

[SVN r80404]
This commit is contained in:
Ion Gaztañaga
2012-09-05 09:28:24 +00:00
parent 3c256c2282
commit a8d20305dc
4 changed files with 467 additions and 423 deletions

View File

@@ -157,18 +157,20 @@ class transform_multiallocation_chain
MultiallocationChain holder_; MultiallocationChain holder_;
typedef typename MultiallocationChain::void_pointer void_pointer; typedef typename MultiallocationChain::void_pointer void_pointer;
typedef typename boost::intrusive::pointer_traits typedef typename boost::intrusive::pointer_traits
<void_pointer>::template rebind_pointer<T>::type pointer; <void_pointer> void_pointer_traits;
typedef typename void_pointer_traits::template
rebind_pointer<T>::type pointer;
typedef typename boost::intrusive::pointer_traits
<pointer> pointer_traits;
static pointer cast(void_pointer p) static pointer cast(const void_pointer &p)
{ { return pointer_traits::static_cast_from(p); }
return pointer(static_cast<T*>(container_detail::to_raw_pointer(p)));
}
public: public:
typedef transform_iterator typedef transform_iterator
< typename MultiallocationChain::iterator < typename MultiallocationChain::iterator
, container_detail::cast_functor <T> > iterator; , container_detail::cast_functor <T> > iterator;
typedef typename MultiallocationChain::size_type size_type; typedef typename MultiallocationChain::size_type size_type;
transform_multiallocation_chain() transform_multiallocation_chain()
: holder_() : holder_()
@@ -234,8 +236,11 @@ class transform_multiallocation_chain
static iterator iterator_to(pointer p) static iterator iterator_to(pointer p)
{ return iterator(MultiallocationChain::iterator_to(p)); } { return iterator(MultiallocationChain::iterator_to(p)); }
std::pair<void_pointer, void_pointer> extract_data() std::pair<pointer, pointer> extract_data()
{ return holder_.extract_data(); } {
std::pair<void_pointer, void_pointer> data(holder_.extract_data());
return std::pair<pointer, pointer>(cast(data.first), cast(data.second));
}
MultiallocationChain extract_multiallocation_chain() MultiallocationChain extract_multiallocation_chain()
{ {

View File

@@ -36,10 +36,11 @@
#include <boost/container/detail/algorithms.hpp> #include <boost/container/detail/algorithms.hpp>
#include <boost/container/allocator_traits.hpp> #include <boost/container/allocator_traits.hpp>
#include <boost/intrusive/pointer_traits.hpp> #include <boost/intrusive/pointer_traits.hpp>
#include <boost/aligned_storage.hpp>
#include <algorithm> #include <algorithm> //max
#include <stdexcept> #include <stdexcept>
#include <memory> #include <memory>
#include <new> //placement new
///@cond ///@cond
@@ -104,23 +105,44 @@ class clear_on_destroy
bool do_clear_; bool do_clear_;
}; };
template<class VoidPtr> template<typename VoidPointer, typename T>
struct node_type_base struct node;
{
node_type_base()
{}
void set_pointer(const VoidPtr &p)
{ up = p; }
VoidPtr up; template<class NodePtr>
struct node_base
{
private:
typedef NodePtr node_ptr;
typedef typename boost::intrusive::
pointer_traits<node_ptr> node_ptr_traits;
typedef typename node_ptr_traits::
template rebind_pointer
<node_base>::type node_base_ptr;
typedef typename node_ptr_traits::
template rebind_pointer
<node_ptr>::type node_ptr_ptr;
public:
node_base(const node_ptr_ptr &n)
: up(n)
{}
node_base()
: up()
{}
node_ptr_ptr up;
}; };
template<typename VoidPointer, typename T> template<typename VoidPointer, typename T>
struct node_type struct node
: public node_type_base<VoidPointer> : public node_base
<typename boost::intrusive::
pointer_traits<VoidPointer>::template
rebind_pointer< node<VoidPointer, T> >::type>
{ {
private: private:
node_type(); node();
public: public:
T value; T value;
@@ -135,90 +157,67 @@ class iterator
, Pointer , Pointer
, Reference> , Reference>
{ {
typedef typename boost::intrusive:: typedef boost::intrusive::
pointer_traits<Pointer>::template pointer_traits<Pointer> ptr_traits;
rebind_pointer<void>::type void_ptr; typedef typename ptr_traits::template
typedef typename boost::intrusive:: rebind_pointer<void>::type void_ptr;
pointer_traits<Pointer>::template typedef node<void_ptr, T> node_type;
rebind_pointer<const void>::type const_void_ptr; typedef typename ptr_traits::template
typedef node_type<void_ptr, T> node_type_t; rebind_pointer<node_type>::type node_ptr;
typedef typename boost::intrusive:: typedef typename ptr_traits::template
pointer_traits<Pointer>::template rebind_pointer<node_ptr>::type node_ptr_ptr;
rebind_pointer<node_type_t>::type node_type_ptr_t; typedef typename ptr_traits::template
typedef typename boost::intrusive:: rebind_pointer<T>::type friend_iterator_pointer;
pointer_traits<Pointer>::template
rebind_pointer<const node_type_t>::type const_node_type_ptr_t;
typedef typename boost::intrusive::
pointer_traits<Pointer>::template
rebind_pointer<void_ptr>::type void_ptr_ptr;
friend class iterator<T, const T, typename boost::intrusive::pointer_traits<Pointer>::template rebind_pointer<T>::type>; friend class iterator<T, const T, friend_iterator_pointer>;
public: public:
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
typedef T value_type; typedef T value_type;
typedef typename boost::intrusive:: typedef typename ptr_traits::difference_type difference_type;
pointer_traits<Pointer>::difference_type difference_type;
typedef Pointer pointer; typedef Pointer pointer;
typedef Reference reference; typedef Reference reference;
iterator() iterator()
{} {}
explicit iterator(node_type_ptr_t pn) explicit iterator(node_ptr p)
: pn(pn) : pn(p)
{} {}
iterator(const iterator<T, T&, typename boost::intrusive::pointer_traits<Pointer>::template rebind_pointer<T>::type>& x) iterator(const iterator<T, T&, friend_iterator_pointer>& x)
: pn(x.pn) : pn(x.pn)
{} {}
node_type_ptr_t node() const
{ return pn; }
private:
static node_type_ptr_t node_ptr_cast(const void_ptr &p)
{
return node_type_ptr_t(static_cast<node_type_t*>(container_detail::to_raw_pointer(p)));
}
static const_node_type_ptr_t node_ptr_cast(const const_void_ptr &p)
{
return const_node_type_ptr_t(static_cast<const node_type_t*>(container_detail::to_raw_pointer(p)));
}
static void_ptr_ptr void_ptr_ptr_cast(const void_ptr &p)
{
return void_ptr_ptr(static_cast<void_ptr*>(container_detail::to_raw_pointer(p)));
}
reference dereference() const
{ return pn->value; }
bool equal(const iterator& x) const
{ return pn==x.pn; }
void increment()
{ pn = node_ptr_cast(*(void_ptr_ptr_cast(pn->up)+1)); }
void decrement()
{ pn = node_ptr_cast(*(void_ptr_ptr_cast(pn->up)-1)); }
void advance(difference_type n)
{ pn = node_ptr_cast(*(void_ptr_ptr_cast(pn->up)+n)); }
difference_type distance_to(const iterator& x)const
{ return void_ptr_ptr_cast(x.pn->up) - void_ptr_ptr_cast(pn->up); }
public: public:
//Pointer like operators //Pointer like operators
reference operator*() const { return this->dereference(); } reference operator*() const
pointer operator->() const { return pointer(&this->dereference()); } { return pn->value; }
pointer operator->() const
{ return ptr_traits::pointer_to(*this); }
//Increment / Decrement //Increment / Decrement
iterator& operator++() iterator& operator++()
{ this->increment(); return *this; } {
if(node_ptr_ptr p = this->pn->up){
++p;
this->pn = *p;
}
return *this;
}
iterator operator++(int) iterator operator++(int)
{ iterator tmp(*this); ++*this; return iterator(tmp); } { iterator tmp(*this); ++*this; return iterator(tmp); }
iterator& operator--() iterator& operator--()
{ this->decrement(); return *this; } {
if(node_ptr_ptr p = this->pn->up){
--p;
this->pn = *p;
}
return *this;
}
iterator operator--(int) iterator operator--(int)
{ iterator tmp(*this); --*this; return iterator(tmp); } { iterator tmp(*this); --*this; return iterator(tmp); }
@@ -232,7 +231,10 @@ class iterator
iterator& operator+=(difference_type off) iterator& operator+=(difference_type off)
{ {
pn = node_ptr_cast(*(void_ptr_ptr_cast(pn->up)+off)); if(node_ptr_ptr p = this->pn->up){
p += off;
this->pn = *p;
}
return *this; return *this;
} }
@@ -262,7 +264,7 @@ class iterator
friend difference_type operator-(const iterator& left, const iterator& right) friend difference_type operator-(const iterator& left, const iterator& right)
{ {
return void_ptr_ptr_cast(left.pn->up) - void_ptr_ptr_cast(right.pn->up); return left.pn->up - right.pn->up;
} }
//Comparison operators //Comparison operators
@@ -273,18 +275,18 @@ class iterator
{ return l.pn != r.pn; } { return l.pn != r.pn; }
friend bool operator< (const iterator& l, const iterator& r) friend bool operator< (const iterator& l, const iterator& r)
{ return void_ptr_ptr_cast(l.pn->up) < void_ptr_ptr_cast(r.pn->up); } { return l.pn->up < r.pn->up; }
friend bool operator<= (const iterator& l, const iterator& r) friend bool operator<= (const iterator& l, const iterator& r)
{ return void_ptr_ptr_cast(l.pn->up) <= void_ptr_ptr_cast(r.pn->up); } { return l.pn->up <= r.pn->up; }
friend bool operator> (const iterator& l, const iterator& r) friend bool operator> (const iterator& l, const iterator& r)
{ return void_ptr_ptr_cast(l.pn->up) > void_ptr_ptr_cast(r.pn->up); } { return l.pn->up > r.pn->up; }
friend bool operator>= (const iterator& l, const iterator& r) friend bool operator>= (const iterator& l, const iterator& r)
{ return void_ptr_ptr_cast(l.pn->up) >= void_ptr_ptr_cast(r.pn->up); } { return l.pn->up >= r.pn->up; }
node_type_ptr_t pn; node_ptr pn;
}; };
template<class A, unsigned int Version> template<class A, unsigned int Version>
@@ -368,34 +370,32 @@ class stable_vector
typedef allocator_traits<A> allocator_traits_type; typedef allocator_traits<A> allocator_traits_type;
typedef typename container_detail:: typedef typename container_detail::
move_const_ref_type<T>::type insert_const_ref_type; move_const_ref_type<T>::type insert_const_ref_type;
typedef boost::intrusive::
pointer_traits
<typename allocator_traits_type::pointer> ptr_traits;
typedef typename boost::intrusive::pointer_traits typedef typename boost::intrusive::pointer_traits
<typename allocator_traits_type::pointer>:: <typename allocator_traits_type::pointer>::
template rebind_pointer<void>::type void_ptr; template rebind_pointer<void>::type void_ptr;
typedef typename boost::intrusive::pointer_traits typedef stable_vector_detail::node<void_ptr, T> node_type;
<void_ptr>::template typedef typename ptr_traits::template
rebind_pointer<const void>::type const_void_ptr; rebind_pointer<node_type>::type node_ptr;
typedef typename boost::intrusive::pointer_traits typedef typename ptr_traits::template
<void_ptr>::template rebind_pointer<node_ptr>::type node_ptr_ptr;
rebind_pointer<void_ptr>::type void_ptr_ptr; typedef stable_vector_detail::
typedef typename boost::intrusive::pointer_traits node_base<node_ptr> node_base_type;
<void_ptr>::template typedef typename ptr_traits::template
rebind_pointer<const void_ptr>::type const_void_ptr_ptr; rebind_pointer<node_base_type>::type node_base_ptr;
typedef stable_vector_detail::node_type typedef boost::intrusive::
<void_ptr, T> node_type_t; pointer_traits<node_base_ptr> node_base_ptr_traits;
typedef typename boost::intrusive::pointer_traits typedef boost::intrusive::
<void_ptr>::template pointer_traits<node_ptr> node_ptr_traits;
rebind_pointer<node_type_t>::type node_type_ptr_t; typedef boost::intrusive::
typedef stable_vector_detail::node_type_base pointer_traits<node_ptr_ptr> node_ptr_ptr_traits;
<void_ptr> node_type_base_t; typedef ::boost::container::vector<node_ptr,
typedef typename boost::intrusive::pointer_traits
<void_ptr>::template
rebind_pointer<node_type_base_t>::type node_type_base_ptr_t;
typedef ::boost::container::vector<void_ptr,
typename allocator_traits_type:: typename allocator_traits_type::
template portable_rebind_alloc template portable_rebind_alloc
<void_ptr>::type> impl_type; <node_ptr>::type> impl_type;
typedef typename impl_type::iterator impl_iterator; typedef typename impl_type::iterator impl_iterator;
typedef typename impl_type::const_iterator const_impl_iterator;
typedef ::boost::container::container_detail:: typedef ::boost::container::container_detail::
integral_constant<unsigned, 1> allocator_v1; integral_constant<unsigned, 1> allocator_v1;
@@ -406,37 +406,37 @@ class stable_vector
version<A>::value> alloc_version; version<A>::value> alloc_version;
typedef typename allocator_traits_type:: typedef typename allocator_traits_type::
template portable_rebind_alloc template portable_rebind_alloc
<node_type_t>::type node_allocator_type; <node_type>::type node_allocator_type;
node_type_ptr_t allocate_one() node_ptr allocate_one()
{ return this->allocate_one(alloc_version()); } { return this->allocate_one(alloc_version()); }
template<class AllocatorVersion> template<class AllocatorVersion>
node_type_ptr_t allocate_one(AllocatorVersion, node_ptr allocate_one(AllocatorVersion,
typename boost::container::container_detail::enable_if_c typename boost::container::container_detail::enable_if_c
<boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
{ return this->priv_node_alloc().allocate(1); } { return this->priv_node_alloc().allocate(1); }
template<class AllocatorVersion> template<class AllocatorVersion>
node_type_ptr_t allocate_one(AllocatorVersion, node_ptr allocate_one(AllocatorVersion,
typename boost::container::container_detail::enable_if_c typename boost::container::container_detail::enable_if_c
<!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
{ return this->priv_node_alloc().allocate_one(); } { return this->priv_node_alloc().allocate_one(); }
void deallocate_one(node_type_ptr_t p) void deallocate_one(node_ptr p)
{ return this->deallocate_one(p, alloc_version()); } { return this->deallocate_one(p, alloc_version()); }
template<class AllocatorVersion> template<class AllocatorVersion>
void deallocate_one(node_type_ptr_t p, AllocatorVersion, void deallocate_one(node_ptr p, AllocatorVersion,
typename boost::container::container_detail::enable_if_c typename boost::container::container_detail::enable_if_c
<boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
{ this->priv_node_alloc().deallocate(p, 1); } { this->priv_node_alloc().deallocate(p, 1); }
template<class AllocatorVersion> template<class AllocatorVersion>
void deallocate_one(node_type_ptr_t p, AllocatorVersion, void deallocate_one(node_ptr p, AllocatorVersion,
typename boost::container::container_detail::enable_if_c typename boost::container::container_detail::enable_if_c
<!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
@@ -469,11 +469,11 @@ class stable_vector
private: private:
BOOST_COPYABLE_AND_MOVABLE(stable_vector) BOOST_COPYABLE_AND_MOVABLE(stable_vector)
static const size_type ExtraPointers = 3; static const size_type ExtraPointers = 3;
//This container stores metadata at the end of the void_ptr vector with additional 3 pointers: //This container stores metadata at the end of the node_ptr vector with additional 3 pointers:
// back() is impl.back() - ExtraPointers; // back() is this->impl.back() - ExtraPointers;
// end node index is impl.end()[-3] // end node index is *(this->impl.end() -3)
// Node cache first is impl.end()[-2]; // Node cache first is *(this->impl.end() - 2);
// Node cache last is *impl.back(); // Node cache last is this->impl.back();
typedef typename stable_vector_detail:: typedef typename stable_vector_detail::
select_multiallocation_chain select_multiallocation_chain
@@ -681,7 +681,6 @@ class stable_vector
} }
//If unequal allocators, then do a one by one move //If unequal allocators, then do a one by one move
else{ else{
typedef typename std::iterator_traits<iterator>::iterator_category ItCat;
this->assign( boost::make_move_iterator(x.begin()) this->assign( boost::make_move_iterator(x.begin())
, boost::make_move_iterator(x.end())); , boost::make_move_iterator(x.end()));
} }
@@ -757,14 +756,13 @@ class stable_vector
stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
{ return this->priv_node_alloc(); } { return this->priv_node_alloc(); }
//! <b>Effects</b>: Returns an iterator to the first element contained in the stable_vector. //! <b>Effects</b>: Returns an iterator to the first element contained in the stable_vector.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
iterator begin() iterator begin()
{ return (impl.empty()) ? end(): iterator(node_ptr_cast(impl.front())) ; } { return (this->impl.empty()) ? this->end(): iterator(this->impl.front()) ; }
//! <b>Effects</b>: Returns a const_iterator to the first element contained in the stable_vector. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the stable_vector.
//! //!
@@ -772,21 +770,21 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_iterator begin()const const_iterator begin()const
{ return (impl.empty()) ? cend() : const_iterator(node_ptr_cast(impl.front())) ; } { return (this->impl.empty()) ? this->cend() : const_iterator(this->impl.front()) ; }
//! <b>Effects</b>: Returns an iterator to the end of the stable_vector. //! <b>Effects</b>: Returns an iterator to the end of the stable_vector.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
iterator end() {return iterator(get_end_node());} iterator end() {return iterator(this->get_end_node());}
//! <b>Effects</b>: Returns a const_iterator to the end of the stable_vector. //! <b>Effects</b>: Returns a const_iterator to the end of the stable_vector.
//! //!
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_iterator end()const {return const_iterator(get_end_node());} const_iterator end()const {return const_iterator(this->get_end_node());}
//! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
//! of the reversed stable_vector. //! of the reversed stable_vector.
@@ -856,7 +854,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
size_type size() const size_type size() const
{ return impl.empty() ? 0 : (impl.size() - ExtraPointers); } { return this->impl.empty() ? 0 : (this->impl.size() - ExtraPointers); }
//! <b>Effects</b>: Returns the largest possible size of the stable_vector. //! <b>Effects</b>: Returns the largest possible size of the stable_vector.
//! //!
@@ -864,7 +862,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
size_type max_size() const size_type max_size() const
{ return impl.max_size() - ExtraPointers; } { return this->impl.max_size() - ExtraPointers; }
//! <b>Effects</b>: Number of elements for which memory has been allocated. //! <b>Effects</b>: Number of elements for which memory has been allocated.
//! capacity() is always greater than or equal to size(). //! capacity() is always greater than or equal to size().
@@ -874,7 +872,7 @@ class stable_vector
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
size_type capacity() const size_type capacity() const
{ {
if(!impl.capacity()){ if(!this->impl.capacity()){
return 0; return 0;
} }
else{ else{
@@ -890,7 +888,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
bool empty() const bool empty() const
{ return impl.empty() || impl.size() == ExtraPointers; } { return this->impl.empty() || this->impl.size() == ExtraPointers; }
//! <b>Effects</b>: Inserts or erases elements at the end such that //! <b>Effects</b>: Inserts or erases elements at the end such that
//! the size becomes n. New elements are copy constructed from x. //! the size becomes n. New elements are copy constructed from x.
@@ -901,7 +899,7 @@ class stable_vector
void resize(size_type n, const T& t) void resize(size_type n, const T& t)
{ {
STABLE_VECTOR_CHECK_INVARIANT; STABLE_VECTOR_CHECK_INVARIANT;
if(n > size()) if(n > this->size())
this->insert(this->cend(), n - this->size(), t); this->insert(this->cend(), n - this->size(), t);
else if(n < this->size()) else if(n < this->size())
this->erase(this->cbegin() + n, this->cend()); this->erase(this->cbegin() + n, this->cend());
@@ -917,7 +915,7 @@ class stable_vector
{ {
typedef default_construct_iterator<value_type, difference_type> default_iterator; typedef default_construct_iterator<value_type, difference_type> default_iterator;
STABLE_VECTOR_CHECK_INVARIANT; STABLE_VECTOR_CHECK_INVARIANT;
if(n > size()) if(n > this->size())
this->insert(this->cend(), default_iterator(n - this->size()), default_iterator()); this->insert(this->cend(), default_iterator(n - this->size()), default_iterator());
else if(n < this->size()) else if(n < this->size())
this->erase(this->cbegin() + n, this->cend()); this->erase(this->cbegin() + n, this->cend());
@@ -940,11 +938,11 @@ class stable_vector
if(n > old_capacity){ if(n > old_capacity){
this->initialize_end_node(n); this->initialize_end_node(n);
const void * old_ptr = &impl[0]; const void * old_ptr = &impl[0];
impl.reserve(n + ExtraPointers); this->impl.reserve(n + ExtraPointers);
bool realloced = &impl[0] != old_ptr; bool realloced = &impl[0] != old_ptr;
//Fix the pointers for the newly allocated buffer //Fix the pointers for the newly allocated buffer
if(realloced){ if(realloced){
this->align_nodes(impl.begin(), impl.begin()+size+1); this->align_nodes(this->impl.begin(), this->impl.begin()+size+1);
} }
//Now fill pool if data is not enough //Now fill pool if data is not enough
if((n - size) > this->internal_data.pool_size){ if((n - size) > this->internal_data.pool_size){
@@ -961,7 +959,8 @@ class stable_vector
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
reference operator[](size_type n){return value(impl[n]);} reference operator[](size_type n)
{ return this->impl[n]->value; }
//! <b>Requires</b>: size() > n. //! <b>Requires</b>: size() > n.
//! //!
@@ -971,7 +970,8 @@ class stable_vector
//! <b>Throws</b>: Nothing. //! <b>Throws</b>: Nothing.
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_reference operator[](size_type n)const{return value(impl[n]);} const_reference operator[](size_type n)const
{ return this->impl[n]->value; }
//! <b>Requires</b>: size() > n. //! <b>Requires</b>: size() > n.
//! //!
@@ -983,7 +983,7 @@ class stable_vector
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
reference at(size_type n) reference at(size_type n)
{ {
if(n>=size()) if(n>=this->size())
throw std::out_of_range("invalid subscript at stable_vector::at"); throw std::out_of_range("invalid subscript at stable_vector::at");
return operator[](n); return operator[](n);
} }
@@ -998,7 +998,7 @@ class stable_vector
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_reference at(size_type n)const const_reference at(size_type n)const
{ {
if(n>=size()) if(n>=this->size())
throw std::out_of_range("invalid subscript at stable_vector::at"); throw std::out_of_range("invalid subscript at stable_vector::at");
return operator[](n); return operator[](n);
} }
@@ -1012,7 +1012,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
reference front() reference front()
{ return value(impl.front()); } { return this->impl.front()->value; }
//! <b>Requires</b>: !empty() //! <b>Requires</b>: !empty()
//! //!
@@ -1023,7 +1023,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_reference front()const const_reference front()const
{ return value(impl.front()); } { return this->impl.front()->value; }
//! <b>Requires</b>: !empty() //! <b>Requires</b>: !empty()
//! //!
@@ -1034,7 +1034,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
reference back() reference back()
{ return value(*(&impl.back() - ExtraPointers)); } { return (*(&this->impl.back() - ExtraPointers))->value; }
//! <b>Requires</b>: !empty() //! <b>Requires</b>: !empty()
//! //!
@@ -1045,7 +1045,7 @@ class stable_vector
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_reference back()const const_reference back()const
{ return value(*(&impl.back() - ExtraPointers)); } { return (*(&this->impl.back() - ExtraPointers))->value; }
//! <b>Effects</b>: Inserts a copy of x at the end of the stable_vector. //! <b>Effects</b>: Inserts a copy of x at the end of the stable_vector.
//! //!
@@ -1183,10 +1183,10 @@ class stable_vector
difference_type d = position - this->cbegin(); difference_type d = position - this->cbegin();
if(n){ if(n){
this->insert_iter_prolog(n, d); this->insert_iter_prolog(n, d);
const impl_iterator it(impl.begin() + d); const impl_iterator it(this->impl.begin() + d);
this->priv_insert_iter_fwd(it, first, last, n); this->priv_insert_iter_fwd(it, first, last, n);
//Fix the pointers for the newly allocated buffer //Fix the pointers for the newly allocated buffer
this->align_nodes(it + n, get_last_align()); this->align_nodes(it + n, this->get_end_align());
} }
return this->begin() + d; return this->begin() + d;
} }
@@ -1277,10 +1277,10 @@ class stable_vector
{ {
STABLE_VECTOR_CHECK_INVARIANT; STABLE_VECTOR_CHECK_INVARIANT;
difference_type d = position - this->cbegin(); difference_type d = position - this->cbegin();
impl_iterator it = impl.begin() + d; impl_iterator it = this->impl.begin() + d;
this->delete_node(*it); this->delete_node(*it);
it = impl.erase(it); it = this->impl.erase(it);
this->align_nodes(it, get_last_align()); this->align_nodes(it, this->get_end_align());
return this->begin()+d; return this->begin()+d;
} }
@@ -1295,11 +1295,11 @@ class stable_vector
STABLE_VECTOR_CHECK_INVARIANT; STABLE_VECTOR_CHECK_INVARIANT;
difference_type d1 = first - this->cbegin(), d2 = last - this->cbegin(); difference_type d1 = first - this->cbegin(), d2 = last - this->cbegin();
if(d1 != d2){ if(d1 != d2){
impl_iterator it1(impl.begin() + d1), it2(impl.begin() + d2); impl_iterator it1(this->impl.begin() + d1), it2(this->impl.begin() + d2);
for(impl_iterator it = it1; it != it2; ++it) for(impl_iterator it = it1; it != it2; ++it)
this->delete_node(*it); this->delete_node(*it);
impl_iterator e = impl.erase(it1, it2); impl_iterator e = this->impl.erase(it1, it2);
this->align_nodes(e, get_last_align()); this->align_nodes(e, this->get_end_align());
} }
return iterator(this->begin() + d1); return iterator(this->begin() + d1);
} }
@@ -1342,7 +1342,7 @@ class stable_vector
if(this->empty()){ if(this->empty()){
this->impl.clear(); this->impl.clear();
this->impl.shrink_to_fit(); this->impl.shrink_to_fit();
this->internal_data.set_end_pointer_to_default_constructed(); this->internal_data.end_node.up = node_ptr_ptr();
} }
//Otherwise, try to shrink-to-fit the index and readjust pointers if necessary //Otherwise, try to shrink-to-fit the index and readjust pointers if necessary
else{ else{
@@ -1352,7 +1352,7 @@ class stable_vector
bool realloced = &impl[0] != old_ptr; bool realloced = &impl[0] != old_ptr;
//Fix the pointers for the newly allocated buffer //Fix the pointers for the newly allocated buffer
if(realloced){ if(realloced){
this->align_nodes(impl.begin(), impl.begin()+size+1); this->align_nodes(this->impl.begin(), this->impl.begin()+size+1);
} }
} }
} }
@@ -1375,14 +1375,14 @@ class stable_vector
<boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
{ {
if(!impl.empty() && impl.back()){ if(!this->impl.empty() && this->impl.back()){
void_ptr &pool_first_ref = impl.end()[-2]; node_ptr &pool_first_ref = *(this->impl.end() - 2);
void_ptr &pool_last_ref = impl.back(); node_ptr &pool_last_ref = this->impl.back();
multiallocation_chain holder; multiallocation_chain holder;
holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, this->internal_data.pool_size); holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, this->internal_data.pool_size);
while(!holder.empty()){ while(!holder.empty()){
node_type_ptr_t n = holder.front(); node_ptr n = holder.front();
holder.pop_front(); holder.pop_front();
this->deallocate_one(n); this->deallocate_one(n);
} }
@@ -1397,9 +1397,9 @@ class stable_vector
<!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
{ {
if(!impl.empty() && impl.back()){ if(!this->impl.empty() && this->impl.back()){
void_ptr &pool_first_ref = impl.end()[-2]; node_ptr &pool_first_ref = *(this->impl.end() - 2);
void_ptr &pool_last_ref = impl.back(); node_ptr &pool_last_ref = this->impl.back();
multiallocation_chain holder; multiallocation_chain holder;
holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size); holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size);
this->priv_node_alloc().deallocate_individual(boost::move(holder)); this->priv_node_alloc().deallocate_individual(boost::move(holder));
@@ -1436,50 +1436,50 @@ class stable_vector
<!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1> <!boost::container::container_detail::is_same<AllocatorVersion, allocator_v1>
::value>::type * = 0) ::value>::type * = 0)
{ {
void_ptr &pool_first_ref = impl.end()[-2]; node_ptr &pool_first_ref = *(this->impl.end() - 2);
void_ptr &pool_last_ref = impl.back(); node_ptr &pool_last_ref = this->impl.back();
multiallocation_chain holder; multiallocation_chain holder;
holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size); holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size);
//BOOST_STATIC_ASSERT((::boost::has_move_emulation_enabled<multiallocation_chain>::value == true)); //BOOST_STATIC_ASSERT((::boost::has_move_emulation_enabled<multiallocation_chain>::value == true));
multiallocation_chain m (this->priv_node_alloc().allocate_individual(n)); multiallocation_chain m (this->priv_node_alloc().allocate_individual(n));
holder.splice_after(holder.before_begin(), m, m.before_begin(), m.last(), n); holder.splice_after(holder.before_begin(), m, m.before_begin(), m.last(), n);
this->internal_data.pool_size += n; this->internal_data.pool_size += n;
std::pair<void_ptr, void_ptr> data(holder.extract_data()); std::pair<node_ptr, node_ptr> data(holder.extract_data());
pool_first_ref = data.first; pool_first_ref = data.first;
pool_last_ref = data.second; pool_last_ref = data.second;
} }
void put_in_pool(node_type_ptr_t p) void put_in_pool(node_ptr p)
{ {
void_ptr &pool_first_ref = impl.end()[-2]; node_ptr &pool_first_ref = *(this->impl.end()-2);
void_ptr &pool_last_ref = impl.back(); node_ptr &pool_last_ref = this->impl.back();
multiallocation_chain holder; multiallocation_chain holder;
holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size); holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size);
holder.push_front(p); holder.push_front(p);
++this->internal_data.pool_size; ++this->internal_data.pool_size;
std::pair<void_ptr, void_ptr> ret(holder.extract_data()); std::pair<node_ptr, node_ptr> ret(holder.extract_data());
pool_first_ref = ret.first; pool_first_ref = ret.first;
pool_last_ref = ret.second; pool_last_ref = ret.second;
} }
node_type_ptr_t priv_get_from_pool() node_ptr priv_get_from_pool()
{ {
if(!impl.back()){ if(!this->impl.back()){
return node_type_ptr_t(0); return node_ptr(0);
} }
else{ else{
void_ptr &pool_first_ref = impl.end()[-2]; node_ptr &pool_first_ref = *(this->impl.end() - 2);
void_ptr &pool_last_ref = impl.back(); node_ptr &pool_last_ref = this->impl.back();
multiallocation_chain holder; multiallocation_chain holder;
holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size); holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size);
node_type_ptr_t ret = holder.front(); node_ptr ret = holder.front();
holder.pop_front(); holder.pop_front();
--this->internal_data.pool_size; --this->internal_data.pool_size;
if(!internal_data.pool_size){ if(!internal_data.pool_size){
pool_first_ref = pool_last_ref = void_ptr(0); pool_first_ref = pool_last_ref = node_ptr();
} }
else{ else{
std::pair<void_ptr, void_ptr> data(holder.extract_data()); std::pair<node_ptr, node_ptr> data(holder.extract_data());
pool_first_ref = data.first; pool_first_ref = data.first;
pool_last_ref = data.second; pool_last_ref = data.second;
} }
@@ -1491,79 +1491,62 @@ class stable_vector
{ {
initialize_end_node(n); initialize_end_node(n);
const void* old_ptr = &impl[0]; const void* old_ptr = &impl[0];
//size_type old_capacity = capacity(); //size_type old_capacity = this->capacity();
//size_type old_size = size(); //size_type old_size = this->size();
impl.insert(impl.begin()+d, n, 0); this->impl.insert(this->impl.begin()+d, n, 0);
bool realloced = &impl[0] != old_ptr; bool realloced = &impl[0] != old_ptr;
//Fix the pointers for the newly allocated buffer //Fix the pointers for the newly allocated buffer
if(realloced){ if(realloced){
align_nodes(impl.begin(), impl.begin()+d); align_nodes(this->impl.begin(), this->impl.begin()+d);
} }
} }
impl_iterator get_last_align() impl_iterator get_end_align()
{ {
return impl.end() - (ExtraPointers - 1); return this->impl.end() - (ExtraPointers - 1);
} }
const_impl_iterator get_last_align() const static node_ptr_ptr ptr_to_node_ptr(node_ptr &n)
{ { return node_ptr_ptr_traits::pointer_to(n); }
return impl.cend() - (ExtraPointers - 1);
}
static node_type_ptr_t node_ptr_cast(const void_ptr &p) void initialize_end_node(const size_type impl_capacity = 0)
{ {
return node_type_ptr_t(static_cast<node_type_t*>(container_detail::to_raw_pointer(p))); if(this->impl.empty()){
} this->impl.reserve(impl_capacity + ExtraPointers);
this->impl.resize(ExtraPointers);
static node_type_base_ptr_t node_base_ptr_cast(const void_ptr &p) node_ptr &end_node_ref = impl[0];
{ end_node_ref = this->get_end_node();
return node_type_base_ptr_t(static_cast<node_type_base_t*>(container_detail::to_raw_pointer(p))); this->internal_data.end_node.up = stable_vector::ptr_to_node_ptr(end_node_ref);
}
static value_type& value(const void_ptr &p)
{
return node_ptr_cast(p)->value;
}
void initialize_end_node(size_type impl_capacity = 0)
{
if(impl.empty()){
impl.reserve(impl_capacity + ExtraPointers);
impl.resize (ExtraPointers, void_ptr(0));
impl[0] = &this->internal_data.end_node;
this->internal_data.end_node.up = &impl[0];
} }
} }
void readjust_end_node() void readjust_end_node()
{ {
if(!this->impl.empty()){ if(!this->impl.empty()){
void_ptr &end_node_ref = *(this->get_last_align()-1); node_ptr &end_node_ref = *(this->get_end_align()-1);
end_node_ref = this->get_end_node(); end_node_ref = this->get_end_node();
this->internal_data.end_node.up = &end_node_ref; this->internal_data.end_node.up = node_ptr_ptr_traits::pointer_to(end_node_ref);
} }
else{ else{
this->internal_data.end_node.up = void_ptr(&this->internal_data.end_node.up); this->internal_data.end_node.up = node_ptr_ptr();
} }
} }
node_type_ptr_t get_end_node() const node_ptr get_end_node() const
{ {
const node_type_base_t* cp = &this->internal_data.end_node; return node_ptr_traits::static_cast_from
node_type_base_t* p = const_cast<node_type_base_t*>(cp); (node_base_ptr_traits::pointer_to(const_cast<node_base_type&>(this->internal_data.end_node)));
return node_ptr_cast(p);
} }
template<class Iter> template<class Iter>
void_ptr new_node(const void_ptr &up, Iter it) node_ptr new_node(const node_ptr_ptr &up, Iter it)
{ {
node_type_ptr_t p = this->allocate_one(); node_ptr p = this->allocate_one();
try{ try{
boost::container::construct_in_place(this->priv_node_alloc(), container_detail::addressof(p->value), it); boost::container::construct_in_place(this->priv_node_alloc(), container_detail::addressof(p->value), it);
//This does not throw //This does not throw
::new(static_cast<node_type_base_t*>(container_detail::to_raw_pointer(p))) node_type_base_t; ::new(static_cast<node_base_type*>(container_detail::to_raw_pointer(p))) node_base_type;
p->set_pointer(up); p->up = up;
} }
catch(...){ catch(...){
this->deallocate_one(p); this->deallocate_one(p);
@@ -1572,18 +1555,18 @@ class stable_vector
return p; return p;
} }
void delete_node(const void_ptr &p) void delete_node(const node_ptr &n)
{ {
node_type_ptr_t n(node_ptr_cast(p));
allocator_traits<node_allocator_type>:: allocator_traits<node_allocator_type>::
destroy(this->priv_node_alloc(), container_detail::to_raw_pointer(n)); destroy(this->priv_node_alloc(), container_detail::to_raw_pointer(n));
static_cast<node_base_type*>(container_detail::to_raw_pointer(n))->~node_base_type();
this->put_in_pool(n); this->put_in_pool(n);
} }
static void align_nodes(impl_iterator first, impl_iterator last) static void align_nodes(impl_iterator first, impl_iterator last)
{ {
while(first!=last){ while(first!=last){
node_ptr_cast(*first)->up = void_ptr(&*first); (*first)->up = stable_vector::ptr_to_node_ptr(*first);
++first; ++first;
} }
} }
@@ -1594,14 +1577,14 @@ class stable_vector
size_type i=0; size_type i=0;
try{ try{
while(first!=last){ while(first!=last){
it[i] = this->new_node(void_ptr_ptr(&it[i]), first); it[i] = this->new_node(stable_vector::ptr_to_node_ptr(it[i]), first);
++first; ++first;
++i; ++i;
} }
} }
catch(...){ catch(...){
impl_iterator e = impl.erase(it + i, it + n); impl_iterator e = this->impl.erase(it + i, it + n);
this->align_nodes(e, get_last_align()); this->align_nodes(e, this->get_end_align());
throw; throw;
} }
} }
@@ -1612,7 +1595,7 @@ class stable_vector
multiallocation_chain mem(this->priv_node_alloc().allocate_individual(n)); multiallocation_chain mem(this->priv_node_alloc().allocate_individual(n));
size_type i = 0; size_type i = 0;
node_type_ptr_t p = 0; node_ptr p = 0;
try{ try{
while(first != last){ while(first != last){
p = mem.front(); p = mem.front();
@@ -1620,8 +1603,8 @@ class stable_vector
//This can throw //This can throw
boost::container::construct_in_place(this->priv_node_alloc(), container_detail::addressof(p->value), first); boost::container::construct_in_place(this->priv_node_alloc(), container_detail::addressof(p->value), first);
//This does not throw //This does not throw
::new(static_cast<node_type_base_t*>(container_detail::to_raw_pointer(p))) node_type_base_t; ::new(static_cast<node_base_type*>(container_detail::to_raw_pointer(p))) node_base_type;
p->set_pointer(void_ptr_ptr(&it[i])); p->up = stable_vector::ptr_to_node_ptr(it[i]);
++first; ++first;
it[i] = p; it[i] = p;
++i; ++i;
@@ -1630,8 +1613,8 @@ class stable_vector
catch(...){ catch(...){
this->priv_node_alloc().deallocate_one(p); this->priv_node_alloc().deallocate_one(p);
this->priv_node_alloc().deallocate_many(boost::move(mem)); this->priv_node_alloc().deallocate_many(boost::move(mem));
impl_iterator e = impl.erase(it+i, it+n); impl_iterator e = this->impl.erase(it+i, it+n);
this->align_nodes(e, get_last_align()); this->align_nodes(e, this->get_end_align());
throw; throw;
} }
} }
@@ -1640,7 +1623,7 @@ class stable_vector
void priv_insert_iter_fwd(const impl_iterator it, FwdIterator first, FwdIterator last, difference_type n) void priv_insert_iter_fwd(const impl_iterator it, FwdIterator first, FwdIterator last, difference_type n)
{ {
size_type i = 0; size_type i = 0;
node_type_ptr_t p = 0; node_ptr p = 0;
try{ try{
while(first != last){ while(first != last){
p = this->priv_get_from_pool(); p = this->priv_get_from_pool();
@@ -1651,8 +1634,8 @@ class stable_vector
//This can throw //This can throw
boost::container::construct_in_place(this->priv_node_alloc(), container_detail::addressof(p->value), first); boost::container::construct_in_place(this->priv_node_alloc(), container_detail::addressof(p->value), first);
//This does not throw //This does not throw
::new(static_cast<node_type_base_t*>(container_detail::to_raw_pointer(p))) node_type_base_t; ::new(static_cast<node_base_type*>(container_detail::to_raw_pointer(p))) node_base_type;
p->set_pointer(void_ptr_ptr(&it[i])); p->up = stable_vector::ptr_to_node_ptr(it[i]);
++first; ++first;
it[i]=p; it[i]=p;
++i; ++i;
@@ -1660,8 +1643,8 @@ class stable_vector
} }
catch(...){ catch(...){
put_in_pool(p); put_in_pool(p);
impl_iterator e = impl.erase(it+i, it+n); impl_iterator e = this->impl.erase(it+i, it+n);
this->align_nodes(e, get_last_align()); this->align_nodes(e, this->get_end_align());
throw; throw;
} }
} }
@@ -1676,23 +1659,32 @@ class stable_vector
#if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING) #if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING)
bool priv_invariant()const bool priv_invariant()const
{ {
if(impl.empty()) impl_type & impl_ref = const_cast<impl_type&>(this->impl);
return !capacity() && !size();
if(get_end_node() != *(impl.end() - ExtraPointers)){ if(impl_ref.empty())
return !this->capacity() && !this->size();
if(this->get_end_node() != *(impl_ref.end() - ExtraPointers)){
return false; return false;
} }
for(const_impl_iterator it = impl.begin(), it_end = get_last_align(); it != it_end; ++it){ for( impl_iterator it = impl_ref.begin()
if(const_void_ptr(node_ptr_cast(*it)->up) != , it_end = const_cast<stable_vector&>(*this).get_end_align()
const_void_ptr(const_void_ptr_ptr(&*it))) ; it != it_end
; ++it){
if((*it)->up != stable_vector::ptr_to_node_ptr(*it)){
return false; return false;
}
} }
size_type n = capacity()-size();
const void_ptr &pool_head = impl.back(); size_type n = this->capacity() - this->size();
node_ptr &pool_first_ref = *(impl_ref.end() - 2);
node_ptr &pool_last_ref = impl_ref.back();
multiallocation_chain holder;
holder.incorporate_after(holder.before_begin(), pool_first_ref, pool_last_ref, internal_data.pool_size);
multiallocation_chain::iterator beg(holder.begin()), end(holder.end());
size_type num_pool = 0; size_type num_pool = 0;
node_type_ptr_t p = node_ptr_cast(pool_head); while(beg != end){
while(p){
++num_pool; ++num_pool;
p = node_ptr_cast(p->up); ++beg;
} }
return n >= num_pool; return n >= num_pool;
} }
@@ -1722,25 +1714,16 @@ class stable_vector
: node_allocator_type(boost::forward<AllocatorRLValue>(a)) : node_allocator_type(boost::forward<AllocatorRLValue>(a))
, pool_size(0) , pool_size(0)
, end_node() , end_node()
{ {}
this->set_end_pointer_to_default_constructed();
}
ebo_holder() ebo_holder()
: node_allocator_type() : node_allocator_type()
, pool_size(0) , pool_size(0)
, end_node() , end_node()
{ {}
this->set_end_pointer_to_default_constructed();
}
void set_end_pointer_to_default_constructed()
{
end_node.set_pointer(void_ptr(&end_node.up));
}
size_type pool_size; size_type pool_size;
node_type_base_t end_node; node_base_type end_node;
} internal_data; } internal_data;
node_allocator_type &priv_node_alloc() { return internal_data; } node_allocator_type &priv_node_alloc() { return internal_data; }
@@ -1805,3 +1788,24 @@ void swap(stable_vector<T,A>& x,stable_vector<T,A>& y)
#include <boost/container/detail/config_end.hpp> #include <boost/container/detail/config_end.hpp>
#endif //BOOST_CONTAINER_STABLE_VECTOR_HPP #endif //BOOST_CONTAINER_STABLE_VECTOR_HPP

View File

@@ -48,6 +48,7 @@
#include <boost/move/move.hpp> #include <boost/move/move.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include <functional> #include <functional>
#include <string> #include <string>
@@ -92,10 +93,11 @@ class basic_string_base
public: public:
typedef A allocator_type; typedef A allocator_type;
//! The stored allocator type //! The stored allocator type
typedef allocator_type stored_allocator_type; typedef allocator_type stored_allocator_type;
typedef typename allocator_traits_type::pointer pointer; typedef typename allocator_traits_type::pointer pointer;
typedef typename allocator_traits_type::value_type value_type; typedef typename allocator_traits_type::value_type value_type;
typedef typename allocator_traits_type::size_type size_type; typedef typename allocator_traits_type::size_type size_type;
typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
basic_string_base() basic_string_base()
: members_() : members_()
@@ -123,10 +125,7 @@ class basic_string_base
{ {
if(!this->is_short()){ if(!this->is_short()){
this->deallocate_block(); this->deallocate_block();
allocator_traits_type::destroy this->is_short(true);
( this->alloc()
, static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
);
} }
} }
@@ -198,11 +197,17 @@ class basic_string_base
long_raw_t r; long_raw_t r;
short_t s; short_t s;
short_t &short_repr() const const short_t &short_repr() const
{ return *const_cast<short_t *>(&s); } { return s; }
long_t &long_repr() const const long_t &long_repr() const
{ return *const_cast<long_t*>(reinterpret_cast<const long_t*>(&r)); } { return *static_cast<const long_t*>(static_cast<const void*>(&r)); }
short_t &short_repr()
{ return s; }
long_t &long_repr()
{ return *static_cast<long_t*>(static_cast<void*>(&r)); }
}; };
struct members_holder struct members_holder
@@ -237,20 +242,22 @@ class basic_string_base
{ return static_cast<bool>(this->members_.m_repr.s.h.is_short != 0); } { return static_cast<bool>(this->members_.m_repr.s.h.is_short != 0); }
void is_short(bool yes) void is_short(bool yes)
{ {
if(yes && !this->is_short()){ const bool was_short = this->is_short();
if(yes && !was_short){
allocator_traits_type::destroy allocator_traits_type::destroy
( this->alloc() ( this->alloc()
, static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r)) , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
); );
this->members_.m_repr.s.h.is_short = true;
} }
else{ else if(!yes && was_short){
allocator_traits_type::construct allocator_traits_type::construct
( this->alloc() ( this->alloc()
, static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r)) , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
); );
this->members_.m_repr.s.h.is_short = false;
} }
this->members_.m_repr.s.h.is_short = yes;
} }
private: private:
@@ -274,7 +281,7 @@ class basic_string_base
size_type &received_size, pointer reuse = 0) size_type &received_size, pointer reuse = 0)
{ {
if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){ if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
reuse = pointer(0); reuse = pointer();
command &= ~(expand_fwd | expand_bwd); command &= ~(expand_fwd | expand_bwd);
} }
return this->allocation_command return this->allocation_command
@@ -292,7 +299,7 @@ class basic_string_base
(void)limit_size; (void)limit_size;
(void)reuse; (void)reuse;
if(!(command & allocate_new)) if(!(command & allocate_new))
return std::pair<pointer, bool>(pointer(0), false); return std::pair<pointer, bool>(pointer(), false);
received_size = preferred_size; received_size = preferred_size;
return std::make_pair(this->alloc().allocate(received_size), false); return std::make_pair(this->alloc().allocate(received_size), false);
} }
@@ -353,7 +360,7 @@ class basic_string_base
pointer p = this->allocation_command(allocate_new, n, new_cap, new_cap).first; pointer p = this->allocation_command(allocate_new, n, new_cap, new_cap).first;
this->is_short(false); this->is_short(false);
this->priv_long_addr(p); this->priv_long_addr(p);
this->priv_size(0); this->priv_long_size(0);
this->priv_storage(new_cap); this->priv_storage(new_cap);
} }
} }
@@ -379,13 +386,26 @@ class basic_string_base
{ return this->priv_storage() - 1; } { return this->priv_storage() - 1; }
pointer priv_short_addr() const pointer priv_short_addr() const
{ return pointer(&this->members_.m_repr.short_repr().data[0]); } { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.m_repr.short_repr().data[0])); }
pointer priv_long_addr() const pointer priv_long_addr() const
{ return this->members_.m_repr.long_repr().start; } { return this->members_.m_repr.long_repr().start; }
pointer priv_addr() const pointer priv_addr() const
{ return this->is_short() ? pointer(&this->members_.m_repr.short_repr().data[0]) : this->members_.m_repr.long_repr().start; } {
return this->is_short()
? priv_short_addr()
: priv_long_addr()
;
}
pointer priv_end_addr() const
{
return this->is_short()
? this->priv_short_addr() + this->priv_short_size()
: this->priv_long_addr() + this->priv_long_size()
;
}
void priv_long_addr(pointer addr) void priv_long_addr(pointer addr)
{ this->members_.m_repr.long_repr().start = addr; } { this->members_.m_repr.long_repr().start = addr; }
@@ -411,7 +431,7 @@ class basic_string_base
} }
size_type priv_size() const size_type priv_size() const
{ return this->is_short() ? priv_short_size() : priv_long_size(); } { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
size_type priv_short_size() const size_type priv_short_size() const
{ return this->members_.m_repr.short_repr().h.length; } { return this->members_.m_repr.short_repr().h.length; }
@@ -434,7 +454,7 @@ class basic_string_base
void priv_long_size(size_type sz) void priv_long_size(size_type sz)
{ {
this->members_.m_repr.long_repr().length = static_cast<typename allocator_traits_type::size_type>(sz); this->members_.m_repr.long_repr().length = sz;
} }
void swap_data(basic_string_base& other) void swap_data(basic_string_base& other)
@@ -444,16 +464,22 @@ class basic_string_base
container_detail::do_swap(this->members_.m_repr, other.members_.m_repr); container_detail::do_swap(this->members_.m_repr, other.members_.m_repr);
} }
else{ else{
repr_t copied(this->members_.m_repr); short_t short_backup(this->members_.m_repr.short_repr());
this->members_.m_repr.long_repr() = other.members_.m_repr.long_repr(); long_t long_backup (other.members_.m_repr.long_repr());
other.members_.m_repr = copied; other.members_.m_repr.long_repr().~long_t();
::new(&this->members_.m_repr.long_repr()) long_t;
this->members_.m_repr.long_repr() = long_backup;
other.members_.m_repr.short_repr() = short_backup;
} }
} }
else{ else{
if(other.is_short()){ if(other.is_short()){
repr_t copied(other.members_.m_repr); short_t short_backup(other.members_.m_repr.short_repr());
other.members_.m_repr.long_repr() = this->members_.m_repr.long_repr(); long_t long_backup (this->members_.m_repr.long_repr());
this->members_.m_repr = copied; this->members_.m_repr.long_repr().~long_t();
::new(&other.members_.m_repr.long_repr()) long_t;
other.members_.m_repr.long_repr() = long_backup;
this->members_.m_repr.short_repr() = short_backup;
} }
else{ else{
container_detail::do_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr()); container_detail::do_swap(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
@@ -581,6 +607,7 @@ class basic_string
typedef typename base_t::allocator_v1 allocator_v1; typedef typename base_t::allocator_v1 allocator_v1;
typedef typename base_t::allocator_v2 allocator_v2; typedef typename base_t::allocator_v2 allocator_v2;
typedef typename base_t::alloc_version alloc_version; typedef typename base_t::alloc_version alloc_version;
typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
/// @endcond /// @endcond
public: // Constructor, destructor, assignment. public: // Constructor, destructor, assignment.
@@ -742,8 +769,8 @@ class basic_string
if(!this->is_short()){ if(!this->is_short()){
this->deallocate_block(); this->deallocate_block();
this->is_short(true); this->is_short(true);
Traits::assign(*this->priv_addr(), this->priv_null()); Traits::assign(*this->priv_addr(), CharT(0));
this->priv_size(0); this->priv_short_size(0);
} }
} }
container_detail::assign_alloc(this->alloc(), x.alloc(), flag); container_detail::assign_alloc(this->alloc(), x.alloc(), flag);
@@ -818,7 +845,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
iterator end() iterator end()
{ return this->priv_addr() + this->priv_size(); } { return this->priv_end_addr(); }
//! <b>Effects</b>: Returns a const_iterator to the end of the vector. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
//! //!
@@ -826,7 +853,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_iterator end() const const_iterator end() const
{ return this->priv_addr() + this->priv_size(); } { return this->priv_end_addr(); }
//! <b>Effects</b>: Returns a const_iterator to the end of the vector. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
//! //!
@@ -834,7 +861,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_iterator cend() const const_iterator cend() const
{ return this->priv_addr() + this->priv_size(); } { return this->priv_end_addr(); }
//! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
//! of the reversed vector. //! of the reversed vector.
@@ -843,7 +870,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
reverse_iterator rbegin() reverse_iterator rbegin()
{ return reverse_iterator(this->priv_addr() + this->priv_size()); } { return reverse_iterator(this->priv_end_addr()); }
//! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
//! of the reversed vector. //! of the reversed vector.
@@ -861,7 +888,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_reverse_iterator crbegin() const const_reverse_iterator crbegin() const
{ return const_reverse_iterator(this->priv_addr() + this->priv_size()); } { return const_reverse_iterator(this->priv_end_addr()); }
//! <b>Effects</b>: Returns a reverse_iterator pointing to the end //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
//! of the reversed vector. //! of the reversed vector.
@@ -950,7 +977,7 @@ class basic_string
//! <b>Complexity</b>: Linear to the difference between size() and new_size. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
void resize(size_type n, CharT c) void resize(size_type n, CharT c)
{ {
if (n <= size()) if (n <= this->size())
this->erase(this->begin() + n, this->end()); this->erase(this->begin() + n, this->end());
else else
this->append(n - this->size(), c); this->append(n - this->size(), c);
@@ -963,7 +990,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Linear to the difference between size() and new_size. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
void resize(size_type n) void resize(size_type n)
{ resize(n, this->priv_null()); } { resize(n, CharT(0)); }
//! <b>Effects</b>: If n is less than or equal to capacity(), this call has no //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
//! effect. Otherwise, it is a request for allocation of additional memory. //! effect. Otherwise, it is a request for allocation of additional memory.
@@ -973,8 +1000,9 @@ class basic_string
//! <b>Throws</b>: If memory allocation allocation throws //! <b>Throws</b>: If memory allocation allocation throws
void reserve(size_type res_arg) void reserve(size_type res_arg)
{ {
if (res_arg > this->max_size()) if (res_arg > this->max_size()){
this->throw_length_error(); this->throw_length_error();
}
if (this->capacity() < res_arg){ if (this->capacity() < res_arg){
size_type n = container_detail::max_value(res_arg, this->size()) + 1; size_type n = container_detail::max_value(res_arg, this->size()) + 1;
@@ -983,13 +1011,14 @@ class basic_string
(allocate_new, n, new_cap, new_cap).first; (allocate_new, n, new_cap, new_cap).first;
size_type new_length = 0; size_type new_length = 0;
const pointer addr = this->priv_addr();
new_length += priv_uninitialized_copy new_length += priv_uninitialized_copy
(this->priv_addr(), this->priv_addr() + this->priv_size(), new_start); (addr, addr + this->priv_size(), new_start);
this->priv_construct_null(new_start + new_length); this->priv_construct_null(new_start + new_length);
this->deallocate_block(); this->deallocate_block();
this->is_short(false); this->is_short(false);
this->priv_long_addr(new_start); this->priv_long_addr(new_start);
this->priv_size(new_length); this->priv_long_size(new_length);
this->priv_storage(new_cap); this->priv_storage(new_cap);
} }
} }
@@ -1010,8 +1039,8 @@ class basic_string
//! <b>Complexity</b>: Linear to the number of elements in the vector. //! <b>Complexity</b>: Linear to the number of elements in the vector.
void clear() void clear()
{ {
if (!empty()) { if (!this->empty()) {
Traits::assign(*this->priv_addr(), this->priv_null()); Traits::assign(*this->priv_addr(), CharT(0));
this->priv_size(0); this->priv_size(0);
} }
} }
@@ -1028,7 +1057,7 @@ class basic_string
if(this->priv_storage() > InternalBufferChars){ if(this->priv_storage() > InternalBufferChars){
//Check if we should pass from dynamically allocated buffer //Check if we should pass from dynamically allocated buffer
//to the internal storage //to the internal storage
if(this->priv_size() < (InternalBufferChars)){ if(this->priv_size() < InternalBufferChars){
//Dynamically allocated buffer attributes //Dynamically allocated buffer attributes
pointer long_addr = this->priv_long_addr(); pointer long_addr = this->priv_long_addr();
size_type long_storage = this->priv_long_storage(); size_type long_storage = this->priv_long_storage();
@@ -1085,9 +1114,10 @@ class basic_string
//! <b>Throws</b>: std::range_error if n >= size() //! <b>Throws</b>: std::range_error if n >= size()
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
reference at(size_type n) { reference at(size_type n)
if (n >= size()) {
this->throw_out_of_range(); if (n >= this->size())
this->throw_out_of_range();
return *(this->priv_addr() + n); return *(this->priv_addr() + n);
} }
@@ -1100,7 +1130,7 @@ class basic_string
//! //!
//! <b>Complexity</b>: Constant. //! <b>Complexity</b>: Constant.
const_reference at(size_type n) const { const_reference at(size_type n) const {
if (n >= size()) if (n >= this->size())
this->throw_out_of_range(); this->throw_out_of_range();
return *(this->priv_addr() + n); return *(this->priv_addr() + n);
} }
@@ -1186,13 +1216,14 @@ class basic_string
{ {
const size_type old_size = this->priv_size(); const size_type old_size = this->priv_size();
if (old_size < this->capacity()){ if (old_size < this->capacity()){
this->priv_construct_null(this->priv_addr() + old_size + 1); const pointer addr = this->priv_addr();
Traits::assign(this->priv_addr()[old_size], c); this->priv_construct_null(addr + old_size + 1);
Traits::assign(addr[old_size], c);
this->priv_size(old_size+1); this->priv_size(old_size+1);
} }
else{ else{
//No enough memory, insert a new object at the end //No enough memory, insert a new object at the end
this->append((size_type)1, c); this->append(size_type(1), c);
} }
} }
@@ -1266,7 +1297,8 @@ class basic_string
) )
{ {
size_type cur = 0; size_type cur = 0;
CharT *ptr = container_detail::to_raw_pointer(this->priv_addr()); const pointer addr = this->priv_addr();
CharT *ptr = container_detail::to_raw_pointer(addr);
const size_type old_size = this->priv_size(); const size_type old_size = this->priv_size();
while (first != last && cur != old_size) { while (first != last && cur != old_size) {
Traits::assign(*ptr, *first); Traits::assign(*ptr, *first);
@@ -1275,7 +1307,7 @@ class basic_string
++ptr; ++ptr;
} }
if (first == last) if (first == last)
this->erase(this->priv_addr() + cur, this->priv_addr() + old_size); this->erase(addr + cur, addr + old_size);
else else
this->append(first, last); this->append(first, last);
return *this; return *this;
@@ -1290,9 +1322,10 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& insert(size_type pos, const basic_string& s) basic_string& insert(size_type pos, const basic_string& s)
{ {
if (pos > size()) const size_type size = this->size();
if (pos > size)
this->throw_out_of_range(); this->throw_out_of_range();
if (this->size() > this->max_size() - s.size()) if (size > this->max_size() - s.size())
this->throw_length_error(); this->throw_length_error();
this->insert(this->priv_addr() + pos, s.begin(), s.end()); this->insert(this->priv_addr() + pos, s.begin(), s.end());
return *this; return *this;
@@ -1308,10 +1341,12 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n) basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n)
{ {
if (pos1 > this->size() || pos2 > s.size()) const size_type size = this->size();
const size_type str_size = s.size();
if (pos1 > size || pos2 > str_size)
this->throw_out_of_range(); this->throw_out_of_range();
size_type len = container_detail::min_value(n, s.size() - pos2); size_type len = container_detail::min_value(n, str_size - pos2);
if (this->size() > this->max_size() - len) if (size > this->max_size() - len)
this->throw_length_error(); this->throw_length_error();
const CharT *beg_ptr = container_detail::to_raw_pointer(s.begin()) + pos2; const CharT *beg_ptr = container_detail::to_raw_pointer(s.begin()) + pos2;
const CharT *end_ptr = beg_ptr + len; const CharT *end_ptr = beg_ptr + len;
@@ -1350,7 +1385,7 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& insert(size_type pos, const CharT* s) basic_string& insert(size_type pos, const CharT* s)
{ {
if (pos > size()) if (pos > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
size_type len = Traits::length(s); size_type len = Traits::length(s);
if (this->size() > this->max_size() - len) if (this->size() > this->max_size() - len)
@@ -1457,11 +1492,11 @@ class basic_string
//Reuse same buffer //Reuse same buffer
if(enough_capacity){ if(enough_capacity){
const size_type elems_after = old_size - (p - this->priv_addr()); const size_type elems_after = old_size - (p - old_start);
const size_type old_length = old_size; const size_type old_length = old_size;
if (elems_after >= n) { if (elems_after >= n) {
const pointer pointer_past_last = this->priv_addr() + old_size + 1; const pointer pointer_past_last = old_start + old_size + 1;
priv_uninitialized_copy(this->priv_addr() + (old_size - n + 1), priv_uninitialized_copy(old_start + (old_size - n + 1),
pointer_past_last, pointer_past_last); pointer_past_last, pointer_past_last);
this->priv_size(old_size+n); this->priv_size(old_size+n);
@@ -1474,12 +1509,13 @@ class basic_string
ForwardIter mid = first; ForwardIter mid = first;
std::advance(mid, elems_after + 1); std::advance(mid, elems_after + 1);
priv_uninitialized_copy(mid, last, this->priv_addr() + old_size + 1); priv_uninitialized_copy(mid, last, old_start + old_size + 1);
this->priv_size(old_size + (n - elems_after)); const size_type newer_size = old_size + (n - elems_after);
this->priv_size(newer_size);
priv_uninitialized_copy priv_uninitialized_copy
(p, const_iterator(this->priv_addr() + old_length + 1), (p, const_iterator(old_start + old_length + 1),
this->priv_addr() + this->priv_size()); old_start + newer_size);
this->priv_size(this->priv_size() + elems_after); this->priv_size(newer_size + elems_after);
this->priv_copy(first, mid, const_cast<CharT*>(container_detail::to_raw_pointer(p))); this->priv_copy(first, mid, const_cast<CharT*>(container_detail::to_raw_pointer(p)));
} }
} }
@@ -1490,11 +1526,11 @@ class basic_string
size_type new_length = 0; size_type new_length = 0;
//This can't throw, since characters are POD //This can't throw, since characters are POD
new_length += priv_uninitialized_copy new_length += priv_uninitialized_copy
(const_iterator(this->priv_addr()), p, new_start); (const_iterator(old_start), p, new_start);
new_length += priv_uninitialized_copy new_length += priv_uninitialized_copy
(first, last, new_start + new_length); (first, last, new_start + new_length);
new_length += priv_uninitialized_copy new_length += priv_uninitialized_copy
(p, const_iterator(this->priv_addr() + old_size), (p, const_iterator(old_start + old_size),
new_start + new_length); new_start + new_length);
this->priv_construct_null(new_start + new_length); this->priv_construct_null(new_start + new_length);
@@ -1542,9 +1578,10 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& erase(size_type pos = 0, size_type n = npos) basic_string& erase(size_type pos = 0, size_type n = npos)
{ {
if (pos > size()) if (pos > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
erase(this->priv_addr() + pos, this->priv_addr() + pos + container_detail::min_value(n, size() - pos)); const pointer addr = this->priv_addr();
erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos));
return *this; return *this;
} }
@@ -1597,7 +1634,7 @@ class basic_string
void pop_back() void pop_back()
{ {
const size_type old_size = this->priv_size(); const size_type old_size = this->priv_size();
Traits::assign(this->priv_addr()[old_size-1], this->priv_null()); Traits::assign(this->priv_addr()[old_size-1], CharT(0));
this->priv_size(old_size-1);; this->priv_size(old_size-1);;
} }
@@ -1610,13 +1647,14 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& replace(size_type pos1, size_type n1, const basic_string& str) basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
{ {
if (pos1 > size()) if (pos1 > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
const size_type len = container_detail::min_value(n1, size() - pos1); const size_type len = container_detail::min_value(n1, this->size() - pos1);
if (this->size() - len >= this->max_size() - str.size()) if (this->size() - len >= this->max_size() - str.size())
this->throw_length_error(); this->throw_length_error();
return this->replace( const_iterator(this->priv_addr() + pos1) const pointer addr = this->priv_addr();
, const_iterator(this->priv_addr() + pos1 + len) return this->replace( const_iterator(addr + pos1)
, const_iterator(addr + pos1 + len)
, str.begin(), str.end()); , str.begin(), str.end());
} }
@@ -1632,14 +1670,16 @@ class basic_string
basic_string& replace(size_type pos1, size_type n1, basic_string& replace(size_type pos1, size_type n1,
const basic_string& str, size_type pos2, size_type n2) const basic_string& str, size_type pos2, size_type n2)
{ {
if (pos1 > size() || pos2 > str.size()) if (pos1 > this->size() || pos2 > str.size())
this->throw_out_of_range(); this->throw_out_of_range();
const size_type len1 = container_detail::min_value(n1, size() - pos1); const size_type len1 = container_detail::min_value(n1, this->size() - pos1);
const size_type len2 = container_detail::min_value(n2, str.size() - pos2); const size_type len2 = container_detail::min_value(n2, str.size() - pos2);
if (this->size() - len1 >= this->max_size() - len2) if (this->size() - len1 >= this->max_size() - len2)
this->throw_length_error(); this->throw_length_error();
return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len1, const pointer addr = this->priv_addr();
str.priv_addr() + pos2, str.priv_addr() + pos2 + len2); const pointer straddr = str.priv_addr();
return this->replace(addr + pos1, addr + pos1 + len1,
straddr + pos2, straddr + pos2 + len2);
} }
//! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT. //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
@@ -1658,12 +1698,13 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2) basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
{ {
if (pos1 > size()) if (pos1 > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
const size_type len = container_detail::min_value(n1, size() - pos1); const size_type len = container_detail::min_value(n1, this->size() - pos1);
if (n2 > this->max_size() || size() - len >= this->max_size() - n2) if (n2 > this->max_size() || size() - len >= this->max_size() - n2)
this->throw_length_error(); this->throw_length_error();
return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len, s, s + n2); const pointer addr = this->priv_addr();
return this->replace(addr + pos1, addr + pos1 + len, s, s + n2);
} }
//! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT. //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
@@ -1682,13 +1723,14 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& replace(size_type pos, size_type n1, const CharT* s) basic_string& replace(size_type pos, size_type n1, const CharT* s)
{ {
if (pos > size()) if (pos > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
const size_type len = container_detail::min_value(n1, size() - pos); const size_type len = container_detail::min_value(n1, this->size() - pos);
const size_type n2 = Traits::length(s); const size_type n2 = Traits::length(s);
if (n2 > this->max_size() || size() - len >= this->max_size() - n2) if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
this->throw_length_error(); this->throw_length_error();
return this->replace(this->priv_addr() + pos, this->priv_addr() + pos + len, const pointer addr = this->priv_addr();
return this->replace(addr + pos, addr + pos + len,
s, s + Traits::length(s)); s, s + Traits::length(s));
} }
@@ -1702,12 +1744,13 @@ class basic_string
//! <b>Returns</b>: *this //! <b>Returns</b>: *this
basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c) basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
{ {
if (pos1 > size()) if (pos1 > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
const size_type len = container_detail::min_value(n1, size() - pos1); const size_type len = container_detail::min_value(n1, this->size() - pos1);
if (n2 > this->max_size() || size() - len >= this->max_size() - n2) if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
this->throw_length_error(); this->throw_length_error();
return this->replace(this->priv_addr() + pos1, this->priv_addr() + pos1 + len, n2, c); const pointer addr = this->priv_addr();
return this->replace(addr + pos1, addr + pos1 + len, n2, c);
} }
//! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges. //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
@@ -1829,9 +1872,9 @@ class basic_string
//! <b>Returns</b>: rlen //! <b>Returns</b>: rlen
size_type copy(CharT* s, size_type n, size_type pos = 0) const size_type copy(CharT* s, size_type n, size_type pos = 0) const
{ {
if (pos > size()) if (pos > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
const size_type len = container_detail::min_value(n, size() - pos); const size_type len = container_detail::min_value(n, this->size() - pos);
Traits::copy(s, container_detail::to_raw_pointer(this->priv_addr() + pos), len); Traits::copy(s, container_detail::to_raw_pointer(this->priv_addr() + pos), len);
return len; return len;
} }
@@ -1880,12 +1923,13 @@ class basic_string
//! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos). //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos).
size_type find(const CharT* s, size_type pos, size_type n) const size_type find(const CharT* s, size_type pos, size_type n) const
{ {
if (pos + n > size()) if (pos + n > this->size())
return npos; return npos;
else { else {
pointer finish = this->priv_addr() + this->priv_size(); const pointer addr = this->priv_addr();
pointer finish = addr + this->priv_size();
const const_iterator result = const const_iterator result =
std::search(container_detail::to_raw_pointer(this->priv_addr() + pos), std::search(container_detail::to_raw_pointer(addr + pos),
container_detail::to_raw_pointer(finish), container_detail::to_raw_pointer(finish),
s, s + n, Eq_traits<Traits>()); s, s + n, Eq_traits<Traits>());
return result != finish ? result - begin() : npos; return result != finish ? result - begin() : npos;
@@ -1898,19 +1942,21 @@ class basic_string
//! //!
//! <b>Returns</b>: find(basic_string(s), pos). //! <b>Returns</b>: find(basic_string(s), pos).
size_type find(const CharT* s, size_type pos = 0) const size_type find(const CharT* s, size_type pos = 0) const
{ return find(s, pos, Traits::length(s)); } { return this->find(s, pos, Traits::length(s)); }
//! <b>Throws</b>: Nothing //! <b>Throws</b>: Nothing
//! //!
//! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos). //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos).
size_type find(CharT c, size_type pos = 0) const size_type find(CharT c, size_type pos = 0) const
{ {
if (pos >= size()) const size_type size = this->size();
if (pos >= size)
return npos; return npos;
else { else {
pointer finish = this->priv_addr() + this->priv_size(); const pointer addr = this->priv_addr();
pointer finish = addr + size;
const const_iterator result = const const_iterator result =
std::find_if(this->priv_addr() + pos, finish, std::find_if(addr + pos, finish,
std::bind2nd(Eq_traits<Traits>(), c)); std::bind2nd(Eq_traits<Traits>(), c));
return result != finish ? result - begin() : npos; return result != finish ? result - begin() : npos;
} }
@@ -1934,7 +1980,7 @@ class basic_string
//! <b>Returns</b>: rfind(basic_string(s, n), pos). //! <b>Returns</b>: rfind(basic_string(s, n), pos).
size_type rfind(const CharT* s, size_type pos, size_type n) const size_type rfind(const CharT* s, size_type pos, size_type n) const
{ {
const size_type len = size(); const size_type len = this->size();
if (n > len) if (n > len)
return npos; return npos;
@@ -1963,7 +2009,7 @@ class basic_string
//! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos). //! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos).
size_type rfind(CharT c, size_type pos = npos) const size_type rfind(CharT c, size_type pos = npos) const
{ {
const size_type len = size(); const size_type len = this->size();
if (len < 1) if (len < 1)
return npos; return npos;
@@ -1993,14 +2039,15 @@ class basic_string
//! <b>Returns</b>: find_first_of(basic_string(s, n), pos). //! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
size_type find_first_of(const CharT* s, size_type pos, size_type n) const size_type find_first_of(const CharT* s, size_type pos, size_type n) const
{ {
if (pos >= size()) const size_type size = this->size();
if (pos >= size)
return npos; return npos;
else { else {
pointer finish = this->priv_addr() + this->priv_size(); const pointer addr = this->priv_addr();
const_iterator result = std::find_first_of(this->priv_addr() + pos, finish, pointer finish = addr + size;
s, s + n, const_iterator result = std::find_first_of
Eq_traits<Traits>()); (addr + pos, finish, s, s + n, Eq_traits<Traits>());
return result != finish ? result - begin() : npos; return result != finish ? result - this->begin() : npos;
} }
} }
@@ -2037,17 +2084,17 @@ class basic_string
//! <b>Returns</b>: find_last_of(basic_string(s, n), pos). //! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
size_type find_last_of(const CharT* s, size_type pos, size_type n) const size_type find_last_of(const CharT* s, size_type pos, size_type n) const
{ {
const size_type len = size(); const size_type len = this->size();
if (len < 1) if (len < 1)
return npos; return npos;
else { else {
const const_iterator last = this->priv_addr() + container_detail::min_value(len - 1, pos) + 1; const pointer addr = this->priv_addr();
const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1;
const const_reverse_iterator rresult = const const_reverse_iterator rresult =
std::find_first_of(const_reverse_iterator(last), rend(), std::find_first_of(const_reverse_iterator(last), rend(),
s, s + n, s, s + n, Eq_traits<Traits>());
Eq_traits<Traits>()); return rresult != rend() ? (rresult.base() - 1) - addr : npos;
return rresult != rend() ? (rresult.base() - 1) - this->priv_addr() : npos;
} }
} }
@@ -2083,13 +2130,14 @@ class basic_string
//! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos). //! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
{ {
if (pos > size()) if (pos > this->size())
return npos; return npos;
else { else {
const pointer finish = this->priv_addr() + this->priv_size(); const pointer addr = this->priv_addr();
const const_iterator result = std::find_if(this->priv_addr() + pos, finish, const pointer finish = addr + this->priv_size();
Not_within_traits<Traits>(s, s + n)); const const_iterator result = std::find_if
return result != finish ? result - this->priv_addr() : npos; (addr + pos, finish, Not_within_traits<Traits>(s, s + n));
return result != finish ? result - addr : npos;
} }
} }
@@ -2106,12 +2154,13 @@ class basic_string
//! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos). //! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
size_type find_first_not_of(CharT c, size_type pos = 0) const size_type find_first_not_of(CharT c, size_type pos = 0) const
{ {
if (pos > size()) if (pos > this->size())
return npos; return npos;
else { else {
const pointer finish = this->priv_addr() + this->priv_size(); const pointer addr = this->priv_addr();
const pointer finish = addr + this->priv_size();
const const_iterator result const const_iterator result
= std::find_if(this->priv_addr() + pos, finish, = std::find_if(addr + pos, finish,
std::not1(std::bind2nd(Eq_traits<Traits>(), c))); std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
return result != finish ? result - begin() : npos; return result != finish ? result - begin() : npos;
} }
@@ -2134,7 +2183,7 @@ class basic_string
//! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos). //! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
{ {
const size_type len = size(); const size_type len = this->size();
if (len < 1) if (len < 1)
return npos; return npos;
@@ -2160,7 +2209,7 @@ class basic_string
//! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos). //! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
size_type find_last_not_of(CharT c, size_type pos = npos) const size_type find_last_not_of(CharT c, size_type pos = npos) const
{ {
const size_type len = size(); const size_type len = this->size();
if (len < 1) if (len < 1)
return npos; return npos;
@@ -2183,10 +2232,11 @@ class basic_string
//! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen). //! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen).
basic_string substr(size_type pos = 0, size_type n = npos) const basic_string substr(size_type pos = 0, size_type n = npos) const
{ {
if (pos > size()) if (pos > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
return basic_string(this->priv_addr() + pos, const pointer addr = this->priv_addr();
this->priv_addr() + pos + container_detail::min_value(n, size() - pos), this->alloc()); return basic_string(addr + pos,
addr + pos + container_detail::min_value(n, size() - pos), this->alloc());
} }
//! <b>Effects</b>: Determines the effective length rlen of the string to copy as //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
@@ -2199,7 +2249,11 @@ class basic_string
//! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(), //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
//! and value > 0 if size() > str.size() //! and value > 0 if size() > str.size()
int compare(const basic_string& str) const int compare(const basic_string& str) const
{ return s_compare(this->priv_addr(), this->priv_addr() + this->priv_size(), str.priv_addr(), str.priv_addr() + str.priv_size()); } {
const pointer addr = this->priv_addr();
const pointer str_addr = str.priv_addr();
return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size());
}
//! <b>Requires</b>: pos1 <= size() //! <b>Requires</b>: pos1 <= size()
//! //!
@@ -2211,11 +2265,13 @@ class basic_string
//! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str). //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
int compare(size_type pos1, size_type n1, const basic_string& str) const int compare(size_type pos1, size_type n1, const basic_string& str) const
{ {
if (pos1 > size()) if (pos1 > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
return s_compare(this->priv_addr() + pos1, const pointer addr = this->priv_addr();
this->priv_addr() + pos1 + container_detail::min_value(n1, size() - pos1), const pointer str_addr = str.priv_addr();
str.priv_addr(), str.priv_addr() + str.priv_size()); return s_compare(addr + pos1,
addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
str_addr, str_addr + str.priv_size());
} }
//! <b>Requires</b>: pos1 <= size() and pos2 <= str.size() //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
@@ -2228,19 +2284,24 @@ class basic_string
//! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)). //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
int compare(size_type pos1, size_type n1, int compare(size_type pos1, size_type n1,
const basic_string& str, size_type pos2, size_type n2) const { const basic_string& str, size_type pos2, size_type n2) const {
if (pos1 > size() || pos2 > str.size()) if (pos1 > this->size() || pos2 > str.size())
this->throw_out_of_range(); this->throw_out_of_range();
return s_compare(this->priv_addr() + pos1, const pointer addr = this->priv_addr();
this->priv_addr() + pos1 + container_detail::min_value(n1, size() - pos1), const pointer str_addr = str.priv_addr();
str.priv_addr() + pos2, return s_compare(addr + pos1,
str.priv_addr() + pos2 + container_detail::min_value(n2, size() - pos2)); addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
str_addr + pos2,
str_addr + pos2 + container_detail::min_value(n2, str.size() - pos2));
} }
//! <b>Throws</b>: Nothing //! <b>Throws</b>: Nothing
//! //!
//! <b>Returns</b>: compare(basic_string(s)). //! <b>Returns</b>: compare(basic_string(s)).
int compare(const CharT* s) const int compare(const CharT* s) const
{ return s_compare(this->priv_addr(), this->priv_addr() + this->priv_size(), s, s + Traits::length(s)); } {
const pointer addr = this->priv_addr();
return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s));
}
//! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT. //! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
@@ -2251,10 +2312,11 @@ class basic_string
int compare(size_type pos1, size_type n1, int compare(size_type pos1, size_type n1,
const CharT* s, size_type n2) const const CharT* s, size_type n2) const
{ {
if (pos1 > size()) if (pos1 > this->size())
this->throw_out_of_range(); this->throw_out_of_range();
return s_compare(this->priv_addr() + pos1, const pointer addr = this->priv_addr();
this->priv_addr() + pos1 + container_detail::min_value(n1, size() - pos1), return s_compare( addr + pos1,
addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
s, s + n2); s, s + n2);
} }
@@ -2322,15 +2384,12 @@ class basic_string
} }
void priv_construct_null(pointer p) void priv_construct_null(pointer p)
{ this->construct(p, 0); } { this->construct(p, CharT(0)); }
static CharT priv_null()
{ return (CharT) 0; }
// Helper functions used by constructors. It is a severe error for // Helper functions used by constructors. It is a severe error for
// any of them to be called anywhere except from within constructors. // any of them to be called anywhere except from within constructors.
void priv_terminate_string() void priv_terminate_string()
{ this->priv_construct_null(this->priv_addr() + this->priv_size()); } { this->priv_construct_null(this->priv_end_addr()); }
template<class FwdIt, class Count> inline template<class FwdIt, class Count> inline
void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val) void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val)
@@ -2377,26 +2436,7 @@ class basic_string
BOOST_CATCH_END BOOST_CATCH_END
return (constructed); return (constructed);
} }
/*
template <class InputIter>
basic_string& priv_assign_dispatch(InputIter f, InputIter l, container_detail::false_)
{
size_type cur = 0;
CharT *ptr = container_detail::to_raw_pointer(this->priv_addr());
const size_type old_size = this->priv_size();
while (f != l && cur != old_size) {
Traits::assign(*ptr, *f);
++f;
++cur;
++ptr;
}
if (f == l)
this->erase(this->priv_addr() + cur, this->priv_addr() + old_size);
else
this->append(f, l);
return *this;
}
*/
template <class InputIterator, class OutIterator> template <class InputIterator, class OutIterator>
void priv_copy(InputIterator first, InputIterator last, OutIterator result) void priv_copy(InputIterator first, InputIterator last, OutIterator result)
{ {

View File

@@ -58,13 +58,8 @@ bool CheckEqualContainers(const MyBoostCont *boostcont, const MyStdCont *stdcont
} }
std::size_t i = 0; std::size_t i = 0;
for(; itboost != itboostend; ++itboost, ++itstd, ++i){ for(; itboost != itboostend; ++itboost, ++itstd, ++i){
if(!CheckEqual(*itstd, *itboost)) if(!CheckEqual(*itstd, *itboost))
return false; return false;
/* value_type val(*itstd);
const value_type &v = *itboost;
if(v != val)
return false;*/
} }
return true; return true;
} }