statistics.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Boost.Geometry Index
  2. //
  3. // R-tree visitor collecting basic statistics
  4. //
  5. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  6. // Copyright (c) 2013 Mateusz Loskot, London, UK.
  7. //
  8. // This file was modified by Oracle on 2019-2023.
  9. // Modifications copyright (c) 2019-2023 Oracle and/or its affiliates.
  10. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  11. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  12. //
  13. // Use, modification and distribution is subject to the Boost Software License,
  14. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_STATISTICS_HPP
  17. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_STATISTICS_HPP
  18. #include <algorithm>
  19. #include <tuple>
  20. #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
  21. #include <boost/geometry/index/detail/rtree/node/variant_visitor.hpp>
  22. #include <boost/geometry/index/detail/rtree/utilities/view.hpp>
  23. namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree { namespace utilities {
  24. namespace visitors {
  25. template <typename MembersHolder>
  26. struct statistics
  27. : public MembersHolder::visitor_const
  28. {
  29. typedef typename MembersHolder::internal_node internal_node;
  30. typedef typename MembersHolder::leaf leaf;
  31. inline statistics()
  32. : level(0)
  33. , levels(1) // count root
  34. , nodes(0)
  35. , leaves(0)
  36. , values(0)
  37. , values_min(0)
  38. , values_max(0)
  39. {}
  40. inline void operator()(internal_node const& n)
  41. {
  42. typedef typename rtree::elements_type<internal_node>::type elements_type;
  43. elements_type const& elements = rtree::elements(n);
  44. ++nodes; // count node
  45. size_t const level_backup = level;
  46. ++level;
  47. levels += level++ > levels ? 1 : 0; // count level (root already counted)
  48. for (typename elements_type::const_iterator it = elements.begin();
  49. it != elements.end(); ++it)
  50. {
  51. rtree::apply_visitor(*this, *it->second);
  52. }
  53. level = level_backup;
  54. }
  55. inline void operator()(leaf const& n)
  56. {
  57. typedef typename rtree::elements_type<leaf>::type elements_type;
  58. elements_type const& elements = rtree::elements(n);
  59. ++leaves; // count leaves
  60. std::size_t const v = elements.size();
  61. // count values spread per node and total
  62. values_min = (std::min)(values_min == 0 ? v : values_min, v);
  63. values_max = (std::max)(values_max, v);
  64. values += v;
  65. }
  66. std::size_t level;
  67. std::size_t levels;
  68. std::size_t nodes;
  69. std::size_t leaves;
  70. std::size_t values;
  71. std::size_t values_min;
  72. std::size_t values_max;
  73. };
  74. } // namespace visitors
  75. template <typename Rtree> inline
  76. std::tuple<std::size_t, std::size_t, std::size_t, std::size_t, std::size_t, std::size_t>
  77. statistics(Rtree const& tree)
  78. {
  79. typedef utilities::view<Rtree> RTV;
  80. RTV rtv(tree);
  81. visitors::statistics<
  82. typename RTV::members_holder
  83. > stats_v;
  84. rtv.apply_visitor(stats_v);
  85. return std::make_tuple(stats_v.levels, stats_v.nodes, stats_v.leaves, stats_v.values, stats_v.values_min, stats_v.values_max);
  86. }
  87. }}}}}} // namespace boost::geometry::index::detail::rtree::utilities
  88. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_STATISTICS_HPP