Fix resize behavior w/r to semi static container

See merge request oss/rotgen!32
This commit is contained in:
Joel Falcou 2025-09-29 21:05:43 +02:00
parent a3aa130563
commit 7d65a05f72
5 changed files with 35 additions and 50 deletions

View file

@ -111,26 +111,30 @@ namespace rotgen
decltype(auto) noalias() const { return *this; } decltype(auto) noalias() const { return *this; }
decltype(auto) noalias() { 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); if constexpr(Rows == 1) parent::resize(1,s);
else parent::resize(1,new_size); 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); if constexpr(Rows == 1) parent::conservativeResize(1,s);
else parent::conservativeResize(1,new_size); else parent::conservativeResize(s,1);
} }
matrix normalized() const requires(IsVectorAtCompileTime) matrix normalized() const requires(IsVectorAtCompileTime)

View file

@ -215,24 +215,32 @@ namespace rotgen
else return base().cwiseSqrt(); 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) static matrix Constant(Scalar value) requires (Rows != -1 && Cols != -1)

View file

@ -18,24 +18,15 @@ namespace rotgen
std::size_t cols(auto const& m) requires(requires{ m.cols(); }){ return m.cols(); } 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(); } 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<typename E> void conservativeResize(auto& a, int s) requires requires{a.conservativeResize(s);}
void resize(E& a, int r, int c)
requires(E::RowsAtCompileTime == -1 && E::ColsAtCompileTime == -1)
{ {
a.resize(r, c); a.conservativeResize(s);
} }
template<typename E> void conservativeResize(auto& a, int r, int c) requires requires{a.conservativeResize(r,c);}
void conservativeResize(E& a, int sz) requires requires{a.conservativeResize(sz);}
{
a.conservativeResize(sz);
}
template<typename E>
void conservativeResize(E& a, int r, int c)
requires(E::RowsAtCompileTime == -1 && E::ColsAtCompileTime == -1)
{ {
a.conservativeResize(r, c); a.conservativeResize(r, c);
} }

View file

@ -77,8 +77,4 @@ TTS_CASE("Matrix size-related operations")
std::pair{1, 2}, std::pair{11, 17}, std::pair{4, 5}, std::pair{1, 2}, std::pair{11, 17}, std::pair{4, 5},
std::pair{9, 1}, std::pair{3, 8}, std::pair{22, 0}, std::pair{9, 1}, std::pair{3, 8}, std::pair{22, 0},
}); });
rotgen::matrix<double, 3, 3> a;
TTS_EXPECT_NOT_COMPILES(a, { rotgen::resize(a, 4, 5); });
TTS_EXPECT_NOT_COMPILES(a, { rotgen::conservativeResize(a, 4, 5); });
}; };

View file

@ -44,13 +44,6 @@ TTS_CASE_TPL("Resizing dynamic matrix", rotgen::tests::types)
TTS_EQUAL(a.cols(), rotgen::Index(2)); TTS_EQUAL(a.cols(), rotgen::Index(2));
}; };
TTS_CASE_TPL("Resizing static matrix", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
rotgen::matrix<double, 3, 3> a;
TTS_EXPECT_NOT_COMPILES(a, { a.resize(4, 5); });
};
TTS_CASE_TPL("Dynamix matrix conservative resizing", rotgen::tests::types) TTS_CASE_TPL("Dynamix matrix conservative resizing", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> ) <typename T, typename O>( tts::type< tts::types<T,O>> )
{ {
@ -105,13 +98,6 @@ TTS_CASE_TPL("Dynamix matrix conservative resizing", rotgen::tests::types)
TTS_EQUAL(a.cols(), rotgen::Index(3)); TTS_EQUAL(a.cols(), rotgen::Index(3));
}; };
TTS_CASE_TPL("Static matrix conservative resizing", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
rotgen::matrix<double, 3, 3> a;
TTS_EXPECT_NOT_COMPILES(a, { a.conservativeResize(4, 5); });
};
TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types) TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> ) <typename T, typename O>( tts::type< tts::types<T,O>> )
{ {