Implement IOFormat wrappers

See merge request oss/rotgen!34
This commit is contained in:
Joel Falcou 2025-10-02 19:28:43 +02:00
parent 71109fa551
commit 5a6cd4b0f2
18 changed files with 333 additions and 115 deletions

View file

@ -46,6 +46,7 @@ if(ROTGEN_FORCE_DYNAMIC)
src/info.cpp src/info.cpp
src/svd.cpp src/svd.cpp
src/operators.cpp src/operators.cpp
src/format.cpp
) )
else() else()
set ( SOURCES set ( SOURCES

View file

@ -112,7 +112,12 @@ namespace rotgen
friend std::ostream& operator<<(std::ostream& os, ref const& r) friend std::ostream& operator<<(std::ostream& os, ref const& r)
{ {
return os << r.base() << "\n"; return os << r.base();
}
friend std::ostream& operator<<(std::ostream& os, format<ref> const& r)
{
return os << format{r.matrix_.base(),r.format_};
} }
}; };
@ -201,7 +206,12 @@ namespace rotgen
friend std::ostream& operator<<(std::ostream& os, ref const& r) friend std::ostream& operator<<(std::ostream& os, ref const& r)
{ {
return os << r.base() << "\n"; return os << r.base();
}
friend std::ostream& operator<<(std::ostream& os, format<ref> const& r)
{
return os << format{r.matrix_.base(),r.format_};
} }
}; };

View file

@ -41,6 +41,10 @@ namespace rotgen
inline constexpr int Aligned64 = 64; inline constexpr int Aligned64 = 64;
inline constexpr int Aligned128 = 128; inline constexpr int Aligned128 = 128;
inline constexpr int Aligned = default_alignment; inline constexpr int Aligned = default_alignment;
inline constexpr int DontAlignCols = 1;
inline constexpr int StreamPrecision = -1;
inline constexpr int FullPrecision = -2;
} }
namespace rotgen::detail namespace rotgen::detail

View file

@ -0,0 +1,51 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
#include <rotgen/config.hpp>
#include <memory>
#include <string>
namespace rotgen
{
class ROTGEN_EXPORT ioformat
{
private:
struct payload;
std::unique_ptr<payload> storage_;
public:
ioformat( int precision, int flags = 0
, std::string const& coeffSeparator = " "
, std::string const& rowSeparator = "\n"
, std::string const& rowPrefix = "", std::string const& rowSuffix = ""
, std::string const& matPrefix = "", std::string const& matSuffix = ""
, char fill = ' '
);
~ioformat();
std::unique_ptr<payload> const& storage() const { return storage_; }
};
template <typename M>
struct format
{
format(const M& m, const ioformat& f) : matrix_(m), format_(f) {}
M const& matrix_;
ioformat const& format_;
};
template <typename M>
std::ostream& operator<<(std::ostream& os, const format<M>& f)
{
return os << format{f.matrix_.base(), f.format_};
}
}

View file

@ -0,0 +1,34 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#pragma once
#include <rotgen/config.hpp>
#include <rotgen/concepts.hpp>
#include <memory>
#include <string>
namespace rotgen
{
using ioformat = Eigen::IOFormat;
template <typename M>
struct format
{
format(const M& m, const ioformat& f) : matrix_(m), format_(f) {}
M const& matrix_;
ioformat const& format_;
};
template <typename M>
std::ostream& operator<<(std::ostream& os, const format<M>& f)
{
if constexpr(concepts::eigen_compatible<M>) return os << f.matrix_.format(f.format_);
else return os << f.matrix_.base().format(f.format_);
}
}

View file

@ -96,6 +96,7 @@ class ROTGEN_EXPORT CLASSNAME
SOURCENAME div(TYPE s) const; SOURCENAME div(TYPE s) const;
friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&,CLASSNAME const&); friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&,CLASSNAME const&);
friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&, format<CLASSNAME> const&);
friend ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs); friend ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs);
friend ROTGEN_EXPORT bool operator!=(CLASSNAME const& lhs, CLASSNAME const& rhs); friend ROTGEN_EXPORT bool operator!=(CLASSNAME const& lhs, CLASSNAME const& rhs);

