Chris@16: Chris@16: // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard. Chris@16: // Copyright (C) 2005-2011 Daniel James Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_UNORDERED_DETAIL_EQUIVALENT_HPP_INCLUDED Chris@16: #define BOOST_UNORDERED_DETAIL_EQUIVALENT_HPP_INCLUDED Chris@16: Chris@101: #include Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace unordered { namespace detail { Chris@16: Chris@16: template struct grouped_node; Chris@16: template struct grouped_ptr_node; Chris@16: template struct grouped_table_impl; Chris@16: Chris@16: template Chris@16: struct grouped_node : Chris@16: boost::unordered::detail::value_base Chris@16: { Chris@16: typedef typename ::boost::unordered::detail::rebind_wrap< Chris@16: A, grouped_node >::type allocator; Chris@16: typedef typename ::boost::unordered::detail:: Chris@16: allocator_traits::pointer node_pointer; Chris@16: typedef node_pointer link_pointer; Chris@16: Chris@16: link_pointer next_; Chris@16: node_pointer group_prev_; Chris@16: std::size_t hash_; Chris@16: Chris@16: grouped_node() : Chris@16: next_(), Chris@16: group_prev_(), Chris@16: hash_(0) Chris@16: {} Chris@16: Chris@16: void init(node_pointer self) Chris@16: { Chris@16: group_prev_ = self; Chris@16: } Chris@16: Chris@16: private: Chris@16: grouped_node& operator=(grouped_node const&); Chris@16: }; Chris@16: Chris@16: template Chris@16: struct grouped_ptr_node : Chris@16: boost::unordered::detail::ptr_bucket Chris@16: { Chris@101: typedef T value_type; Chris@16: typedef boost::unordered::detail::ptr_bucket bucket_base; Chris@16: typedef grouped_ptr_node* node_pointer; Chris@16: typedef ptr_bucket* link_pointer; Chris@16: Chris@16: node_pointer group_prev_; Chris@16: std::size_t hash_; Chris@101: boost::unordered::detail::value_base value_base_; Chris@16: Chris@16: grouped_ptr_node() : Chris@16: bucket_base(), Chris@16: group_prev_(0), Chris@16: hash_(0) Chris@16: {} Chris@16: Chris@16: void init(node_pointer self) Chris@16: { Chris@16: group_prev_ = self; Chris@16: } Chris@16: Chris@101: void* address() { return value_base_.address(); } Chris@101: value_type& value() { return value_base_.value(); } Chris@101: value_type* value_ptr() { return value_base_.value_ptr(); } Chris@101: Chris@16: private: Chris@16: grouped_ptr_node& operator=(grouped_ptr_node const&); Chris@16: }; Chris@16: Chris@16: // If the allocator uses raw pointers use grouped_ptr_node Chris@16: // Otherwise use grouped_node. Chris@16: Chris@16: template Chris@16: struct pick_grouped_node2 Chris@16: { Chris@16: typedef boost::unordered::detail::grouped_node node; Chris@16: Chris@16: typedef typename boost::unordered::detail::allocator_traits< Chris@16: typename boost::unordered::detail::rebind_wrap::type Chris@16: >::pointer node_pointer; Chris@16: Chris@16: typedef boost::unordered::detail::bucket bucket; Chris@16: typedef node_pointer link_pointer; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct pick_grouped_node2*, Chris@16: boost::unordered::detail::ptr_bucket*> Chris@16: { Chris@16: typedef boost::unordered::detail::grouped_ptr_node node; Chris@16: typedef boost::unordered::detail::ptr_bucket bucket; Chris@16: typedef bucket* link_pointer; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct pick_grouped_node Chris@16: { Chris@16: typedef boost::unordered::detail::allocator_traits< Chris@16: typename boost::unordered::detail::rebind_wrap >::type Chris@16: > tentative_node_traits; Chris@16: Chris@16: typedef boost::unordered::detail::allocator_traits< Chris@16: typename boost::unordered::detail::rebind_wrap::type Chris@16: > tentative_bucket_traits; Chris@16: Chris@16: typedef pick_grouped_node2 pick; Chris@16: Chris@16: typedef typename pick::node node; Chris@16: typedef typename pick::bucket bucket; Chris@16: typedef typename pick::link_pointer link_pointer; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct multiset Chris@16: { Chris@16: typedef boost::unordered::detail::multiset types; Chris@16: Chris@16: typedef A allocator; Chris@16: typedef T value_type; Chris@16: typedef H hasher; Chris@16: typedef P key_equal; Chris@16: typedef T key_type; Chris@16: Chris@16: typedef boost::unordered::detail::allocator_traits traits; Chris@16: typedef boost::unordered::detail::pick_grouped_node pick; Chris@16: typedef typename pick::node node; Chris@16: typedef typename pick::bucket bucket; Chris@16: typedef typename pick::link_pointer link_pointer; Chris@16: Chris@16: typedef boost::unordered::detail::grouped_table_impl table; Chris@16: typedef boost::unordered::detail::set_extractor extractor; Chris@16: Chris@101: typedef typename boost::unordered::detail::pick_policy::type policy; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct multimap Chris@16: { Chris@16: typedef boost::unordered::detail::multimap types; Chris@16: Chris@16: typedef A allocator; Chris@16: typedef std::pair value_type; Chris@16: typedef H hasher; Chris@16: typedef P key_equal; Chris@16: typedef K key_type; Chris@16: Chris@16: typedef boost::unordered::detail::allocator_traits traits; Chris@16: typedef boost::unordered::detail::pick_grouped_node pick; Chris@16: typedef typename pick::node node; Chris@16: typedef typename pick::bucket bucket; Chris@16: typedef typename pick::link_pointer link_pointer; Chris@16: Chris@16: typedef boost::unordered::detail::grouped_table_impl table; Chris@16: typedef boost::unordered::detail::map_extractor Chris@16: extractor; Chris@16: Chris@101: typedef typename boost::unordered::detail::pick_policy::type policy; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct grouped_table_impl : boost::unordered::detail::table Chris@16: { Chris@16: typedef boost::unordered::detail::table table; Chris@16: typedef typename table::value_type value_type; Chris@16: typedef typename table::bucket bucket; Chris@16: typedef typename table::policy policy; Chris@16: typedef typename table::node_pointer node_pointer; Chris@16: typedef typename table::node_allocator node_allocator; Chris@16: typedef typename table::node_allocator_traits node_allocator_traits; Chris@16: typedef typename table::bucket_pointer bucket_pointer; Chris@16: typedef typename table::link_pointer link_pointer; Chris@16: typedef typename table::hasher hasher; Chris@16: typedef typename table::key_equal key_equal; Chris@16: typedef typename table::key_type key_type; Chris@16: typedef typename table::node_constructor node_constructor; Chris@16: typedef typename table::extractor extractor; Chris@16: typedef typename table::iterator iterator; Chris@16: typedef typename table::c_iterator c_iterator; Chris@16: Chris@16: // Constructors Chris@16: Chris@16: grouped_table_impl(std::size_t n, Chris@16: hasher const& hf, Chris@16: key_equal const& eq, Chris@16: node_allocator const& a) Chris@16: : table(n, hf, eq, a) Chris@16: {} Chris@16: Chris@16: grouped_table_impl(grouped_table_impl const& x) Chris@16: : table(x, node_allocator_traits:: Chris@16: select_on_container_copy_construction(x.node_alloc())) Chris@16: { Chris@16: this->init(x); Chris@16: } Chris@16: Chris@16: grouped_table_impl(grouped_table_impl const& x, Chris@16: node_allocator const& a) Chris@16: : table(x, a) Chris@16: { Chris@16: this->init(x); Chris@16: } Chris@16: Chris@16: grouped_table_impl(grouped_table_impl& x, Chris@16: boost::unordered::detail::move_tag m) Chris@16: : table(x, m) Chris@16: {} Chris@16: Chris@16: grouped_table_impl(grouped_table_impl& x, Chris@16: node_allocator const& a, Chris@16: boost::unordered::detail::move_tag m) Chris@16: : table(x, a, m) Chris@16: { Chris@16: this->move_init(x); Chris@16: } Chris@16: Chris@16: // Accessors Chris@16: Chris@16: template Chris@16: iterator find_node_impl( Chris@16: std::size_t key_hash, Chris@16: Key const& k, Chris@16: Pred const& eq) const Chris@16: { Chris@16: std::size_t bucket_index = this->hash_to_bucket(key_hash); Chris@16: iterator n = this->begin(bucket_index); Chris@16: Chris@16: for (;;) Chris@16: { Chris@16: if (!n.node_) return n; Chris@16: Chris@16: std::size_t node_hash = n.node_->hash_; Chris@16: if (key_hash == node_hash) Chris@16: { Chris@16: if (eq(k, this->get_key(*n))) Chris@16: return n; Chris@16: } Chris@16: else Chris@16: { Chris@16: if (this->hash_to_bucket(node_hash) != bucket_index) Chris@16: return iterator(); Chris@16: } Chris@16: Chris@16: n = iterator(n.node_->group_prev_->next_); Chris@16: } Chris@16: } Chris@16: Chris@16: std::size_t count(key_type const& k) const Chris@16: { Chris@16: iterator n = this->find_node(k); Chris@16: if (!n.node_) return 0; Chris@16: Chris@16: std::size_t x = 0; Chris@16: node_pointer it = n.node_; Chris@16: do { Chris@16: it = it->group_prev_; Chris@16: ++x; Chris@16: } while(it != n.node_); Chris@16: Chris@16: return x; Chris@16: } Chris@16: Chris@16: std::pair Chris@16: equal_range(key_type const& k) const Chris@16: { Chris@16: iterator n = this->find_node(k); Chris@16: return std::make_pair( Chris@16: n, n.node_ ? iterator(n.node_->group_prev_->next_) : n); Chris@16: } Chris@16: Chris@16: // Equality Chris@16: Chris@16: bool equals(grouped_table_impl const& other) const Chris@16: { Chris@16: if(this->size_ != other.size_) return false; Chris@16: Chris@16: for(iterator n1 = this->begin(); n1.node_;) Chris@16: { Chris@16: iterator n2 = other.find_matching_node(n1); Chris@16: if (!n2.node_) return false; Chris@16: iterator end1(n1.node_->group_prev_->next_); Chris@16: iterator end2(n2.node_->group_prev_->next_); Chris@16: if (!group_equals(n1, end1, n2, end2)) return false; Chris@16: n1 = end1; Chris@16: } Chris@16: Chris@16: return true; Chris@16: } Chris@16: Chris@16: static bool group_equals(iterator n1, iterator end1, Chris@16: iterator n2, iterator end2) Chris@16: { Chris@16: for(;;) Chris@16: { Chris@16: if (*n1 != *n2) break; Chris@16: Chris@16: ++n1; Chris@16: ++n2; Chris@16: Chris@16: if (n1 == end1) return n2 == end2; Chris@16: if (n2 == end2) return false; Chris@16: } Chris@16: Chris@16: for(iterator n1a = n1, n2a = n2;;) Chris@16: { Chris@16: ++n1a; Chris@16: ++n2a; Chris@16: Chris@16: if (n1a == end1) Chris@16: { Chris@16: if (n2a == end2) break; Chris@16: else return false; Chris@16: } Chris@16: Chris@16: if (n2a == end2) return false; Chris@16: } Chris@16: Chris@16: iterator start = n1; Chris@16: for(;n1 != end1; ++n1) Chris@16: { Chris@16: value_type const& v = *n1; Chris@16: if (find(start, n1, v)) continue; Chris@16: std::size_t matches = count_equal(n2, end2, v); Chris@16: if (!matches) return false; Chris@16: iterator next = n1; Chris@16: ++next; Chris@16: if (matches != 1 + count_equal(next, end1, v)) return false; Chris@16: } Chris@16: Chris@16: return true; Chris@16: } Chris@16: Chris@16: static bool find(iterator n, iterator end, value_type const& v) Chris@16: { Chris@16: for(;n != end; ++n) Chris@16: if (*n == v) Chris@16: return true; Chris@16: return false; Chris@16: } Chris@16: Chris@16: static std::size_t count_equal(iterator n, iterator end, Chris@16: value_type const& v) Chris@16: { Chris@16: std::size_t count = 0; Chris@16: for(;n != end; ++n) Chris@16: if (*n == v) ++count; Chris@16: return count; Chris@16: } Chris@16: Chris@16: // Emplace/Insert Chris@16: Chris@16: static inline void add_after_node( Chris@16: node_pointer n, Chris@16: node_pointer pos) Chris@16: { Chris@16: n->next_ = pos->group_prev_->next_; Chris@16: n->group_prev_ = pos->group_prev_; Chris@16: pos->group_prev_->next_ = n; Chris@16: pos->group_prev_ = n; Chris@16: } Chris@16: Chris@16: inline iterator add_node( Chris@16: node_constructor& a, Chris@16: std::size_t key_hash, Chris@16: iterator pos) Chris@16: { Chris@16: node_pointer n = a.release(); Chris@16: n->hash_ = key_hash; Chris@16: if (pos.node_) { Chris@16: this->add_after_node(n, pos.node_); Chris@16: if (n->next_) { Chris@16: std::size_t next_bucket = this->hash_to_bucket( Chris@16: static_cast(n->next_)->hash_); Chris@16: if (next_bucket != this->hash_to_bucket(key_hash)) { Chris@16: this->get_bucket(next_bucket)->next_ = n; Chris@16: } Chris@16: } Chris@16: } Chris@16: else { Chris@16: bucket_pointer b = this->get_bucket( Chris@16: this->hash_to_bucket(key_hash)); Chris@16: Chris@16: if (!b->next_) Chris@16: { Chris@16: link_pointer start_node = this->get_previous_start(); Chris@16: Chris@16: if (start_node->next_) { Chris@16: this->get_bucket(this->hash_to_bucket( Chris@16: static_cast(start_node->next_)->hash_ Chris@16: ))->next_ = n; Chris@16: } Chris@16: Chris@16: b->next_ = start_node; Chris@16: n->next_ = start_node->next_; Chris@16: start_node->next_ = n; Chris@16: } Chris@16: else Chris@16: { Chris@16: n->next_ = b->next_->next_; Chris@16: b->next_->next_ = n; Chris@16: } Chris@16: } Chris@16: ++this->size_; Chris@16: return iterator(n); Chris@16: } Chris@16: Chris@16: iterator emplace_impl(node_constructor& a) Chris@16: { Chris@16: key_type const& k = this->get_key(a.value()); Chris@16: std::size_t key_hash = this->hash(k); Chris@16: iterator position = this->find_node(key_hash, k); Chris@16: Chris@16: // reserve has basic exception safety if the hash function Chris@16: // throws, strong otherwise. Chris@16: this->reserve_for_insert(this->size_ + 1); Chris@16: return this->add_node(a, key_hash, position); Chris@16: } Chris@16: Chris@16: void emplace_impl_no_rehash(node_constructor& a) Chris@16: { Chris@16: key_type const& k = this->get_key(a.value()); Chris@16: std::size_t key_hash = this->hash(k); Chris@16: this->add_node(a, key_hash, this->find_node(key_hash, k)); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@16: # if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) Chris@16: iterator emplace(boost::unordered::detail::emplace_args1< Chris@16: boost::unordered::detail::please_ignore_this_overload> const&) Chris@16: { Chris@16: BOOST_ASSERT(false); Chris@16: return iterator(); Chris@16: } Chris@16: # else Chris@16: iterator emplace( Chris@16: boost::unordered::detail::please_ignore_this_overload const&) Chris@16: { Chris@16: BOOST_ASSERT(false); Chris@16: return iterator(); Chris@16: } Chris@16: # endif Chris@16: #endif Chris@16: Chris@16: template Chris@16: iterator emplace(BOOST_UNORDERED_EMPLACE_ARGS) Chris@16: { Chris@16: node_constructor a(this->node_alloc()); Chris@16: a.construct_with_value(BOOST_UNORDERED_EMPLACE_FORWARD); Chris@16: Chris@16: return iterator(emplace_impl(a)); Chris@16: } Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////// Chris@16: // Insert range methods Chris@16: Chris@16: // if hash function throws, or inserting > 1 element, basic exception Chris@16: // safety. Strong otherwise Chris@16: template Chris@16: typename boost::unordered::detail::enable_if_forward::type Chris@16: insert_range(I i, I j) Chris@16: { Chris@16: if(i == j) return; Chris@16: Chris@16: std::size_t distance = boost::unordered::detail::distance(i, j); Chris@16: if(distance == 1) { Chris@16: node_constructor a(this->node_alloc()); Chris@16: a.construct_with_value2(*i); Chris@16: emplace_impl(a); Chris@16: } Chris@16: else { Chris@16: // Only require basic exception safety here Chris@16: this->reserve_for_insert(this->size_ + distance); Chris@16: Chris@16: node_constructor a(this->node_alloc()); Chris@16: for (; i != j; ++i) { Chris@16: a.construct_with_value2(*i); Chris@16: emplace_impl_no_rehash(a); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: typename boost::unordered::detail::disable_if_forward::type Chris@16: insert_range(I i, I j) Chris@16: { Chris@16: node_constructor a(this->node_alloc()); Chris@16: for (; i != j; ++i) { Chris@16: a.construct_with_value2(*i); Chris@16: emplace_impl(a); Chris@16: } Chris@16: } Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////// Chris@16: // Erase Chris@16: // Chris@16: // no throw Chris@16: Chris@16: std::size_t erase_key(key_type const& k) Chris@16: { Chris@16: if(!this->size_) return 0; Chris@16: Chris@16: std::size_t key_hash = this->hash(k); Chris@16: std::size_t bucket_index = this->hash_to_bucket(key_hash); Chris@16: link_pointer prev = this->get_previous_start(bucket_index); Chris@16: if (!prev) return 0; Chris@16: Chris@16: for (;;) Chris@16: { Chris@16: if (!prev->next_) return 0; Chris@16: std::size_t node_hash = Chris@16: static_cast(prev->next_)->hash_; Chris@16: if (this->hash_to_bucket(node_hash) != bucket_index) Chris@16: return 0; Chris@16: if (node_hash == key_hash && Chris@16: this->key_eq()(k, this->get_key( Chris@16: static_cast(prev->next_)->value()))) Chris@16: break; Chris@16: prev = static_cast(prev->next_)->group_prev_; Chris@16: } Chris@16: Chris@16: node_pointer first_node = static_cast(prev->next_); Chris@16: link_pointer end = first_node->group_prev_->next_; Chris@16: Chris@101: std::size_t deleted_count = this->delete_nodes(prev, end); Chris@16: this->fix_bucket(bucket_index, prev); Chris@101: return deleted_count; Chris@16: } Chris@16: Chris@16: iterator erase(c_iterator r) Chris@16: { Chris@16: BOOST_ASSERT(r.node_); Chris@16: iterator next(r.node_); Chris@16: ++next; Chris@16: erase_nodes(r.node_, next.node_); Chris@16: return next; Chris@16: } Chris@16: Chris@16: iterator erase_range(c_iterator r1, c_iterator r2) Chris@16: { Chris@16: if (r1 == r2) return iterator(r2.node_); Chris@16: erase_nodes(r1.node_, r2.node_); Chris@16: return iterator(r2.node_); Chris@16: } Chris@16: Chris@101: link_pointer erase_nodes(node_pointer i, node_pointer j) Chris@16: { Chris@101: std::size_t bucket_index = this->hash_to_bucket(i->hash_); Chris@16: Chris@101: // Split the groups containing 'i' and 'j'. Chris@101: // And get the pointer to the node before i while Chris@16: // we're at it. Chris@101: link_pointer prev = split_groups(i, j); Chris@16: Chris@101: // If we don't have a 'prev' it means that i is at the Chris@16: // beginning of a block, so search through the blocks in the Chris@16: // same bucket. Chris@16: if (!prev) { Chris@16: prev = this->get_previous_start(bucket_index); Chris@101: while (prev->next_ != i) Chris@16: prev = static_cast(prev->next_)->group_prev_; Chris@16: } Chris@16: Chris@16: // Delete the nodes. Chris@16: do { Chris@16: link_pointer group_end = Chris@16: static_cast(prev->next_)->group_prev_->next_; Chris@16: this->delete_nodes(prev, group_end); Chris@16: bucket_index = this->fix_bucket(bucket_index, prev); Chris@101: } while(prev->next_ != j); Chris@16: Chris@16: return prev; Chris@16: } Chris@16: Chris@101: static link_pointer split_groups(node_pointer i, node_pointer j) Chris@16: { Chris@101: node_pointer prev = i->group_prev_; Chris@101: if (prev->next_ != i) prev = node_pointer(); Chris@16: Chris@101: if (j) { Chris@101: node_pointer first = j; Chris@101: while (first != i && first->group_prev_->next_ == first) { Chris@16: first = first->group_prev_; Chris@16: } Chris@16: Chris@101: boost::swap(first->group_prev_, j->group_prev_); Chris@101: if (first == i) return prev; Chris@16: } Chris@16: Chris@16: if (prev) { Chris@16: node_pointer first = prev; Chris@16: while (first->group_prev_->next_ == first) { Chris@16: first = first->group_prev_; Chris@16: } Chris@101: boost::swap(first->group_prev_, i->group_prev_); Chris@16: } Chris@16: Chris@16: return prev; Chris@16: } Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////// Chris@16: // fill_buckets Chris@16: Chris@16: template Chris@16: static void fill_buckets(iterator n, table& dst, Chris@16: NodeCreator& creator) Chris@16: { Chris@16: link_pointer prev = dst.get_previous_start(); Chris@16: Chris@16: while (n.node_) { Chris@16: std::size_t key_hash = n.node_->hash_; Chris@16: iterator group_end(n.node_->group_prev_->next_); Chris@16: Chris@16: node_pointer first_node = creator.create(*n); Chris@16: node_pointer end = first_node; Chris@16: first_node->hash_ = key_hash; Chris@16: prev->next_ = first_node; Chris@16: ++dst.size_; Chris@16: Chris@16: for (++n; n != group_end; ++n) Chris@16: { Chris@16: end = creator.create(*n); Chris@16: end->hash_ = key_hash; Chris@16: add_after_node(end, first_node); Chris@16: ++dst.size_; Chris@16: } Chris@16: Chris@16: prev = place_in_bucket(dst, prev, end); Chris@16: } Chris@16: } Chris@16: Chris@16: // strong otherwise exception safety Chris@16: void rehash_impl(std::size_t num_buckets) Chris@16: { Chris@16: BOOST_ASSERT(this->buckets_); Chris@16: Chris@16: this->create_buckets(num_buckets); Chris@16: link_pointer prev = this->get_previous_start(); Chris@16: while (prev->next_) Chris@16: prev = place_in_bucket(*this, prev, Chris@16: static_cast(prev->next_)->group_prev_); Chris@16: } Chris@16: Chris@16: // Iterate through the nodes placing them in the correct buckets. Chris@16: // pre: prev->next_ is not null. Chris@16: static link_pointer place_in_bucket(table& dst, Chris@16: link_pointer prev, node_pointer end) Chris@16: { Chris@16: bucket_pointer b = dst.get_bucket(dst.hash_to_bucket(end->hash_)); Chris@16: Chris@16: if (!b->next_) { Chris@16: b->next_ = prev; Chris@16: return end; Chris@16: } Chris@16: else { Chris@16: link_pointer next = end->next_; Chris@16: end->next_ = b->next_->next_; Chris@16: b->next_->next_ = prev->next_; Chris@16: prev->next_ = next; Chris@16: return prev; Chris@16: } Chris@16: } Chris@16: }; Chris@16: }}} Chris@16: Chris@16: #endif