Files
test/source/blender/blenlib/BLI_offset_span.hh
Campbell Barton e955c94ed3 License Headers: Set copyright to "Blender Authors", add AUTHORS
Listing the "Blender Foundation" as copyright holder implied the Blender
Foundation holds copyright to files which may include work from many
developers.

While keeping copyright on headers makes sense for isolated libraries,
Blender's own code may be refactored or moved between files in a way
that makes the per file copyright holders less meaningful.

Copyright references to the "Blender Foundation" have been replaced with
"Blender Authors", with the exception of `./extern/` since these this
contains libraries which are more isolated, any changed to license
headers there can be handled on a case-by-case basis.

Some directories in `./intern/` have also been excluded:

- `./intern/cycles/` it's own `AUTHORS` file is planned.
- `./intern/opensubdiv/`.

An "AUTHORS" file has been added, using the chromium projects authors
file as a template.

Design task: #110784

Ref !110783.
2023-08-16 00:20:26 +10:00

111 lines
2.1 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "BLI_span.hh"
namespace blender {
/**
* An #OffsetSpan is a #Span with a constant offset that is added to every value when accessed.
* This allows e.g. storing multiple `int64_t` indices as an array of `int16_t` with an additional
* `int64_t` offset.
*/
template<typename T, typename BaseT> class OffsetSpan {
private:
/** Value that is added to every element in #data_ when accessed. */
T offset_ = 0;
/** Original span where each element is offset by #offset_. */
Span<BaseT> data_;
public:
OffsetSpan() = default;
OffsetSpan(const T offset, const Span<BaseT> data) : offset_(offset), data_(data) {}
/** \return Underlying span containing the values that are not offset. */
Span<BaseT> base_span() const
{
return data_;
}
T offset() const
{
return offset_;
}
bool is_empty() const
{
return data_.is_empty();
}
int64_t size() const
{
return data_.size();
}
T last(const int64_t n = 0) const
{
return offset_ + data_.last(n);
}
IndexRange index_range() const
{
return data_.index_range();
}
T operator[](const int64_t i) const
{
return T(data_[i]) + offset_;
}
OffsetSpan slice(const IndexRange &range) const
{
return {offset_, data_.slice(range)};
}
OffsetSpan slice(const int64_t start, const int64_t size) const
{
return {offset_, data_.slice(start, size)};
}
class Iterator {
private:
T offset_;
const BaseT *data_;
public:
Iterator(const T offset, const BaseT *data) : offset_(offset), data_(data) {}
Iterator &operator++()
{
data_++;
return *this;
}
T operator*() const
{
return T(*data_) + offset_;
}
friend bool operator!=(const Iterator &a, const Iterator &b)
{
BLI_assert(a.offset_ == b.offset_);
return a.data_ != b.data_;
}
};
Iterator begin() const
{
return {offset_, data_.begin()};
}
Iterator end() const
{
return {offset_, data_.end()};
}
};
} // namespace blender