print.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Boost.Geometry Index
  2. //
  3. // R-tree ostreaming visitor implementation
  4. //
  5. // Copyright (c) 2011-2013 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_UTILITIES_PRINT_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP
  16. #include <iostream>
  17. namespace boost { namespace geometry { namespace index { namespace detail {
  18. namespace utilities {
  19. namespace dispatch {
  20. template <typename Point, size_t Dimension>
  21. struct print_point
  22. {
  23. BOOST_STATIC_ASSERT(0 < Dimension);
  24. static inline void apply(std::ostream & os, Point const& p)
  25. {
  26. print_point<Point, Dimension - 1>::apply(os, p);
  27. os << ", " << geometry::get<Dimension - 1>(p);
  28. }
  29. };
  30. template <typename Point>
  31. struct print_point<Point, 1>
  32. {
  33. static inline void apply(std::ostream & os, Point const& p)
  34. {
  35. os << geometry::get<0>(p);
  36. }
  37. };
  38. template <typename Box, size_t Corner, size_t Dimension>
  39. struct print_corner
  40. {
  41. BOOST_STATIC_ASSERT(0 < Dimension);
  42. static inline void apply(std::ostream & os, Box const& b)
  43. {
  44. print_corner<Box, Corner, Dimension - 1>::apply(os, b);
  45. os << ", " << geometry::get<Corner, Dimension - 1>(b);
  46. }
  47. };
  48. template <typename Box, size_t Corner>
  49. struct print_corner<Box, Corner, 1>
  50. {
  51. static inline void apply(std::ostream & os, Box const& b)
  52. {
  53. os << geometry::get<Corner, 0>(b);
  54. }
  55. };
  56. template <typename Indexable, typename Tag>
  57. struct print_indexable
  58. {
  59. BOOST_MPL_ASSERT_MSG((false), NOT_IMPLEMENTED_FOR_THIS_TAG, (Tag));
  60. };
  61. template <typename Indexable>
  62. struct print_indexable<Indexable, box_tag>
  63. {
  64. static const size_t dimension = geometry::dimension<Indexable>::value;
  65. static inline void apply(std::ostream &os, Indexable const& i)
  66. {
  67. os << '(';
  68. print_corner<Indexable, min_corner, dimension>::apply(os, i);
  69. os << ")x(";
  70. print_corner<Indexable, max_corner, dimension>::apply(os, i);
  71. os << ')';
  72. }
  73. };
  74. template <typename Indexable>
  75. struct print_indexable<Indexable, point_tag>
  76. {
  77. static const size_t dimension = geometry::dimension<Indexable>::value;
  78. static inline void apply(std::ostream &os, Indexable const& i)
  79. {
  80. os << '(';
  81. print_point<Indexable, dimension>::apply(os, i);
  82. os << ')';
  83. }
  84. };
  85. template <typename Indexable>
  86. struct print_indexable<Indexable, segment_tag>
  87. {
  88. static const size_t dimension = geometry::dimension<Indexable>::value;
  89. static inline void apply(std::ostream &os, Indexable const& i)
  90. {
  91. os << '(';
  92. print_corner<Indexable, 0, dimension>::apply(os, i);
  93. os << ")-(";
  94. print_corner<Indexable, 1, dimension>::apply(os, i);
  95. os << ')';
  96. }
  97. };
  98. } // namespace dispatch
  99. template <typename Indexable> inline
  100. void print_indexable(std::ostream & os, Indexable const& i)
  101. {
  102. dispatch::print_indexable<
  103. Indexable,
  104. typename tag<Indexable>::type
  105. >::apply(os, i);
  106. }
  107. } // namespace utilities
  108. namespace rtree { namespace utilities {
  109. namespace visitors {
  110. template <typename MembersHolder>
  111. struct print
  112. : public MembersHolder::visitor_const
  113. {
  114. typedef typename MembersHolder::translator_type translator_type;
  115. typedef typename MembersHolder::internal_node internal_node;
  116. typedef typename MembersHolder::leaf leaf;
  117. inline print(std::ostream & o, translator_type const& t)
  118. : os(o), tr(t), level(0)
  119. {}
  120. inline void operator()(internal_node const& n)
  121. {
  122. typedef typename rtree::elements_type<internal_node>::type elements_type;
  123. elements_type const& elements = rtree::elements(n);
  124. spaces(level) << "INTERNAL NODE - L:" << level << " Ch:" << elements.size() << " @:" << &n << '\n';
  125. for (typename elements_type::const_iterator it = elements.begin();
  126. it != elements.end(); ++it)
  127. {
  128. spaces(level);
  129. detail::utilities::print_indexable(os, it->first);
  130. os << " ->" << it->second << '\n';
  131. }
  132. size_t level_backup = level;
  133. ++level;
  134. for (typename elements_type::const_iterator it = elements.begin();
  135. it != elements.end(); ++it)
  136. {
  137. rtree::apply_visitor(*this, *it->second);
  138. }
  139. level = level_backup;
  140. }
  141. inline void operator()(leaf const& n)
  142. {
  143. typedef typename rtree::elements_type<leaf>::type elements_type;
  144. elements_type const& elements = rtree::elements(n);
  145. spaces(level) << "LEAF - L:" << level << " V:" << elements.size() << " @:" << &n << '\n';
  146. for (typename elements_type::const_iterator it = elements.begin();
  147. it != elements.end(); ++it)
  148. {
  149. spaces(level);
  150. detail::utilities::print_indexable(os, tr(*it));
  151. os << '\n';
  152. }
  153. }
  154. inline std::ostream & spaces(size_t level)
  155. {
  156. for ( size_t i = 0 ; i < 2 * level ; ++i )
  157. os << ' ';
  158. return os;
  159. }
  160. std::ostream & os;
  161. translator_type const& tr;
  162. size_t level;
  163. };
  164. } // namespace visitors
  165. template <typename Rtree> inline
  166. void print(std::ostream & os, Rtree const& tree)
  167. {
  168. typedef utilities::view<Rtree> RTV;
  169. RTV rtv(tree);
  170. visitors::print<
  171. typename RTV::members_holder
  172. > print_v(os, rtv.translator());
  173. rtv.apply_visitor(print_v);
  174. }
  175. }} // namespace rtree::utilities
  176. }}}} // namespace boost::geometry::index::detail
  177. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP