Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / algorithms / for_each.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
7
8 // This file was modified by Oracle on 2014.
9 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
10
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15
16 // Use, modification and distribution is subject to the Boost Software License,
17 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
22
23
24 #include <algorithm>
25
26 #include <boost/range.hpp>
27 #include <boost/type_traits/is_const.hpp>
28 #include <boost/type_traits/remove_reference.hpp>
29
30 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
31 #include <boost/geometry/algorithms/not_implemented.hpp>
32 #include <boost/geometry/core/closure.hpp>
33 #include <boost/geometry/core/exterior_ring.hpp>
34 #include <boost/geometry/core/interior_rings.hpp>
35 #include <boost/geometry/core/point_type.hpp>
36 #include <boost/geometry/core/tag_cast.hpp>
37 #include <boost/geometry/core/tags.hpp>
38
39 #include <boost/geometry/geometries/concepts/check.hpp>
40
41 #include <boost/geometry/geometries/segment.hpp>
42
43 #include <boost/geometry/util/add_const_if_c.hpp>
44 #include <boost/geometry/util/range.hpp>
45
46
47 namespace boost { namespace geometry
48 {
49
50 #ifndef DOXYGEN_NO_DETAIL
51 namespace detail { namespace for_each
52 {
53
54
55 struct fe_point_per_point
56 {
57     template <typename Point, typename Functor>
58     static inline void apply(Point& point, Functor& f)
59     {
60         f(point);
61     }
62 };
63
64
65 struct fe_point_per_segment
66 {
67     template <typename Point, typename Functor>
68     static inline void apply(Point& , Functor& /*f*/)
69     {
70         // TODO: if non-const, we should extract the points from the segment
71         // and call the functor on those two points
72     }
73 };
74
75
76 struct fe_range_per_point
77 {
78     template <typename Range, typename Functor>
79     static inline void apply(Range& range, Functor& f)
80     {
81         // The previous implementation called the std library:
82         // return (std::for_each(boost::begin(range), boost::end(range), f));
83         // But that is not accepted for capturing lambda's.
84         // It needs to do it like that to return the state of Functor f (f is passed by value in std::for_each).
85
86         // So we now loop manually.
87
88         for (typename boost::range_iterator<Range>::type
89                 it = boost::begin(range); it != boost::end(range); ++it)
90         {
91             f(*it);
92         }
93     }
94 };
95
96
97 template <closure_selector Closure>
98 struct fe_range_per_segment_with_closure
99 {
100     template <typename Range, typename Functor>
101     static inline void apply(Range& range, Functor& f)
102     {
103         typedef typename add_const_if_c
104             <
105                 is_const<Range>::value,
106                 typename point_type<Range>::type
107             >::type point_type;
108
109         typedef typename boost::range_iterator<Range>::type iterator_type;
110
111         iterator_type it = boost::begin(range);
112         if (it == boost::end(range))
113         {
114             return;
115         }
116
117         iterator_type previous = it++;
118         while(it != boost::end(range))
119         {
120             model::referring_segment<point_type> s(*previous, *it);
121             f(s);
122             previous = it++;
123         }
124     }
125 };
126
127
128 template <>
129 struct fe_range_per_segment_with_closure<open>
130 {
131     template <typename Range, typename Functor>
132     static inline void apply(Range& range, Functor& f)
133     {    
134         fe_range_per_segment_with_closure<closed>::apply(range, f);
135
136         model::referring_segment
137             <
138                 typename add_const_if_c
139                     <
140                         is_const<Range>::value,
141                         typename point_type<Range>::type
142                     >::type
143             > s(range::back(range), range::front(range));
144
145         f(s);
146     }
147 };
148
149
150 struct fe_range_per_segment
151 {
152     template <typename Range, typename Functor>
153     static inline void apply(Range& range, Functor& f)
154     {
155         fe_range_per_segment_with_closure
156             <
157                 closure<Range>::value
158             >::apply(range, f);
159     }
160 };
161
162
163 struct fe_polygon_per_point
164 {
165     template <typename Polygon, typename Functor>
166     static inline void apply(Polygon& poly, Functor& f)
167     {
168         fe_range_per_point::apply(exterior_ring(poly), f);
169
170         typename interior_return_type<Polygon>::type
171             rings = interior_rings(poly);
172
173         for (typename detail::interior_iterator<Polygon>::type
174                 it = boost::begin(rings); it != boost::end(rings); ++it)
175         {
176             fe_range_per_point::apply(*it, f);
177         }
178     }
179
180 };
181
182 struct fe_polygon_per_segment
183 {
184     template <typename Polygon, typename Functor>
185     static inline void apply(Polygon& poly, Functor& f)
186     {
187         fe_range_per_segment::apply(exterior_ring(poly), f);
188
189         typename interior_return_type<Polygon>::type
190             rings = interior_rings(poly);
191
192         for (typename detail::interior_iterator<Polygon>::type
193                 it = boost::begin(rings); it != boost::end(rings); ++it)
194         {
195             fe_range_per_segment::apply(*it, f);
196         }
197     }
198
199 };
200
201 // Implementation of multi, for both point and segment,
202 // just calling the single version.
203 template <typename Policy>
204 struct for_each_multi
205 {
206     template <typename MultiGeometry, typename Functor>
207     static inline void apply(MultiGeometry& multi, Functor& f)
208     {
209         for (typename boost::range_iterator<MultiGeometry>::type
210                 it = boost::begin(multi); it != boost::end(multi); ++it)
211         {
212             Policy::apply(*it, f);
213         }
214     }
215 };
216
217 }} // namespace detail::for_each
218 #endif // DOXYGEN_NO_DETAIL
219
220
221 #ifndef DOXYGEN_NO_DISPATCH
222 namespace dispatch
223 {
224
225 template
226 <
227     typename Geometry,
228     typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
229 >
230 struct for_each_point: not_implemented<Tag>
231 {};
232
233
234 template <typename Point>
235 struct for_each_point<Point, point_tag>
236     : detail::for_each::fe_point_per_point
237 {};
238
239
240 template <typename Linestring>
241 struct for_each_point<Linestring, linestring_tag>
242     : detail::for_each::fe_range_per_point
243 {};
244
245
246 template <typename Ring>
247 struct for_each_point<Ring, ring_tag>
248     : detail::for_each::fe_range_per_point
249 {};
250
251
252 template <typename Polygon>
253 struct for_each_point<Polygon, polygon_tag>
254     : detail::for_each::fe_polygon_per_point
255 {};
256
257
258 template
259 <
260     typename Geometry,
261     typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
262 >
263 struct for_each_segment: not_implemented<Tag>
264 {};
265
266 template <typename Point>
267 struct for_each_segment<Point, point_tag>
268     : detail::for_each::fe_point_per_segment
269 {};
270
271
272 template <typename Linestring>
273 struct for_each_segment<Linestring, linestring_tag>
274     : detail::for_each::fe_range_per_segment
275 {};
276
277
278 template <typename Ring>
279 struct for_each_segment<Ring, ring_tag>
280     : detail::for_each::fe_range_per_segment
281 {};
282
283
284 template <typename Polygon>
285 struct for_each_segment<Polygon, polygon_tag>
286     : detail::for_each::fe_polygon_per_segment
287 {};
288
289
290 template <typename MultiGeometry>
291 struct for_each_point<MultiGeometry, multi_tag>
292     : detail::for_each::for_each_multi
293         <
294             // Specify the dispatch of the single-version as policy
295             for_each_point
296                 <
297                     typename add_const_if_c
298                         <
299                             is_const<MultiGeometry>::value,
300                             typename boost::range_value<MultiGeometry>::type
301                         >::type
302                 >
303         >
304 {};
305
306
307 template <typename MultiGeometry>
308 struct for_each_segment<MultiGeometry, multi_tag>
309     : detail::for_each::for_each_multi
310         <
311             // Specify the dispatch of the single-version as policy
312             for_each_segment
313                 <
314                     typename add_const_if_c
315                         <
316                             is_const<MultiGeometry>::value,
317                             typename boost::range_value<MultiGeometry>::type
318                         >::type
319                 >
320         >
321 {};
322
323
324 } // namespace dispatch
325 #endif // DOXYGEN_NO_DISPATCH
326
327
328 /*!
329 \brief \brf_for_each{point}
330 \details \det_for_each{point}
331 \ingroup for_each
332 \param geometry \param_geometry
333 \param f \par_for_each_f{point}
334 \tparam Geometry \tparam_geometry
335 \tparam Functor \tparam_functor
336
337 \qbk{[include reference/algorithms/for_each_point.qbk]}
338 \qbk{[heading Example]}
339 \qbk{[for_each_point] [for_each_point_output]}
340 \qbk{[for_each_point_const] [for_each_point_const_output]}
341 */
342 template<typename Geometry, typename Functor>
343 inline Functor for_each_point(Geometry& geometry, Functor f)
344 {
345     concepts::check<Geometry>();
346
347     dispatch::for_each_point<Geometry>::apply(geometry, f);
348     return f;
349 }
350
351
352 /*!
353 \brief \brf_for_each{segment}
354 \details \det_for_each{segment}
355 \ingroup for_each
356 \param geometry \param_geometry
357 \param f \par_for_each_f{segment}
358 \tparam Geometry \tparam_geometry
359 \tparam Functor \tparam_functor
360
361 \qbk{[include reference/algorithms/for_each_segment.qbk]}
362 \qbk{[heading Example]}
363 \qbk{[for_each_segment_const] [for_each_segment_const_output]}
364 */
365 template<typename Geometry, typename Functor>
366 inline Functor for_each_segment(Geometry& geometry, Functor f)
367 {
368     concepts::check<Geometry>();
369
370     dispatch::for_each_segment<Geometry>::apply(geometry, f);
371     return f;
372 }
373
374
375 }} // namespace boost::geometry
376
377
378 #endif // BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP