Blender  V2.93
BLI_index_range.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
55 #include <algorithm>
56 #include <cmath>
57 #include <iostream>
58 
59 #include "BLI_utildefines.h"
60 
61 namespace blender {
62 
63 template<typename T> class Span;
64 
65 class IndexRange {
66  private:
67  int64_t start_ = 0;
68  int64_t size_ = 0;
69 
70  public:
71  constexpr IndexRange() = default;
72 
73  constexpr explicit IndexRange(int64_t size) : start_(0), size_(size)
74  {
75  BLI_assert(size >= 0);
76  }
77 
78  constexpr IndexRange(int64_t start, int64_t size) : start_(start), size_(size)
79  {
80  BLI_assert(start >= 0);
81  BLI_assert(size >= 0);
82  }
83 
84  class Iterator {
85  public:
86  using iterator_category = std::forward_iterator_tag;
88  using pointer = const int64_t *;
89  using reference = const int64_t &;
90  using difference_type = std::ptrdiff_t;
91 
92  private:
93  int64_t current_;
94 
95  public:
96  constexpr explicit Iterator(int64_t current) : current_(current)
97  {
98  }
99 
100  constexpr Iterator &operator++()
101  {
102  current_++;
103  return *this;
104  }
105 
106  constexpr Iterator operator++(int) const
107  {
108  Iterator copied_iterator = *this;
109  ++copied_iterator;
110  return copied_iterator;
111  }
112 
113  constexpr friend bool operator!=(const Iterator &a, const Iterator &b)
114  {
115  return a.current_ != b.current_;
116  }
117 
118  constexpr friend bool operator==(const Iterator &a, const Iterator &b)
119  {
120  return a.current_ == b.current_;
121  }
122 
123  constexpr int64_t operator*() const
124  {
125  return current_;
126  }
127  };
128 
129  constexpr Iterator begin() const
130  {
131  return Iterator(start_);
132  }
133 
134  constexpr Iterator end() const
135  {
136  return Iterator(start_ + size_);
137  }
138 
142  constexpr int64_t operator[](int64_t index) const
143  {
144  BLI_assert(index >= 0);
145  BLI_assert(index < this->size());
146  return start_ + index;
147  }
148 
152  constexpr friend bool operator==(IndexRange a, IndexRange b)
153  {
154  return (a.size_ == b.size_) && (a.start_ == b.start_ || a.size_ == 0);
155  }
156 
160  constexpr int64_t size() const
161  {
162  return size_;
163  }
164 
168  constexpr IndexRange after(int64_t n) const
169  {
170  BLI_assert(n >= 0);
171  return IndexRange(start_ + size_, n);
172  }
173 
177  constexpr IndexRange before(int64_t n) const
178  {
179  BLI_assert(n >= 0);
180  return IndexRange(start_ - n, n);
181  }
182 
187  constexpr int64_t first() const
188  {
189  BLI_assert(this->size() > 0);
190  return start_;
191  }
192 
197  constexpr int64_t last() const
198  {
199  BLI_assert(this->size() > 0);
200  return start_ + size_ - 1;
201  }
202 
206  constexpr int64_t one_after_last() const
207  {
208  return start_ + size_;
209  }
210 
214  constexpr int64_t start() const
215  {
216  return start_;
217  }
218 
222  constexpr bool contains(int64_t value) const
223  {
224  return value >= start_ && value < start_ + size_;
225  }
226 
231  {
232  BLI_assert(start >= 0);
233  BLI_assert(size >= 0);
234  int64_t new_start = start_ + start;
235  BLI_assert(new_start + size <= start_ + size_ || size == 0);
236  return IndexRange(new_start, size);
237  }
238  constexpr IndexRange slice(IndexRange range) const
239  {
240  return this->slice(range.start(), range.size());
241  }
242 
246  Span<int64_t> as_span() const;
247 
248  friend std::ostream &operator<<(std::ostream &stream, IndexRange range)
249  {
250  stream << "[" << range.start() << ", " << range.one_after_last() << ")";
251  return stream;
252  }
253 };
254 
255 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
constexpr friend bool operator!=(const Iterator &a, const Iterator &b)
constexpr Iterator(int64_t current)
std::forward_iterator_tag iterator_category
constexpr int64_t operator*() const
constexpr friend bool operator==(const Iterator &a, const Iterator &b)
constexpr Iterator operator++(int) const
constexpr Iterator & operator++()
constexpr int64_t first() const
constexpr int64_t one_after_last() const
constexpr IndexRange slice(IndexRange range) const
constexpr friend bool operator==(IndexRange a, IndexRange b)
constexpr int64_t operator[](int64_t index) const
Span< int64_t > as_span() const
constexpr Iterator end() const
friend std::ostream & operator<<(std::ostream &stream, IndexRange range)
constexpr IndexRange(int64_t start, int64_t size)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr IndexRange after(int64_t n) const
constexpr int64_t start() const
constexpr IndexRange before(int64_t n) const
constexpr Iterator begin() const
constexpr IndexRange(int64_t size)
constexpr IndexRange slice(int64_t start, int64_t size) const
constexpr bool contains(int64_t value) const
constexpr int64_t last() const
static unsigned a[3]
Definition: RandGen.cpp:92
__int64 int64_t
Definition: stdint.h:92