From 7d65a05f7242fc83a0210ada4b0279c7eee327df Mon Sep 17 00:00:00 2001 From: Joel Falcou Date: Mon, 29 Sep 2025 21:05:43 +0200 Subject: [PATCH] Fix resize behavior w/r to semi static container See merge request oss/rotgen!32 --- include/rotgen/dynamic/matrix.hpp | 24 ++++++++++++++---------- include/rotgen/fixed/matrix.hpp | 24 ++++++++++++++++-------- include/rotgen/functions.hpp | 19 +++++-------------- test/unit/functions/size_related.cpp | 4 ---- test/unit/matrix/basic_api.cpp | 14 -------------- 5 files changed, 35 insertions(+), 50 deletions(-) diff --git a/include/rotgen/dynamic/matrix.hpp b/include/rotgen/dynamic/matrix.hpp index 9431c07..6d8d208 100644 --- a/include/rotgen/dynamic/matrix.hpp +++ b/include/rotgen/dynamic/matrix.hpp @@ -111,26 +111,30 @@ namespace rotgen decltype(auto) noalias() const { return *this; } decltype(auto) noalias() { return *this; } - void resize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) + void resize(int r, int c) { - parent::resize(new_rows, new_cols); + if constexpr(Rows == 1) assert(c == Cols && "Mismatched between dynamic and static col size"); + if constexpr(Cols == 1) assert(r == Rows && "Mismatched between dynamic and static row size"); + parent::resize(r, c); } - void resize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + void resize(int s) requires(IsVectorAtCompileTime) { - if constexpr(Rows == -1) parent::resize(new_size,1); - else parent::resize(1,new_size); + if constexpr(Rows == 1) parent::resize(1,s); + else parent::resize(s,1); } - void conservativeResize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) + void conservativeResize(int r, int c) { - parent::conservativeResize(new_rows, new_cols); + if constexpr(Rows == 1) assert(c == Cols && "Mismatched between dynamic and static col size"); + if constexpr(Cols == 1) assert(r == Rows && "Mismatched between dynamic and static row size"); + parent::conservativeResize(r, c); } - void conservativeResize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + void conservativeResize(int s) requires(IsVectorAtCompileTime) { - if constexpr(Rows == -1) parent::conservativeResize(new_size,1); - else parent::conservativeResize(1,new_size); + if constexpr(Rows == 1) parent::conservativeResize(1,s); + else parent::conservativeResize(s,1); } matrix normalized() const requires(IsVectorAtCompileTime) diff --git a/include/rotgen/fixed/matrix.hpp b/include/rotgen/fixed/matrix.hpp index 35b8447..a9fea82 100644 --- a/include/rotgen/fixed/matrix.hpp +++ b/include/rotgen/fixed/matrix.hpp @@ -215,24 +215,32 @@ namespace rotgen else return base().cwiseSqrt(); } - void resize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + void resize(int s) requires(IsVectorAtCompileTime) { - parent::resize(new_size); + if constexpr(Rows == 1) assert(s == Cols && "Mismatched between dynamic and static col size"); + if constexpr(Cols == 1) assert(s == Rows && "Mismatched between dynamic and static row size"); + parent::resize(s); } - void resize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) + void resize(int r, int c) { - parent::resize(new_rows, new_cols); + if constexpr(Rows == 1) assert(c == Cols && "Mismatched between dynamic and static col size"); + if constexpr(Cols == 1) assert(r == Rows && "Mismatched between dynamic and static row size"); + parent::resize(r, c); } - void conservativeResize(int new_size) requires(IsVectorAtCompileTime && (Rows == -1 || Cols == -1)) + void conservativeResize(int s) requires(IsVectorAtCompileTime) { - parent::conservativeResize(new_size); + if constexpr(Rows == 1) assert(s == Cols && "Mismatched between dynamic and static col size"); + if constexpr(Cols == 1) assert(s == Rows && "Mismatched between dynamic and static row size"); + parent::conservativeResize(s); } - void conservativeResize(int new_rows, int new_cols) requires(Rows == -1 && Cols == -1) + void conservativeResize(int r, int c) { - parent::conservativeResize(new_rows, new_cols); + if constexpr(Rows == 1) assert(c == Cols && "Mismatched between dynamic and static col size"); + if constexpr(Cols == 1) assert(r == Rows && "Mismatched between dynamic and static row size"); + parent::conservativeResize(r, c); } static matrix Constant(Scalar value) requires (Rows != -1 && Cols != -1) diff --git a/include/rotgen/functions.hpp b/include/rotgen/functions.hpp index 80300e0..6e43082 100644 --- a/include/rotgen/functions.hpp +++ b/include/rotgen/functions.hpp @@ -18,24 +18,15 @@ namespace rotgen std::size_t cols(auto const& m) requires(requires{ m.cols(); }){ return m.cols(); } std::size_t size(auto const& m) requires(requires{ m.size(); }){ return m.size(); } - void resize(auto& a, int sz) requires requires{a.resize(sz);} { a.resize(sz); } + void resize(auto& a, int s) requires requires{a.resize(s);} { a.resize(s); } + void resize(auto& a, int r, int c) requires requires{a.resize(r,c);} { a.resize(r,c); } - template - void resize(E& a, int r, int c) - requires(E::RowsAtCompileTime == -1 && E::ColsAtCompileTime == -1) + void conservativeResize(auto& a, int s) requires requires{a.conservativeResize(s);} { - a.resize(r, c); + a.conservativeResize(s); } - template - void conservativeResize(E& a, int sz) requires requires{a.conservativeResize(sz);} - { - a.conservativeResize(sz); - } - - template - void conservativeResize(E& a, int r, int c) - requires(E::RowsAtCompileTime == -1 && E::ColsAtCompileTime == -1) + void conservativeResize(auto& a, int r, int c) requires requires{a.conservativeResize(r,c);} { a.conservativeResize(r, c); } diff --git a/test/unit/functions/size_related.cpp b/test/unit/functions/size_related.cpp index 9eac1a5..a1886be 100644 --- a/test/unit/functions/size_related.cpp +++ b/test/unit/functions/size_related.cpp @@ -77,8 +77,4 @@ TTS_CASE("Matrix size-related operations") std::pair{1, 2}, std::pair{11, 17}, std::pair{4, 5}, std::pair{9, 1}, std::pair{3, 8}, std::pair{22, 0}, }); - - rotgen::matrix a; - TTS_EXPECT_NOT_COMPILES(a, { rotgen::resize(a, 4, 5); }); - TTS_EXPECT_NOT_COMPILES(a, { rotgen::conservativeResize(a, 4, 5); }); }; diff --git a/test/unit/matrix/basic_api.cpp b/test/unit/matrix/basic_api.cpp index f61f60d..0312c99 100644 --- a/test/unit/matrix/basic_api.cpp +++ b/test/unit/matrix/basic_api.cpp @@ -44,13 +44,6 @@ TTS_CASE_TPL("Resizing dynamic matrix", rotgen::tests::types) TTS_EQUAL(a.cols(), rotgen::Index(2)); }; -TTS_CASE_TPL("Resizing static matrix", rotgen::tests::types) -( tts::type< tts::types> ) -{ - rotgen::matrix a; - TTS_EXPECT_NOT_COMPILES(a, { a.resize(4, 5); }); -}; - TTS_CASE_TPL("Dynamix matrix conservative resizing", rotgen::tests::types) ( tts::type< tts::types> ) { @@ -105,13 +98,6 @@ TTS_CASE_TPL("Dynamix matrix conservative resizing", rotgen::tests::types) TTS_EQUAL(a.cols(), rotgen::Index(3)); }; -TTS_CASE_TPL("Static matrix conservative resizing", rotgen::tests::types) -( tts::type< tts::types> ) -{ - rotgen::matrix a; - TTS_EXPECT_NOT_COMPILES(a, { a.conservativeResize(4, 5); }); -}; - TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types) ( tts::type< tts::types> ) {