View file

@ -107,6 +107,7 @@ class ROTGEN_EXPORT CLASSNAME
SOURCENAME inverse() const; SOURCENAME inverse() const;
friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&, CLASSNAME const&); friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&, CLASSNAME const&);
friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&, format<CLASSNAME> const&);
const TYPE* data() const; const TYPE* data() const;
#if !defined(USE_CONST) #if !defined(USE_CONST)

View file

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <rotgen/detail/generators.hpp> #include <rotgen/detail/generators.hpp>
#include <rotgen/dynamic/format.hpp>
#include <rotgen/config.hpp> #include <rotgen/config.hpp>
#include <initializer_list> #include <initializer_list>
#include <cstddef> #include <cstddef>

View file

@ -76,6 +76,7 @@ class ROTGEN_EXPORT CLASSNAME
CLASSNAME& operator/=(TYPE d); CLASSNAME& operator/=(TYPE d);
friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&,CLASSNAME const&); friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&,CLASSNAME const&);
friend ROTGEN_EXPORT std::ostream& operator<<(std::ostream&, format<CLASSNAME> const&);
friend ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs); friend ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs);
friend ROTGEN_EXPORT bool operator!=(CLASSNAME const& lhs, CLASSNAME const& rhs); friend ROTGEN_EXPORT bool operator!=(CLASSNAME const& lhs, CLASSNAME const& rhs);

View file

@ -12,6 +12,7 @@
#include <rotgen/impl/matrix.hpp> #include <rotgen/impl/matrix.hpp>
#include <rotgen/impl/map.hpp> #include <rotgen/impl/map.hpp>
#include <Eigen/Dense> #include <Eigen/Dense>
#include <Eigen/Core>
namespace rotgen namespace rotgen
{ {
@ -181,4 +182,19 @@ namespace rotgen
payload (double* ptr, Index r, Index c) : data(ptr,r,c) {} payload (double* ptr, Index r, Index c) : data(ptr,r,c) {}
payload (double* ptr, Index r, Index c, stride_type s) : data(ptr,r,c,s) {} payload (double* ptr, Index r, Index c, stride_type s) : data(ptr,r,c,s) {}
}; };
//
struct ioformat::payload
{
Eigen::IOFormat instance;
payload ( int p, int f
, std::string const& cs, std::string const& rsp
, std::string const& rp, std::string const& rs
, std::string const& mp, std::string const& ms
, char fill
)
: instance(p,f,cs,rsp,rp,rs,mp,ms,fill)
{}
};
} }

View file

@ -17,11 +17,13 @@
#include <rotgen/dynamic/block.hpp> #include <rotgen/dynamic/block.hpp>
#include <rotgen/dynamic/map.hpp> #include <rotgen/dynamic/map.hpp>
#include <rotgen/dynamic/svd.hpp> #include <rotgen/dynamic/svd.hpp>
#include <rotgen/dynamic/format.hpp>
#else #else
#include <rotgen/fixed/matrix.hpp> #include <rotgen/fixed/matrix.hpp>
#include <rotgen/fixed/block.hpp> #include <rotgen/fixed/block.hpp>
#include <rotgen/fixed/map.hpp> #include <rotgen/fixed/map.hpp>
#include <rotgen/fixed/svd.hpp> #include <rotgen/fixed/svd.hpp>
#include <rotgen/fixed/format.hpp>
#endif #endif
#include <rotgen/common/ref.hpp> #include <rotgen/common/ref.hpp>

View file

@ -1,3 +1,10 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#define SIZE 64 #define SIZE 64
#define TYPE double #define TYPE double
#define STORAGE_ORDER Eigen::ColMajor #define STORAGE_ORDER Eigen::ColMajor

View file

