3 // Copyright (c) 2021, Oracle and/or its affiliates.
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7 // Licensed under the Boost Software License version 1.0.
8 // http://www.boost.org/users/license.html
10 #ifndef BOOST_GEOMETRY_UTIL_ALGORITHM_HPP
11 #define BOOST_GEOMETRY_UTIL_ALGORITHM_HPP
14 #include <boost/geometry/core/coordinate_dimension.hpp>
15 #include <boost/geometry/util/type_traits_std.hpp>
18 namespace boost { namespace geometry
21 #ifndef DOXYGEN_NO_DETAIL
25 // Other implementations can be found in the history of this file.
26 // The discussion and benchmarks can be found here:
27 // https://github.com/boostorg/geometry/pull/827
31 template <std::size_t N>
32 struct for_each_index_impl2
34 static const std::size_t N1 = N / 2;
35 static const std::size_t N2 = N - N1;
37 template <std::size_t Offset, typename UnaryFunction>
38 constexpr static inline void apply(UnaryFunction& function)
40 for_each_index_impl2<N1>::template apply<Offset>(function);
41 for_each_index_impl2<N2>::template apply<Offset + N1>(function);
46 struct for_each_index_impl2<3>
48 template <std::size_t Offset, typename UnaryFunction>
49 constexpr static inline void apply(UnaryFunction& function)
51 function(util::index_constant<Offset>());
52 function(util::index_constant<Offset + 1>());
53 function(util::index_constant<Offset + 2>());
58 struct for_each_index_impl2<2>
60 template <std::size_t Offset, typename UnaryFunction>
61 constexpr static inline void apply(UnaryFunction& function)
63 function(util::index_constant<Offset>());
64 function(util::index_constant<Offset + 1>());
69 struct for_each_index_impl2<1>
71 template <std::size_t Offset, typename UnaryFunction>
72 constexpr static inline void apply(UnaryFunction& function)
74 function(util::index_constant<Offset>());
79 struct for_each_index_impl2<0>
81 template <std::size_t Offset, typename UnaryFunction>
82 constexpr static inline void apply(UnaryFunction& )
88 template <std::size_t N, typename UnaryFunction>
89 constexpr inline UnaryFunction for_each_index(UnaryFunction function)
94 >::template apply<0>(function);
98 template <typename Geometry, typename UnaryFunction>
99 constexpr inline UnaryFunction for_each_dimension(UnaryFunction function)
103 geometry::dimension<Geometry>::value
104 >::template apply<0>(function);
108 // ----------------------------------------------------------------------------
112 template <std::size_t N>
113 struct all_indexes_of_impl2
115 static const std::size_t N1 = N / 2;
116 static const std::size_t N2 = N - N1;
118 template <std::size_t Offset, typename UnaryPredicate>
119 constexpr static inline bool apply(UnaryPredicate& predicate)
121 return all_indexes_of_impl2<N1>::template apply<Offset>(predicate)
122 && all_indexes_of_impl2<N2>::template apply<Offset + N1>(predicate);
127 struct all_indexes_of_impl2<3>
129 template <std::size_t Offset, typename UnaryPredicate>
130 constexpr static inline bool apply(UnaryPredicate& predicate)
132 return predicate(util::index_constant<Offset>())
133 && predicate(util::index_constant<Offset + 1>())
134 && predicate(util::index_constant<Offset + 2>());
139 struct all_indexes_of_impl2<2>
141 template <std::size_t Offset, typename UnaryPredicate>
142 constexpr static inline bool apply(UnaryPredicate& predicate)
144 return predicate(util::index_constant<Offset>())
145 && predicate(util::index_constant<Offset + 1>());
150 struct all_indexes_of_impl2<1>
152 template <std::size_t Offset, typename UnaryPredicate>
153 constexpr static inline bool apply(UnaryPredicate& predicate)
155 return predicate(util::index_constant<Offset>());
160 struct all_indexes_of_impl2<0>
162 template <std::size_t Offset, typename UnaryPredicate>
163 constexpr static inline bool apply(UnaryPredicate& )
171 template <std::size_t N, typename UnaryPredicate>
172 constexpr inline bool all_indexes_of(UnaryPredicate predicate)
174 return all_indexes_of_impl2<N>::template apply<0>(predicate);
177 template <typename Geometry, typename UnaryPredicate>
178 constexpr inline bool all_dimensions_of(UnaryPredicate predicate)
180 return all_indexes_of_impl2
182 geometry::dimension<Geometry>::value
183 >::template apply<0>(predicate);
186 // ----------------------------------------------------------------------------
190 template <std::size_t N>
191 struct any_index_of_impl2
193 static const std::size_t N1 = N / 2;
194 static const std::size_t N2 = N - N1;
196 template <std::size_t Offset, typename UnaryPredicate>
197 constexpr static inline bool apply(UnaryPredicate& predicate)
199 return any_index_of_impl2<N1>::template apply<Offset>(predicate)
200 || any_index_of_impl2<N2>::template apply<Offset + N1>(predicate);
205 struct any_index_of_impl2<3>
207 template <std::size_t Offset, typename UnaryPredicate>
208 constexpr static inline bool apply(UnaryPredicate& predicate)
210 return predicate(util::index_constant<Offset>())
211 || predicate(util::index_constant<Offset + 1>())
212 || predicate(util::index_constant<Offset + 2>());
217 struct any_index_of_impl2<2>
219 template <std::size_t Offset, typename UnaryPredicate>
220 constexpr static inline bool apply(UnaryPredicate& predicate)
222 return predicate(util::index_constant<Offset>())
223 || predicate(util::index_constant<Offset + 1>());
228 struct any_index_of_impl2<1>
230 template <std::size_t Offset, typename UnaryPredicate>
231 constexpr static inline bool apply(UnaryPredicate& predicate)
233 return predicate(util::index_constant<Offset>());
238 struct any_index_of_impl2<0>
240 template <std::size_t Offset, typename UnaryPredicate>
241 constexpr static inline bool apply(UnaryPredicate& )
249 template <std::size_t N, typename UnaryPredicate>
250 constexpr inline bool any_index_of(UnaryPredicate predicate)
252 return any_index_of_impl2<N>::template apply<0>(predicate);
255 template <typename Geometry, typename UnaryPredicate>
256 constexpr inline bool any_dimension_of(UnaryPredicate predicate)
258 return any_index_of_impl2
260 geometry::dimension<Geometry>::value
261 >::template apply<0>(predicate);
264 template <std::size_t N, typename UnaryPredicate>
265 constexpr inline bool none_index_of(UnaryPredicate predicate)
267 return ! any_index_of_impl2<N>::template apply<0>(predicate);
270 template <typename Geometry, typename UnaryPredicate>
271 constexpr inline bool none_dimension_of(UnaryPredicate predicate)
273 return ! any_index_of_impl2
275 geometry::dimension<Geometry>::value
276 >::template apply<0>(predicate);
279 // ----------------------------------------------------------------------------
282 } // namespace detail
283 #endif // DOXYGEN_NO_DETAIL
285 }} // namespace boost::geometry
287 #endif // BOOST_GEOMETRY_UTIL_ALGORITHM_HPP