28template<std::size_t N, std::size_t... Is>
30 const std::array<stratax::core::Slice, N>& ranges,
31 std::index_sequence<Is...>)
44template<std::
size_t N>
46 const std::array<stratax::core::Slice, N>& ranges)
75 "Vector slice out of bounds.");
77 auto begin =
slice.start();
78 auto end =
slice.stop();
82 for (std::size_t i = begin; i < end; ++i)
84 result[i - begin] = vec[i];
115 "Matrix row slice out of bounds.");
119 "Matrix column slice out of bounds.");
121 auto begin_rows = rows.
start();
122 auto begin_cols = cols.
start();
124 auto end_rows = rows.
stop();
125 auto end_cols = cols.
stop();
129 for (std::size_t i = begin_rows; i < end_rows; ++i)
131 for (std::size_t j = begin_cols; j < end_cols; ++j)
133 result(i - begin_rows, j - begin_cols) = mat(i, j);
140template<
typename T,
typename... Slices>
161 (std::is_same_v<Slices, stratax::core::Slice> && ...),
162 "All arguments must be Slice."
170 "Slice rank must match tensor rank.");
172 for (std::size_t dim = 0; dim < ranges.size(); ++dim)
177 "Tensor slice out of bounds.");
183 const auto& tensor_strides = tensor.
strides();
190 for (std::size_t flat = 0; flat < result.
size(); ++flat)
192 std::size_t remainder = flat;
193 std::size_t source_offset = 0;
195 for (std::size_t dim = 0; dim < ranges.size(); ++dim)
197 const std::size_t index = remainder / result_strides(dim);
198 remainder %= result_strides(dim);
199 const std::size_t source_index =
203 "Tensor slice offset overflow.");
204 const std::size_t term =
208 "Tensor slice offset overflow.");
213 "Tensor slice offset overflow.");
216 result[flat] = tensor[source_offset];
Shared runtime validation helpers.
std::size_t rows() const noexcept
Returns the number of rows.
std::size_t cols() const noexcept
Returns the number of columns.
const core::Shape & shape() const noexcept
Returns the tensor shape.
std::size_t rank() const noexcept
Returns the tensor rank.
bool empty() const noexcept
Returns whether the tensor contains no elements.
std::size_t size() const noexcept
Returns the total number of elements in the tensor.
const core::Strides & strides() const noexcept
Returns the tensor strides.
std::size_t size() const noexcept
Returns the number of stored elements.
Stores a list of dimension lengths for an array shape.
Represents a half-open range of indices.
std::size_t stop() const noexcept
Returns the index one past the end of the slice.
std::size_t size() const noexcept
Returns the number of indices covered by the slice.
std::size_t start() const noexcept
Returns the first index in the slice.
Stores strides for a shape in contiguous memory.
std::size_t checked_multiply(std::size_t lhs, std::size_t rhs, const char *message)
Multiplies two sizes while checking for overflow.
void require_at_most(std::size_t value, std::size_t upper, const char *message)
Requires a value to be less than or equal to an upper bound.
void require_rank(std::size_t actual, std::size_t expected, const char *message)
Requires a rank value to match an expected rank.
std::size_t checked_add(std::size_t lhs, std::size_t rhs, const char *message)
Adds two sizes while checking for overflow.
stratax::core::Shape shape_from_slices(const std::array< stratax::core::Slice, N > &ranges)
Builds a shape from a slice array.
stratax::core::Shape shape_from_slices_impl(const std::array< stratax::core::Slice, N > &ranges, std::index_sequence< Is... >)
Builds a shape from a compile-time slice array.
stratax::container::Vector< T > slice(const stratax::container::Vector< T > &vec, const stratax::core::Slice &slice)
Copies a half-open range from a vector.