From 16ea7cfc4d834a3c0cedb4a833ff0f212eeee2dc Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 6 Aug 2025 18:37:49 +0200 Subject: Fixing math operators --- modules/codec/c++/data_math.hpp | 19 +++++++++----- modules/codec/c++/iterator.hpp | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 modules/codec/c++/iterator.hpp diff --git a/modules/codec/c++/data_math.hpp b/modules/codec/c++/data_math.hpp index 7277168..b5491eb 100644 --- a/modules/codec/c++/data_math.hpp +++ b/modules/codec/c++/data_math.hpp @@ -2,6 +2,7 @@ #include "data.hpp" #include "schema_math.hpp" +#include "iterator.hpp" #include @@ -71,31 +72,37 @@ public: using Schema = schema::Tensor; private: data, encode::Native> values_; + public: data(): values_{data>{{data{Dims}...}}} {} - data& at(const data, encode::Native>& index){ + data& at(const data, encode::Native>& index){ return values_.at(index); } - const data& at(const data, encode::Native>& index) const { + const data& at(const data, encode::Native>& index) const { return values_.at(index); } - data& operator()(const data, encode::Native>& index){ + data& operator()(const data, encode::Native>& index){ return values_.at(index); } - const data& operator()(const data, encode::Native>& index) const { + const data& operator()(const data, encode::Native>& index) const { return values_.at(index); } - data, encode::Native> operator+(const data, encode::Native>& rhs) const { + data, encode::Native> operator+(const data, encode::Native>& rhs) { data, encode::Native> c; - return {}; + rank_iterator::in_fixed_bounds([&](const data, encode::Native>& index) -> error_or{ + c.at(index) = at(index) + rhs.at(index); + return make_void(); + }); + + return c; } }; diff --git a/modules/codec/c++/iterator.hpp b/modules/codec/c++/iterator.hpp new file mode 100644 index 0000000..96a4441 --- /dev/null +++ b/modules/codec/c++/iterator.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "data.hpp" + +namespace saw { +namespace impl { +template +struct iterator_index_parameter_value { + static_assert(i > 0, "Shouldn't happen"); + static constexpr uint64_t value = iterator_index_parameter_value::value; +}; + +template +struct iterator_index_parameter_value<0u, val_0, vals...> { + static constexpr uint64_t value = val_0; +}; + +template +struct iterator_in_bounds { +private: + static constexpr std::array dims_{Dims...}; + + template + static error_or apply_i(Func& func, data, encode::Native>& index){ + if constexpr (level >= sizeof...(Dims)){ + return func(index); + }else{ + index.at({level}).set(0); + + for(;index.at({level}) < impl::iterator_index_parameter_value::value; ++index.at({level})){ + auto eov = apply_i(func, index); + if(eov.is_error()){ + return eov; + } + } + } + return saw::make_void(); + } +public: + static error_or apply(Func& func){ + data, encode::Native> index; + return apply_i<0u>(func, index); + } +}; +} + +template +struct rank_iterator { + template + static error_or in_fixed_bounds(Func&& func){ + // static_assert(D == 2u, "Currently a lazy implementation for AND combinations of intervalls."); + return impl::iterator_in_bounds::apply(func); + } +}; +} -- cgit v1.2.3