17#ifndef _NDARRAY_ARRAY_H_
18#define _NDARRAY_ARRAY_H_
36template <
size_t... Nums>
using index_sequence = std::index_sequence<Nums...>;
44 int operator[](
size_t idx)
const noexcept {
return _strides[idx]; }
49 template <
size_t Num,
size_t... Nums,
typename T,
typename... Ts>
52 static constexpr size_t get(
const std::array<int, 8> &strides,
int x, Ts... xs)
55 x * std::get<Num>(strides);
61 static constexpr size_t get(
const std::array<int, 8> &strides,
int x)
63 return x * std::get<Num>(strides);
67 template <
typename Seq,
typename... Ts>
constexpr size_t offset(Seq, Ts... x)
const noexcept
74 void fillStrides(
const Shape &s)
noexcept
77 _strides[rank - 1] = 1;
78 for (
int d = rank - 2; d >= 0; --d)
80 _strides[d] = _strides[d + 1] * s.dim(d + 1);
84 std::array<int, NDARRAY_MAX_DIMENSION_COUNT> _strides;
87template <
typename T>
class Array
94 Array(
Array &&a) noexcept : _data(a._data), _shape(a._shape), _strides(a._strides)
99 template <
typename... Ts> T &
at(Ts... x)
const noexcept {
return _at(
static_cast<size_t>(x)...); }
108 assert(
sizeof...(Ts) == _shape.
rank() - 1);
109 return {&
at(x..., 0ul), _shape.
dim(_shape.
rank() - 1)};
119 assert(
sizeof...(Ts) == _shape.
rank() - 1);
120 return {&
at(x..., 0ul), _shape.
dim(_shape.
rank() - 1)};
133 template <
typename... Ts> T &_at(Ts... x)
const noexcept
135 assert(
sizeof...(x) == _shape.
rank());
137 return _data[offset(Indices{}, x...)];
140 template <
typename... Ts,
size_t... Nums>
141 size_t offset(index_sequence<Nums...> seq, Ts... x)
const noexcept
144 sizeof...(Ts) ==
sizeof...(Nums),
145 "Sanity check failed. Generated index sequence size is not equal to argument count");
147 return _strides.
offset(seq, x...);
157 assert(from.shape().element_count() / (
sizeof(To) /
sizeof(From)) == newShape.
element_count());
158 return Array<To>(
reinterpret_cast<To *
>(from.flat().data()), newShape);
161template <
typename To,
typename From>
165 return Array<To>(
reinterpret_cast<const To *
>(from.
flat().data()), newShape);
168#ifndef NDARRAY_INLINE_TEMPLATES
170extern template class Array<float>;
171extern template class Array<int32_t>;
172extern template class Array<uint32_t>;
173extern template class Array<uint8_t>;
T & at(Ts... x) const noexcept
const Shape & shape() const noexcept
ContiguousSpan< T, true > flat() const noexcept
ContiguousSpan< T, std::is_const< T >::value > slice(Ts... x) noexcept
returns last dimension as ContigniousSpan
ContiguousSpan< T, true > slice(Ts... x) const noexcept
returns last dimension as ContigniousSpan
Array(T *data, Shape shape) noexcept
Array(const Array &)=delete
Array(Array &&a) noexcept
ContiguousSpan< T, std::is_const< T >::value > flat() noexcept
size_t dim(int i) const noexcept
size_t rank() const noexcept
size_t element_count() const noexcept
Array< To > array_cast(Array< From > &&from, Shape newShape)
std::make_index_sequence< _Num > make_index_sequence
std::index_sequence< Nums... > index_sequence
static constexpr size_t get(const std::array< int, 8 > &strides, int x, Ts... xs)
static constexpr size_t get(const std::array< int, 8 > &strides, int x)
int operator[](size_t idx) const noexcept
constexpr size_t offset(Seq, Ts... x) const noexcept