Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
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 
43 #include <geogram/basic/numeric.h>
44 #include <geogram/basic/memory.h>
45 #include <geogram/basic/assert.h>
46 #include <geogram/basic/argused.h>
47 
55 namespace 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) {
76  return val_ == rhs.val_;
77  }
78 
79  bool operator!=(index_as_iterator rhs) {
80  return val_ != rhs.val_;
81  }
82 
83  bool operator<(index_as_iterator rhs) {
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 
106  class index_range {
107  public:
108 
109  typedef index_as_iterator iterator;
111 
117  index_range(index_as_iterator b, index_as_iterator e) : begin_(b), end_(e) {
118  }
119 
125  return begin_;
126  }
127 
134  return end_;
135  }
136 
140  index_t nb() const {
141  return end_ - begin_;
142  }
143 
150  geo_debug_assert(i < nb());
151  return *(begin_ + i);
152  }
153 
154  private:
155  index_as_iterator begin_;
156  index_as_iterator end_;
157  };
158 
159  /*************************************************************************/
160 
166  public:
168  const index_t* ptr, const index_t* begin, const index_t* end
169  ) : ptr_(ptr)
170 #ifdef GEO_DEBUG
171  ,begin_(begin),
172  end_(end)
173 #endif
174  {
175  geo_argused(begin);
176  geo_argused(end);
177  }
178 
179  void operator++() {
180  ++ptr_;
181  }
182 
183  bool operator==(const_index_ptr_in_array rhs) {
184  return ptr_ == rhs.ptr_;
185  }
186 
187  bool operator!=(const_index_ptr_in_array rhs) {
188  return ptr_ != rhs.ptr_;
189  }
190 
191  bool operator<(const_index_ptr_in_array rhs) {
192  return ptr_ < rhs.ptr_;
193  }
194 
195  const index_t& operator*() const {
196  geo_debug_assert(ptr_ >= begin_ && ptr_ < end_);
197  return *ptr_;
198  }
199 
200  index_t operator-(const_index_ptr_in_array it) const {
201  return index_t(ptr_ - it.ptr_);
202  }
203 
204  const_index_ptr_in_array operator+(index_t i) const {
205 #ifdef GEO_DEBUG
206  return const_index_ptr_in_array(ptr_ + i, begin_, end_);
207 #else
208  return const_index_ptr_in_array(ptr_ + i, nullptr, nullptr);
209 #endif
210  }
211 
212  private:
213  const index_t* ptr_;
214 #ifdef GEO_DEBUG
215  const index_t* begin_;
216  const index_t* end_;
217 #endif
218  };
219 
220  /*************************************************************************/
221 
227  public:
229  index_t* ptr, index_t* begin, index_t* end
230  ) : ptr_(ptr)
231 #ifdef GEO_DEBUG
232  ,begin_(begin),
233  end_(end)
234 #endif
235  {
236  geo_argused(begin);
237  geo_argused(end);
238  }
239 
240  void operator++() {
241  ++ptr_;
242  }
243 
244  bool operator==(index_ptr_in_array rhs) {
245  return ptr_ == rhs.ptr_;
246  }
247 
248  bool operator!=(index_ptr_in_array rhs) {
249  return ptr_ != rhs.ptr_;
250  }
251 
252  bool operator<(index_ptr_in_array rhs) {
253  return ptr_ < rhs.ptr_;
254  }
255 
256  const index_t& operator*() const {
257  geo_debug_assert(ptr_ >= begin_ && ptr_ < end_);
258  return *ptr_;
259  }
260 
261  index_t& operator*() {
262  geo_debug_assert(ptr_ >= begin_ && ptr_ < end_);
263  return *ptr_;
264  }
265 
266  index_t operator-(index_ptr_in_array it) const {
267  return index_t(ptr_ - it.ptr_);
268  }
269 
270  index_ptr_in_array operator+(index_t i) const {
271 #ifdef GEO_DEBUG
272  return index_ptr_in_array(ptr_ + i, begin_, end_);
273 #else
274  return index_ptr_in_array(ptr_ + i, nullptr, nullptr);
275 #endif
276  }
277 
278  operator const_index_ptr_in_array() const {
279 #ifdef GEO_DEBUG
280  return const_index_ptr_in_array(ptr_, begin_, end_);
281 #else
282  return const_index_ptr_in_array(ptr_, nullptr, nullptr);
283 #endif
284  }
285 
286  private:
287  index_t* ptr_;
288 #ifdef GEO_DEBUG
289  index_t* begin_;
290  index_t* end_;
291 #endif
292  };
293 
294  /*************************************************************************/
295 
297  public:
300 
301  index_ptr_range(index_t* begin, index_t* end) :
302  begin_(begin, begin, end),
303  end_(end,begin,end) {
304  }
305 
307  begin_(V.data()+b, V.data() + b, V.data() + e),
308  end_(V.data()+e, V.data() + b, V.data() + e) {
309  }
310 
311  iterator begin() {
312  return begin_;
313  }
314 
315  iterator end() {
316  return end_;
317  }
318 
319  const_iterator begin() const {
320  return begin_;
321  }
322 
323  const_iterator end() const {
324  return end_;
325  }
326 
327  private:
328  iterator begin_;
329  iterator end_;
330  };
331 
332  /*************************************************************************/
333 
335  public:
338 
339  const_index_ptr_range(const index_t* begin, const index_t* end) :
340  begin_(begin, begin, end),
341  end_(end,begin,end) {
342  }
343 
345  begin_(V.data()+b, V.data() + b, V.data() + e),
346  end_(V.data()+e, V.data() + b, V.data() + e) {
347  }
348 
349  const_iterator begin() const {
350  return begin_;
351  }
352 
353  const_iterator end() const {
354  return end_;
355  }
356 
357  private:
358  const_iterator begin_;
359  const_iterator end_;
360  };
361 
362  /***********************************************************************/
363 
364 }
365 
366 #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:165
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:226
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:140
index_as_iterator end() const
gets one position past the last index
Definition: range.h:133
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:149
index_as_iterator begin() const
gets the first index
Definition: range.h:124
T * data()
Gets a pointer to the array of elements.
Definition: memory.h:774
Types and functions for memory manipulation.
Global Vorpaline namespace.
Definition: basic.h:55
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition: argused.h:60
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Types and functions for numbers manipulation.