9
3

count.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Boost.Geometry Index
  2. //
  3. // R-tree count visitor implementation
  4. //
  5. // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2019.
  8. // Modifications copyright (c) 2019 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_DETAIL_RTREE_VISITORS_COUNT_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COUNT_HPP
  16. namespace boost { namespace geometry { namespace index {
  17. namespace detail { namespace rtree { namespace visitors {
  18. template <typename Indexable, typename Value>
  19. struct count_helper
  20. {
  21. template <typename Translator>
  22. static inline typename Translator::result_type indexable(Indexable const& i, Translator const&)
  23. {
  24. return i;
  25. }
  26. template <typename Translator, typename Strategy>
  27. static inline bool equals(Indexable const& i, Value const& v, Translator const& tr, Strategy const& s)
  28. {
  29. return index::detail::equals<Indexable>::apply(i, tr(v), s);
  30. }
  31. };
  32. template <typename Value>
  33. struct count_helper<Value, Value>
  34. {
  35. template <typename Translator>
  36. static inline typename Translator::result_type indexable(Value const& v, Translator const& tr)
  37. {
  38. return tr(v);
  39. }
  40. template <typename Translator, typename Strategy>
  41. static inline bool equals(Value const& v1, Value const& v2, Translator const& tr, Strategy const& s)
  42. {
  43. return tr.equals(v1, v2, s);
  44. }
  45. };
  46. template <typename ValueOrIndexable, typename Value, typename Options, typename Translator, typename Box, typename Allocators>
  47. struct count
  48. : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  49. {
  50. typedef typename Options::parameters_type parameters_type;
  51. typedef typename rtree::node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type node;
  52. typedef typename rtree::internal_node<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  53. typedef typename rtree::leaf<Value, parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  54. typedef count_helper<ValueOrIndexable, Value> count_help;
  55. inline count(ValueOrIndexable const& vori, parameters_type const& parameters, Translator const& t)
  56. : value_or_indexable(vori), m_parameters(parameters), tr(t), found_count(0)
  57. {}
  58. inline void operator()(internal_node const& n)
  59. {
  60. typedef typename rtree::elements_type<internal_node>::type elements_type;
  61. elements_type const& elements = rtree::elements(n);
  62. // traverse nodes meeting predicates
  63. for (typename elements_type::const_iterator it = elements.begin();
  64. it != elements.end(); ++it)
  65. {
  66. if ( index::detail::covered_by_bounds(count_help::indexable(value_or_indexable, tr),
  67. it->first,
  68. index::detail::get_strategy(m_parameters)) )
  69. {
  70. rtree::apply_visitor(*this, *it->second);
  71. }
  72. }
  73. }
  74. inline void operator()(leaf const& n)
  75. {
  76. typedef typename rtree::elements_type<leaf>::type elements_type;
  77. elements_type const& elements = rtree::elements(n);
  78. // get all values meeting predicates
  79. for (typename elements_type::const_iterator it = elements.begin();
  80. it != elements.end(); ++it)
  81. {
  82. // if value meets predicates
  83. if ( count_help::equals(value_or_indexable, *it, tr,
  84. index::detail::get_strategy(m_parameters)) )
  85. {
  86. ++found_count;
  87. }
  88. }
  89. }
  90. ValueOrIndexable const& value_or_indexable;
  91. parameters_type const& m_parameters;
  92. Translator const& tr;
  93. typename Allocators::size_type found_count;
  94. };
  95. }}} // namespace detail::rtree::visitors
  96. }}} // namespace boost::geometry::index
  97. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COUNT_HPP