]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/util/algorithm.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / geometry / util / algorithm.hpp
1 // Boost.Geometry
2
3 // Copyright (c) 2021, Oracle and/or its affiliates.
4
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
6
7 // Licensed under the Boost Software License version 1.0.
8 // http://www.boost.org/users/license.html
9
10 #ifndef BOOST_GEOMETRY_UTIL_ALGORITHM_HPP
11 #define BOOST_GEOMETRY_UTIL_ALGORITHM_HPP
12
13
14 #include <boost/geometry/core/coordinate_dimension.hpp>
15 #include <boost/geometry/util/type_traits_std.hpp>
16
17
18 namespace boost { namespace geometry
19 {
20
21 #ifndef DOXYGEN_NO_DETAIL
22 namespace detail
23 {
24
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
28
29 // O(logN) version 2
30
31 template <std::size_t N>
32 struct for_each_index_impl2
33 {
34 static const std::size_t N1 = N / 2;
35 static const std::size_t N2 = N - N1;
36
37 template <std::size_t Offset, typename UnaryFunction>
38 constexpr static inline void apply(UnaryFunction& function)
39 {
40 for_each_index_impl2<N1>::template apply<Offset>(function);
41 for_each_index_impl2<N2>::template apply<Offset + N1>(function);
42 }
43 };
44
45 template <>
46 struct for_each_index_impl2<3>
47 {
48 template <std::size_t Offset, typename UnaryFunction>
49 constexpr static inline void apply(UnaryFunction& function)
50 {
51 function(util::index_constant<Offset>());
52 function(util::index_constant<Offset + 1>());
53 function(util::index_constant<Offset + 2>());
54 }
55 };
56
57 template <>
58 struct for_each_index_impl2<2>
59 {
60 template <std::size_t Offset, typename UnaryFunction>
61 constexpr static inline void apply(UnaryFunction& function)
62 {
63 function(util::index_constant<Offset>());
64 function(util::index_constant<Offset + 1>());
65 }
66 };
67
68 template <>
69 struct for_each_index_impl2<1>
70 {
71 template <std::size_t Offset, typename UnaryFunction>
72 constexpr static inline void apply(UnaryFunction& function)
73 {
74 function(util::index_constant<Offset>());
75 }
76 };
77
78 template <>
79 struct for_each_index_impl2<0>
80 {
81 template <std::size_t Offset, typename UnaryFunction>
82 constexpr static inline void apply(UnaryFunction& )
83 {}
84 };
85
86 // Interface
87
88 template <std::size_t N, typename UnaryFunction>
89 constexpr inline UnaryFunction for_each_index(UnaryFunction function)
90 {
91 for_each_index_impl2
92 <
93 N
94 >::template apply<0>(function);
95 return function;
96 }
97
98 template <typename Geometry, typename UnaryFunction>
99 constexpr inline UnaryFunction for_each_dimension(UnaryFunction function)
100 {
101 for_each_index_impl2
102 <
103 geometry::dimension<Geometry>::value
104 >::template apply<0>(function);
105 return function;
106 }
107
108 // ----------------------------------------------------------------------------
109
110 // O(logN) version 2
111
112 template <std::size_t N>
113 struct all_indexes_of_impl2
114 {
115 static const std::size_t N1 = N / 2;
116 static const std::size_t N2 = N - N1;
117
118 template <std::size_t Offset, typename UnaryPredicate>
119 constexpr static inline bool apply(UnaryPredicate& predicate)
120 {
121 return all_indexes_of_impl2<N1>::template apply<Offset>(predicate)
122 && all_indexes_of_impl2<N2>::template apply<Offset + N1>(predicate);
123 }
124 };
125
126 template <>
127 struct all_indexes_of_impl2<3>
128 {
129 template <std::size_t Offset, typename UnaryPredicate>
130 constexpr static inline bool apply(UnaryPredicate& predicate)
131 {
132 return predicate(util::index_constant<Offset>())
133 && predicate(util::index_constant<Offset + 1>())
134 && predicate(util::index_constant<Offset + 2>());
135 }
136 };
137
138 template <>
139 struct all_indexes_of_impl2<2>
140 {
141 template <std::size_t Offset, typename UnaryPredicate>
142 constexpr static inline bool apply(UnaryPredicate& predicate)
143 {
144 return predicate(util::index_constant<Offset>())
145 && predicate(util::index_constant<Offset + 1>());
146 }
147 };
148
149 template <>
150 struct all_indexes_of_impl2<1>
151 {
152 template <std::size_t Offset, typename UnaryPredicate>
153 constexpr static inline bool apply(UnaryPredicate& predicate)
154 {
155 return predicate(util::index_constant<Offset>());
156 }
157 };
158
159 template <>
160 struct all_indexes_of_impl2<0>
161 {
162 template <std::size_t Offset, typename UnaryPredicate>
163 constexpr static inline bool apply(UnaryPredicate& )
164 {
165 return true;
166 }
167 };
168
169 // Interface
170
171 template <std::size_t N, typename UnaryPredicate>
172 constexpr inline bool all_indexes_of(UnaryPredicate predicate)
173 {
174 return all_indexes_of_impl2<N>::template apply<0>(predicate);
175 }
176
177 template <typename Geometry, typename UnaryPredicate>
178 constexpr inline bool all_dimensions_of(UnaryPredicate predicate)
179 {
180 return all_indexes_of_impl2
181 <
182 geometry::dimension<Geometry>::value
183 >::template apply<0>(predicate);
184 }
185
186 // ----------------------------------------------------------------------------
187
188 // O(logN) version 2
189
190 template <std::size_t N>
191 struct any_index_of_impl2
192 {
193 static const std::size_t N1 = N / 2;
194 static const std::size_t N2 = N - N1;
195
196 template <std::size_t Offset, typename UnaryPredicate>
197 constexpr static inline bool apply(UnaryPredicate& predicate)
198 {
199 return any_index_of_impl2<N1>::template apply<Offset>(predicate)
200 || any_index_of_impl2<N2>::template apply<Offset + N1>(predicate);
201 }
202 };
203
204 template <>
205 struct any_index_of_impl2<3>
206 {
207 template <std::size_t Offset, typename UnaryPredicate>
208 constexpr static inline bool apply(UnaryPredicate& predicate)
209 {
210 return predicate(util::index_constant<Offset>())
211 || predicate(util::index_constant<Offset + 1>())
212 || predicate(util::index_constant<Offset + 2>());
213 }
214 };
215
216 template <>
217 struct any_index_of_impl2<2>
218 {
219 template <std::size_t Offset, typename UnaryPredicate>
220 constexpr static inline bool apply(UnaryPredicate& predicate)
221 {
222 return predicate(util::index_constant<Offset>())
223 || predicate(util::index_constant<Offset + 1>());
224 }
225 };
226
227 template <>
228 struct any_index_of_impl2<1>
229 {
230 template <std::size_t Offset, typename UnaryPredicate>
231 constexpr static inline bool apply(UnaryPredicate& predicate)
232 {
233 return predicate(util::index_constant<Offset>());
234 }
235 };
236
237 template <>
238 struct any_index_of_impl2<0>
239 {
240 template <std::size_t Offset, typename UnaryPredicate>
241 constexpr static inline bool apply(UnaryPredicate& )
242 {
243 return false;
244 }
245 };
246
247 // Interface
248
249 template <std::size_t N, typename UnaryPredicate>
250 constexpr inline bool any_index_of(UnaryPredicate predicate)
251 {
252 return any_index_of_impl2<N>::template apply<0>(predicate);
253 }
254
255 template <typename Geometry, typename UnaryPredicate>
256 constexpr inline bool any_dimension_of(UnaryPredicate predicate)
257 {
258 return any_index_of_impl2
259 <
260 geometry::dimension<Geometry>::value
261 >::template apply<0>(predicate);
262 }
263
264 template <std::size_t N, typename UnaryPredicate>
265 constexpr inline bool none_index_of(UnaryPredicate predicate)
266 {
267 return ! any_index_of_impl2<N>::template apply<0>(predicate);
268 }
269
270 template <typename Geometry, typename UnaryPredicate>
271 constexpr inline bool none_dimension_of(UnaryPredicate predicate)
272 {
273 return ! any_index_of_impl2
274 <
275 geometry::dimension<Geometry>::value
276 >::template apply<0>(predicate);
277 }
278
279 // ----------------------------------------------------------------------------
280
281
282 } // namespace detail
283 #endif // DOXYGEN_NO_DETAIL
284
285 }} // namespace boost::geometry
286
287 #endif // BOOST_GEOMETRY_UTIL_ALGORITHM_HPP