diff options
Diffstat (limited to 'source/blender/blenlib/BLI_span.hh')
-rw-r--r-- | source/blender/blenlib/BLI_span.hh | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh index 57ef9ce9eb6..67bb32304de 100644 --- a/source/blender/blenlib/BLI_span.hh +++ b/source/blender/blenlib/BLI_span.hh @@ -88,7 +88,7 @@ namespace blender { template<typename T> class Span { private: const T *start_ = nullptr; - uint size_ = 0; + int64_t size_ = 0; public: /** @@ -96,13 +96,15 @@ template<typename T> class Span { */ Span() = default; - Span(const T *start, uint size) : start_(start), size_(size) + Span(const T *start, int64_t size) : start_(start), size_(size) { + BLI_assert(size >= 0); } template<typename U, typename std::enable_if_t<is_convertible_pointer_v<U, T>> * = nullptr> - Span(const U *start, uint size) : start_((const T *)start), size_(size) + Span(const U *start, int64_t size) : start_((const T *)start), size_(size) { + BLI_assert(size >= 0); } /** @@ -116,11 +118,11 @@ template<typename T> class Span { * Span<int> span = {1, 2, 3, 4}; * call_function_with_array(span); */ - Span(const std::initializer_list<T> &list) : Span(list.begin(), (uint)list.size()) + Span(const std::initializer_list<T> &list) : Span(list.begin(), (int64_t)list.size()) { } - Span(const std::vector<T> &vector) : Span(vector.data(), (uint)vector.size()) + Span(const std::vector<T> &vector) : Span(vector.data(), (int64_t)vector.size()) { } @@ -142,8 +144,10 @@ template<typename T> class Span { * Returns a contiguous part of the array. This invokes undefined behavior when the slice does * not stay within the bounds of the array. */ - Span slice(uint start, uint size) const + Span slice(int64_t start, int64_t size) const { + BLI_assert(start >= 0); + BLI_assert(size >= 0); BLI_assert(start + size <= this->size() || size == 0); return Span(start_ + start, size); } @@ -157,8 +161,9 @@ template<typename T> class Span { * Returns a new Span with n elements removed from the beginning. This invokes undefined * behavior when the array is too small. */ - Span drop_front(uint n) const + Span drop_front(int64_t n) const { + BLI_assert(n >= 0); BLI_assert(n <= this->size()); return this->slice(n, this->size() - n); } @@ -167,8 +172,9 @@ template<typename T> class Span { * Returns a new Span with n elements removed from the beginning. This invokes undefined * behavior when the array is too small. */ - Span drop_back(uint n) const + Span drop_back(int64_t n) const { + BLI_assert(n >= 0); BLI_assert(n <= this->size()); return this->slice(0, this->size() - n); } @@ -177,8 +183,9 @@ template<typename T> class Span { * Returns a new Span that only contains the first n elements. This invokes undefined * behavior when the array is too small. */ - Span take_front(uint n) const + Span take_front(int64_t n) const { + BLI_assert(n >= 0); BLI_assert(n <= this->size()); return this->slice(0, n); } @@ -187,8 +194,9 @@ template<typename T> class Span { * Returns a new Span that only contains the last n elements. This invokes undefined * behavior when the array is too small. */ - Span take_back(uint n) const + Span take_back(int64_t n) const { + BLI_assert(n >= 0); BLI_assert(n <= this->size()); return this->slice(this->size() - n, n); } @@ -216,8 +224,9 @@ template<typename T> class Span { * Access an element in the array. This invokes undefined behavior when the index is out of * bounds. */ - const T &operator[](uint index) const + const T &operator[](int64_t index) const { + BLI_assert(index >= 0); BLI_assert(index < size_); return start_[index]; } @@ -225,7 +234,7 @@ template<typename T> class Span { /** * Returns the number of elements in the referenced array. */ - uint size() const + int64_t size() const { return size_; } @@ -241,7 +250,7 @@ template<typename T> class Span { /** * Returns the number of bytes referenced by this Span. */ - uint size_in_bytes() const + int64_t size_in_bytes() const { return sizeof(T) * size_; } @@ -273,9 +282,9 @@ template<typename T> class Span { * Does a linear search to count how often the value is in the array. * Returns the number of occurrences. */ - uint count(const T &value) const + int64_t count(const T &value) const { - uint counter = 0; + int64_t counter = 0; for (const T &element : *this) { if (element == value) { counter++; @@ -308,9 +317,9 @@ template<typename T> class Span { * Returns the element at the given index. If the index is out of range, return the fallback * value. */ - T get(uint index, const T &fallback) const + T get(int64_t index, const T &fallback) const { - if (index < size_) { + if (index < size_ && index >= 0) { return start_[index]; } return fallback; @@ -326,9 +335,9 @@ template<typename T> class Span { * changed. */ BLI_assert(size_ < 1000); - for (uint i = 0; i < size_; i++) { + for (int64_t i = 0; i < size_; i++) { const T &value = start_[i]; - for (uint j = i + 1; j < size_; j++) { + for (int64_t j = i + 1; j < size_; j++) { if (value == start_[j]) { return true; } @@ -348,7 +357,7 @@ template<typename T> class Span { * changed. */ BLI_assert(size_ < 1000); - for (uint i = 0; i < size_; i++) { + for (int64_t i = 0; i < size_; i++) { const T &value = start_[i]; if (other.contains(value)) { return true; @@ -361,19 +370,19 @@ template<typename T> class Span { * Returns the index of the first occurrence of the given value. This invokes undefined behavior * when the value is not in the array. */ - uint first_index(const T &search_value) const + int64_t first_index(const T &search_value) const { - const int index = this->first_index_try(search_value); + const int64_t index = this->first_index_try(search_value); BLI_assert(index >= 0); - return (uint)index; + return (int64_t)index; } /** * Returns the index of the first occurrence of the given value or -1 if it does not exist. */ - int first_index_try(const T &search_value) const + int64_t first_index_try(const T &search_value) const { - for (uint i = 0; i < size_; i++) { + for (int64_t i = 0; i < size_; i++) { if (start_[i] == search_value) { return i; } @@ -396,7 +405,7 @@ template<typename T> class Span { template<typename NewT> Span<NewT> cast() const { BLI_assert((size_ * sizeof(T)) % sizeof(NewT) == 0); - uint new_size = size_ * sizeof(T) / sizeof(NewT); + int64_t new_size = size_ * sizeof(T) / sizeof(NewT); return Span<NewT>(reinterpret_cast<const NewT *>(start_), new_size); } @@ -431,12 +440,12 @@ template<typename T> class Span { template<typename T> class MutableSpan { private: T *start_; - uint size_; + int64_t size_; public: MutableSpan() = default; - MutableSpan(T *start, const uint size) : start_(start), size_(size) + MutableSpan(T *start, const int64_t size) : start_(start), size_(size) { } @@ -456,7 +465,7 @@ template<typename T> class MutableSpan { /** * Returns the number of elements in the array. */ - uint size() const + int64_t size() const { return size_; } @@ -473,9 +482,9 @@ template<typename T> class MutableSpan { * Replace a subset of all elements with the given value. This invokes undefined behavior when * one of the indices is out of bounds. */ - void fill_indices(Span<uint> indices, const T &value) + void fill_indices(Span<int64_t> indices, const T &value) { - for (uint i : indices) { + for (int64_t i : indices) { BLI_assert(i < size_); start_[i] = value; } @@ -500,7 +509,7 @@ template<typename T> class MutableSpan { return start_ + size_; } - T &operator[](const uint index) const + T &operator[](const int64_t index) const { BLI_assert(index < this->size()); return start_[index]; @@ -510,7 +519,7 @@ template<typename T> class MutableSpan { * Returns a contiguous part of the array. This invokes undefined behavior when the slice would * go out of bounds. */ - MutableSpan slice(const uint start, const uint length) const + MutableSpan slice(const int64_t start, const int64_t length) const { BLI_assert(start + length <= this->size()); return MutableSpan(start_ + start, length); @@ -520,7 +529,7 @@ template<typename T> class MutableSpan { * Returns a new MutableSpan with n elements removed from the beginning. This invokes * undefined behavior when the array is too small. */ - MutableSpan drop_front(const uint n) const + MutableSpan drop_front(const int64_t n) const { BLI_assert(n <= this->size()); return this->slice(n, this->size() - n); @@ -530,7 +539,7 @@ template<typename T> class MutableSpan { * Returns a new MutableSpan with n elements removed from the end. This invokes undefined * behavior when the array is too small. */ - MutableSpan drop_back(const uint n) const + MutableSpan drop_back(const int64_t n) const { BLI_assert(n <= this->size()); return this->slice(0, this->size() - n); @@ -540,7 +549,7 @@ template<typename T> class MutableSpan { * Returns a new MutableSpan that only contains the first n elements. This invokes undefined * behavior when the array is too small. */ - MutableSpan take_front(const uint n) const + MutableSpan take_front(const int64_t n) const { BLI_assert(n <= this->size()); return this->slice(0, n); @@ -550,7 +559,7 @@ template<typename T> class MutableSpan { * Return a new MutableSpan that only contains the last n elements. This invokes undefined * behavior when the array is too small. */ - MutableSpan take_back(const uint n) const + MutableSpan take_back(const int64_t n) const { BLI_assert(n <= this->size()); return this->slice(this->size() - n, n); @@ -590,7 +599,7 @@ template<typename T> class MutableSpan { template<typename NewT> MutableSpan<NewT> cast() const { BLI_assert((size_ * sizeof(T)) % sizeof(NewT) == 0); - uint new_size = size_ * sizeof(T) / sizeof(NewT); + int64_t new_size = size_ * sizeof(T) / sizeof(NewT); return MutableSpan<NewT>(reinterpret_cast<NewT *>(start_), new_size); } }; @@ -602,7 +611,7 @@ template<typename T1, typename T2> void assert_same_size(const T1 &v1, const T2 { UNUSED_VARS_NDEBUG(v1, v2); #ifdef DEBUG - uint size = v1.size(); + int64_t size = v1.size(); BLI_assert(size == v1.size()); BLI_assert(size == v2.size()); #endif @@ -613,7 +622,7 @@ void assert_same_size(const T1 &v1, const T2 &v2, const T3 &v3) { UNUSED_VARS_NDEBUG(v1, v2, v3); #ifdef DEBUG - uint size = v1.size(); + int64_t size = v1.size(); BLI_assert(size == v1.size()); BLI_assert(size == v2.size()); BLI_assert(size == v3.size()); |