YouCompleteMe/cpp/BoostParts/boost/unordered/detail/buckets.hpp

889 lines
26 KiB
C++
Raw Normal View History

2012-05-10 00:45:30 -04:00
// Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
// Copyright (C) 2005-2011 Daniel James
// 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)
#ifndef BOOST_UNORDERED_DETAIL_MANAGER_HPP_INCLUDED
#define BOOST_UNORDERED_DETAIL_MANAGER_HPP_INCLUDED
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
#include <boost/unordered/detail/util.hpp>
2013-01-13 17:38:19 -05:00
#include <boost/unordered/detail/allocate.hpp>
2012-05-10 00:45:30 -04:00
#include <boost/type_traits/aligned_storage.hpp>
#include <boost/type_traits/alignment_of.hpp>
2013-08-25 17:35:47 -04:00
#include <boost/type_traits/is_nothrow_move_constructible.hpp>
#include <boost/type_traits/is_nothrow_move_assignable.hpp>
2012-05-10 00:45:30 -04:00
#include <boost/swap.hpp>
#include <boost/assert.hpp>
2012-07-21 14:37:40 -04:00
#include <boost/limits.hpp>
#include <boost/iterator.hpp>
2012-05-10 00:45:30 -04:00
namespace boost { namespace unordered { namespace detail {
template <typename Types> struct table;
template <typename NodePointer> struct bucket;
struct ptr_bucket;
2012-07-21 14:37:40 -04:00
template <typename Types> struct table_impl;
template <typename Types> struct grouped_table_impl;
2012-05-10 00:45:30 -04:00
2012-07-21 14:37:40 -04:00
}}}
2014-03-01 14:00:20 -05:00
// The 'iterator_detail' namespace was a misguided attempt at avoiding ADL
// in the detail namespace. It didn't work because the template parameters
// were in detail. I'm not changing it at the moment to be safe. I might
// do in the future if I change the iterator types.
2012-07-21 14:37:40 -04:00
namespace boost { namespace unordered { namespace iterator_detail {
////////////////////////////////////////////////////////////////////////////
// Iterators
//
// all no throw
2013-02-13 23:03:52 -05:00
template <typename Node> struct iterator;
template <typename Node, typename ConstNodePointer> struct c_iterator;
template <typename Node, typename Policy> struct l_iterator;
template <typename Node, typename ConstNodePointer, typename Policy>
struct cl_iterator;
2012-07-21 14:37:40 -04:00
// Local Iterators
//
// all no throw
2013-02-13 23:03:52 -05:00
template <typename Node, typename Policy>
2012-07-21 14:37:40 -04:00
struct l_iterator
: public boost::iterator<
2013-02-13 23:03:52 -05:00
std::forward_iterator_tag,
typename Node::value_type,
std::ptrdiff_t,
typename Node::node_pointer,
typename Node::value_type&>
2012-07-21 14:37:40 -04:00
{
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
2013-02-13 23:03:52 -05:00
template <typename Node2, typename ConstNodePointer, typename Policy2>
2012-07-21 14:37:40 -04:00
friend struct boost::unordered::iterator_detail::cl_iterator;
private:
#endif
2013-02-13 23:03:52 -05:00
typedef typename Node::node_pointer node_pointer;
typedef boost::unordered::iterator_detail::iterator<Node> iterator;
2012-07-21 14:37:40 -04:00
node_pointer ptr_;
std::size_t bucket_;
std::size_t bucket_count_;
public:
2013-02-13 23:03:52 -05:00
typedef typename Node::value_type value_type;
2014-03-01 14:00:20 -05:00
l_iterator() BOOST_NOEXCEPT : ptr_() {}
2012-07-21 14:37:40 -04:00
2014-03-01 14:00:20 -05:00
l_iterator(iterator x, std::size_t b, std::size_t c) BOOST_NOEXCEPT
2012-07-21 14:37:40 -04:00
: ptr_(x.node_), bucket_(b), bucket_count_(c) {}
2013-02-13 23:03:52 -05:00
value_type& operator*() const {
2012-07-21 14:37:40 -04:00
return ptr_->value();
}
2013-02-13 23:03:52 -05:00
value_type* operator->() const {
2012-07-21 14:37:40 -04:00
return ptr_->value_ptr();
}
l_iterator& operator++() {
ptr_ = static_cast<node_pointer>(ptr_->next_);
if (ptr_ && Policy::to_bucket(bucket_count_, ptr_->hash_)
!= bucket_)
ptr_ = node_pointer();
return *this;
}
l_iterator operator++(int) {
l_iterator tmp(*this);
++(*this);
return tmp;
}
2014-03-01 14:00:20 -05:00
bool operator==(l_iterator x) const BOOST_NOEXCEPT {
2012-07-21 14:37:40 -04:00
return ptr_ == x.ptr_;
}
2014-03-01 14:00:20 -05:00
bool operator!=(l_iterator x) const BOOST_NOEXCEPT {
2012-07-21 14:37:40 -04:00
return ptr_ != x.ptr_;
}
};
2013-02-13 23:03:52 -05:00
template <typename Node, typename ConstNodePointer, typename Policy>
2012-07-21 14:37:40 -04:00
struct cl_iterator
: public boost::iterator<
2013-02-13 23:03:52 -05:00
std::forward_iterator_tag,
typename Node::value_type,
std::ptrdiff_t,
ConstNodePointer,
typename Node::value_type const&>
2012-07-21 14:37:40 -04:00
{
friend struct boost::unordered::iterator_detail::l_iterator
2013-02-13 23:03:52 -05:00
<Node, Policy>;
2012-07-21 14:37:40 -04:00
private:
2013-02-13 23:03:52 -05:00
typedef typename Node::node_pointer node_pointer;
typedef boost::unordered::iterator_detail::iterator<Node> iterator;
2012-07-21 14:37:40 -04:00
node_pointer ptr_;
std::size_t bucket_;
std::size_t bucket_count_;
public:
2013-02-13 23:03:52 -05:00
typedef typename Node::value_type value_type;
2014-03-01 14:00:20 -05:00
cl_iterator() BOOST_NOEXCEPT : ptr_() {}
2012-07-21 14:37:40 -04:00
2014-03-01 14:00:20 -05:00
cl_iterator(iterator x, std::size_t b, std::size_t c) BOOST_NOEXCEPT :
2012-07-21 14:37:40 -04:00
ptr_(x.node_), bucket_(b), bucket_count_(c) {}
cl_iterator(boost::unordered::iterator_detail::l_iterator<
2014-03-01 14:00:20 -05:00
Node, Policy> const& x) BOOST_NOEXCEPT :
2012-07-21 14:37:40 -04:00
ptr_(x.ptr_), bucket_(x.bucket_), bucket_count_(x.bucket_count_)
{}
2013-02-13 23:03:52 -05:00
value_type const& operator*() const {
2012-07-21 14:37:40 -04:00
return ptr_->value();
}
2013-02-13 23:03:52 -05:00
value_type const* operator->() const {
2012-07-21 14:37:40 -04:00
return ptr_->value_ptr();
}
cl_iterator& operator++() {
ptr_ = static_cast<node_pointer>(ptr_->next_);
if (ptr_ && Policy::to_bucket(bucket_count_, ptr_->hash_)
!= bucket_)
ptr_ = node_pointer();
return *this;
}
cl_iterator operator++(int) {
cl_iterator tmp(*this);
++(*this);
return tmp;
}
2014-03-01 14:00:20 -05:00
friend bool operator==(cl_iterator const& x, cl_iterator const& y)
BOOST_NOEXCEPT
{
2012-07-21 14:37:40 -04:00
return x.ptr_ == y.ptr_;
}
2014-03-01 14:00:20 -05:00
friend bool operator!=(cl_iterator const& x, cl_iterator const& y)
BOOST_NOEXCEPT
{
2012-07-21 14:37:40 -04:00
return x.ptr_ != y.ptr_;
}
};
2013-02-13 23:03:52 -05:00
template <typename Node>
2012-07-21 14:37:40 -04:00
struct iterator
: public boost::iterator<
2013-02-13 23:03:52 -05:00
std::forward_iterator_tag,
typename Node::value_type,
std::ptrdiff_t,
typename Node::node_pointer,
typename Node::value_type&>
2012-07-21 14:37:40 -04:00
{
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
2013-02-13 23:03:52 -05:00
template <typename, typename>
2012-07-21 14:37:40 -04:00
friend struct boost::unordered::iterator_detail::c_iterator;
2013-02-13 23:03:52 -05:00
template <typename, typename>
2012-07-21 14:37:40 -04:00
friend struct boost::unordered::iterator_detail::l_iterator;
2013-02-13 23:03:52 -05:00
template <typename, typename, typename>
2012-07-21 14:37:40 -04:00
friend struct boost::unordered::iterator_detail::cl_iterator;
template <typename>
friend struct boost::unordered::detail::table;
template <typename>
friend struct boost::unordered::detail::table_impl;
template <typename>
friend struct boost::unordered::detail::grouped_table_impl;
private:
#endif
2013-02-13 23:03:52 -05:00
typedef typename Node::node_pointer node_pointer;
2012-07-21 14:37:40 -04:00
node_pointer node_;
public:
2013-02-13 23:03:52 -05:00
typedef typename Node::value_type value_type;
2014-03-01 14:00:20 -05:00
iterator() BOOST_NOEXCEPT : node_() {}
2012-07-21 14:37:40 -04:00
2014-03-01 14:00:20 -05:00
explicit iterator(typename Node::link_pointer x) BOOST_NOEXCEPT :
2013-02-13 23:03:52 -05:00
node_(static_cast<node_pointer>(x)) {}
2012-07-21 14:37:40 -04:00
2013-02-13 23:03:52 -05:00
value_type& operator*() const {
2012-07-21 14:37:40 -04:00
return node_->value();
}
2013-02-13 23:03:52 -05:00
value_type* operator->() const {
2012-07-21 14:37:40 -04:00
return &node_->value();
}
iterator& operator++() {
node_ = static_cast<node_pointer>(node_->next_);
return *this;
}
iterator operator++(int) {
iterator tmp(node_);
node_ = static_cast<node_pointer>(node_->next_);
return tmp;
}
2014-03-01 14:00:20 -05:00
bool operator==(iterator const& x) const BOOST_NOEXCEPT {
2012-07-21 14:37:40 -04:00
return node_ == x.node_;
}
2014-03-01 14:00:20 -05:00
bool operator!=(iterator const& x) const BOOST_NOEXCEPT {
2012-07-21 14:37:40 -04:00
return node_ != x.node_;
}
};
2013-02-13 23:03:52 -05:00
template <typename Node, typename ConstNodePointer>
2012-07-21 14:37:40 -04:00
struct c_iterator
: public boost::iterator<
2013-02-13 23:03:52 -05:00
std::forward_iterator_tag,
typename Node::value_type,
std::ptrdiff_t,
ConstNodePointer,
typename Node::value_type const&>
2012-07-21 14:37:40 -04:00
{
2013-02-13 23:03:52 -05:00
friend struct boost::unordered::iterator_detail::iterator<Node>;
2012-07-21 14:37:40 -04:00
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
template <typename>
friend struct boost::unordered::detail::table;
template <typename>
friend struct boost::unordered::detail::table_impl;
template <typename>
friend struct boost::unordered::detail::grouped_table_impl;
private:
#endif
2013-02-13 23:03:52 -05:00
typedef typename Node::node_pointer node_pointer;
typedef boost::unordered::iterator_detail::iterator<Node> iterator;
2012-07-21 14:37:40 -04:00
node_pointer node_;
public:
2013-02-13 23:03:52 -05:00
typedef typename Node::value_type value_type;
2014-03-01 14:00:20 -05:00
c_iterator() BOOST_NOEXCEPT : node_() {}
2012-07-21 14:37:40 -04:00
2014-03-01 14:00:20 -05:00
explicit c_iterator(typename Node::link_pointer x) BOOST_NOEXCEPT :
2013-02-13 23:03:52 -05:00
node_(static_cast<node_pointer>(x)) {}
2012-07-21 14:37:40 -04:00
2014-03-01 14:00:20 -05:00
c_iterator(iterator const& x) BOOST_NOEXCEPT : node_(x.node_) {}
2012-07-21 14:37:40 -04:00
2013-02-13 23:03:52 -05:00
value_type const& operator*() const {
2012-07-21 14:37:40 -04:00
return node_->value();
}
2013-02-13 23:03:52 -05:00
value_type const* operator->() const {
2012-07-21 14:37:40 -04:00
return &node_->value();
}
c_iterator& operator++() {
node_ = static_cast<node_pointer>(node_->next_);
return *this;
}
c_iterator operator++(int) {
c_iterator tmp(node_);
node_ = static_cast<node_pointer>(node_->next_);
return tmp;
}
2014-03-01 14:00:20 -05:00
friend bool operator==(c_iterator const& x, c_iterator const& y)
BOOST_NOEXCEPT
{
2012-07-21 14:37:40 -04:00
return x.node_ == y.node_;
}
2014-03-01 14:00:20 -05:00
friend bool operator!=(c_iterator const& x, c_iterator const& y)
BOOST_NOEXCEPT
{
2012-07-21 14:37:40 -04:00
return x.node_ != y.node_;
}
};
}}}
namespace boost { namespace unordered { namespace detail {
///////////////////////////////////////////////////////////////////
//
2013-01-13 17:38:19 -05:00
// Node construction
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
template <typename NodeAlloc>
struct node_constructor
2012-07-21 14:37:40 -04:00
{
2013-01-13 17:38:19 -05:00
private:
typedef NodeAlloc node_allocator;
typedef boost::unordered::detail::allocator_traits<NodeAlloc>
node_allocator_traits;
typedef typename node_allocator_traits::value_type node;
typedef typename node_allocator_traits::pointer node_pointer;
typedef typename node::value_type value_type;
protected:
node_allocator& alloc_;
node_pointer node_;
bool node_constructed_;
bool value_constructed_;
public:
node_constructor(node_allocator& n) :
alloc_(n),
node_(),
node_constructed_(false),
value_constructed_(false)
{
2012-07-21 14:37:40 -04:00
}
2013-01-13 17:38:19 -05:00
~node_constructor();
void construct();
template <BOOST_UNORDERED_EMPLACE_TEMPLATE>
void construct_with_value(BOOST_UNORDERED_EMPLACE_ARGS)
{
construct();
2014-03-01 14:00:20 -05:00
boost::unordered::detail::func::construct_value_impl(
2013-01-13 17:38:19 -05:00
alloc_, node_->value_ptr(), BOOST_UNORDERED_EMPLACE_FORWARD);
value_constructed_ = true;
2012-07-21 14:37:40 -04:00
}
2013-01-13 17:38:19 -05:00
template <typename A0>
void construct_with_value2(BOOST_FWD_REF(A0) a0)
{
construct();
2014-03-01 14:00:20 -05:00
boost::unordered::detail::func::construct_value_impl(
2013-01-13 17:38:19 -05:00
alloc_, node_->value_ptr(),
BOOST_UNORDERED_EMPLACE_ARGS1(boost::forward<A0>(a0)));
value_constructed_ = true;
2012-07-21 14:37:40 -04:00
}
2013-01-13 17:38:19 -05:00
value_type const& value() const {
BOOST_ASSERT(node_ && node_constructed_ && value_constructed_);
return node_->value();
2012-07-21 14:37:40 -04:00
}
2013-01-13 17:38:19 -05:00
// no throw
node_pointer release()
{
BOOST_ASSERT(node_ && node_constructed_);
node_pointer p = node_;
node_ = node_pointer();
return p;
}
private:
node_constructor(node_constructor const&);
node_constructor& operator=(node_constructor const&);
2012-07-21 14:37:40 -04:00
};
2013-01-13 17:38:19 -05:00
template <typename Alloc>
node_constructor<Alloc>::~node_constructor()
2012-07-21 14:37:40 -04:00
{
2013-01-13 17:38:19 -05:00
if (node_) {
if (value_constructed_) {
2014-03-01 14:00:20 -05:00
boost::unordered::detail::func::destroy_value_impl(alloc_,
2013-01-13 17:38:19 -05:00
node_->value_ptr());
}
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
if (node_constructed_) {
node_allocator_traits::destroy(alloc_,
boost::addressof(*node_));
}
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
node_allocator_traits::deallocate(alloc_, node_, 1);
2012-07-21 14:37:40 -04:00
}
2013-01-13 17:38:19 -05:00
}
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
template <typename Alloc>
void node_constructor<Alloc>::construct()
{
if(!node_) {
node_constructed_ = false;
value_constructed_ = false;
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
node_ = node_allocator_traits::allocate(alloc_, 1);
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
node_allocator_traits::construct(alloc_,
boost::addressof(*node_), node());
2013-02-13 23:03:52 -05:00
node_->init(node_);
2013-01-13 17:38:19 -05:00
node_constructed_ = true;
}
else {
BOOST_ASSERT(node_constructed_);
2012-07-21 14:37:40 -04:00
2013-01-13 17:38:19 -05:00
if (value_constructed_)
{
2014-03-01 14:00:20 -05:00
boost::unordered::detail::func::destroy_value_impl(alloc_,
2013-01-13 17:38:19 -05:00
node_->value_ptr());
value_constructed_ = false;
}
}
}
2012-07-21 14:37:40 -04:00
2012-05-10 00:45:30 -04:00
///////////////////////////////////////////////////////////////////
//
2013-01-13 17:38:19 -05:00
// Node Holder
//
// Temporary store for nodes. Deletes any that aren't used.
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <typename NodeAlloc>
struct node_holder : private node_constructor<NodeAlloc>
2012-05-10 00:45:30 -04:00
{
private:
2013-01-13 17:38:19 -05:00
typedef node_constructor<NodeAlloc> base;
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
typedef NodeAlloc node_allocator;
typedef boost::unordered::detail::allocator_traits<NodeAlloc>
node_allocator_traits;
typedef typename node_allocator_traits::value_type node;
typedef typename node_allocator_traits::pointer node_pointer;
typedef typename node::value_type value_type;
typedef typename node::link_pointer link_pointer;
2013-02-13 23:03:52 -05:00
typedef boost::unordered::iterator_detail::iterator<node> iterator;
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
node_pointer nodes_;
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
public:
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <typename Table>
explicit node_holder(Table& b) :
base(b.node_alloc()),
nodes_()
2012-05-10 00:45:30 -04:00
{
2013-01-13 17:38:19 -05:00
if (b.size_) {
2013-02-13 23:03:52 -05:00
typename Table::link_pointer prev = b.get_previous_start();
2013-01-13 17:38:19 -05:00
nodes_ = static_cast<node_pointer>(prev->next_);
prev->next_ = link_pointer();
b.size_ = 0;
}
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
~node_holder();
2012-05-10 00:45:30 -04:00
2013-02-13 23:03:52 -05:00
void node_for_assignment()
{
if (!this->node_ && nodes_) {
this->node_ = nodes_;
nodes_ = static_cast<node_pointer>(nodes_->next_);
this->node_->init(this->node_);
this->node_->next_ = link_pointer();
this->node_constructed_ = true;
this->value_constructed_ = true;
}
}
2013-01-13 17:38:19 -05:00
template <typename T>
inline void assign_impl(T const& v) {
2013-02-13 23:03:52 -05:00
if (this->node_ && this->value_constructed_) {
this->node_->value() = v;
}
else {
this->construct_with_value2(v);
}
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
template <typename T1, typename T2>
inline void assign_impl(std::pair<T1 const, T2> const& v) {
2013-02-13 23:03:52 -05:00
this->construct_with_value2(v);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
template <typename T>
inline void move_assign_impl(T& v) {
2013-02-13 23:03:52 -05:00
if (this->node_ && this->value_constructed_) {
this->node_->value() = boost::move(v);
}
else {
this->construct_with_value2(boost::move(v));
}
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
template <typename T1, typename T2>
inline void move_assign_impl(std::pair<T1 const, T2>& v) {
2013-02-13 23:03:52 -05:00
this->construct_with_value2(boost::move(v));
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
node_pointer copy_of(value_type const& v)
2012-05-10 00:45:30 -04:00
{
2013-02-13 23:03:52 -05:00
node_for_assignment();
assign_impl(v);
return base::release();
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
node_pointer move_copy_of(value_type& v)
2012-05-10 00:45:30 -04:00
{
2013-02-13 23:03:52 -05:00
node_for_assignment();
move_assign_impl(v);
return base::release();
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
iterator begin() const
2012-05-10 00:45:30 -04:00
{
2013-01-13 17:38:19 -05:00
return iterator(nodes_);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
};
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <typename Alloc>
node_holder<Alloc>::~node_holder()
{
while (nodes_) {
node_pointer p = nodes_;
nodes_ = static_cast<node_pointer>(p->next_);
2012-05-10 00:45:30 -04:00
2014-03-01 14:00:20 -05:00
boost::unordered::detail::func::destroy_value_impl(this->alloc_,
2013-01-13 17:38:19 -05:00
p->value_ptr());
node_allocator_traits::destroy(this->alloc_, boost::addressof(*p));
node_allocator_traits::deallocate(this->alloc_, p, 1);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
}
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
///////////////////////////////////////////////////////////////////
//
// Bucket
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <typename NodePointer>
struct bucket
{
2013-02-13 23:03:52 -05:00
typedef NodePointer link_pointer;
link_pointer next_;
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
bucket() : next_() {}
2012-05-10 00:45:30 -04:00
2013-02-13 23:03:52 -05:00
link_pointer first_from_start()
2012-05-10 00:45:30 -04:00
{
2013-01-13 17:38:19 -05:00
return next_;
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
enum { extra_node = true };
};
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
struct ptr_bucket
{
2013-02-13 23:03:52 -05:00
typedef ptr_bucket* link_pointer;
link_pointer next_;
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
ptr_bucket() : next_(0) {}
2012-05-10 00:45:30 -04:00
2013-02-13 23:03:52 -05:00
link_pointer first_from_start()
2012-05-10 00:45:30 -04:00
{
2013-01-13 17:38:19 -05:00
return this;
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
enum { extra_node = false };
};
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
///////////////////////////////////////////////////////////////////
//
// Hash Policy
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <typename SizeT>
struct prime_policy
{
template <typename Hash, typename T>
static inline SizeT apply_hash(Hash const& hf, T const& x) {
return hf(x);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
static inline SizeT to_bucket(SizeT bucket_count, SizeT hash) {
return hash % bucket_count;
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
static inline SizeT new_bucket_count(SizeT min) {
return boost::unordered::detail::next_prime(min);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
static inline SizeT prev_bucket_count(SizeT max) {
return boost::unordered::detail::prev_prime(max);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
};
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <typename SizeT>
struct mix64_policy
{
template <typename Hash, typename T>
static inline SizeT apply_hash(Hash const& hf, T const& x) {
SizeT key = hf(x);
key = (~key) + (key << 21); // key = (key << 21) - key - 1;
key = key ^ (key >> 24);
key = (key + (key << 3)) + (key << 8); // key * 265
key = key ^ (key >> 14);
key = (key + (key << 2)) + (key << 4); // key * 21
key = key ^ (key >> 28);
key = key + (key << 31);
return key;
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
static inline SizeT to_bucket(SizeT bucket_count, SizeT hash) {
return hash & (bucket_count - 1);
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
static inline SizeT new_bucket_count(SizeT min) {
if (min <= 4) return 4;
--min;
min |= min >> 1;
min |= min >> 2;
min |= min >> 4;
min |= min >> 8;
min |= min >> 16;
min |= min >> 32;
return min + 1;
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
static inline SizeT prev_bucket_count(SizeT max) {
max |= max >> 1;
max |= max >> 2;
max |= max >> 4;
max |= max >> 8;
max |= max >> 16;
max |= max >> 32;
return (max >> 1) + 1;
2012-05-10 00:45:30 -04:00
}
2013-01-13 17:38:19 -05:00
};
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <int digits, int radix>
struct pick_policy_impl {
typedef prime_policy<std::size_t> type;
};
2012-05-10 00:45:30 -04:00
2013-01-13 17:38:19 -05:00
template <>
struct pick_policy_impl<64, 2> {
typedef mix64_policy<std::size_t> type;
2012-05-10 00:45:30 -04:00
};
2013-01-13 17:38:19 -05:00
struct pick_policy :
pick_policy_impl<
std::numeric_limits<std::size_t>::digits,
std::numeric_limits<std::size_t>::radix> {};
2012-05-10 00:45:30 -04:00
////////////////////////////////////////////////////////////////////////////
// Functions
// Assigning and swapping the equality and hash function objects
// needs strong exception safety. To implement that normally we'd
// require one of them to be known to not throw and the other to
// guarantee strong exception safety. Unfortunately they both only
// have basic exception safety. So to acheive strong exception
// safety we have storage space for two copies, and assign the new
// copies to the unused space. Then switch to using that to use
// them. This is implemented in 'set_hash_functions' which
// atomically assigns the new function objects in a strongly
// exception safe manner.
2013-08-25 17:35:47 -04:00
template <class H, class P, bool NoThrowMoveAssign>
class set_hash_functions;
2012-05-10 00:45:30 -04:00
template <class H, class P>
class functions
{
2013-08-25 17:35:47 -04:00
public:
static const bool nothrow_move_assignable =
boost::is_nothrow_move_assignable<H>::value &&
boost::is_nothrow_move_assignable<P>::value;
static const bool nothrow_move_constructible =
boost::is_nothrow_move_constructible<H>::value &&
boost::is_nothrow_move_constructible<P>::value;
private:
friend class boost::unordered::detail::set_hash_functions<H, P,
nothrow_move_assignable>;
2012-05-10 00:45:30 -04:00
functions& operator=(functions const&);
typedef compressed<H, P> function_pair;
typedef typename boost::aligned_storage<
sizeof(function_pair),
boost::alignment_of<function_pair>::value>::type aligned_function;
bool current_; // The currently active functions.
aligned_function funcs_[2];
function_pair const& current() const {
return *static_cast<function_pair const*>(
static_cast<void const*>(&funcs_[current_]));
}
2013-08-25 17:35:47 -04:00
function_pair& current() {
return *static_cast<function_pair*>(
static_cast<void*>(&funcs_[current_]));
}
2012-05-10 00:45:30 -04:00
void construct(bool which, H const& hf, P const& eq)
{
new((void*) &funcs_[which]) function_pair(hf, eq);
}
2014-03-01 14:00:20 -05:00
void construct(bool which, function_pair const& f,
boost::unordered::detail::false_type =
boost::unordered::detail::false_type())
2012-05-10 00:45:30 -04:00
{
new((void*) &funcs_[which]) function_pair(f);
}
2013-08-25 17:35:47 -04:00
void construct(bool which, function_pair& f,
2014-03-01 14:00:20 -05:00
boost::unordered::detail::true_type)
2013-08-25 17:35:47 -04:00
{
2014-03-01 14:00:20 -05:00
new((void*) &funcs_[which]) function_pair(f,
boost::unordered::detail::move_tag());
2013-08-25 17:35:47 -04:00
}
2012-05-10 00:45:30 -04:00
void destroy(bool which)
{
2014-03-01 14:00:20 -05:00
boost::unordered::detail::func::destroy((function_pair*)(&funcs_[which]));
2012-05-10 00:45:30 -04:00
}
public:
2013-08-25 17:35:47 -04:00
typedef boost::unordered::detail::set_hash_functions<H, P,
nothrow_move_assignable> set_hash_functions;
2012-05-10 00:45:30 -04:00
functions(H const& hf, P const& eq)
: current_(false)
{
construct(current_, hf, eq);
}
functions(functions const& bf)
: current_(false)
{
construct(current_, bf.current());
}
2014-03-01 14:00:20 -05:00
functions(functions& bf, boost::unordered::detail::move_tag)
2013-08-25 17:35:47 -04:00
: current_(false)
{
2014-03-01 14:00:20 -05:00
construct(current_, bf.current(),
boost::unordered::detail::integral_constant<bool,
nothrow_move_constructible>());
2013-08-25 17:35:47 -04:00
}
2012-05-10 00:45:30 -04:00
~functions() {
this->destroy(current_);
}
H const& hash_function() const {
return current().first();
}
P const& key_eq() const {
return current().second();
}
};
2013-08-25 17:35:47 -04:00
2012-05-10 00:45:30 -04:00
template <class H, class P>
2013-08-25 17:35:47 -04:00
class set_hash_functions<H, P, false>
2012-05-10 00:45:30 -04:00
{
set_hash_functions(set_hash_functions const&);
set_hash_functions& operator=(set_hash_functions const&);
2013-08-25 17:35:47 -04:00
typedef functions<H, P> functions_type;
2012-05-10 00:45:30 -04:00
2013-08-25 17:35:47 -04:00
functions_type& functions_;
2012-05-10 00:45:30 -04:00
bool tmp_functions_;
public:
2013-08-25 17:35:47 -04:00
set_hash_functions(functions_type& f, H const& h, P const& p)
2012-05-10 00:45:30 -04:00
: functions_(f),
tmp_functions_(!f.current_)
{
f.construct(tmp_functions_, h, p);
}
2013-08-25 17:35:47 -04:00
set_hash_functions(functions_type& f, functions_type const& other)
2012-05-10 00:45:30 -04:00
: functions_(f),
tmp_functions_(!f.current_)
{
f.construct(tmp_functions_, other.current());
}
~set_hash_functions()
{
functions_.destroy(tmp_functions_);
}
void commit()
{
functions_.current_ = tmp_functions_;
tmp_functions_ = !tmp_functions_;
}
};
2013-08-25 17:35:47 -04:00
template <class H, class P>
class set_hash_functions<H, P, true>
{
set_hash_functions(set_hash_functions const&);
set_hash_functions& operator=(set_hash_functions const&);
typedef functions<H, P> functions_type;
functions_type& functions_;
H hash_;
P pred_;
public:
set_hash_functions(functions_type& f, H const& h, P const& p) :
functions_(f),
hash_(h),
pred_(p) {}
set_hash_functions(functions_type& f, functions_type const& other) :
functions_(f),
hash_(other.hash_function()),
pred_(other.key_eq()) {}
void commit()
{
functions_.current().first() = boost::move(hash_);
functions_.current().second() = boost::move(pred_);
}
};
2013-01-13 17:38:19 -05:00
////////////////////////////////////////////////////////////////////////////
// rvalue parameters when type can't be a BOOST_RV_REF(T) parameter
// e.g. for int
2013-02-13 23:03:52 -05:00
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
2013-01-13 17:38:19 -05:00
# define BOOST_UNORDERED_RV_REF(T) BOOST_RV_REF(T)
#else
struct please_ignore_this_overload {
typedef please_ignore_this_overload type;
};
template <typename T>
struct rv_ref_impl {
typedef BOOST_RV_REF(T) type;
};
template <typename T>
struct rv_ref :
boost::detail::if_true<
boost::is_class<T>::value
>::BOOST_NESTED_TEMPLATE then <
boost::unordered::detail::rv_ref_impl<T>,
please_ignore_this_overload
>::type
{};
# define BOOST_UNORDERED_RV_REF(T) \
typename boost::unordered::detail::rv_ref<T>::type
2012-05-10 00:45:30 -04:00
#endif
2013-01-13 17:38:19 -05:00
}}}
2012-05-10 00:45:30 -04:00
#endif