print.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Boost.Geometry Index
  2. //
  3. // R-tree ostreaming visitor implementation
  4. //
  5. // Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP
  11. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP
  12. #include <iostream>
  13. namespace boost { namespace geometry { namespace index { namespace detail {
  14. namespace utilities {
  15. namespace dispatch {
  16. template <typename Point, size_t Dimension>
  17. struct print_point
  18. {
  19. BOOST_STATIC_ASSERT(0 < Dimension);
  20. static inline void apply(std::ostream & os, Point const& p)
  21. {
  22. print_point<Point, Dimension - 1>::apply(os, p);
  23. os << ", " << geometry::get<Dimension - 1>(p);
  24. }
  25. };
  26. template <typename Point>
  27. struct print_point<Point, 1>
  28. {
  29. static inline void apply(std::ostream & os, Point const& p)
  30. {
  31. os << geometry::get<0>(p);
  32. }
  33. };
  34. template <typename Box, size_t Corner, size_t Dimension>
  35. struct print_corner
  36. {
  37. BOOST_STATIC_ASSERT(0 < Dimension);
  38. static inline void apply(std::ostream & os, Box const& b)
  39. {
  40. print_corner<Box, Corner, Dimension - 1>::apply(os, b);
  41. os << ", " << geometry::get<Corner, Dimension - 1>(b);
  42. }
  43. };
  44. template <typename Box, size_t Corner>
  45. struct print_corner<Box, Corner, 1>
  46. {
  47. static inline void apply(std::ostream & os, Box const& b)
  48. {
  49. os << geometry::get<Corner, 0>(b);
  50. }
  51. };
  52. template <typename Indexable, typename Tag>
  53. struct print_indexable
  54. {
  55. };
  56. template <typename Indexable>
  57. struct print_indexable<Indexable, box_tag>
  58. {
  59. static const size_t dimension = geometry::dimension<Indexable>::value;
  60. static inline void apply(std::ostream &os, Indexable const& i)
  61. {
  62. os << '(';
  63. print_corner<Indexable, min_corner, dimension>::apply(os, i);
  64. os << ")x(";
  65. print_corner<Indexable, max_corner, dimension>::apply(os, i);
  66. os << ')';
  67. }
  68. };
  69. template <typename Indexable>
  70. struct print_indexable<Indexable, point_tag>
  71. {
  72. static const size_t dimension = geometry::dimension<Indexable>::value;
  73. static inline void apply(std::ostream &os, Indexable const& i)
  74. {
  75. os << '(';
  76. print_point<Indexable, dimension>::apply(os, i);
  77. os << ')';
  78. }
  79. };
  80. } // namespace dispatch
  81. template <typename Indexable> inline
  82. void print_indexable(std::ostream & os, Indexable const& i)
  83. {
  84. dispatch::print_indexable<
  85. Indexable,
  86. typename tag<Indexable>::type
  87. >::apply(os, i);
  88. }
  89. } // namespace utilities
  90. namespace rtree { namespace utilities {
  91. namespace visitors {
  92. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
  93. struct print : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  94. {
  95. typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  96. typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  97. inline print(std::ostream & o, Translator const& t)
  98. : os(o), tr(t), level(0)
  99. {}
  100. inline void operator()(internal_node const& n)
  101. {
  102. typedef typename rtree::elements_type<internal_node>::type elements_type;
  103. elements_type const& elements = rtree::elements(n);
  104. spaces(level) << "INTERNAL NODE - L:" << level << " Ch:" << elements.size() << " @:" << &n << '\n';
  105. for (typename elements_type::const_iterator it = elements.begin();
  106. it != elements.end(); ++it)
  107. {
  108. spaces(level);
  109. detail::utilities::print_indexable(os, it->first);
  110. os << " ->" << it->second << '\n';
  111. }
  112. size_t level_backup = level;
  113. ++level;
  114. for (typename elements_type::const_iterator it = elements.begin();
  115. it != elements.end(); ++it)
  116. {
  117. rtree::apply_visitor(*this, *it->second);
  118. }
  119. level = level_backup;
  120. }
  121. inline void operator()(leaf const& n)
  122. {
  123. typedef typename rtree::elements_type<leaf>::type elements_type;
  124. elements_type const& elements = rtree::elements(n);
  125. spaces(level) << "LEAF - L:" << level << " V:" << elements.size() << " @:" << &n << '\n';
  126. for (typename elements_type::const_iterator it = elements.begin();
  127. it != elements.end(); ++it)
  128. {
  129. spaces(level);
  130. detail::utilities::print_indexable(os, tr(*it));
  131. os << '\n';
  132. }
  133. }
  134. inline std::ostream & spaces(size_t level)
  135. {
  136. for ( size_t i = 0 ; i < 2 * level ; ++i )
  137. os << ' ';
  138. return os;
  139. }
  140. std::ostream & os;
  141. Translator const& tr;
  142. size_t level;
  143. };
  144. } // namespace visitors
  145. template <typename Rtree> inline
  146. void print(std::ostream & os, Rtree const& tree)
  147. {
  148. typedef utilities::view<Rtree> RTV;
  149. RTV rtv(tree);
  150. visitors::print<
  151. typename RTV::value_type,
  152. typename RTV::options_type,
  153. typename RTV::translator_type,
  154. typename RTV::box_type,
  155. typename RTV::allocators_type
  156. > print_v(os, rtv.translator());
  157. rtv.apply_visitor(print_v);
  158. }
  159. }} // namespace rtree::utilities
  160. }}}} // namespace boost::geometry::index::detail
  161. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP