parameters.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Boost.Geometry Index
  2. //
  3. // R-tree algorithms parameters
  4. //
  5. // Copyright (c) 2011-2017 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2019-2020.
  8. // Modifications copyright (c) 2019-2020 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. //
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_INDEX_PARAMETERS_HPP
  15. #define BOOST_GEOMETRY_INDEX_PARAMETERS_HPP
  16. #include <limits>
  17. #include <boost/geometry/core/static_assert.hpp>
  18. #include <boost/geometry/index/detail/exception.hpp>
  19. namespace boost { namespace geometry { namespace index {
  20. namespace detail {
  21. template <size_t MaxElements>
  22. struct default_min_elements_s
  23. {
  24. static const size_t raw_value = (MaxElements * 3) / 10;
  25. static const size_t value = 1 <= raw_value ? raw_value : 1;
  26. };
  27. inline size_t default_min_elements_d()
  28. {
  29. return (std::numeric_limits<size_t>::max)();
  30. }
  31. inline size_t default_min_elements_d_calc(size_t max_elements, size_t min_elements)
  32. {
  33. if ( default_min_elements_d() == min_elements )
  34. {
  35. size_t raw_value = (max_elements * 3) / 10;
  36. return 1 <= raw_value ? raw_value : 1;
  37. }
  38. return min_elements;
  39. }
  40. template <size_t MaxElements>
  41. struct default_rstar_reinserted_elements_s
  42. {
  43. static const size_t value = (MaxElements * 3) / 10;
  44. };
  45. inline size_t default_rstar_reinserted_elements_d()
  46. {
  47. return (std::numeric_limits<size_t>::max)();
  48. }
  49. inline size_t default_rstar_reinserted_elements_d_calc(size_t max_elements, size_t reinserted_elements)
  50. {
  51. if ( default_rstar_reinserted_elements_d() == reinserted_elements )
  52. {
  53. return (max_elements * 3) / 10;
  54. }
  55. return reinserted_elements;
  56. }
  57. } // namespace detail
  58. /*!
  59. \brief Linear r-tree creation algorithm parameters.
  60. \tparam MaxElements Maximum number of elements in nodes.
  61. \tparam MinElements Minimum number of elements in nodes. Default: 0.3*Max.
  62. */
  63. template <size_t MaxElements,
  64. size_t MinElements = detail::default_min_elements_s<MaxElements>::value>
  65. struct linear
  66. {
  67. BOOST_GEOMETRY_STATIC_ASSERT((0 < MinElements && 2*MinElements <= MaxElements+1),
  68. "Invalid MaxElements or MinElements.",
  69. std::integer_sequence<size_t, MaxElements, MinElements>);
  70. static const size_t max_elements = MaxElements;
  71. static const size_t min_elements = MinElements;
  72. static size_t get_max_elements() { return MaxElements; }
  73. static size_t get_min_elements() { return MinElements; }
  74. };
  75. /*!
  76. \brief Quadratic r-tree creation algorithm parameters.
  77. \tparam MaxElements Maximum number of elements in nodes.
  78. \tparam MinElements Minimum number of elements in nodes. Default: 0.3*Max.
  79. */
  80. template <size_t MaxElements,
  81. size_t MinElements = detail::default_min_elements_s<MaxElements>::value>
  82. struct quadratic
  83. {
  84. BOOST_GEOMETRY_STATIC_ASSERT((0 < MinElements && 2*MinElements <= MaxElements+1),
  85. "Invalid MaxElements or MinElements.",
  86. std::integer_sequence<size_t, MaxElements, MinElements>);
  87. static const size_t max_elements = MaxElements;
  88. static const size_t min_elements = MinElements;
  89. static size_t get_max_elements() { return MaxElements; }
  90. static size_t get_min_elements() { return MinElements; }
  91. };
  92. /*!
  93. \brief R*-tree creation algorithm parameters.
  94. \tparam MaxElements Maximum number of elements in nodes.
  95. \tparam MinElements Minimum number of elements in nodes. Default: 0.3*Max.
  96. \tparam ReinsertedElements The number of elements reinserted by forced reinsertions algorithm.
  97. If 0 forced reinsertions are disabled. Maximum value is Max+1-Min.
  98. Greater values are truncated. Default: 0.3*Max.
  99. \tparam OverlapCostThreshold The number of most suitable leafs taken into account while choosing
  100. the leaf node to which currently inserted value will be added. If
  101. value is in range (0, MaxElements) - the algorithm calculates
  102. nearly minimum overlap cost, otherwise all leafs are analyzed
  103. and true minimum overlap cost is calculated. Default: 32.
  104. */
  105. template <size_t MaxElements,
  106. size_t MinElements = detail::default_min_elements_s<MaxElements>::value,
  107. size_t ReinsertedElements = detail::default_rstar_reinserted_elements_s<MaxElements>::value,
  108. size_t OverlapCostThreshold = 32>
  109. struct rstar
  110. {
  111. BOOST_GEOMETRY_STATIC_ASSERT((0 < MinElements && 2*MinElements <= MaxElements+1),
  112. "Invalid MaxElements or MinElements.",
  113. std::integer_sequence<size_t, MaxElements, MinElements>);
  114. static const size_t max_elements = MaxElements;
  115. static const size_t min_elements = MinElements;
  116. static const size_t reinserted_elements = ReinsertedElements;
  117. static const size_t overlap_cost_threshold = OverlapCostThreshold;
  118. static size_t get_max_elements() { return MaxElements; }
  119. static size_t get_min_elements() { return MinElements; }
  120. static size_t get_reinserted_elements() { return ReinsertedElements; }
  121. static size_t get_overlap_cost_threshold() { return OverlapCostThreshold; }
  122. };
  123. //template <size_t MaxElements, size_t MinElements>
  124. //struct kmeans
  125. //{
  126. // static const size_t max_elements = MaxElements;
  127. // static const size_t min_elements = MinElements;
  128. //};
  129. /*!
  130. \brief Linear r-tree creation algorithm parameters - run-time version.
  131. */
  132. class dynamic_linear
  133. {
  134. public:
  135. /*!
  136. \brief The constructor.
  137. \param max_elements Maximum number of elements in nodes.
  138. \param min_elements Minimum number of elements in nodes. Default: 0.3*Max.
  139. */
  140. explicit dynamic_linear(size_t max_elements,
  141. size_t min_elements = detail::default_min_elements_d())
  142. : m_max_elements(max_elements)
  143. , m_min_elements(detail::default_min_elements_d_calc(max_elements, min_elements))
  144. {
  145. if (!(0 < m_min_elements && 2*m_min_elements <= m_max_elements+1))
  146. detail::throw_invalid_argument("invalid min or/and max parameters of dynamic_linear");
  147. }
  148. size_t get_max_elements() const { return m_max_elements; }
  149. size_t get_min_elements() const { return m_min_elements; }
  150. private:
  151. size_t m_max_elements;
  152. size_t m_min_elements;
  153. };
  154. /*!
  155. \brief Quadratic r-tree creation algorithm parameters - run-time version.
  156. */
  157. class dynamic_quadratic
  158. {
  159. public:
  160. /*!
  161. \brief The constructor.
  162. \param max_elements Maximum number of elements in nodes.
  163. \param min_elements Minimum number of elements in nodes. Default: 0.3*Max.
  164. */
  165. explicit dynamic_quadratic(size_t max_elements,
  166. size_t min_elements = detail::default_min_elements_d())
  167. : m_max_elements(max_elements)
  168. , m_min_elements(detail::default_min_elements_d_calc(max_elements, min_elements))
  169. {
  170. if (!(0 < m_min_elements && 2*m_min_elements <= m_max_elements+1))
  171. detail::throw_invalid_argument("invalid min or/and max parameters of dynamic_quadratic");
  172. }
  173. size_t get_max_elements() const { return m_max_elements; }
  174. size_t get_min_elements() const { return m_min_elements; }
  175. private:
  176. size_t m_max_elements;
  177. size_t m_min_elements;
  178. };
  179. /*!
  180. \brief R*-tree creation algorithm parameters - run-time version.
  181. */
  182. class dynamic_rstar
  183. {
  184. public:
  185. /*!
  186. \brief The constructor.
  187. \param max_elements Maximum number of elements in nodes.
  188. \param min_elements Minimum number of elements in nodes. Default: 0.3*Max.
  189. \param reinserted_elements The number of elements reinserted by forced reinsertions algorithm.
  190. If 0 forced reinsertions are disabled. Maximum value is Max-Min+1.
  191. Greater values are truncated. Default: 0.3*Max.
  192. \param overlap_cost_threshold The number of most suitable leafs taken into account while choosing
  193. the leaf node to which currently inserted value will be added. If
  194. value is in range (0, MaxElements) - the algorithm calculates
  195. nearly minimum overlap cost, otherwise all leafs are analyzed
  196. and true minimum overlap cost is calculated. Default: 32.
  197. */
  198. explicit dynamic_rstar(size_t max_elements,
  199. size_t min_elements = detail::default_min_elements_d(),
  200. size_t reinserted_elements = detail::default_rstar_reinserted_elements_d(),
  201. size_t overlap_cost_threshold = 32)
  202. : m_max_elements(max_elements)
  203. , m_min_elements(detail::default_min_elements_d_calc(max_elements, min_elements))
  204. , m_reinserted_elements(detail::default_rstar_reinserted_elements_d_calc(max_elements, reinserted_elements))
  205. , m_overlap_cost_threshold(overlap_cost_threshold)
  206. {
  207. if (!(0 < m_min_elements && 2*m_min_elements <= m_max_elements+1))
  208. detail::throw_invalid_argument("invalid min or/and max parameters of dynamic_rstar");
  209. }
  210. size_t get_max_elements() const { return m_max_elements; }
  211. size_t get_min_elements() const { return m_min_elements; }
  212. size_t get_reinserted_elements() const { return m_reinserted_elements; }
  213. size_t get_overlap_cost_threshold() const { return m_overlap_cost_threshold; }
  214. private:
  215. size_t m_max_elements;
  216. size_t m_min_elements;
  217. size_t m_reinserted_elements;
  218. size_t m_overlap_cost_threshold;
  219. };
  220. template <typename Parameters, typename Strategy>
  221. class parameters
  222. : public Parameters
  223. , private Strategy
  224. {
  225. public:
  226. parameters()
  227. : Parameters(), Strategy()
  228. {}
  229. parameters(Parameters const& params)
  230. : Parameters(params), Strategy()
  231. {}
  232. parameters(Parameters const& params, Strategy const& strategy)
  233. : Parameters(params), Strategy(strategy)
  234. {}
  235. Strategy const& strategy() const
  236. {
  237. return static_cast<Strategy const&>(*this);
  238. }
  239. };
  240. namespace detail
  241. {
  242. template <typename Parameters>
  243. struct strategy_type
  244. {
  245. typedef default_strategy type;
  246. typedef default_strategy result_type;
  247. };
  248. template <typename Parameters, typename Strategy>
  249. struct strategy_type< parameters<Parameters, Strategy> >
  250. {
  251. typedef Strategy type;
  252. typedef Strategy const& result_type;
  253. };
  254. template <typename Parameters>
  255. struct get_strategy_impl
  256. {
  257. static inline default_strategy apply(Parameters const&)
  258. {
  259. return default_strategy();
  260. }
  261. };
  262. template <typename Parameters, typename Strategy>
  263. struct get_strategy_impl<parameters<Parameters, Strategy> >
  264. {
  265. static inline Strategy const& apply(parameters<Parameters, Strategy> const& parameters)
  266. {
  267. return parameters.strategy();
  268. }
  269. };
  270. template <typename Parameters>
  271. inline typename strategy_type<Parameters>::result_type
  272. get_strategy(Parameters const& parameters)
  273. {
  274. return get_strategy_impl<Parameters>::apply(parameters);
  275. }
  276. } // namespace detail
  277. }}} // namespace boost::geometry::index
  278. #endif // BOOST_GEOMETRY_INDEX_PARAMETERS_HPP