Added debug assertions via BOOST_ASSERT to check preconditions in several members. All checks are O(1)

This commit is contained in:
Ion Gaztañaga committed 2015-09-18 14:39:17 +02:00
1 parent 3f4a5dec6e
commit 85b2ed509b
8 files changed
+331 -75

No files matched your search

+21 -9
View File
@@ -1,6 +1,6 @@
////////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
// (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@@ -376,35 +376,39 @@ class flat_tree
return i;
}
iterator insert_unique(const_iterator pos, const value_type& val)
iterator insert_unique(const_iterator hint, const value_type& val)
{
BOOST_ASSERT(this->priv_in_range_or_end(hint));
std::pair<iterator,bool> ret;
insert_commit_data data;
return this->priv_insert_unique_prepare(pos, val, data)
return this->priv_insert_unique_prepare(hint, val, data)
? this->priv_insert_commit(data, val)
: iterator(vector_iterator_get_ptr(data.position));
}
iterator insert_unique(const_iterator pos, BOOST_RV_REF(value_type) val)
iterator insert_unique(const_iterator hint, BOOST_RV_REF(value_type) val)
{
BOOST_ASSERT(this->priv_in_range_or_end(hint));
std::pair<iterator,bool> ret;
insert_commit_data data;
return this->priv_insert_unique_prepare(pos, val, data)
return this->priv_insert_unique_prepare(hint, val, data)
? this->priv_insert_commit(data, boost::move(val))
: iterator(vector_iterator_get_ptr(data.position));
}
iterator insert_equal(const_iterator pos, const value_type& val)
iterator insert_equal(const_iterator hint, const value_type& val)
{
BOOST_ASSERT(this->priv_in_range_or_end(hint));
insert_commit_data data;
this->priv_insert_equal_prepare(pos, val, data);
this->priv_insert_equal_prepare(hint, val, data);
return this->priv_insert_commit(data, val);
}
iterator insert_equal(const_iterator pos, BOOST_RV_REF(value_type) mval)
iterator insert_equal(const_iterator hint, BOOST_RV_REF(value_type) mval)
{
BOOST_ASSERT(this->priv_in_range_or_end(hint));
insert_commit_data data;
this->priv_insert_equal_prepare(pos, mval, data);
this->priv_insert_equal_prepare(hint, mval, data);
return this->priv_insert_commit(data, boost::move(mval));
}
@@ -524,6 +528,7 @@ class flat_tree
template <class... Args>
iterator emplace_hint_unique(const_iterator hint, BOOST_FWD_REF(Args)... args)
{
//hint checked in insert_unique
typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
stored_allocator_type &a = this->get_stored_allocator();
@@ -546,6 +551,7 @@ class flat_tree
template <class... Args>
iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
{
//hint checked in insert_equal
typename aligned_storage<sizeof(value_type), alignment_of<value_type>::value>::type v;
value_type &val = *static_cast<value_type *>(static_cast<void *>(&v));
stored_allocator_type &a = this->get_stored_allocator();
@@ -732,6 +738,12 @@ class flat_tree
{ x.swap(y); }
private:
bool priv_in_range_or_end(const_iterator pos) const
{
return (this->begin() <= pos) && (pos <= this->end());
}
struct insert_commit_data
{
const_iterator position;
+27 -3
View File
@@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
// (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@@ -811,6 +811,7 @@ class tree
std::pair<iterator,bool> insert_unique_check
(const_iterator hint, const key_type& key, insert_commit_data &data)
{
BOOST_ASSERT((priv_is_linked)(hint));
std::pair<iiterator, bool> ret =
this->icont().insert_unique_check(hint.get(), key, KeyNodeCompare(value_comp()), data);
return std::pair<iterator, bool>(iterator(ret.first), ret.second);
@@ -861,6 +862,15 @@ class tree
private:
bool priv_is_linked(const_iterator const position) const
{
iiterator const cur(position.get());
return cur == this->icont().end() ||
cur == this->icont().root() ||
iiterator(cur).go_parent().go_left() == cur ||
iiterator(cur).go_parent().go_right() == cur;
}
template<class MovableConvertible>
void push_back_impl(BOOST_FWD_REF(MovableConvertible) v)
{
@@ -888,6 +898,7 @@ class tree
iterator emplace_unique_hint_impl(const_iterator hint, NodePtr p)
{
BOOST_ASSERT((priv_is_linked)(hint));
value_type &v = p->get_data();
insert_commit_data data;
std::pair<iterator,bool> ret =
@@ -924,6 +935,7 @@ class tree
template <class... Args>
iterator emplace_hint_equal(const_iterator hint, BOOST_FWD_REF(Args)... args)
{
BOOST_ASSERT((priv_is_linked)(hint));
NodePtr tmp(AllocHolder::create_node(boost::forward<Args>(args)...));
scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
iterator ret(this->icont().insert_equal(hint.get(), *tmp));
@@ -955,6 +967,7 @@ class tree
BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
iterator emplace_hint_equal(const_iterator hint BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
{\
BOOST_ASSERT((priv_is_linked)(hint));\
NodePtr tmp(AllocHolder::create_node(BOOST_MOVE_FWD##N));\
scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());\
iterator ret(this->icont().insert_equal(hint.get(), *tmp));\
@@ -969,6 +982,7 @@ class tree
iterator insert_unique(const_iterator hint, const value_type& v)
{
BOOST_ASSERT((priv_is_linked)(hint));
insert_commit_data data;
std::pair<iterator,bool> ret =
this->insert_unique_check(hint, KeyOfValue()(v), data);
@@ -980,6 +994,7 @@ class tree
template<class MovableConvertible>
iterator insert_unique(const_iterator hint, BOOST_FWD_REF(MovableConvertible) v)
{
BOOST_ASSERT((priv_is_linked)(hint));
insert_commit_data data;
std::pair<iterator,bool> ret =
this->insert_unique_check(hint, KeyOfValue()(v), data);
@@ -1016,6 +1031,7 @@ class tree
iterator insert_equal(const_iterator hint, const value_type& v)
{
BOOST_ASSERT((priv_is_linked)(hint));
NodePtr tmp(AllocHolder::create_node(v));
scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
iterator ret(this->icont().insert_equal(hint.get(), *tmp));
@@ -1026,6 +1042,7 @@ class tree
template<class MovableConvertible>
iterator insert_equal(const_iterator hint, BOOST_FWD_REF(MovableConvertible) v)
{
BOOST_ASSERT((priv_is_linked)(hint));
NodePtr tmp(AllocHolder::create_node(boost::forward<MovableConvertible>(v)));
scoped_destroy_deallocator<NodeAlloc> destroy_deallocator(tmp, this->node_alloc());
iterator ret(this->icont().insert_equal(hint.get(), *tmp));
@@ -1041,13 +1058,20 @@ class tree
}
iterator erase(const_iterator position)
{ return iterator(this->icont().erase_and_dispose(position.get(), Destroyer(this->node_alloc()))); }
{
BOOST_ASSERT(position != this->cend() && (priv_is_linked)(position));
return iterator(this->icont().erase_and_dispose(position.get(), Destroyer(this->node_alloc())));
}
size_type erase(const key_type& k)
{ return AllocHolder::erase_key(k, KeyNodeCompare(value_comp()), alloc_version()); }
iterator erase(const_iterator first, const_iterator last)
{ return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version())); }
{
BOOST_ASSERT(first == last || (first != this->cend() && (priv_is_linked)(first)));
BOOST_ASSERT(first == last || (priv_is_linked)(last));
return iterator(AllocHolder::erase_range(first.get(), last.get(), alloc_version()));
}
void clear()
{ AllocHolder::clear(alloc_version()); }