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
15 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP
16 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP
17
18 #include <iostream>
19
20 namespace boost { namespace geometry { namespace index { namespace detail {
21
22 namespace utilities {
23
24 namespace dispatch {
25
26 template <typename Point, size_t Dimension>
27 struct print_point
28 {
29 BOOST_STATIC_ASSERT(0 < Dimension);
30
applyboost::geometry::index::detail::utilities::dispatch::print_point31 static inline void apply(std::ostream & os, Point const& p)
32 {
33 print_point<Point, Dimension - 1>::apply(os, p);
34
35 os << ", " << geometry::get<Dimension - 1>(p);
36 }
37 };
38
39 template <typename Point>
40 struct print_point<Point, 1>
41 {
applyboost::geometry::index::detail::utilities::dispatch::print_point42 static inline void apply(std::ostream & os, Point const& p)
43 {
44 os << geometry::get<0>(p);
45 }
46 };
47
48 template <typename Box, size_t Corner, size_t Dimension>
49 struct print_corner
50 {
51 BOOST_STATIC_ASSERT(0 < Dimension);
52
applyboost::geometry::index::detail::utilities::dispatch::print_corner53 static inline void apply(std::ostream & os, Box const& b)
54 {
55 print_corner<Box, Corner, Dimension - 1>::apply(os, b);
56
57 os << ", " << geometry::get<Corner, Dimension - 1>(b);
58 }
59 };
60
61 template <typename Box, size_t Corner>
62 struct print_corner<Box, Corner, 1>
63 {
applyboost::geometry::index::detail::utilities::dispatch::print_corner64 static inline void apply(std::ostream & os, Box const& b)
65 {
66 os << geometry::get<Corner, 0>(b);
67 }
68 };
69
70 template <typename Indexable, typename Tag>
71 struct print_indexable
72 {
73 BOOST_MPL_ASSERT_MSG((false), NOT_IMPLEMENTED_FOR_THIS_TAG, (Tag));
74 };
75
76 template <typename Indexable>
77 struct print_indexable<Indexable, box_tag>
78 {
79 static const size_t dimension = geometry::dimension<Indexable>::value;
80
applyboost::geometry::index::detail::utilities::dispatch::print_indexable81 static inline void apply(std::ostream &os, Indexable const& i)
82 {
83 os << '(';
84 print_corner<Indexable, min_corner, dimension>::apply(os, i);
85 os << ")x(";
86 print_corner<Indexable, max_corner, dimension>::apply(os, i);
87 os << ')';
88 }
89 };
90
91 template <typename Indexable>
92 struct print_indexable<Indexable, point_tag>
93 {
94 static const size_t dimension = geometry::dimension<Indexable>::value;
95
applyboost::geometry::index::detail::utilities::dispatch::print_indexable96 static inline void apply(std::ostream &os, Indexable const& i)
97 {
98 os << '(';
99 print_point<Indexable, dimension>::apply(os, i);
100 os << ')';
101 }
102 };
103
104 template <typename Indexable>
105 struct print_indexable<Indexable, segment_tag>
106 {
107 static const size_t dimension = geometry::dimension<Indexable>::value;
108
applyboost::geometry::index::detail::utilities::dispatch::print_indexable109 static inline void apply(std::ostream &os, Indexable const& i)
110 {
111 os << '(';
112 print_corner<Indexable, 0, dimension>::apply(os, i);
113 os << ")-(";
114 print_corner<Indexable, 1, dimension>::apply(os, i);
115 os << ')';
116 }
117 };
118
119 } // namespace dispatch
120
121 template <typename Indexable> inline
print_indexable(std::ostream & os,Indexable const & i)122 void print_indexable(std::ostream & os, Indexable const& i)
123 {
124 dispatch::print_indexable<
125 Indexable,
126 typename tag<Indexable>::type
127 >::apply(os, i);
128 }
129
130 } // namespace utilities
131
132 namespace rtree { namespace utilities {
133
134 namespace visitors {
135
136 template <typename MembersHolder>
137 struct print
138 : public MembersHolder::visitor_const
139 {
140 typedef typename MembersHolder::translator_type translator_type;
141
142 typedef typename MembersHolder::internal_node internal_node;
143 typedef typename MembersHolder::leaf leaf;
144
printboost::geometry::index::detail::rtree::utilities::visitors::print145 inline print(std::ostream & o, translator_type const& t)
146 : os(o), tr(t), level(0)
147 {}
148
operator ()boost::geometry::index::detail::rtree::utilities::visitors::print149 inline void operator()(internal_node const& n)
150 {
151 typedef typename rtree::elements_type<internal_node>::type elements_type;
152 elements_type const& elements = rtree::elements(n);
153
154 spaces(level) << "INTERNAL NODE - L:" << level << " Ch:" << elements.size() << " @:" << &n << '\n';
155
156 for (typename elements_type::const_iterator it = elements.begin();
157 it != elements.end(); ++it)
158 {
159 spaces(level);
160 detail::utilities::print_indexable(os, it->first);
161 os << " ->" << it->second << '\n';
162 }
163
164 size_t level_backup = level;
165 ++level;
166
167 for (typename elements_type::const_iterator it = elements.begin();
168 it != elements.end(); ++it)
169 {
170 rtree::apply_visitor(*this, *it->second);
171 }
172
173 level = level_backup;
174 }
175
operator ()boost::geometry::index::detail::rtree::utilities::visitors::print176 inline void operator()(leaf const& n)
177 {
178 typedef typename rtree::elements_type<leaf>::type elements_type;
179 elements_type const& elements = rtree::elements(n);
180
181 spaces(level) << "LEAF - L:" << level << " V:" << elements.size() << " @:" << &n << '\n';
182 for (typename elements_type::const_iterator it = elements.begin();
183 it != elements.end(); ++it)
184 {
185 spaces(level);
186 detail::utilities::print_indexable(os, tr(*it));
187 os << '\n';
188 }
189 }
190
spacesboost::geometry::index::detail::rtree::utilities::visitors::print191 inline std::ostream & spaces(size_t level)
192 {
193 for ( size_t i = 0 ; i < 2 * level ; ++i )
194 os << ' ';
195 return os;
196 }
197
198 std::ostream & os;
199 translator_type const& tr;
200
201 size_t level;
202 };
203
204 } // namespace visitors
205
206 template <typename Rtree> inline
print(std::ostream & os,Rtree const & tree)207 void print(std::ostream & os, Rtree const& tree)
208 {
209 typedef utilities::view<Rtree> RTV;
210 RTV rtv(tree);
211
212 visitors::print<
213 typename RTV::members_holder
214 > print_v(os, rtv.translator());
215 rtv.apply_visitor(print_v);
216 }
217
218 }} // namespace rtree::utilities
219
220 }}}} // namespace boost::geometry::index::detail
221
222 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_UTILITIES_PRINT_HPP
223