Unordered: some more formatting + namespaces

[SVN r74767]
This commit is contained in:
Daniel James
2011-10-06 21:31:25 +00:00
parent 597d93537d
commit 3d7b6c64b5
2 changed files with 90 additions and 63 deletions

View File

@ -69,7 +69,8 @@ namespace boost { namespace unordered { namespace detail {
// If the allocator uses raw pointers use grouped_ptr_node // If the allocator uses raw pointers use grouped_ptr_node
// Otherwise use grouped_node. // Otherwise use grouped_node.
template <typename A, typename T, typename VoidPointer, typename NodePtr, typename BucketPtr> template <typename A, typename T,
typename VoidPointer, typename NodePtr, typename BucketPtr>
struct pick_grouped_node2 struct pick_grouped_node2
{ {
typedef boost::unordered::detail::grouped_node<VoidPointer, T> node; typedef boost::unordered::detail::grouped_node<VoidPointer, T> node;
@ -118,7 +119,7 @@ namespace boost { namespace unordered { namespace detail {
template <typename A, typename H, typename P> template <typename A, typename H, typename P>
struct multiset struct multiset
{ {
typedef multiset<A, H, P> types; typedef boost::unordered::detail::multiset<A, H, P> types;
typedef A allocator; typedef A allocator;
typedef H hasher; typedef H hasher;
@ -129,18 +130,19 @@ namespace boost { namespace unordered { namespace detail {
typedef typename traits::void_pointer void_pointer; typedef typename traits::void_pointer void_pointer;
typedef value_type key_type; typedef value_type key_type;
typedef typename boost::unordered::detail::pick_grouped_node<A, value_type>::node node; typedef boost::unordered::detail::pick_grouped_node<A, value_type> pick;
typedef typename boost::unordered::detail::pick_grouped_node<A, value_type>::bucket bucket; typedef typename pick::node node;
typedef typename boost::unordered::detail::pick_grouped_node<A, value_type>::link_pointer link_pointer; typedef typename pick::bucket bucket;
typedef boost::unordered::detail::grouped_table_impl<types> table; typedef typename pick::link_pointer link_pointer;
typedef boost::unordered::detail::grouped_table_impl<types> table;
typedef boost::unordered::detail::set_extractor<value_type> extractor; typedef boost::unordered::detail::set_extractor<value_type> extractor;
}; };
template <typename A, typename K, typename H, typename P> template <typename A, typename K, typename H, typename P>
struct multimap struct multimap
{ {
typedef multimap<A, K, H, P> types; typedef boost::unordered::detail::multimap<A, K, H, P> types;
typedef A allocator; typedef A allocator;
typedef H hasher; typedef H hasher;
@ -151,12 +153,14 @@ namespace boost { namespace unordered { namespace detail {
typedef typename traits::value_type value_type; typedef typename traits::value_type value_type;
typedef typename traits::void_pointer void_pointer; typedef typename traits::void_pointer void_pointer;
typedef typename boost::unordered::detail::pick_grouped_node<A, value_type>::node node; typedef boost::unordered::detail::pick_grouped_node<A, value_type> pick;
typedef typename boost::unordered::detail::pick_grouped_node<A, value_type>::bucket bucket; typedef typename pick::node node;
typedef typename boost::unordered::detail::pick_grouped_node<A, value_type>::link_pointer link_pointer; typedef typename pick::bucket bucket;
typedef boost::unordered::detail::grouped_table_impl<types> table; typedef typename pick::link_pointer link_pointer;
typedef boost::unordered::detail::map_extractor<key_type, value_type> extractor; typedef boost::unordered::detail::grouped_table_impl<types> table;
typedef boost::unordered::detail::map_extractor<key_type, value_type>
extractor;
}; };
template <typename Types> template <typename Types>
@ -234,7 +238,8 @@ namespace boost { namespace unordered { namespace detail {
return node_pointer(); return node_pointer();
} }
n = static_cast<node_pointer>(static_cast<node_pointer>(n->group_prev_)->next_); n = static_cast<node_pointer>(
static_cast<node_pointer>(n->group_prev_)->next_);
} }
} }
@ -273,8 +278,10 @@ namespace boost { namespace unordered { namespace detail {
{ {
node_pointer n2 = other.find_matching_node(n1); node_pointer n2 = other.find_matching_node(n1);
if (!n2) return false; if (!n2) return false;
node_pointer end1 = static_cast<node_pointer>(static_cast<node_pointer>(n1->group_prev_)->next_); node_pointer end1 = static_cast<node_pointer>(
node_pointer end2 = static_cast<node_pointer>(static_cast<node_pointer>(n2->group_prev_)->next_); static_cast<node_pointer>(n1->group_prev_)->next_);
node_pointer end2 = static_cast<node_pointer>(
static_cast<node_pointer>(n2->group_prev_)->next_);
if (!group_equals(n1, end1, n2, end2)) return false; if (!group_equals(n1, end1, n2, end2)) return false;
n1 = end1; n1 = end1;
} }
@ -319,7 +326,8 @@ namespace boost { namespace unordered { namespace detail {
value_type const& v = n1->value(); value_type const& v = n1->value();
if (find(start, n1, v)) continue; if (find(start, n1, v)) continue;
std::size_t matches = count_equal(n2, end2, v); std::size_t matches = count_equal(n2, end2, v);
if (!matches || matches != 1 + count_equal(static_cast<node_pointer>(n1->next_), end1, v)) if (!matches || matches != 1 + count_equal(
static_cast<node_pointer>(n1->next_), end1, v))
return false; return false;
} }
@ -334,7 +342,8 @@ namespace boost { namespace unordered { namespace detail {
return false; return false;
} }
static std::size_t count_equal(node_pointer n, node_pointer end, value_type const& v) static std::size_t count_equal(node_pointer n, node_pointer end,
value_type const& v)
{ {
std::size_t count = 0; std::size_t count = 0;
for(;n != end; n = static_cast<node_pointer>(n->next_)) for(;n != end; n = static_cast<node_pointer>(n->next_))
@ -371,7 +380,8 @@ namespace boost { namespace unordered { namespace detail {
{ {
n->next_ = static_cast<node_pointer>(pos->group_prev_)->next_; n->next_ = static_cast<node_pointer>(pos->group_prev_)->next_;
n->group_prev_ = pos->group_prev_; n->group_prev_ = pos->group_prev_;
static_cast<node_pointer>(pos->group_prev_)->next_ = static_cast<link_pointer>(n); static_cast<node_pointer>(pos->group_prev_)->next_ =
static_cast<link_pointer>(n);
pos->group_prev_ = static_cast<link_pointer>(n); pos->group_prev_ = static_cast<link_pointer>(n);
} }
@ -402,8 +412,8 @@ namespace boost { namespace unordered { namespace detail {
if (start_node->next_) { if (start_node->next_) {
this->get_bucket( this->get_bucket(
static_cast<node_pointer>(start_node->next_)->hash_ % static_cast<node_pointer>(start_node->next_)->hash_
this->bucket_count_)->next_ = n; % this->bucket_count_)->next_ = n;
} }
b->next_ = start_node; b->next_ = start_node;
@ -502,7 +512,7 @@ namespace boost { namespace unordered { namespace detail {
} }
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Erase // Erase
// //
// no throw // no throw
@ -521,18 +531,21 @@ namespace boost { namespace unordered { namespace detail {
for (;;) for (;;)
{ {
if (!prev->next_) return 0; if (!prev->next_) return 0;
std::size_t node_hash = static_cast<node_pointer>(prev->next_)->hash_; std::size_t node_hash =
static_cast<node_pointer>(prev->next_)->hash_;
if (node_hash % this->bucket_count_ != bucket_index) if (node_hash % this->bucket_count_ != bucket_index)
return 0; return 0;
if (node_hash == hash && if (node_hash == hash &&
this->key_eq()(k, this->get_key(static_cast<node_pointer>(prev->next_)->value()))) this->key_eq()(k, this->get_key(
static_cast<node_pointer>(prev->next_)->value())))
break; break;
prev = static_cast<previous_pointer>( prev = static_cast<previous_pointer>(
static_cast<node_pointer>(prev->next_)->group_prev_); static_cast<node_pointer>(prev->next_)->group_prev_);
} }
node_pointer pos = static_cast<node_pointer>(prev->next_); node_pointer pos = static_cast<node_pointer>(prev->next_);
link_pointer end1 = static_cast<node_pointer>(pos->group_prev_)->next_; link_pointer end1 =
static_cast<node_pointer>(pos->group_prev_)->next_;
node_pointer end = static_cast<node_pointer>(end1); node_pointer end = static_cast<node_pointer>(end1);
prev->next_ = end1; prev->next_ = end1;
this->fix_buckets(bucket, prev, end); this->fix_buckets(bucket, prev, end);
@ -571,7 +584,8 @@ namespace boost { namespace unordered { namespace detail {
static previous_pointer unlink_node(bucket& b, node_pointer n) static previous_pointer unlink_node(bucket& b, node_pointer n)
{ {
node_pointer next = static_cast<node_pointer>(n->next_); node_pointer next = static_cast<node_pointer>(n->next_);
previous_pointer prev = static_cast<previous_pointer>(n->group_prev_); previous_pointer prev =
static_cast<previous_pointer>(n->group_prev_);
if(prev->next_ != n) { if(prev->next_ != n) {
// The node is at the beginning of a group. // The node is at the beginning of a group.
@ -579,7 +593,8 @@ namespace boost { namespace unordered { namespace detail {
// Find the previous node pointer: // Find the previous node pointer:
prev = b.next_; prev = b.next_;
while(prev->next_ != n) { while(prev->next_ != n) {
prev = static_cast<previous_pointer>(static_cast<node_pointer>(prev->next_)->group_prev_); prev = static_cast<previous_pointer>(
static_cast<node_pointer>(prev->next_)->group_prev_);
} }
// Remove from group // Remove from group
@ -609,12 +624,13 @@ namespace boost { namespace unordered { namespace detail {
return prev; return prev;
} }
static previous_pointer unlink_nodes(bucket& b, node_pointer begin, node_pointer end) static previous_pointer unlink_nodes(bucket& b,
node_pointer begin, node_pointer end)
{ {
previous_pointer prev = static_cast<previous_pointer>(begin->group_prev_); previous_pointer prev = static_cast<previous_pointer>(
begin->group_prev_);
if(prev->next_ != static_cast<link_pointer>(begin)) { if(prev->next_ != static_cast<link_pointer>(begin)) {
std::cout << "A" << std::endl;
// The node is at the beginning of a group. // The node is at the beginning of a group.
// Find the previous node pointer: // Find the previous node pointer:
@ -626,7 +642,6 @@ std::cout << "A" << std::endl;
if (end) split_group(end); if (end) split_group(end);
} }
else { else {
std::cout << "B" << std::endl;
node_pointer group1 = split_group(begin); node_pointer group1 = split_group(begin);
if (end) { if (end) {
@ -653,7 +668,8 @@ std::cout << "B" << std::endl;
{ {
// Find first node in group. // Find first node in group.
node_pointer first = split; node_pointer first = split;
while (static_cast<node_pointer>(first->group_prev_)->next_ == static_cast<link_pointer>(first)) while (static_cast<node_pointer>(first->group_prev_)->next_ ==
static_cast<link_pointer>(first))
first = static_cast<node_pointer>(first->group_prev_); first = static_cast<node_pointer>(first->group_prev_);
if(first == split) return split; if(first == split) return split;
@ -665,7 +681,7 @@ std::cout << "B" << std::endl;
return first; return first;
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// copy_buckets_to // copy_buckets_to
// //
// Basic exception safety. If an exception is thrown this will // Basic exception safety. If an exception is thrown this will
@ -712,11 +728,11 @@ std::cout << "B" << std::endl;
} }
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// move_buckets_to // move_buckets_to
// //
// Basic exception safety. The source nodes are left in an unusable state // Basic exception safety. The source nodes are left in an unusable
// if an exception throws. // state if an exception throws.
static void move_buckets_to(buckets& src, buckets& dst) static void move_buckets_to(buckets& src, buckets& dst)
{ {
@ -788,7 +804,8 @@ std::cout << "B" << std::endl;
// Iterate through the nodes placing them in the correct buckets. // Iterate through the nodes placing them in the correct buckets.
// pre: prev->next_ is not null. // pre: prev->next_ is not null.
static previous_pointer place_in_bucket(buckets& dst, previous_pointer prev, node_pointer end) static previous_pointer place_in_bucket(buckets& dst,
previous_pointer prev, node_pointer end)
{ {
bucket_pointer b = dst.get_bucket(end->hash_ % dst.bucket_count_); bucket_pointer b = dst.get_bucket(end->hash_ % dst.bucket_count_);

View File

@ -65,7 +65,8 @@ namespace boost { namespace unordered { namespace detail {
// If the allocator uses raw pointers use ptr_node // If the allocator uses raw pointers use ptr_node
// Otherwise use node. // Otherwise use node.
template <typename A, typename T, typename VoidPointer, typename NodePtr, typename BucketPtr> template <typename A, typename T,
typename VoidPointer, typename NodePtr, typename BucketPtr>
struct pick_node2 struct pick_node2
{ {
typedef boost::unordered::detail::node<VoidPointer, T> node; typedef boost::unordered::detail::node<VoidPointer, T> node;
@ -114,7 +115,7 @@ namespace boost { namespace unordered { namespace detail {
template <typename A, typename H, typename P> template <typename A, typename H, typename P>
struct set struct set
{ {
typedef set<A, H, P> types; typedef boost::unordered::detail::set<A, H, P> types;
typedef A allocator; typedef A allocator;
typedef H hasher; typedef H hasher;
@ -125,18 +126,19 @@ namespace boost { namespace unordered { namespace detail {
typedef typename traits::void_pointer void_pointer; typedef typename traits::void_pointer void_pointer;
typedef value_type key_type; typedef value_type key_type;
typedef typename boost::unordered::detail::pick_node<A, value_type>::node node; typedef boost::unordered::detail::pick_node<A, value_type> pick;
typedef typename boost::unordered::detail::pick_node<A, value_type>::bucket bucket; typedef typename pick::node node;
typedef typename boost::unordered::detail::pick_node<A, value_type>::link_pointer link_pointer; typedef typename pick::bucket bucket;
typedef boost::unordered::detail::table_impl<types> table; typedef typename pick::link_pointer link_pointer;
typedef boost::unordered::detail::table_impl<types> table;
typedef boost::unordered::detail::set_extractor<value_type> extractor; typedef boost::unordered::detail::set_extractor<value_type> extractor;
}; };
template <typename A, typename K, typename H, typename P> template <typename A, typename K, typename H, typename P>
struct map struct map
{ {
typedef map<A, K, H, P> types; typedef boost::unordered::detail::map<A, K, H, P> types;
typedef A allocator; typedef A allocator;
typedef H hasher; typedef H hasher;
@ -147,12 +149,14 @@ namespace boost { namespace unordered { namespace detail {
typedef typename traits::value_type value_type; typedef typename traits::value_type value_type;
typedef typename traits::void_pointer void_pointer; typedef typename traits::void_pointer void_pointer;
typedef typename boost::unordered::detail::pick_node<A, value_type>::node node; typedef boost::unordered::detail::pick_node<A, value_type> pick;
typedef typename boost::unordered::detail::pick_node<A, value_type>::bucket bucket; typedef typename pick::node node;
typedef typename boost::unordered::detail::pick_node<A, value_type>::link_pointer link_pointer; typedef typename pick::bucket bucket;
typedef boost::unordered::detail::table_impl<types> table; typedef typename pick::link_pointer link_pointer;
typedef boost::unordered::detail::map_extractor<key_type, value_type> extractor; typedef boost::unordered::detail::table_impl<types> table;
typedef boost::unordered::detail::map_extractor<key_type, value_type>
extractor;
}; };
template <typename Types> template <typename Types>
@ -454,12 +458,13 @@ namespace boost { namespace unordered { namespace detail {
do { do {
// Note: can't use get_key as '*i' might not be value_type - it // Note: can't use get_key as '*i' might not be value_type - it
// could be a pair with first_types as key_type without const or a // could be a pair with first_types as key_type without const or
// different second_type. // a different second_type.
// //
// TODO: Might be worth storing the value_type instead of the key // TODO: Might be worth storing the value_type instead of the
// here. Could be more efficient if '*i' is expensive. Could be // key here. Could be more efficient if '*i' is expensive. Could
// less efficient if copying the full value_type is expensive. // be less efficient if copying the full value_type is
// expensive.
insert_range_impl2(a, extractor::extract(*i), i, j); insert_range_impl2(a, extractor::extract(*i), i, j);
} while(++i != j); } while(++i != j);
} }
@ -509,7 +514,7 @@ namespace boost { namespace unordered { namespace detail {
} while(++i != j); } while(++i != j);
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Erase // Erase
// //
// no throw // no throw
@ -528,11 +533,13 @@ namespace boost { namespace unordered { namespace detail {
for (;;) for (;;)
{ {
if (!prev->next_) return 0; if (!prev->next_) return 0;
std::size_t node_hash = static_cast<node_pointer>(prev->next_)->hash_; std::size_t node_hash =
static_cast<node_pointer>(prev->next_)->hash_;
if (node_hash % this->bucket_count_ != bucket_index) if (node_hash % this->bucket_count_ != bucket_index)
return 0; return 0;
if (node_hash == hash && if (node_hash == hash &&
this->key_eq()(k, this->get_key(static_cast<node_pointer>(prev->next_)->value()))) this->key_eq()(k, this->get_key(
static_cast<node_pointer>(prev->next_)->value())))
break; break;
prev = static_cast<previous_pointer>(prev->next_); prev = static_cast<previous_pointer>(prev->next_);
} }
@ -578,16 +585,18 @@ namespace boost { namespace unordered { namespace detail {
return unlink_nodes(b, n, static_cast<node_pointer>(n->next_)); return unlink_nodes(b, n, static_cast<node_pointer>(n->next_));
} }
static previous_pointer unlink_nodes(bucket& b, node_pointer begin, node_pointer end) static previous_pointer unlink_nodes(bucket& b,
node_pointer begin, node_pointer end)
{ {
previous_pointer prev = b.next_; previous_pointer prev = b.next_;
link_pointer begin_void = static_cast<link_pointer>(begin); link_pointer begin_void = static_cast<link_pointer>(begin);
while(prev->next_ != begin_void) prev = static_cast<previous_pointer>(prev->next_); while(prev->next_ != begin_void)
prev = static_cast<previous_pointer>(prev->next_);
prev->next_ = static_cast<link_pointer>(end); prev->next_ = static_cast<link_pointer>(end);
return prev; return prev;
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// copy_buckets_to // copy_buckets_to
// //
// Basic exception safety. If an exception is thrown this will // Basic exception safety. If an exception is thrown this will
@ -618,11 +627,11 @@ namespace boost { namespace unordered { namespace detail {
} }
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// move_buckets_to // move_buckets_to
// //
// Basic exception safety. The source nodes are left in an unusable state // Basic exception safety. The source nodes are left in an unusable
// if an exception throws. // state if an exception throws.
static void move_buckets_to(buckets& src, buckets& dst) static void move_buckets_to(buckets& src, buckets& dst)
{ {
@ -676,7 +685,8 @@ namespace boost { namespace unordered { namespace detail {
// Iterate through the nodes placing them in the correct buckets. // Iterate through the nodes placing them in the correct buckets.
// pre: prev->next_ is not null. // pre: prev->next_ is not null.
static previous_pointer place_in_bucket(buckets& dst, previous_pointer prev) static previous_pointer place_in_bucket(buckets& dst,
previous_pointer prev)
{ {
node_pointer n = static_cast<node_pointer>(prev->next_); node_pointer n = static_cast<node_pointer>(prev->next_);
bucket_pointer b = dst.get_bucket(n->hash_ % dst.bucket_count_); bucket_pointer b = dst.get_bucket(n->hash_ % dst.bucket_count_);