parameters.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Boost.Geometry Index
  2. //
  3. // R-tree algorithms parameters
  4. //
  5. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_INDEX_PARAMETERS_HPP
  11. #define BOOST_GEOMETRY_INDEX_PARAMETERS_HPP
  12. #include <limits>
  13. namespace boost { namespace geometry { namespace index {
  14. namespace detail {
  15. template <size_t MaxElements>
  16. struct default_min_elements_s
  17. {
  18. static const size_t raw_value = (MaxElements * 3) / 10;
  19. static const size_t value = 1 <= raw_value ? raw_value : 1;
  20. };
  21. inline size_t default_min_elements_d()
  22. {
  23. return (std::numeric_limits<size_t>::max)();
  24. }
  25. inline size_t default_min_elements_d_calc(size_t max_elements, size_t min_elements)
  26. {
  27. if ( default_min_elements_d() == min_elements )
  28. {
  29. size_t raw_value = (max_elements * 3) / 10;
  30. return 1 <= raw_value ? raw_value : 1;
  31. }
  32. return min_elements;
  33. }
  34. template <size_t MaxElements>
  35. struct default_rstar_reinserted_elements_s
  36. {
  37. static const size_t value = (MaxElements * 3) / 10;
  38. };
  39. inline size_t default_rstar_reinserted_elements_d()
  40. {
  41. return (std::numeric_limits<size_t>::max)();
  42. }
  43. inline size_t default_rstar_reinserted_elements_d_calc(size_t max_elements, size_t reinserted_elements)
  44. {
  45. if ( default_rstar_reinserted_elements_d() == reinserted_elements )
  46. {
  47. return (max_elements * 3) / 10;
  48. }
  49. return reinserted_elements;
  50. }
  51. } // namespace detail
  52. /*!
  53. \brief Linear r-tree creation algorithm parameters.
  54. \tparam MaxElements Maximum number of elements in nodes.
  55. \tparam MinElements Minimum number of elements in nodes. Default: 0.3*Max.
  56. */
  57. template <size_t MaxElements,
  58. size_t MinElements = detail::default_min_elements_s<MaxElements>::value>
  59. struct linear
  60. {
  61. BOOST_MPL_ASSERT_MSG((0 < MinElements && 2*MinElements <= MaxElements+1),
  62. INVALID_STATIC_MIN_MAX_PARAMETERS, (linear));
  63. static const size_t max_elements = MaxElements;
  64. static const size_t min_elements = MinElements;
  65. static size_t get_max_elements() { return MaxElements; }
  66. static size_t get_min_elements() { return MinElements; }
  67. };
  68. /*!
  69. \brief Quadratic r-tree creation algorithm parameters.
  70. \tparam MaxElements Maximum number of elements in nodes.
  71. \tparam MinElements Minimum number of elements in nodes. Default: 0.3*Max.
  72. */
  73. template <size_t MaxElements,
  74. size_t MinElements = detail::default_min_elements_s<MaxElements>::value>
  75. struct quadratic
  76. {
  77. BOOST_MPL_ASSERT_MSG((0 < MinElements && 2*MinElements <= MaxElements+1),
  78. INVALID_STATIC_MIN_MAX_PARAMETERS, (quadratic));
  79. static const size_t max_elements = MaxElements;
  80. static const size_t min_elements = MinElements;
  81. static size_t get_max_elements() { return MaxElements; }
  82. static size_t get_min_elements() { return MinElements; }
  83. };
  84. /*!
  85. \brief R*-tree creation algorithm parameters.
  86. \tparam MaxElements Maximum number of elements in nodes.
  87. \tparam MinElements Minimum number of elements in nodes. Default: 0.3*Max.
  88. \tparam ReinsertedElements The number of elements reinserted by forced reinsertions algorithm.
  89. If 0 forced reinsertions are disabled. Maximum value is Max+1-Min.
  90. Greater values are truncated. Default: 0.3*Max.
  91. \tparam OverlapCostThreshold The number of most suitable leafs taken into account while choosing
  92. the leaf node to which currently inserted value will be added. If
  93. value is in range (0, MaxElements) - the algorithm calculates
  94. nearly minimum overlap cost, otherwise all leafs are analyzed
  95. and true minimum overlap cost is calculated. Default: 32.
  96. */
  97. template <size_t MaxElements,
  98. size_t MinElements = detail::default_min_elements_s<MaxElements>::value,
  99. size_t ReinsertedElements = detail::default_rstar_reinserted_elements_s<MaxElements>::value,
  100. size_t OverlapCostThreshold = 32>
  101. struct rstar
  102. {
  103. BOOST_MPL_ASSERT_MSG((0 < MinElements && 2*MinElements <= MaxElements+1),
  104. INVALID_STATIC_MIN_MAX_PARAMETERS, (rstar));
  105. static const size_t max_elements = MaxElements;
  106. static const size_t min_elements = MinElements;
  107. static const size_t reinserted_elements = ReinsertedElements;
  108. static const size_t overlap_cost_threshold = OverlapCostThreshold;
  109. static size_t get_max_elements() { return MaxElements; }
  110. static size_t get_min_elements() { return MinElements; }
  111. static size_t get_reinserted_elements() { return ReinsertedElements; }
  112. static size_t get_overlap_cost_threshold() { return OverlapCostThreshold; }
  113. };
  114. //template <size_t MaxElements, size_t MinElements>
  115. //struct kmeans
  116. //{
  117. // static const size_t max_elements = MaxElements;
  118. // static const size_t min_elements = MinElements;
  119. //};
  120. /*!
  121. \brief Linear r-tree creation algorithm parameters - run-time version.
  122. */
  123. class dynamic_linear
  124. {
  125. public:
  126. /*!
  127. \brief The constructor.
  128. \param max_elements Maximum number of elements in nodes.
  129. \param min_elements Minimum number of elements in nodes. Default: 0.3*Max.
  130. */
  131. dynamic_linear(size_t max_elements,
  132. size_t min_elements = detail::default_min_elements_d())
  133. : m_max_elements(max_elements)
  134. , m_min_elements(detail::default_min_elements_d_calc(max_elements, min_elements))
  135. {
  136. if (!(0 < m_min_elements && 2*m_min_elements <= m_max_elements+1))
  137. detail::throw_invalid_argument("invalid min or/and max parameters of dynamic_linear");
  138. }
  139. size_t get_max_elements() const { return m_max_elements; }
  140. size_t get_min_elements() const { return m_min_elements; }
  141. private:
  142. size_t m_max_elements;
  143. size_t m_min_elements;
  144. };
  145. /*!
  146. \brief Quadratic r-tree creation algorithm parameters - run-time version.
  147. */
  148. class dynamic_quadratic
  149. {
  150. public:
  151. /*!
  152. \brief The constructor.
  153. \param max_elements Maximum number of elements in nodes.
  154. \param min_elements Minimum number of elements in nodes. Default: 0.3*Max.
  155. */
  156. dynamic_quadratic(size_t max_elements,
  157. size_t min_elements = detail::default_min_elements_d())
  158. : m_max_elements(max_elements)
  159. , m_min_elements(detail::default_min_elements_d_calc(max_elements, min_elements))
  160. {
  161. if (!(0 < m_min_elements && 2*m_min_elements <= m_max_elements+1))
  162. detail::throw_invalid_argument("invalid min or/and max parameters of dynamic_quadratic");
  163. }
  164. size_t get_max_elements() const { return m_max_elements; }
  165. size_t get_min_elements() const { return m_min_elements; }
  166. private:
  167. size_t m_max_elements;
  168. size_t m_min_elements;
  169. };
  170. /*!
  171. \brief R*-tree creation algorithm parameters - run-time version.
  172. */
  173. class dynamic_rstar
  174. {
  175. public:
  176. /*!
  177. \brief The constructor.
  178. \param max_elements Maximum number of elements in nodes.
  179. \param min_elements Minimum number of elements in nodes. Default: 0.3*Max.
  180. \param reinserted_elements The number of elements reinserted by forced reinsertions algorithm.
  181. If 0 forced reinsertions are disabled. Maximum value is Max-Min+1.
  182. Greater values are truncated. Default: 0.3*Max.
  183. \param overlap_cost_threshold The number of most suitable leafs taken into account while choosing
  184. the leaf node to which currently inserted value will be added. If
  185. value is in range (0, MaxElements) - the algorithm calculates
  186. nearly minimum overlap cost, otherwise all leafs are analyzed
  187. and true minimum overlap cost is calculated. Default: 32.
  188. */
  189. dynamic_rstar(size_t max_elements,
  190. size_t min_elements = detail::default_min_elements_d(),
  191. size_t reinserted_elements = detail::default_rstar_reinserted_elements_d(),
  192. size_t overlap_cost_threshold = 32)
  193. : m_max_elements(max_elements)
  194. , m_min_elements(detail::default_min_elements_d_calc(max_elements, min_elements))
  195. , m_reinserted_elements(detail::default_rstar_reinserted_elements_d_calc(max_elements, reinserted_elements))
  196. , m_overlap_cost_threshold(overlap_cost_threshold)
  197. {
  198. if (!(0 < m_min_elements && 2*m_min_elements <= m_max_elements+1))
  199. detail::throw_invalid_argument("invalid min or/and max parameters of dynamic_rstar");
  200. }
  201. size_t get_max_elements() const { return m_max_elements; }
  202. size_t get_min_elements() const { return m_min_elements; }
  203. size_t get_reinserted_elements() const { return m_reinserted_elements; }
  204. size_t get_overlap_cost_threshold() const { return m_overlap_cost_threshold; }
  205. private:
  206. size_t m_max_elements;
  207. size_t m_min_elements;
  208. size_t m_reinserted_elements;
  209. size_t m_overlap_cost_threshold;
  210. };
  211. }}} // namespace boost::geometry::index
  212. #endif // BOOST_GEOMETRY_INDEX_PARAMETERS_HPP