2009-08-30 16:42:28 +00:00
|
|
|
|
|
|
|
// Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
|
2011-06-04 16:17:07 +00:00
|
|
|
// Copyright (C) 2005-2011 Daniel James
|
2009-08-30 16:42:28 +00:00
|
|
|
// 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_UTIL_HPP_INCLUDED
|
|
|
|
#define BOOST_UNORDERED_DETAIL_UTIL_HPP_INCLUDED
|
|
|
|
|
2011-04-16 18:47:33 +00:00
|
|
|
#include <algorithm>
|
2009-08-30 16:42:28 +00:00
|
|
|
#include <cstddef>
|
2011-04-16 18:47:33 +00:00
|
|
|
#include <stdexcept>
|
2009-08-30 16:42:28 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <boost/limits.hpp>
|
2011-04-16 18:47:33 +00:00
|
|
|
#include <boost/config.hpp>
|
|
|
|
#include <boost/config/no_tr1/cmath.hpp>
|
|
|
|
#include <boost/detail/workaround.hpp>
|
|
|
|
#include <boost/detail/select_type.hpp>
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/iterator.hpp>
|
2009-08-30 16:42:28 +00:00
|
|
|
#include <boost/iterator/iterator_categories.hpp>
|
2011-04-16 18:47:33 +00:00
|
|
|
#include <boost/type_traits/aligned_storage.hpp>
|
|
|
|
#include <boost/type_traits/alignment_of.hpp>
|
|
|
|
#include <boost/type_traits/remove_const.hpp>
|
2011-08-15 21:34:01 +00:00
|
|
|
#include <boost/type_traits/is_empty.hpp>
|
2011-04-16 18:47:33 +00:00
|
|
|
#include <boost/throw_exception.hpp>
|
2011-08-29 09:40:41 +00:00
|
|
|
#include <boost/move/move.hpp>
|
|
|
|
#include <boost/swap.hpp>
|
2009-08-30 16:42:28 +00:00
|
|
|
#include <boost/preprocessor/seq/size.hpp>
|
|
|
|
#include <boost/preprocessor/seq/enum.hpp>
|
2011-08-29 09:40:41 +00:00
|
|
|
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
2011-08-03 23:52:37 +00:00
|
|
|
#include <boost/preprocessor/repetition/enum.hpp>
|
2011-08-29 09:40:41 +00:00
|
|
|
#include <boost/preprocessor/repetition/enum_params.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#if !defined(BOOST_NO_0X_HDR_TUPLE) || defined(BOOST_HAS_TR1_TUPLE)
|
|
|
|
#include <tuple>
|
|
|
|
#endif
|
|
|
|
#include <boost/unordered/detail/allocator_helpers.hpp>
|
2009-08-30 16:42:28 +00:00
|
|
|
|
2011-04-16 18:47:33 +00:00
|
|
|
// Template parameters:
|
|
|
|
//
|
|
|
|
// H = Hash Function
|
|
|
|
// P = Predicate
|
|
|
|
// A = Value Allocator
|
|
|
|
// G = Bucket group policy, 'grouped' or 'ungrouped'
|
|
|
|
// E = Key Extractor
|
|
|
|
|
|
|
|
#if !defined(BOOST_NO_RVALUE_REFERENCES) && \
|
|
|
|
!defined(BOOST_NO_VARIADIC_TEMPLATES)
|
|
|
|
# if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
|
2011-08-07 08:55:28 +00:00
|
|
|
# elif defined(__STD_RWCOMPILER_H__) || defined(_RWSTD_VER)
|
|
|
|
# elif defined(_LIBCPP_VERSION)
|
|
|
|
# define BOOST_UNORDERED_STD_FORWARD_MOVE
|
|
|
|
# elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
|
|
|
|
# if defined(__GLIBCXX__) && __GLIBCXX__ >= 20090804
|
|
|
|
# define BOOST_UNORDERED_STD_FORWARD_MOVE
|
|
|
|
# endif
|
|
|
|
# elif defined(__STL_CONFIG_H)
|
|
|
|
# elif defined(__MSL_CPP__)
|
|
|
|
# elif defined(__IBMCPP__)
|
|
|
|
# elif defined(MSIPL_COMPILE_H)
|
|
|
|
# elif (defined(_YVALS) && !defined(__IBMCPP__)) || defined(_CPPLIB_VER)
|
|
|
|
// Visual C++. A version check would be a good idea.
|
|
|
|
# define BOOST_UNORDERED_STD_FORWARD_MOVE
|
2011-04-16 18:47:33 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(BOOST_UNORDERED_EMPLACE_LIMIT)
|
|
|
|
#define BOOST_UNORDERED_EMPLACE_LIMIT 10
|
|
|
|
#endif
|
|
|
|
|
2011-08-07 10:22:41 +00:00
|
|
|
#if defined(__SUNPRO_CC)
|
|
|
|
#define BOOST_UNORDERED_USE_RV_REF 0
|
|
|
|
#else
|
|
|
|
#define BOOST_UNORDERED_USE_RV_REF 1
|
|
|
|
#endif
|
|
|
|
|
2011-08-07 08:55:28 +00:00
|
|
|
#if !defined(BOOST_UNORDERED_STD_FORWARD_MOVE)
|
2011-04-16 18:47:33 +00:00
|
|
|
|
|
|
|
#include <boost/preprocessor/repetition/enum_params.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
|
|
|
|
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
|
|
|
|
|
|
|
#define BOOST_UNORDERED_TEMPLATE_ARGS(z, num_params) \
|
|
|
|
BOOST_PP_ENUM_PARAMS_Z(z, num_params, class Arg)
|
2011-08-03 23:52:37 +00:00
|
|
|
|
2011-04-16 18:47:33 +00:00
|
|
|
#define BOOST_UNORDERED_FUNCTION_PARAMS(z, num_params) \
|
2011-08-03 23:52:37 +00:00
|
|
|
BOOST_PP_ENUM_##z(num_params, BOOST_UNORDERED_FUNCTION_PARAMS2, _)
|
|
|
|
#define BOOST_UNORDERED_FUNCTION_PARAMS2(z, i, _) \
|
|
|
|
BOOST_FWD_REF(Arg##i) arg##i
|
|
|
|
|
2011-04-16 18:47:33 +00:00
|
|
|
#define BOOST_UNORDERED_CALL_PARAMS(z, num_params) \
|
2011-08-03 23:52:37 +00:00
|
|
|
BOOST_PP_ENUM_##z(num_params, BOOST_UNORDERED_CALL_PARAMS2, _)
|
|
|
|
#define BOOST_UNORDERED_CALL_PARAMS2(z, i, _) \
|
|
|
|
boost::forward<Arg##i>(arg##i)
|
2011-04-16 18:47:33 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace boost { namespace unordered { namespace detail {
|
|
|
|
|
|
|
|
static const float minimum_max_load_factor = 1e-3f;
|
|
|
|
static const std::size_t default_bucket_count = 11;
|
|
|
|
struct move_tag {};
|
|
|
|
|
2011-08-03 23:52:37 +00:00
|
|
|
struct empty_emplace {};
|
|
|
|
|
2011-04-16 18:47:33 +00:00
|
|
|
template <class T> class unique_table;
|
|
|
|
template <class T> class equivalent_table;
|
|
|
|
template <class Alloc, bool Unique> class node_constructor;
|
|
|
|
template <class ValueType>
|
|
|
|
struct set_extractor;
|
|
|
|
template <class Key, class ValueType>
|
|
|
|
struct map_extractor;
|
|
|
|
struct no_key;
|
|
|
|
|
|
|
|
// Explicitly call a destructor
|
|
|
|
|
|
|
|
#if defined(BOOST_MSVC)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4100) // unreferenced formal parameter
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline void destroy(T* x) {
|
|
|
|
x->~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(BOOST_MSVC)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2009-08-30 16:42:28 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// convert double to std::size_t
|
|
|
|
|
|
|
|
inline std::size_t double_to_size_t(double f)
|
|
|
|
{
|
2009-09-20 21:55:15 +00:00
|
|
|
return f >= static_cast<double>(
|
|
|
|
(std::numeric_limits<std::size_t>::max)()) ?
|
2009-08-30 16:42:28 +00:00
|
|
|
(std::numeric_limits<std::size_t>::max)() :
|
|
|
|
static_cast<std::size_t>(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// primes
|
|
|
|
|
|
|
|
#define BOOST_UNORDERED_PRIMES \
|
|
|
|
(5ul)(11ul)(17ul)(29ul)(37ul)(53ul)(67ul)(79ul) \
|
|
|
|
(97ul)(131ul)(193ul)(257ul)(389ul)(521ul)(769ul) \
|
|
|
|
(1031ul)(1543ul)(2053ul)(3079ul)(6151ul)(12289ul)(24593ul) \
|
|
|
|
(49157ul)(98317ul)(196613ul)(393241ul)(786433ul) \
|
|
|
|
(1572869ul)(3145739ul)(6291469ul)(12582917ul)(25165843ul) \
|
|
|
|
(50331653ul)(100663319ul)(201326611ul)(402653189ul)(805306457ul) \
|
|
|
|
(1610612741ul)(3221225473ul)(4294967291ul)
|
|
|
|
|
2010-01-21 18:01:53 +00:00
|
|
|
template<class T> struct prime_list_template
|
|
|
|
{
|
|
|
|
static std::size_t const value[];
|
|
|
|
|
|
|
|
#if !defined(SUNPRO_CC)
|
|
|
|
static std::ptrdiff_t const length;
|
|
|
|
#else
|
|
|
|
static std::ptrdiff_t const length
|
|
|
|
= BOOST_PP_SEQ_SIZE(BOOST_UNORDERED_PRIMES);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2009-08-30 16:42:28 +00:00
|
|
|
template<class T>
|
|
|
|
std::size_t const prime_list_template<T>::value[] = {
|
|
|
|
BOOST_PP_SEQ_ENUM(BOOST_UNORDERED_PRIMES)
|
|
|
|
};
|
|
|
|
|
2010-01-21 18:01:53 +00:00
|
|
|
#if !defined(SUNPRO_CC)
|
2009-08-30 16:42:28 +00:00
|
|
|
template<class T>
|
|
|
|
std::ptrdiff_t const prime_list_template<T>::length
|
|
|
|
= BOOST_PP_SEQ_SIZE(BOOST_UNORDERED_PRIMES);
|
2010-01-21 18:01:53 +00:00
|
|
|
#endif
|
2009-08-30 16:42:28 +00:00
|
|
|
|
|
|
|
#undef BOOST_UNORDERED_PRIMES
|
|
|
|
|
|
|
|
typedef prime_list_template<std::size_t> prime_list;
|
|
|
|
|
|
|
|
// no throw
|
2009-09-21 21:17:19 +00:00
|
|
|
inline std::size_t next_prime(std::size_t num) {
|
2009-08-30 16:42:28 +00:00
|
|
|
std::size_t const* const prime_list_begin = prime_list::value;
|
|
|
|
std::size_t const* const prime_list_end = prime_list_begin +
|
|
|
|
prime_list::length;
|
|
|
|
std::size_t const* bound =
|
2009-09-21 21:17:19 +00:00
|
|
|
std::lower_bound(prime_list_begin, prime_list_end, num);
|
2009-08-30 16:42:28 +00:00
|
|
|
if(bound == prime_list_end)
|
|
|
|
bound--;
|
|
|
|
return *bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no throw
|
2009-09-21 21:17:19 +00:00
|
|
|
inline std::size_t prev_prime(std::size_t num) {
|
2009-08-30 16:42:28 +00:00
|
|
|
std::size_t const* const prime_list_begin = prime_list::value;
|
|
|
|
std::size_t const* const prime_list_end = prime_list_begin +
|
|
|
|
prime_list::length;
|
|
|
|
std::size_t const* bound =
|
2009-09-21 21:17:19 +00:00
|
|
|
std::upper_bound(prime_list_begin,prime_list_end, num);
|
2009-08-30 16:42:28 +00:00
|
|
|
if(bound != prime_list_begin)
|
|
|
|
bound--;
|
|
|
|
return *bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// insert_size/initial_size
|
|
|
|
|
|
|
|
#if !defined(BOOST_NO_STD_DISTANCE)
|
|
|
|
using ::std::distance;
|
|
|
|
#else
|
|
|
|
template <class ForwardIterator>
|
|
|
|
inline std::size_t distance(ForwardIterator i, ForwardIterator j) {
|
|
|
|
std::size_t x;
|
|
|
|
std::distance(i, j, x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class I>
|
2011-04-16 18:47:33 +00:00
|
|
|
inline std::size_t insert_size(I i, I j, ::boost::forward_traversal_tag)
|
2009-08-30 16:42:28 +00:00
|
|
|
{
|
|
|
|
return std::distance(i, j);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class I>
|
2011-04-16 18:47:33 +00:00
|
|
|
inline std::size_t insert_size(I, I, ::boost::incrementable_traversal_tag)
|
2009-08-30 16:42:28 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class I>
|
|
|
|
inline std::size_t insert_size(I i, I j)
|
|
|
|
{
|
2011-08-28 11:31:23 +00:00
|
|
|
return insert_size(i, j,
|
|
|
|
BOOST_DEDUCED_TYPENAME ::boost::iterator_traversal<I>::type());
|
2009-08-30 16:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class I>
|
|
|
|
inline std::size_t initial_size(I i, I j,
|
2011-04-16 18:47:33 +00:00
|
|
|
std::size_t num_buckets = ::boost::unordered::detail::default_bucket_count)
|
2009-08-30 16:42:28 +00:00
|
|
|
{
|
2009-09-21 21:17:19 +00:00
|
|
|
return (std::max)(static_cast<std::size_t>(insert_size(i, j)) + 1,
|
|
|
|
num_buckets);
|
2009-08-30 16:42:28 +00:00
|
|
|
}
|
2011-08-15 21:34:01 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// compressed_pair
|
|
|
|
|
|
|
|
template <typename T, int Index>
|
|
|
|
struct compressed_base : private T
|
|
|
|
{
|
|
|
|
compressed_base(T const& x) : T(x) {}
|
|
|
|
compressed_base(T& x, move_tag) : T(boost::move(x)) {}
|
|
|
|
|
|
|
|
T& get() { return *this; }
|
|
|
|
T const& get() const { return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, int Index>
|
|
|
|
struct uncompressed_base
|
|
|
|
{
|
|
|
|
uncompressed_base(T const& x) : value_(x) {}
|
|
|
|
uncompressed_base(T& x, move_tag) : value_(boost::move(x)) {}
|
|
|
|
|
|
|
|
T& get() { return value_; }
|
|
|
|
T const& get() const { return value_; }
|
|
|
|
private:
|
|
|
|
T value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, int Index>
|
|
|
|
struct generate_base
|
|
|
|
: boost::detail::if_true<
|
|
|
|
boost::is_empty<T>::value
|
|
|
|
>:: BOOST_NESTED_TEMPLATE then<
|
|
|
|
compressed_base<T, Index>,
|
|
|
|
uncompressed_base<T, Index>
|
|
|
|
>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
struct compressed_pair
|
|
|
|
: private generate_base<T1, 1>::type,
|
|
|
|
private generate_base<T2, 2>::type
|
|
|
|
{
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME generate_base<T1, 1>::type base1;
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME generate_base<T2, 2>::type base2;
|
|
|
|
|
|
|
|
typedef T1 first_type;
|
|
|
|
typedef T2 second_type;
|
|
|
|
|
|
|
|
first_type& first() {
|
|
|
|
return static_cast<base1*>(this)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
first_type const& first() const {
|
|
|
|
return static_cast<base1 const*>(this)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
second_type& second() {
|
|
|
|
return static_cast<base2*>(this)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
second_type const& second() const {
|
|
|
|
return static_cast<base2 const*>(this)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename First, typename Second>
|
|
|
|
compressed_pair(First const& x1, Second const& x2)
|
|
|
|
: base1(x1), base2(x2) {}
|
|
|
|
|
|
|
|
compressed_pair(compressed_pair const& x)
|
|
|
|
: base1(x.first()), base2(x.second()) {}
|
|
|
|
|
|
|
|
compressed_pair(compressed_pair& x, move_tag m)
|
|
|
|
: base1(x.first(), m), base2(x.second(), m) {}
|
|
|
|
|
|
|
|
void assign(compressed_pair const& x)
|
|
|
|
{
|
|
|
|
first() = x.first();
|
|
|
|
second() = x.second();
|
|
|
|
}
|
|
|
|
|
|
|
|
void move_assign(compressed_pair& x)
|
|
|
|
{
|
|
|
|
first() = boost::move(x.first());
|
|
|
|
second() = boost::move(x.second());
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(compressed_pair& x)
|
|
|
|
{
|
|
|
|
boost::swap(first(), x.first());
|
|
|
|
boost::swap(second(), x.second());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Prevent assignment just to make use of assign or
|
|
|
|
// move_assign explicit.
|
|
|
|
compressed_pair& operator=(compressed_pair const&);
|
|
|
|
};
|
2011-04-16 18:47:33 +00:00
|
|
|
}}}
|
2009-08-30 16:42:28 +00:00
|
|
|
|
|
|
|
#endif
|