Unordered: Get rid of get_start.

[SVN r80561]
This commit is contained in:
Daniel James
2012-09-17 18:59:03 +00:00
parent 549b93e629
commit 53f278312f
6 changed files with 55 additions and 67 deletions

View File

@ -234,11 +234,9 @@ namespace boost { namespace unordered { namespace detail {
Key const& k, Key const& k,
Pred const& eq) const Pred const& eq) const
{ {
if (!this->size_) return iterator();
std::size_t bucket_index = std::size_t bucket_index =
policy::to_bucket(this->bucket_count_, key_hash); policy::to_bucket(this->bucket_count_, key_hash);
iterator n = this->get_start(bucket_index); iterator n = this->begin(bucket_index);
for (;;) for (;;)
{ {
@ -293,9 +291,8 @@ namespace boost { namespace unordered { namespace detail {
bool equals(grouped_table_impl const& other) const bool equals(grouped_table_impl const& other) const
{ {
if(this->size_ != other.size_) return false; if(this->size_ != other.size_) return false;
if(!this->size_) return true;
for(iterator n1 = this->get_start(); n1.node_;) for(iterator n1 = this->begin(); n1.node_;)
{ {
iterator n2 = other.find_matching_node(n1); iterator n2 = other.find_matching_node(n1);
if (!n2.node_) return false; if (!n2.node_) return false;

View File

@ -222,6 +222,7 @@ namespace boost { namespace unordered { namespace detail {
bucket_pointer get_bucket(std::size_t bucket_index) const bucket_pointer get_bucket(std::size_t bucket_index) const
{ {
BOOST_ASSERT(buckets_);
return buckets_ + static_cast<std::ptrdiff_t>(bucket_index); return buckets_ + static_cast<std::ptrdiff_t>(bucket_index);
} }
@ -235,14 +236,15 @@ namespace boost { namespace unordered { namespace detail {
return get_bucket(bucket_index)->next_; return get_bucket(bucket_index)->next_;
} }
iterator get_start() const iterator begin() const
{ {
return iterator(static_cast<node_pointer>( return size_ ? iterator(static_cast<node_pointer>(
get_previous_start()->next_)); get_previous_start()->next_)) : iterator();
} }
iterator get_start(std::size_t bucket_index) const iterator begin(std::size_t bucket_index) const
{ {
if (!size_) return iterator();
previous_pointer prev = get_previous_start(bucket_index); previous_pointer prev = get_previous_start(bucket_index);
return prev ? iterator(static_cast<node_pointer>(prev->next_)) : return prev ? iterator(static_cast<node_pointer>(prev->next_)) :
iterator(); iterator();
@ -257,8 +259,7 @@ namespace boost { namespace unordered { namespace detail {
std::size_t bucket_size(std::size_t index) const std::size_t bucket_size(std::size_t index) const
{ {
if (!size_) return 0; iterator it = begin(index);
iterator it = get_start(index);
if (!it.node_) return 0; if (!it.node_) return 0;
std::size_t count = 0; std::size_t count = 0;
@ -384,7 +385,7 @@ namespace boost { namespace unordered { namespace detail {
if (x.size_) { if (x.size_) {
create_buckets(bucket_count_); create_buckets(bucket_count_);
copy_nodes<node_allocator> copy(node_alloc()); copy_nodes<node_allocator> copy(node_alloc());
table_impl::fill_buckets(x.get_start(), *this, copy); table_impl::fill_buckets(x.begin(), *this, copy);
} }
} }
@ -526,7 +527,7 @@ namespace boost { namespace unordered { namespace detail {
void delete_buckets() void delete_buckets()
{ {
if(buckets_) { if(buckets_) {
delete_nodes(get_start(), iterator()); delete_nodes(begin(), iterator());
if (bucket::extra_node) { if (bucket::extra_node) {
node_pointer n = static_cast<node_pointer>( node_pointer n = static_cast<node_pointer>(
@ -548,7 +549,7 @@ namespace boost { namespace unordered { namespace detail {
{ {
if(!size_) return; if(!size_) return;
delete_nodes(get_start(), iterator()); delete_nodes(begin(), iterator());
get_previous_start()->next_ = link_pointer(); get_previous_start()->next_ = link_pointer();
clear_buckets(); clear_buckets();
@ -651,13 +652,6 @@ namespace boost { namespace unordered { namespace detail {
} }
} }
////////////////////////////////////////////////////////////////////////
// Iterators
iterator begin() const {
return !buckets_ ? iterator() : get_start();
}
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Assignment // Assignment
@ -694,10 +688,7 @@ namespace boost { namespace unordered { namespace detail {
// assigning to them if possible, and deleting any that are // assigning to them if possible, and deleting any that are
// left over. // left over.
assign_nodes<table> assign(*this); assign_nodes<table> assign(*this);
table_impl::fill_buckets(x.begin(), *this, assign);
if (x.size_) {
table_impl::fill_buckets(x.get_start(), *this, assign);
}
} }
void assign(table const& x, true_type) void assign(table const& x, true_type)
@ -725,7 +716,7 @@ namespace boost { namespace unordered { namespace detail {
if (x.size_) { if (x.size_) {
create_buckets(bucket_count_); create_buckets(bucket_count_);
copy_nodes<node_allocator> copy(node_alloc()); copy_nodes<node_allocator> copy(node_alloc());
table_impl::fill_buckets(x.get_start(), *this, copy); table_impl::fill_buckets(x.begin(), *this, copy);
} }
} }
} }

View File

@ -231,11 +231,9 @@ namespace boost { namespace unordered { namespace detail {
Key const& k, Key const& k,
Pred const& eq) const Pred const& eq) const
{ {
if (!this->size_) return iterator();
std::size_t bucket_index = std::size_t bucket_index =
policy::to_bucket(this->bucket_count_, key_hash); policy::to_bucket(this->bucket_count_, key_hash);
iterator n = this->get_start(bucket_index); iterator n = this->begin(bucket_index);
for (;;) for (;;)
{ {
@ -288,9 +286,8 @@ namespace boost { namespace unordered { namespace detail {
bool equals(table_impl const& other) const bool equals(table_impl const& other) const
{ {
if(this->size_ != other.size_) return false; if(this->size_ != other.size_) return false;
if(!this->size_) return true;
for(iterator n1 = this->get_start(); n1.node_; ++n1) for(iterator n1 = this->begin(); n1.node_; ++n1)
{ {
iterator n2 = other.find_matching_node(n1); iterator n2 = other.find_matching_node(n1);

View File

@ -469,16 +469,14 @@ namespace unordered
local_iterator begin(size_type n) local_iterator begin(size_type n)
{ {
return table_.size_ ? local_iterator( return local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
local_iterator();
} }
const_local_iterator begin(size_type n) const const_local_iterator begin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
local_iterator end(size_type) local_iterator end(size_type)
@ -493,9 +491,8 @@ namespace unordered
const_local_iterator cbegin(size_type n) const const_local_iterator cbegin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
const_local_iterator cend(size_type) const const_local_iterator cend(size_type) const
@ -951,16 +948,14 @@ namespace unordered
local_iterator begin(size_type n) local_iterator begin(size_type n)
{ {
return table_.size_ ? local_iterator( return local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
local_iterator();
} }
const_local_iterator begin(size_type n) const const_local_iterator begin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
local_iterator end(size_type) local_iterator end(size_type)
@ -975,9 +970,8 @@ namespace unordered
const_local_iterator cbegin(size_type n) const const_local_iterator cbegin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
const_local_iterator cend(size_type) const const_local_iterator cend(size_type) const

View File

@ -454,16 +454,14 @@ namespace unordered
local_iterator begin(size_type n) local_iterator begin(size_type n)
{ {
return table_.size_ ? local_iterator( return local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
local_iterator();
} }
const_local_iterator begin(size_type n) const const_local_iterator begin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
local_iterator end(size_type) local_iterator end(size_type)
@ -478,9 +476,8 @@ namespace unordered
const_local_iterator cbegin(size_type n) const const_local_iterator cbegin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
const_local_iterator cend(size_type) const const_local_iterator cend(size_type) const
@ -926,16 +923,14 @@ namespace unordered
local_iterator begin(size_type n) local_iterator begin(size_type n)
{ {
return table_.size_ ? local_iterator( return local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
local_iterator();
} }
const_local_iterator begin(size_type n) const const_local_iterator begin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
local_iterator end(size_type) local_iterator end(size_type)
@ -950,9 +945,8 @@ namespace unordered
const_local_iterator cbegin(size_type n) const const_local_iterator cbegin(size_type n) const
{ {
return table_.size_ ? const_local_iterator( return const_local_iterator(
table_.get_start(n), n, table_.bucket_count_) : table_.begin(n), n, table_.bucket_count_);
const_local_iterator();
} }
const_local_iterator cend(size_type) const const_local_iterator cend(size_type) const

View File

@ -276,6 +276,21 @@ void insert_tests2(X*, test::random_generator generator)
test::check_equivalent_keys(x); test::check_equivalent_keys(x);
} }
std::cerr<<"insert copy iterator range test 2.\n";
{
test::check_instances check_;
X x;
test::random_values<X> v1(500, generator);
test::random_values<X> v2(500, generator);
x.insert(test::copy_iterator(v1.begin()), test::copy_iterator(v1.end()));
x.insert(test::copy_iterator(v2.begin()), test::copy_iterator(v2.end()));
test::check_equivalent_keys(x);
}
} }
#if !defined(BOOST_NO_RVALUE_REFERENCES) && !defined(BOOST_NO_VARIADIC_TEMPLATES) #if !defined(BOOST_NO_RVALUE_REFERENCES) && !defined(BOOST_NO_VARIADIC_TEMPLATES)