util.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
  2. // Copyright (C) 2005-2011 Daniel James
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_UNORDERED_DETAIL_UTIL_HPP_INCLUDED
  6. #define BOOST_UNORDERED_DETAIL_UTIL_HPP_INCLUDED
  7. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  8. # pragma once
  9. #endif
  10. #include <boost/type_traits/is_convertible.hpp>
  11. #include <boost/type_traits/is_empty.hpp>
  12. #include <boost/iterator/iterator_categories.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include <boost/detail/select_type.hpp>
  15. #include <boost/move/move.hpp>
  16. #include <boost/preprocessor/seq/size.hpp>
  17. #include <boost/preprocessor/seq/enum.hpp>
  18. #include <boost/swap.hpp>
  19. namespace boost { namespace unordered { namespace detail {
  20. static const float minimum_max_load_factor = 1e-3f;
  21. static const std::size_t default_bucket_count = 11;
  22. struct move_tag {};
  23. struct empty_emplace {};
  24. namespace func {
  25. template <class T>
  26. inline void ignore_unused_variable_warning(T const&) {}
  27. }
  28. ////////////////////////////////////////////////////////////////////////////
  29. // iterator SFINAE
  30. template <typename I>
  31. struct is_forward :
  32. boost::is_convertible<
  33. typename boost::iterator_traversal<I>::type,
  34. boost::forward_traversal_tag>
  35. {};
  36. template <typename I, typename ReturnType>
  37. struct enable_if_forward :
  38. boost::enable_if_c<
  39. boost::unordered::detail::is_forward<I>::value,
  40. ReturnType>
  41. {};
  42. template <typename I, typename ReturnType>
  43. struct disable_if_forward :
  44. boost::disable_if_c<
  45. boost::unordered::detail::is_forward<I>::value,
  46. ReturnType>
  47. {};
  48. ////////////////////////////////////////////////////////////////////////////
  49. // primes
  50. #define BOOST_UNORDERED_PRIMES \
  51. (17ul)(29ul)(37ul)(53ul)(67ul)(79ul) \
  52. (97ul)(131ul)(193ul)(257ul)(389ul)(521ul)(769ul) \
  53. (1031ul)(1543ul)(2053ul)(3079ul)(6151ul)(12289ul)(24593ul) \
  54. (49157ul)(98317ul)(196613ul)(393241ul)(786433ul) \
  55. (1572869ul)(3145739ul)(6291469ul)(12582917ul)(25165843ul) \
  56. (50331653ul)(100663319ul)(201326611ul)(402653189ul)(805306457ul) \
  57. (1610612741ul)(3221225473ul)(4294967291ul)
  58. template<class T> struct prime_list_template
  59. {
  60. static std::size_t const value[];
  61. #if !defined(SUNPRO_CC)
  62. static std::ptrdiff_t const length;
  63. #else
  64. static std::ptrdiff_t const length
  65. = BOOST_PP_SEQ_SIZE(BOOST_UNORDERED_PRIMES);
  66. #endif
  67. };
  68. template<class T>
  69. std::size_t const prime_list_template<T>::value[] = {
  70. BOOST_PP_SEQ_ENUM(BOOST_UNORDERED_PRIMES)
  71. };
  72. #if !defined(SUNPRO_CC)
  73. template<class T>
  74. std::ptrdiff_t const prime_list_template<T>::length
  75. = BOOST_PP_SEQ_SIZE(BOOST_UNORDERED_PRIMES);
  76. #endif
  77. #undef BOOST_UNORDERED_PRIMES
  78. typedef prime_list_template<std::size_t> prime_list;
  79. // no throw
  80. inline std::size_t next_prime(std::size_t num) {
  81. std::size_t const* const prime_list_begin = prime_list::value;
  82. std::size_t const* const prime_list_end = prime_list_begin +
  83. prime_list::length;
  84. std::size_t const* bound =
  85. std::lower_bound(prime_list_begin, prime_list_end, num);
  86. if(bound == prime_list_end)
  87. bound--;
  88. return *bound;
  89. }
  90. // no throw
  91. inline std::size_t prev_prime(std::size_t num) {
  92. std::size_t const* const prime_list_begin = prime_list::value;
  93. std::size_t const* const prime_list_end = prime_list_begin +
  94. prime_list::length;
  95. std::size_t const* bound =
  96. std::upper_bound(prime_list_begin,prime_list_end, num);
  97. if(bound != prime_list_begin)
  98. bound--;
  99. return *bound;
  100. }
  101. ////////////////////////////////////////////////////////////////////////////
  102. // insert_size/initial_size
  103. #if !defined(BOOST_NO_STD_DISTANCE)
  104. using ::std::distance;
  105. #else
  106. template <class ForwardIterator>
  107. inline std::size_t distance(ForwardIterator i, ForwardIterator j) {
  108. std::size_t x;
  109. std::distance(i, j, x);
  110. return x;
  111. }
  112. #endif
  113. template <class I>
  114. inline typename
  115. boost::unordered::detail::enable_if_forward<I, std::size_t>::type
  116. insert_size(I i, I j)
  117. {
  118. return std::distance(i, j);
  119. }
  120. template <class I>
  121. inline typename
  122. boost::unordered::detail::disable_if_forward<I, std::size_t>::type
  123. insert_size(I, I)
  124. {
  125. return 1;
  126. }
  127. template <class I>
  128. inline std::size_t initial_size(I i, I j,
  129. std::size_t num_buckets =
  130. boost::unordered::detail::default_bucket_count)
  131. {
  132. // TODO: Why +1?
  133. return (std::max)(
  134. boost::unordered::detail::insert_size(i, j) + 1,
  135. num_buckets);
  136. }
  137. ////////////////////////////////////////////////////////////////////////////
  138. // compressed
  139. template <typename T, int Index>
  140. struct compressed_base : private T
  141. {
  142. compressed_base(T const& x) : T(x) {}
  143. compressed_base(T& x, move_tag) : T(boost::move(x)) {}
  144. T& get() { return *this; }
  145. T const& get() const { return *this; }
  146. };
  147. template <typename T, int Index>
  148. struct uncompressed_base
  149. {
  150. uncompressed_base(T const& x) : value_(x) {}
  151. uncompressed_base(T& x, move_tag) : value_(boost::move(x)) {}
  152. T& get() { return value_; }
  153. T const& get() const { return value_; }
  154. private:
  155. T value_;
  156. };
  157. template <typename T, int Index>
  158. struct generate_base
  159. : boost::detail::if_true<
  160. boost::is_empty<T>::value
  161. >:: BOOST_NESTED_TEMPLATE then<
  162. boost::unordered::detail::compressed_base<T, Index>,
  163. boost::unordered::detail::uncompressed_base<T, Index>
  164. >
  165. {};
  166. template <typename T1, typename T2>
  167. struct compressed
  168. : private boost::unordered::detail::generate_base<T1, 1>::type,
  169. private boost::unordered::detail::generate_base<T2, 2>::type
  170. {
  171. typedef typename generate_base<T1, 1>::type base1;
  172. typedef typename generate_base<T2, 2>::type base2;
  173. typedef T1 first_type;
  174. typedef T2 second_type;
  175. first_type& first() {
  176. return static_cast<base1*>(this)->get();
  177. }
  178. first_type const& first() const {
  179. return static_cast<base1 const*>(this)->get();
  180. }
  181. second_type& second() {
  182. return static_cast<base2*>(this)->get();
  183. }
  184. second_type const& second() const {
  185. return static_cast<base2 const*>(this)->get();
  186. }
  187. template <typename First, typename Second>
  188. compressed(First const& x1, Second const& x2)
  189. : base1(x1), base2(x2) {}
  190. compressed(compressed const& x)
  191. : base1(x.first()), base2(x.second()) {}
  192. compressed(compressed& x, move_tag m)
  193. : base1(x.first(), m), base2(x.second(), m) {}
  194. void assign(compressed const& x)
  195. {
  196. first() = x.first();
  197. second() = x.second();
  198. }
  199. void move_assign(compressed& x)
  200. {
  201. first() = boost::move(x.first());
  202. second() = boost::move(x.second());
  203. }
  204. void swap(compressed& x)
  205. {
  206. boost::swap(first(), x.first());
  207. boost::swap(second(), x.second());
  208. }
  209. private:
  210. // Prevent assignment just to make use of assign or
  211. // move_assign explicit.
  212. compressed& operator=(compressed const&);
  213. };
  214. }}}
  215. #endif