@ -394,6 +394,12 @@ struct CLASSNAME::payload
return os; return os;
} }
ROTGEN_EXPORT std::ostream& operator<<(std::ostream& os,format<CLASSNAME> const& m)
{
m.matrix_.storage_->apply([&](const auto& blk) { os << blk.format(m.format_.storage()->instance); });
return os;
}
ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs) ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs)
{ {
return std::visit ( [](auto const& lhs_blk, auto const& rhs_blk) { return lhs_blk.first == rhs_blk.first; } return std::visit ( [](auto const& lhs_blk, auto const& rhs_blk) { return lhs_blk.first == rhs_blk.first; }

27
src/format.cpp Normal file
View file

@ -0,0 +1,27 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#include <rotgen/config.hpp>
#include <rotgen/dynamic/format.hpp>
#include <rotgen/impl/payload.hpp>
#include <Eigen/Core>
#include <string>
namespace rotgen
{
ioformat::ioformat( int p, int f
, std::string const& cs, std::string const& rsp
, std::string const& rp, std::string const& rs
, std::string const& mp, std::string const& ms
, char fill
)
: storage_(std::make_unique<payload>(p,f,cs,rsp,rp,rs,mp,ms,fill))
{}
ioformat::~ioformat() = default;
}

View file

@ -1,3 +1,10 @@
//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#define SIZE 64 #define SIZE 64
#define TYPE double #define TYPE double
#define STORAGE_ORDER Eigen::ColMajor #define STORAGE_ORDER Eigen::ColMajor

View file

@ -265,6 +265,11 @@
return os << m.storage_->data; return os << m.storage_->data;
} }
ROTGEN_EXPORT std::ostream& operator<<(std::ostream& os,format<CLASSNAME> const& m)
{
return os << m.matrix_.storage_->data.format(m.format_.storage()->instance);
}
ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs) ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs)
{ {
return lhs.storage_->data == rhs.storage_->data; return lhs.storage_->data == rhs.storage_->data;

View file

@ -175,6 +175,11 @@ ROTGEN_EXPORT std::ostream& operator<<(std::ostream& os,CLASSNAME const& m)
return os << m.storage_->data; return os << m.storage_->data;
} }
ROTGEN_EXPORT std::ostream& operator<<(std::ostream& os,format<CLASSNAME> const& m)
{
return os << m.matrix_.storage_->data.format(m.format_.storage()->instance);
}
ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs) ROTGEN_EXPORT bool operator==(CLASSNAME const& lhs, CLASSNAME const& rhs)
{ {
return lhs.storage_->data == rhs.storage_->data; return lhs.storage_->data == rhs.storage_->data;

View file

@ -45,3 +45,42 @@ TTS_CASE_TPL("I/O for map test", rotgen::tests::types)
if constexpr(O::value) TTS_EQUAL(os.str(), std::string{"1 2\n3 4"}); if constexpr(O::value) TTS_EQUAL(os.str(), std::string{"1 2\n3 4"});
else TTS_EQUAL(os.str(), std::string{"1 3\n2 4"}); else TTS_EQUAL(os.str(), std::string{"1 3\n2 4"});
}; };
TTS_CASE_TPL("I/O using format", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
rotgen::ioformat io(rotgen::StreamPrecision, 0, ", ", ";\n", "<", ">", "[", "]");
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> x({ {1,2} , {3,4} });
{
std::ostringstream os;
os << rotgen::format(x,io);
TTS_EQUAL(os.str(), std::string{"[<1, 2>;\n <3, 4>]"});
}
rotgen::map<rotgen::matrix<T,2,2,O::value>> m{x.data()};
{
std::ostringstream os;
os << rotgen::format(m,io);
TTS_EQUAL(os.str(), std::string{"[<1, 2>;\n <3, 4>]"});
}
auto b = rotgen::extract(x,0,0,2,2);
{
std::ostringstream os;
os << rotgen::format(b,io);
TTS_EQUAL(os.str(), std::string{"[<1, 2>;\n <3, 4>]"});
}
auto printer = [&](rotgen::ref<const rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>> r)
{
std::ostringstream st;
st << rotgen::format(r,io);
return st.str();
};
TTS_EQUAL(printer(x), std::string{"[<1, 2>;\n <3, 4>]"});
TTS_EQUAL(printer(m), std::string{"[<1, 2>;\n <3, 4>]"});
TTS_EQUAL(printer(b), std::string{"[<1, 2>;\n <3, 4>]"});
};