diff options
Diffstat (limited to 'source/blender/blenlib/BLI_index_range.hh')
-rw-r--r-- | source/blender/blenlib/BLI_index_range.hh | 100 |
1 files changed, 54 insertions, 46 deletions
diff --git a/source/blender/blenlib/BLI_index_range.hh b/source/blender/blenlib/BLI_index_range.hh index 25192429a5d..7c813f58b2c 100644 --- a/source/blender/blenlib/BLI_index_range.hh +++ b/source/blender/blenlib/BLI_index_range.hh @@ -27,29 +27,29 @@ * I'd argue that the second loop is more readable and less error prone than the first one. That is * not necessarily always the case, but often it is. * - * for (uint i = 0; i < 10; i++) { - * for (uint j = 0; j < 20; j++) { - * for (uint k = 0; k < 30; k++) { + * for (int64_t i = 0; i < 10; i++) { + * for (int64_t j = 0; j < 20; j++) { + * for (int64_t k = 0; k < 30; k++) { * - * for (uint i : IndexRange(10)) { - * for (uint j : IndexRange(20)) { - * for (uint k : IndexRange(30)) { + * for (int64_t i : IndexRange(10)) { + * for (int64_t j : IndexRange(20)) { + * for (int64_t k : IndexRange(30)) { * * Some containers like blender::Vector have an index_range() method. This will return the * IndexRange that contains all indices that can be used to access the container. This is * particularly useful when you want to iterate over the indices and the elements (much like * Python's enumerate(), just worse). Again, I think the second example here is better: * - * for (uint i = 0; i < my_vector_with_a_long_name.size(); i++) { + * for (int64_t i = 0; i < my_vector_with_a_long_name.size(); i++) { * do_something(i, my_vector_with_a_long_name[i]); * - * for (uint i : my_vector_with_a_long_name.index_range()) { + * for (int64_t i : my_vector_with_a_long_name.index_range()) { * do_something(i, my_vector_with_a_long_name[i]); * * Ideally this could be could be even closer to Python's enumerate(). We might get that in the * future with newer C++ versions. * - * One other important feature is the as_span method. This method returns an Span<uint> + * One other important feature is the as_span method. This method returns an Span<int64_t> * that contains the interval as individual numbers. */ @@ -70,68 +70,72 @@ template<typename T> class Span; class IndexRange { private: - uint m_start = 0; - uint m_size = 0; + int64_t start_ = 0; + int64_t size_ = 0; public: IndexRange() = default; - explicit IndexRange(uint size) : m_start(0), m_size(size) + explicit IndexRange(int64_t size) : start_(0), size_(size) { + BLI_assert(size >= 0); } - IndexRange(uint start, uint size) : m_start(start), m_size(size) + IndexRange(int64_t start, int64_t size) : start_(start), size_(size) { + BLI_assert(start >= 0); + BLI_assert(size >= 0); } template<typename T> - IndexRange(const tbb::blocked_range<T> &range) : m_start(range.begin()), m_size(range.size()) + IndexRange(const tbb::blocked_range<T> &range) : start_(range.begin()), size_(range.size()) { } class Iterator { private: - uint m_current; + int64_t current_; public: - Iterator(uint current) : m_current(current) + Iterator(int64_t current) : current_(current) { } Iterator &operator++() { - m_current++; + current_++; return *this; } bool operator!=(const Iterator &iterator) const { - return m_current != iterator.m_current; + return current_ != iterator.current_; } - uint operator*() const + int64_t operator*() const { - return m_current; + return current_; } }; Iterator begin() const { - return Iterator(m_start); + return Iterator(start_); } Iterator end() const { - return Iterator(m_start + m_size); + return Iterator(start_ + size_); } /** * Access an element in the range. */ - uint operator[](uint index) const + int64_t operator[](int64_t index) const { + BLI_assert(index >= 0); BLI_assert(index < this->size()); - return m_start + index; + return start_ + index; } /** @@ -139,84 +143,88 @@ class IndexRange { */ friend bool operator==(IndexRange a, IndexRange b) { - return (a.m_size == b.m_size) && (a.m_start == b.m_start || a.m_size == 0); + return (a.size_ == b.size_) && (a.start_ == b.start_ || a.size_ == 0); } /** * Get the amount of numbers in the range. */ - uint size() const + int64_t size() const { - return m_size; + return size_; } /** * Create a new range starting at the end of the current one. */ - IndexRange after(uint n) const + IndexRange after(int64_t n) const { - return IndexRange(m_start + m_size, n); + BLI_assert(n >= 0); + return IndexRange(start_ + size_, n); } /** * Create a new range that ends at the start of the current one. */ - IndexRange before(uint n) const + IndexRange before(int64_t n) const { - return IndexRange(m_start - n, n); + BLI_assert(n >= 0); + return IndexRange(start_ - n, n); } /** * Get the first element in the range. * Asserts when the range is empty. */ - uint first() const + int64_t first() const { BLI_assert(this->size() > 0); - return m_start; + return start_; } /** * Get the last element in the range. * Asserts when the range is empty. */ - uint last() const + int64_t last() const { BLI_assert(this->size() > 0); - return m_start + m_size - 1; + return start_ + size_ - 1; } /** * Get the element one after the end. The returned value is undefined when the range is empty. */ - uint one_after_last() const + int64_t one_after_last() const { - return m_start + m_size; + return start_ + size_; } /** * Get the first element in the range. The returned value is undefined when the range is empty. */ - uint start() const + int64_t start() const { - return m_start; + return start_; } /** * Returns true when the range contains a certain number, otherwise false. */ - bool contains(uint value) const + bool contains(int64_t value) const { - return value >= m_start && value < m_start + m_size; + return value >= start_ && value < start_ + size_; } /** - * Returns a new range, that contains a subinterval of the current one. + * Returns a new range, that contains a sub-interval of the current one. */ - IndexRange slice(uint start, uint size) const + IndexRange slice(int64_t start, int64_t size) const { - uint new_start = m_start + start; - BLI_assert(new_start + size <= m_start + m_size || size == 0); + BLI_assert(start >= 0); + BLI_assert(size >= 0); + int64_t new_start = start_ + start; + BLI_assert(new_start + size <= start_ + size_ || size == 0); return IndexRange(new_start, size); } IndexRange slice(IndexRange range) const @@ -227,7 +235,7 @@ class IndexRange { /** * Get read-only access to a memory buffer that contains the range as actual numbers. */ - Span<uint> as_span() const; + Span<int64_t> as_span() const; friend std::ostream &operator<<(std::ostream &stream, IndexRange range) { |