Blender V5.3
BLI_index_range.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
43
44#include <algorithm>
45#include <iosfwd>
46
47#include "BLI_assert.hh"
49
50namespace blender {
51
52template<typename T> class Span;
53
55 private:
56 int64_t start_ = 0;
57 int64_t size_ = 0;
58
59 public:
60 constexpr IndexRange() = default;
61
62 constexpr explicit IndexRange(int64_t size) : size_(size)
63 {
64 BLI_assert(size >= 0);
65 }
66
67 constexpr IndexRange(const int64_t start, const int64_t size) : start_(start), size_(size)
68 {
69 BLI_assert(start >= 0);
70 BLI_assert(size >= 0);
71 }
72
73 constexpr static IndexRange from_begin_size(const int64_t begin, const int64_t size)
74 {
75 return IndexRange(begin, size);
76 }
77
78 constexpr static IndexRange from_begin_end(const int64_t begin, const int64_t end)
79 {
80 return IndexRange(begin, end - begin);
81 }
82
84 {
85 return IndexRange(begin, last - begin + 1);
86 }
87
88 constexpr static IndexRange from_end_size(const int64_t end, const int64_t size)
89 {
90 return IndexRange(end - size, size);
91 }
92
93 constexpr static IndexRange from_single(const int64_t index)
94 {
95 return IndexRange(index, 1);
96 }
97
99 public:
101 using pointer = const int64_t *;
103
104 private:
105 int64_t current_;
106
107 public:
108 constexpr explicit Iterator(int64_t current) : current_(current) {}
109
110 constexpr int64_t operator*() const
111 {
112 return current_;
113 }
114
115 const int64_t &iter_prop() const
116 {
117 return current_;
118 }
119 };
120
121 constexpr Iterator begin() const
122 {
123 return Iterator(start_);
124 }
125
126 constexpr Iterator end() const
127 {
128 return Iterator(start_ + size_);
129 }
130
135 {
136 BLI_assert(index >= 0);
137 BLI_assert(index < this->size());
138 return start_ + index;
139 }
140
144 constexpr friend bool operator==(IndexRange a, IndexRange b)
145 {
146 return (a.size_ == b.size_) && (a.start_ == b.start_ || a.size_ == 0);
147 }
148 constexpr friend bool operator!=(IndexRange a, IndexRange b)
149 {
150 return !(a == b);
151 }
152
156 constexpr int64_t size() const
157 {
158 return size_;
159 }
160
161 constexpr IndexRange index_range() const
162 {
163 return IndexRange(size_);
164 }
165
169 constexpr bool is_empty() const
170 {
171 return size_ == 0;
172 }
173
177 constexpr IndexRange with_new_end(const int64_t new_end) const
178 {
179 return IndexRange::from_begin_end(start_, new_end);
180 }
181
185 constexpr IndexRange after(int64_t n) const
186 {
187 BLI_assert(n >= 0);
188 return IndexRange(start_ + size_, n);
189 }
190
194 constexpr IndexRange before(int64_t n) const
195 {
196 BLI_assert(n >= 0);
197 return IndexRange(start_ - n, n);
198 }
199
204 constexpr int64_t first() const
205 {
206 BLI_assert(this->size() > 0);
207 return start_;
208 }
209
214 constexpr int64_t last(const int64_t n = 0) const
215 {
216 BLI_assert(n >= 0);
217 BLI_assert(n < size_);
218 BLI_assert(this->size() > 0);
219 return start_ + size_ - 1 - n;
220 }
221
226 constexpr int64_t one_before_start() const
227 {
228 BLI_assert(start_ > 0);
229 return start_ - 1;
230 }
231
235 constexpr int64_t one_after_last() const
236 {
237 return start_ + size_;
238 }
239
243 constexpr int64_t start() const
244 {
245 return start_;
246 }
247
251 constexpr bool contains(int64_t value) const
252 {
253 return value >= start_ && value < start_ + size_;
254 }
255
259 constexpr bool contains(const IndexRange range) const
260 {
261 if (range.is_empty()) {
262 return true;
263 }
264 if (range.start_ < start_) {
265 return false;
266 }
267 if (range.start_ + range.size_ > start_ + size_) {
268 return false;
269 }
270 return true;
271 }
272
277 {
278 BLI_assert(start >= 0);
279 BLI_assert(size >= 0);
280 int64_t new_start = start_ + start;
281 BLI_assert(new_start + size <= start_ + size_ || size == 0);
282 return IndexRange(new_start, size);
283 }
284 constexpr IndexRange slice(IndexRange range) const
285 {
286 return this->slice(range.start(), range.size());
287 }
288
294 constexpr IndexRange intersect(IndexRange other) const
295 {
296 const int64_t old_end = start_ + size_;
297 const int64_t new_start = std::min(old_end, std::max(start_, other.start_));
298 const int64_t new_end = std::max(new_start, std::min(old_end, other.start_ + other.size_));
299 return IndexRange(new_start, new_end - new_start);
300 }
301
306 constexpr IndexRange drop_front(int64_t n) const
307 {
308 BLI_assert(n >= 0);
309 const int64_t new_size = std::max<int64_t>(0, size_ - n);
310 return IndexRange(start_ + n, new_size);
311 }
312
317 constexpr IndexRange drop_back(int64_t n) const
318 {
319 BLI_assert(n >= 0);
320 const int64_t new_size = std::max<int64_t>(0, size_ - n);
321 return IndexRange(start_, new_size);
322 }
323
328 constexpr IndexRange take_front(int64_t n) const
329 {
330 BLI_assert(n >= 0);
331 const int64_t new_size = std::min<int64_t>(size_, n);
332 return IndexRange(start_, new_size);
333 }
334
339 constexpr IndexRange take_back(int64_t n) const
340 {
341 BLI_assert(n >= 0);
342 const int64_t new_size = std::min<int64_t>(size_, n);
343 return IndexRange(start_ + size_ - new_size, new_size);
344 }
345
350 constexpr IndexRange shift(int64_t n) const
351 {
352 return IndexRange(start_ + n, size_);
353 }
354
355 friend std::ostream &operator<<(std::ostream &stream, IndexRange range);
356};
357
363
372
373} // namespace blender
constexpr Iterator(int64_t current)
const int64_t & iter_prop() const
constexpr int64_t operator*() const
constexpr int64_t one_before_start() const
constexpr int64_t first() const
constexpr friend bool operator!=(IndexRange a, IndexRange b)
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
constexpr IndexRange shift(int64_t n) const
constexpr Iterator end() const
constexpr IndexRange intersect(IndexRange other) const
constexpr IndexRange drop_back(int64_t n) const
constexpr IndexRange(const int64_t start, const int64_t size)
static constexpr IndexRange from_end_size(const int64_t end, const int64_t size)
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr bool is_empty() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange with_new_end(const int64_t new_end) const
constexpr IndexRange()=default
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr IndexRange after(int64_t n) const
constexpr int64_t start() const
constexpr bool contains(const IndexRange range) const
constexpr IndexRange before(int64_t n) const
constexpr IndexRange take_back(int64_t n) const
constexpr Iterator begin() const
static constexpr IndexRange from_single(const int64_t index)
constexpr IndexRange(int64_t size)
constexpr IndexRange slice(int64_t start, int64_t size) const
constexpr bool contains(int64_t value) const
constexpr IndexRange index_range() const
constexpr IndexRange take_front(int64_t n) const
static constexpr IndexRange from_begin_end_inclusive(const int64_t begin, const int64_t last)
constexpr IndexRange drop_front(int64_t n) const
std::ostream & operator<<(std::ostream &stream, const AssetCatalogPath &path_to_append)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
AlignedIndexRanges split_index_range_by_alignment(const IndexRange range, const int64_t alignment)