diff options
author | Hans Goudey <h.goudey@me.com> | 2022-03-03 19:14:44 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-03-03 19:14:44 +0300 |
commit | d793d184137afb786a4777ce9e995b569e82f5a8 (patch) | |
tree | 57b6fd23127d0154ae20c812d365ef5e83bd48f6 /source/blender/blenlib/BLI_index_range.hh | |
parent | e8dc82311d1a7012adaacc93bcc5e92c0fd7adbf (diff) |
BLI: Add functions to IndexRange to mirror Span
Adds functions for special cases of slicing and an `is_empty` method.
Diffstat (limited to 'source/blender/blenlib/BLI_index_range.hh')
-rw-r--r-- | source/blender/blenlib/BLI_index_range.hh | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_index_range.hh b/source/blender/blenlib/BLI_index_range.hh index 25edba8c631..832e438fa75 100644 --- a/source/blender/blenlib/BLI_index_range.hh +++ b/source/blender/blenlib/BLI_index_range.hh @@ -149,6 +149,14 @@ class IndexRange { } /** + * Returns true if the size is zero. + */ + constexpr bool is_empty() const + { + return size_ == 0; + } + + /** * Create a new range starting at the end of the current one. */ constexpr IndexRange after(int64_t n) const @@ -227,6 +235,50 @@ class IndexRange { } /** + * Returns a new IndexRange with n elements removed from the beginning. This invokes undefined + * behavior when n is negative. + */ + constexpr IndexRange drop_front(int64_t n) const + { + BLI_assert(n >= 0); + const int64_t new_size = std::max<int64_t>(0, size_ - n); + return IndexRange(start_ + n, new_size); + } + + /** + * Returns a new IndexRange with n elements removed from the beginning. This invokes undefined + * behavior when n is negative. + */ + constexpr IndexRange drop_back(int64_t n) const + { + BLI_assert(n >= 0); + const int64_t new_size = std::max<int64_t>(0, size_ - n); + return IndexRange(start_, new_size); + } + + /** + * Returns a new IndexRange that only contains the first n elements. This invokes undefined + * behavior when n is negative. + */ + constexpr IndexRange take_front(int64_t n) const + { + BLI_assert(n >= 0); + const int64_t new_size = std::min<int64_t>(size_, n); + return IndexRange(start_, new_size); + } + + /** + * Returns a new IndexRange that only contains the last n elements. This invokes undefined + * behavior when n is negative. + */ + constexpr IndexRange take_back(int64_t n) const + { + BLI_assert(n >= 0); + const int64_t new_size = std::min<int64_t>(size_, n); + return IndexRange(start_ + size_ - new_size, new_size); + } + + /** * Get read-only access to a memory buffer that contains the range as actual numbers. */ Span<int64_t> as_span() const; |