Geogram Version 1.9.6-rc
A programming library of geometric algorithms
Loading...
Searching...
No Matches
range.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_BASIC_RANGE
41#define GEOGRAM_BASIC_RANGE
42
47
55namespace GEO {
56
67 public:
68 index_as_iterator(index_t val) : val_(val) {
69 }
70
71 void operator++() {
72 ++val_;
73 }
74
75 bool operator==(index_as_iterator rhs) const {
76 return val_ == rhs.val_;
77 }
78
79 bool operator!=(index_as_iterator rhs) const {
80 return val_ != rhs.val_;
81 }
82
83 bool operator<(index_as_iterator rhs) const {
84 return val_ < rhs.val_;
85 }
86
87 index_t operator*() const {
88 return val_;
89 }
90
91 index_t operator-(index_as_iterator it) const {
92 return val_ - it.val_;
93 }
94
95 index_as_iterator operator+(index_t i) const {
96 return index_as_iterator(val_ + i);
97 }
98
99 private:
100 index_t val_;
101 };
102
107 public:
108
111
119 ) : begin_(b), end_(e) {
120 }
121
127 return begin_;
128 }
129
136 return end_;
137 }
138
142 index_t nb() const {
143 return end_ - begin_;
144 }
145
152 geo_debug_assert(i < nb());
153 return *(begin_ + i);
154 }
155
156 private:
157 index_as_iterator begin_;
159 };
160
161 /*************************************************************************/
162
168 public:
170 const index_t* ptr, const index_t* begin, const index_t* end
171 ) : ptr_(ptr)
172#ifdef GEO_DEBUG
173 ,begin_(begin),
174 end_(end)
175#endif
176 {
177 geo_argused(begin);
178 geo_argused(end);
179 }
180
181 void operator++() {
182 ++ptr_;
183 }
184
185 bool operator==(const_index_ptr_in_array rhs) const {
186 return ptr_ == rhs.ptr_;
187 }
188
189 bool operator!=(const_index_ptr_in_array rhs) const {
190 return ptr_ != rhs.ptr_;
191 }
192
193 bool operator<(const_index_ptr_in_array rhs) const {
194 return ptr_ < rhs.ptr_;
195 }
196
197 const index_t& operator*() const {
198 geo_debug_assert(ptr_ >= begin_ && ptr_ < end_);
199 return *ptr_;
200 }
201
202 index_t operator-(const_index_ptr_in_array it) const {
203 return index_t(ptr_ - it.ptr_);
204 }
205
206 const_index_ptr_in_array operator+(index_t i) const {
207#ifdef GEO_DEBUG
208 return const_index_ptr_in_array(ptr_ + i, begin_, end_);
209#else
210 return const_index_ptr_in_array(ptr_ + i, nullptr, nullptr);
211#endif
212 }
213
214 private:
215 const index_t* ptr_;
216#ifdef GEO_DEBUG
217 const index_t* begin_;
218 const index_t* end_;
219#endif
220 };
221
222 /*************************************************************************/
223
229 public:
231 index_t* ptr, index_t* begin, index_t* end
232 ) : ptr_(ptr)
233#ifdef GEO_DEBUG
234 ,begin_(begin),
235 end_(end)
236#endif
237 {
238 geo_argused(begin);
239 geo_argused(end);
240 }
241
242 void operator++() {
243 ++ptr_;
244 }
245
246 bool operator==(index_ptr_in_array rhs) const {
247 return ptr_ == rhs.ptr_;
248 }
249
250 bool operator!=(index_ptr_in_array rhs) const {
251 return ptr_ != rhs.ptr_;
252 }
253
254 bool operator<(index_ptr_in_array rhs) const {
255 return ptr_ < rhs.ptr_;
256 }
257
258 const index_t& operator*() const {
259 geo_debug_assert(ptr_ >= begin_ && ptr_ < end_);
260 return *ptr_;
261 }
262
263 index_t& operator*() {
264 geo_debug_assert(ptr_ >= begin_ && ptr_ < end_);
265 return *ptr_;
266 }
267
268 index_t operator-(index_ptr_in_array it) const {
269 return index_t(ptr_ - it.ptr_);
270 }
271
272 index_ptr_in_array operator+(index_t i) const {
273#ifdef GEO_DEBUG
274 return index_ptr_in_array(ptr_ + i, begin_, end_);
275#else
276 return index_ptr_in_array(ptr_ + i, nullptr, nullptr);
277#endif
278 }
279
280 operator const_index_ptr_in_array() const {
281#ifdef GEO_DEBUG
282 return const_index_ptr_in_array(ptr_, begin_, end_);
283#else
284 return const_index_ptr_in_array(ptr_, nullptr, nullptr);
285#endif
286 }
287
288 private:
289 index_t* ptr_;
290#ifdef GEO_DEBUG
291 index_t* begin_;
292 index_t* end_;
293#endif
294 };
295
296 /*************************************************************************/
297
299 public:
302
303 index_ptr_range(index_t* begin, index_t* end) :
304 begin_(begin, begin, end),
305 end_(end,begin,end) {
306 }
307
309 begin_(V.data()+b, V.data() + b, V.data() + e),
310 end_(V.data()+e, V.data() + b, V.data() + e) {
311 }
312
313 iterator begin() {
314 return begin_;
315 }
316
317 iterator end() {
318 return end_;
319 }
320
321 const_iterator begin() const {
322 return begin_;
323 }
324
325 const_iterator end() const {
326 return end_;
327 }
328
329 private:
330 iterator begin_;
331 iterator end_;
332 };
333
334 /*************************************************************************/
335
337 public:
340
341 const_index_ptr_range(const index_t* begin, const index_t* end) :
342 begin_(begin, begin, end),
343 end_(end,begin,end) {
344 }
345
347 begin_(V.data()+b, V.data() + b, V.data() + e),
348 end_(V.data()+e, V.data() + b, V.data() + e) {
349 }
350
351 const_iterator begin() const {
352 return begin_;
353 }
354
355 const_iterator end() const {
356 return end_;
357 }
358
359 private:
360 const_iterator begin_;
361 const_iterator end_;
362 };
363
364
365 /***********************************************************************/
366
367 #ifndef GOMGEN
368
376 template<class IT, typename XFORM> class transformed_iterator {
377 public:
379
380 transformed_iterator(const IT& it, XFORM xform) :
381 wrapped_(it), xform_(xform) {
382 }
383
384 auto operator*() const {
385 return xform_(*wrapped_);
386 }
387
388 bool operator==(const thisclass& rhs) const {
389 return wrapped_ == rhs.wrapped_;
390 }
391
392 bool operator!=(const thisclass& rhs) const {
393 return wrapped_ != rhs.wrapped_;
394 }
395
396 bool operator<(const thisclass& rhs) const {
397 return wrapped_ < rhs.wrapped_;
398 }
399
400 void operator++() {
401 ++wrapped_;
402 }
403
404 private:
405 IT wrapped_;
406 XFORM xform_;
407 };
408
416 template<class IT, typename XFORM> class transformed_iterator_ref {
417 public:
419
420 transformed_iterator_ref(const IT& it, XFORM xform) :
421 wrapped_(it), xform_(xform) {
422 }
423
424 auto& operator*() {
425 return xform_(*wrapped_);
426 }
427
428 const auto& operator*() const {
429 return xform_(*wrapped_);
430 }
431
432 bool operator==(const thisclass& rhs) const {
433 return wrapped_ == rhs.wrapped_;
434 }
435
436 bool operator!=(const thisclass& rhs) const {
437 return wrapped_ != rhs.wrapped_;
438 }
439
440 bool operator<(const thisclass& rhs) const {
441 return wrapped_ < rhs.wrapped_;
442 }
443
444 void operator++() {
445 ++wrapped_;
446 }
447
448 private:
449 IT wrapped_;
450 XFORM xform_;
451 };
452
453 /***********************************************************************/
454
461 template<class RANGE, typename XFORM> class transformed_range {
462 public:
464
465 typedef transformed_iterator<
466 typename RANGE::const_iterator, XFORM
468
469 transformed_range(const RANGE& range, XFORM xform) :
470 wrapped_(range), xform_(xform) {
471 }
472
473 iterator begin() {
474 return iterator(wrapped_.begin(), xform_);
475 }
476
477 iterator end() {
478 return iterator(wrapped_.end(), xform_);
479 }
480
481 const_iterator begin() const {
482 return const_iterator(wrapped_.begin(), xform_);
483 }
484
485 const_iterator end() const {
486 return const_iterator(wrapped_.end(), xform_);
487 }
488
489 private:
490 RANGE wrapped_;
491 XFORM xform_;
492 };
493
494 /***********************************************************************/
495
504 template<class RANGE, typename XFORM> class transformed_range_ref {
505 public:
507 typename RANGE::iterator, XFORM
508 > iterator;
509
511 typename RANGE::const_iterator, XFORM
513
514 transformed_range_ref(const RANGE& range, XFORM xform) :
515 wrapped_(range), xform_(xform) {
516 }
517
518 iterator begin() {
519 return iterator(wrapped_.begin(), xform_);
520 }
521
522 iterator end() {
523 return iterator(wrapped_.end(), xform_);
524 }
525
526 const_iterator begin() const {
527 return const_iterator(wrapped_.begin(), xform_);
528 }
529
530 const_iterator end() const {
531 return const_iterator(wrapped_.end(), xform_);
532 }
533
534 private:
535 RANGE wrapped_;
536 XFORM xform_;
537 };
538
539 /***********************************************************************/
540
551 template <class RANGE, typename XFORM> inline auto
552 transform_range(const RANGE& range, XFORM xform) {
553 return transformed_range<RANGE,XFORM>(range, xform);
554 }
555
556 /***********************************************************************/
557
568 template <class RANGE, typename XFORM> inline auto
569 transform_range_ref(const RANGE& range, XFORM xform) {
570 return transformed_range_ref<RANGE,XFORM>(range, xform);
571 }
572
573 /***********************************************************************/
574
575 #endif
576}
577
578#endif
A function to suppress unused parameters compilation warnings.
Assertion checking mechanism.
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:196
Encapsulates a const pointer to an element in an index_t array.
Definition range.h:167
Wraps an integer to be used with the range-based for construct.
Definition range.h:66
Encapsulates a pointer to an element in an index_t array.
Definition range.h:228
A generic index_range bounded by two "non-iterators".
Definition range.h:106
index_t nb() const
gets the number of elements in the index_range
Definition range.h:142
index_as_iterator end() const
gets one position past the last index
Definition range.h:135
index_range(index_as_iterator b, index_as_iterator e)
index_range constructor
Definition range.h:117
index_t operator[](index_t i) const
direct access to an arbitrary element in the index_range
Definition range.h:151
index_as_iterator begin() const
gets the first index
Definition range.h:126
An iterator that applies a user-defined function when deferenced.
Definition range.h:416
An iterator that applies a user-defined function when deferenced.
Definition range.h:376
A range composed with a user-defined function.
Definition range.h:504
A range composed with a user-defined function.
Definition range.h:461
Vector with aligned memory allocation.
Definition memory.h:660
T * data()
Gets a pointer to the array of elements.
Definition memory.h:806
Types and functions for memory manipulation.
Global Vorpaline namespace.
Definition basic.h:55
auto transform_range_ref(const RANGE &range, XFORM xform)
Creates a range that applies a user-defined function to each element when accessed.
Definition range.h:569
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
auto transform_range(const RANGE &range, XFORM xform)
Creates a range that applies a user-defined function to each element when accessed.
Definition range.h:552
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:329
Types and functions for numbers manipulation.