From 8ea6e2b680d00f82655584fcc199f90803f00fa2 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Fri, 27 Sep 2024 12:20:10 +0200 Subject: [PATCH] feat(eo): adds eoRealToIntInit --- eo/src/eo | 1 + eo/src/eoRealToIntInit.h | 74 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 eo/src/eoRealToIntInit.h diff --git a/eo/src/eo b/eo/src/eo index 77a65e249..0f6f133d3 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -227,6 +227,7 @@ #include "eoInt.h" #include "eoRealToIntMonOp.h" #include "eoRealToIntQuadOp.h" +#include "eoRealToIntInit.h" #endif diff --git a/eo/src/eoRealToIntInit.h b/eo/src/eoRealToIntInit.h new file mode 100644 index 000000000..4b79ca641 --- /dev/null +++ b/eo/src/eoRealToIntInit.h @@ -0,0 +1,74 @@ +#ifndef eoRealToIntInit_h_INCLUDED +#define eoRealToIntInit_h_INCLUDED + +#include "es/eoReal.h" +#include "utils/eoIntBounds.h" + +template> +class eoRealToIntInit : public eoInit +{ +public: + + using EOTreal = EOTREAL; + + enum Repair { + folds, + truncate + }; + + eoRealToIntInit( eoInit& init ) : + _whenout(Repair::truncate), + _nobounds(), + _bounds(_nobounds), + _init(init) + { } + + eoRealToIntInit( eoInit& init, eoIntBounds& bounds, Repair whenout = Repair::truncate ) : + _whenout(whenout), + _nobounds(), + _bounds(bounds), + _init(init) + { } + + virtual void operator()(EOTINT& intsol) override + { + #ifndef NDEBUG + for(size_t i=0; i < intsol.size(); ++i) { + assert(_bounds.isInBounds(intsol[i])); + } + #endif + + EOTreal floatsol; + std::copy( std::begin(intsol), std::end(intsol), std::back_inserter(floatsol) ); + + _init(floatsol); + + intsol.resize(floatsol.size()); + + for(size_t i=0; i < floatsol.size(); ++i) { + typename EOTreal::AtomType rounded = std::round(floatsol[i]); + if( not _bounds.isInBounds(rounded) ) { + switch(_whenout) { + case Repair::truncate: + _bounds.truncate(rounded); + break; + case Repair::folds: + _bounds.foldsInBounds(rounded); + break; + } + } + intsol[i] = static_cast(rounded); + } + } + +protected: + Repair _whenout; + eoIntNoBounds _nobounds; + + eoIntBounds& _bounds; + eoInit& _init; +}; + + + +#endif // eoRealToIntInit_h_INCLUDED