count.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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-2023.
  8. // Modifications copyright (c) 2019-2023 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. //
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COUNT_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COUNT_HPP
  17. #include <boost/geometry/index/equal_to.hpp>
  18. #include <boost/geometry/index/detail/algorithms/bounds.hpp>
  19. #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
  20. #include <boost/geometry/index/detail/rtree/node/variant_visitor.hpp>
  21. #include <boost/geometry/index/parameters.hpp>
  22. namespace boost { namespace geometry { namespace index {
  23. namespace detail { namespace rtree { namespace visitors {
  24. template <typename Indexable, typename Value>
  25. struct count_helper
  26. {
  27. template <typename Translator>
  28. static inline typename Translator::result_type indexable(Indexable const& i, Translator const&)
  29. {
  30. return i;
  31. }
  32. template <typename Translator, typename Strategy>
  33. static inline bool equals(Indexable const& i, Value const& v, Translator const& tr, Strategy const& s)
  34. {
  35. return index::detail::equals<Indexable>::apply(i, tr(v), s);
  36. }
  37. };
  38. template <typename Value>
  39. struct count_helper<Value, Value>
  40. {
  41. template <typename Translator>
  42. static inline typename Translator::result_type indexable(Value const& v, Translator const& tr)
  43. {
  44. return tr(v);
  45. }
  46. template <typename Translator, typename Strategy>
  47. static inline bool equals(Value const& v1, Value const& v2, Translator const& tr, Strategy const& s)
  48. {
  49. return tr.equals(v1, v2, s);
  50. }
  51. };
  52. template <typename ValueOrIndexable, typename MembersHolder>
  53. struct count
  54. : public MembersHolder::visitor_const
  55. {
  56. typedef typename MembersHolder::value_type value_type;
  57. typedef typename MembersHolder::parameters_type parameters_type;
  58. typedef typename MembersHolder::translator_type translator_type;
  59. typedef typename MembersHolder::node node;
  60. typedef typename MembersHolder::internal_node internal_node;
  61. typedef typename MembersHolder::leaf leaf;
  62. typedef count_helper<ValueOrIndexable, value_type> count_help;
  63. inline count(ValueOrIndexable const& vori, parameters_type const& parameters, translator_type const& t)
  64. : value_or_indexable(vori), m_parameters(parameters), tr(t), found_count(0)
  65. {}
  66. inline void operator()(internal_node const& n)
  67. {
  68. typedef typename rtree::elements_type<internal_node>::type elements_type;
  69. elements_type const& elements = rtree::elements(n);
  70. // traverse nodes meeting predicates
  71. for (typename elements_type::const_iterator it = elements.begin();
  72. it != elements.end(); ++it)
  73. {
  74. if ( index::detail::covered_by_bounds(count_help::indexable(value_or_indexable, tr),
  75. it->first,
  76. index::detail::get_strategy(m_parameters)) )
  77. {
  78. rtree::apply_visitor(*this, *it->second);
  79. }
  80. }
  81. }
  82. inline void operator()(leaf const& n)
  83. {
  84. typedef typename rtree::elements_type<leaf>::type elements_type;
  85. elements_type const& elements = rtree::elements(n);
  86. // get all values meeting predicates
  87. for (typename elements_type::const_iterator it = elements.begin();
  88. it != elements.end(); ++it)
  89. {
  90. // if value meets predicates
  91. if ( count_help::equals(value_or_indexable, *it, tr,
  92. index::detail::get_strategy(m_parameters)) )
  93. {
  94. ++found_count;
  95. }
  96. }
  97. }
  98. ValueOrIndexable const& value_or_indexable;
  99. parameters_type const& m_parameters;
  100. translator_type const& tr;
  101. typename MembersHolder::size_type found_count;
  102. };
  103. }}} // namespace detail::rtree::visitors
  104. }}} // namespace boost::geometry::index
  105. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COUNT_HPP