print.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. BOOST_MPL_ASSERT_MSG((false), NOT_IMPLEMENTED_FOR_THIS_TAG, (Tag));
  56. };
  57. template <typename Indexable>
  58. struct print_indexable<Indexable, box_tag>
  59. {
  60. static const size_t dimension = geometry::dimension<Indexable>::value;
  61. static inline void apply(std::ostream &os, Indexable const& i)
  62. {
  63. os << '(';
  64. print_corner<Indexable, min_corner, dimension>::apply(os, i);
  65. os << ")x(";
  66. print_corner<Indexable, max_corner, dimension>::apply(os, i);
  67. os << ')';
  68. }
  69. };
  70. template <typename Indexable>
  71. struct print_indexable<Indexable, point_tag>
  72. {
  73. static const size_t dimension = geometry::dimension<Indexable>::value;
  74. static inline void apply(std::ostream &os, Indexable const& i)
  75. {
  76. os << '(';
  77. print_point<Indexable, dimension>::apply(os, i);
  78. os << ')';
  79. }
  80. };
  81. template <typename Indexable>
  82. struct print_indexable<Indexable, segment_tag>
  83. {
  84. static const size_t dimension = geometry::dimension<Indexable>::value;
  85. static inline void apply(std::ostream &os, Indexable const& i)
  86. {
  87. os << '(';
  88. print_corner<Indexable, 0, dimension>::apply(os, i);
  89. os << ")-(";
  90. print_corner<Indexable, 1, dimension>::apply(os, i);
  91. os << ')';
  92. }
  93. };
  94. } // namespace dispatch
  95. template <typename Indexable> inline
  96. void print_indexable(std::ostream & os, Indexable const& i)
  97. {
  98. dispatch::print_indexable<
  99. Indexable,
  100. typename tag<Indexable>::type
  101. >::apply(os, i);
  102. }
  103. } // namespace utilities
  104. namespace rtree { namespace utilities {
  105. namespace visitors {
  106. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
  107. struct print : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  108. {
  109. typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  110. typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  111. inline print(std::ostream & o, Translator const& t)
  112. : os(o), tr(t), level(0)
  113. {}
  114. inline void operator()(internal_node const& n)
  115. {
  116. typedef typename rtree::elements_type<internal_node>::type elements_type;
  117. elements_type const& elements = rtree::elements(n);
  118. spaces(level) << "INTERNAL NODE - L:" << level << " Ch:" << elements.size() << " @:" << &n << '\n';
  119. for (typename elements_type::const_iterator it = elements.begin();
  120. it != elements.end(); ++it)
  121. {
  122. spaces(level);
  123. detail::utilities::print_indexable(os, it->first);
  124. os << " ->" << it->second << '\n';
  125. }
  126. size_t level_backup = level;
  127. ++level;
  128. for (typename elements_type::const_iterator it = elements.begin();
  129. it != elements.end(); ++it)
  130. {
  131. rtree::apply_visitor(*this, *it->second);
  132. }
  133. level = level_backup;
  134. }
  135. inline void operator()(leaf const& n)
  136. {
  137. typedef typename rtree::elements_type<leaf>::type elements_type;
  138. elements_type const& elements = rtree::elements(n);
  139. spaces(level) << "LEAF - L:" << level << " V:" << elements.size() << " @:" << &n << '\n';
  140. for (typename elements_type::const_iterator it = elements.begin();
  141. it != elements.end(); ++it)
  142. {
  143. spaces(level);
  144. detail::utilities::print_indexable(os, tr(*it));
  145. os << '\n';
  146. }
  147. }
  148. inline std::ostream & spaces(size_t level)
  149. {
  150. for ( size_t i = 0 ; i < 2 * level ; ++i )
  151. os << ' ';
  152. return os;
  153. }
  154. std::ostream & os;
  155. Translator const& tr;
  156. size_t level;
  157. };
  158. } // namespace visitors
  159. template <typename Rtree> inline
  160. void print(std::ostream & os, Rtree const& tree)
  161. {
  162. typedef utilities::view<Rtree> RTV;
  163. RTV rtv(tree);
  164. visitors::print<
  165. typename RTV::value_type,
  166. typename RTV::options_type,
  167. typename RTV::translator_type,
  168. typename RTV::box_type,
  169. typename RTV::allocators_type
  170. > print_v(os, rtv.translator());
  171. rtv.apply_visitor(print_v);
  172. }
  173. }} // namespace rtree::utilities
  174. }}}} // namespace boost::geometry::index::detail
  175. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP