15#include <fmt/format.h>
23template <
typename T, s
size_t Ndim = 2>
class NDArray {
76 for (
int i = 0; i <
size_; ++i) {
81 throw(std::runtime_error(
"Shape of NDArray must match"));
103 for (
int i = 0; i <
size_; ++i) {
110 template <
typename... Ix>
typename std::enable_if<
sizeof...(Ix) == Ndim, T &>::type
operator()(Ix... index) {
114 template <
typename... Ix>
typename std::enable_if<
sizeof...(Ix) == Ndim, T &>::type
operator()(Ix... index)
const {
118 template <
typename... Ix>
typename std::enable_if<
sizeof...(Ix) == Ndim, T>::type
value(Ix... index) {
126 std::byte *
buffer() {
return reinterpret_cast<std::byte *
>(
data_); }
129 std::array<ssize_t, Ndim>
shape() const noexcept {
return shape_; }
162 if (
this != &other) {
165 shape_ = other.shape_;
167 strides_ = other.strides_;
180 if (shape_ == other.
shape_) {
181 for (
int i = 0; i < size_; ++i) {
182 data_[i] += other.
data_[i];
186 throw(std::runtime_error(
"Shape of ImageDatas must match"));
198 if (shape_ == other.
shape_) {
199 for (
int i = 0; i < size_; ++i) {
200 data_[i] -= other.
data_[i];
204 throw(std::runtime_error(
"Shape of ImageDatas must match"));
215 if (shape_ == other.
shape_) {
216 for (
int i = 0; i < size_; ++i) {
217 data_[i] *= other.
data_[i];
221 throw(std::runtime_error(
"Shape of ImageDatas must match"));
232 for (
auto it = begin(); it != end(); ++it)
253 if (shape_ == other.
shape_) {
255 for (
int i = 0; i < size_; ++i) {
256 result(i) = (data_[i] > other.
data_[i]);
260 throw(std::runtime_error(
"Shape of ImageDatas must match"));
265 if (
this != &other) {
270 data_ =
new T[size_];
271 std::copy(other.
data_, other.
data_ + size_, data_);
277 if (shape_ != other.
shape_)
280 for (
int i = 0; i != size_; ++i)
281 if (data_[i] != other.
data_[i])
288 return !((*this) == other);
291 for (
int i = 0; i < size_; ++i)
296 std::fill_n(data_, size_, value);
301 for (
int i = 0; i < size_; ++i)
312 for (
int i = 0; i < size_; ++i)
323 for (
int i = 0; i < size_; ++i)
333 for (
int i = 0; i < size_; ++i)
343 if (shape_[0] < 20 && shape_[1] < 20)
349 for (
auto row = 0; row < shape_[0]; ++row) {
350 for (
auto col = 0; col < shape_[1]; ++col) {
351 std::cout << std::setw(3);
352 std::cout << (*this)(row, col) <<
" ";
358 for (
auto row = 0; row < 5; ++row) {
359 for (
auto col = 0; col < 5; ++col) {
360 std::cout << std::setw(7);
361 std::cout << (*this)(row, col) <<
" ";
369 f.open(pathname, std::ios::binary);
370 f.write(img.
buffer(), img.
size() *
sizeof(T));
374template <
typename T, s
size_t Ndim>
378 f.open(pathname, std::ios::binary);
379 f.read(img.buffer(), img.size() *
sizeof(T));
Definition NDArray.hpp:23
NDArray & operator++()
Definition NDArray.hpp:290
NDArray(NDView< T, Ndim > span)
Definition NDArray.hpp:35
T value_type
Definition NDArray.hpp:60
NDArray & operator=(NDArray &&other)
Definition NDArray.hpp:161
std::array< ssize_t, Ndim > strides() const noexcept
Definition NDArray.hpp:131
NDArray(const NDArray &other)
Definition NDArray.hpp:49
NDArray & operator+=(const NDArray &other)
Definition NDArray.hpp:178
NDArray & operator/=(const NDArray< V, Ndim > &other)
Definition NDArray.hpp:73
std::array< ssize_t, Ndim > byte_strides() const noexcept
Definition NDArray.hpp:132
NDArray operator*(const T &)
Definition NDArray.hpp:337
NDArray(std::array< ssize_t, Ndim > shape)
Definition NDArray.hpp:27
auto begin()
Definition NDArray.hpp:57
NDArray()
Definition NDArray.hpp:25
std::array< ssize_t, Ndim > strides_
Definition NDArray.hpp:155
NDArray & operator*=(const NDArray &other)
Definition NDArray.hpp:213
NDArray< bool, Ndim > operator>(const NDArray &other)
Definition NDArray.hpp:252
NDArray operator/(const NDArray &other)
Definition NDArray.hpp:225
void Print_some()
Definition NDArray.hpp:357
NDArray & operator=(const T &)
Definition NDArray.hpp:295
NDArray(std::array< ssize_t, Ndim > shape, T value)
Definition NDArray.hpp:31
void Print_all()
Definition NDArray.hpp:348
NDArray operator/(const T &)
Definition NDArray.hpp:327
std::array< ssize_t, Ndim > shape() const noexcept
Definition NDArray.hpp:129
~NDArray()
Definition NDArray.hpp:55
NDArray & operator=(const NDArray &other)
Definition NDArray.hpp:264
T * data()
Definition NDArray.hpp:125
NDArray(NDArray &&other)
Definition NDArray.hpp:41
NDArray operator+(const T &)
Definition NDArray.hpp:306
NDArray & operator/=(const T &)
Definition NDArray.hpp:322
ssize_t size() const
Definition NDArray.hpp:127
T & operator()(int i)
Definition NDArray.hpp:122
bool operator!=(const NDArray &other) const
Definition NDArray.hpp:287
void reset()
Definition NDArray.hpp:146
NDArray & operator+=(const T &)
Definition NDArray.hpp:300
NDArray operator-(const NDArray &other)
Definition NDArray.hpp:190
NDArray & operator-=(const NDArray &other)
Definition NDArray.hpp:196
NDArray & operator&=(const T &)
Definition NDArray.hpp:231
size_t total_bytes() const
Definition NDArray.hpp:128
NDView< T, Ndim > span() const
Definition NDArray.hpp:140
ssize_t size_
Definition NDArray.hpp:156
bool operator==(const NDArray &other) const
Definition NDArray.hpp:276
T * data_
Definition NDArray.hpp:157
void Print()
Definition NDArray.hpp:342
NDArray & operator-=(const T &)
Definition NDArray.hpp:311
std::enable_if< sizeof...(Ix)==Ndim, T >::type value(Ix... index)
Definition NDArray.hpp:118
const T & operator()(int i) const
Definition NDArray.hpp:123
NDArray operator*(const NDArray &other)
Definition NDArray.hpp:207
NDArray operator-(const T &)
Definition NDArray.hpp:316
auto end()
Definition NDArray.hpp:58
std::array< ssize_t, Ndim > shape_
Definition NDArray.hpp:154
std::byte * buffer()
Definition NDArray.hpp:126
void sqrt()
Definition NDArray.hpp:102
ssize_t shape(ssize_t i) const noexcept
Definition NDArray.hpp:130
NDArray operator+(const NDArray &other)
Definition NDArray.hpp:173
NDArray & operator*=(const T &)
Definition NDArray.hpp:332
Frame class to represent a single frame of data model class should be able to work with streams comin...
Definition CircularFifo.hpp:11
NDArray< T, Ndim > load(const std::string &pathname, std::array< ssize_t, Ndim > shape)
Definition NDArray.hpp:375
ssize_t element_offset(const Strides &)
Definition NDView.hpp:23
std::array< ssize_t, Ndim > c_strides(const std::array< ssize_t, Ndim > &shape)
Definition NDView.hpp:30
void save(NDArray< T, Ndim > &img, std::string pathname)
Definition NDArray.hpp:367