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