Stratax 0.3.1
Loading...
Searching...
No Matches
ArrayView.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <type_traits>
6
7#include <stratax/core/Shape.hpp>
8#include <stratax/indexing/Indexing.hpp>
9
10namespace stratax::core {
11
12template<typename T>
13class ArrayView
14{
15public:
16 using value_type = T;
17 using size_type = std::size_t;
18 using reference = T&;
19 using const_reference = const T&;
20 using pointer = T*;
21 using const_pointer = const T*;
22
23 ArrayView(pointer data, const Shape& shape, const Shape& strides)
24 : data_(data),
25 shape_(shape),
26 strides_(strides)
27 {}
28
29 [[nodiscard]]
30 const Shape& shape() const noexcept
31 {
32 return shape_;
33 }
34
35 [[nodiscard]]
36 const Shape& strides() const noexcept
37 {
38 return strides_;
39 }
40
41 [[nodiscard]]
42 size_type ndim() const noexcept
43 {
44 return shape_.rank();
45 }
46
47 [[nodiscard]]
48 size_type size() const noexcept
49 {
50 return shape_.elements();
51 }
52
53 [[nodiscard]]
54 pointer data() noexcept
55 {
56 return data_;
57 }
58
59 [[nodiscard]]
60 const_pointer data() const noexcept
61 {
62 return data_;
63 }
64
65 template<typename... Rest>
66 requires ((std::is_integral_v<Rest>) && ...)
67 reference operator()(size_type first, Rest... rest)
68 {
69 const std::array<size_type, sizeof...(Rest) + 1> indices{
70 first,
71 static_cast<size_type>(rest)...
72 };
73
74 return data_[indexing::offset(strides_, indices)];
75 }
76
77 template<typename... Rest>
78 requires ((std::is_integral_v<Rest>) && ...)
79 const_reference operator()(size_type first, Rest... rest) const
80 {
81 const std::array<size_type, sizeof...(Rest) + 1> indices{
82 first,
83 static_cast<size_type>(rest)...
84 };
85
86 return data_[indexing::offset(strides_, indices)];
87 }
88
89private:
90 pointer data_;
91 Shape shape_;
92 Shape strides_;
93};
94
95} // namespace stratax::core
Stores the dimensions of a multidimensional array.
Definition Shape.hpp:33