Clarify single index API for data access.

This commit is contained in:
Joel Falcou 2025-09-18 14:31:33 +02:00
parent a50098f761
commit d89da18709
13 changed files with 249 additions and 128 deletions

View file

@ -85,20 +85,45 @@ TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types)
TTS_CASE_TPL("Test one index coefficient accessors", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
using base = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
auto vs = [&]()
{
if constexpr(O::value == rotgen::ColMajor)
{
using base = rotgen::matrix<T,1,rotgen::Dynamic>;
base m(12);
for(int k=0;k<12;++k) m(k) = k+1;
return std::tuple{m,rotgen::block<base,1,rotgen::Dynamic>(m, 0, 0, 1, 12)};
}
else
{
using base = rotgen::matrix<T,rotgen::Dynamic,1>;
base m(12);
for(int k=0;k<12;++k) m(k) = k+1;
return std::tuple{m,rotgen::block<base,rotgen::Dynamic,1>(m, 0, 0, 12, 1)};
}
}();
T data[] = {1,2,3,4,5,6,7,8,9,10,11,12};
base mat(4,3);
for(int k=0;k<12;++k) mat.data()[k] = data[k];
auto mat = get<0>(vs);
auto b = get<1>(vs);
TTS_EXPECT(b.IsVectorAtCompileTime);
auto b = rotgen::block<base,rotgen::Dynamic,rotgen::Dynamic>(mat, 0, 0, 4, 3);
for(rotgen::Index i=0;i<b.size();i++)
TTS_EQUAL(b(i), data[i]) << "Index: " << i << "\n";
TTS_EQUAL(b(i), mat(i)) << "Index: " << i << "\n";
for(rotgen::Index i=0;i<b.size();i++)
TTS_EQUAL(b[i], mat(i)) << "Index: " << i << "\n";
b(1) = 42;
TTS_EQUAL(mat.data()[1], 42);
TTS_EQUAL(mat(1), 42);
T& ref = b(2);
ref = 17;
TTS_EQUAL(mat.data()[2], 17);
TTS_EQUAL(mat(2), 17);
b[1] = 77;
TTS_EQUAL(mat(1), 77);
T& bref = b[3];
bref = 331;
TTS_EQUAL(mat(3), 331);
};

View file

@ -66,18 +66,43 @@ TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types)
TTS_CASE_TPL("Test one index coefficient accessors", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
using base = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
T data[] = {1,2,3,4,5,6,7,8,9,10,11,12};
rotgen::map<base> a(data,4,3);
for(rotgen::Index i=0;i<a.size();i++)
TTS_EQUAL(a(i), data[i]) << "Index: " << i << "\n";
auto m = [&]()
{
if constexpr(O::value == rotgen::ColMajor)
{
using base = rotgen::matrix<T,1,rotgen::Dynamic>;
rotgen::map<base> a(data,12);
return a;
}
else
{
using base = rotgen::matrix<T,rotgen::Dynamic,1>;
rotgen::map<base> a(data,12);
return a;
}
}();
a(1) = 42;
TTS_EXPECT(m.IsVectorAtCompileTime);
for(rotgen::Index i=0;i<m.size();i++)
TTS_EQUAL(m(i), data[i]) << "Index: " << i << "\n";
for(rotgen::Index i=0;i<m.size();i++)
TTS_EQUAL(m[i], data[i]) << "Index: " << i << "\n";
m(1) = 42;
TTS_EQUAL(data[1], 42);
T& ref = a(2);
T& ref = m(2);
ref = 17;
TTS_EQUAL(data[2], 17);
m[1] = 77;
TTS_EQUAL(data[1], 77);
T& bref = m[3];
bref = 331;
TTS_EQUAL(data[3], 331);
};

View file

@ -138,27 +138,30 @@ TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types)
TTS_CASE_TPL("Test one index coefficient accessors", rotgen::tests::types)
<typename T, typename O>( tts::type< tts::types<T,O>> )
{
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(2, 4);
auto a = [&]()
{
if constexpr(O::value == rotgen::ColMajor) return rotgen::matrix<T,1,rotgen::Dynamic>(1,8);
else return rotgen::matrix<T,rotgen::Dynamic,1>(8,1);
}();
TTS_EXPECT(a.IsVectorAtCompileTime);
for(rotgen::Index s=0;s<a.size();++s)
a(s) = s+1;
int i = 1;
if constexpr(!O::value)
{
for(rotgen::Index c=0;c<a.cols();++c)
for(rotgen::Index r=0;r<a.rows();++r)
a(r, c) = i++;
}
else
{
for(rotgen::Index r=0;r<a.rows();++r)
for(rotgen::Index c=0;c<a.cols();++c)
a(r, c) = i++;
}
for(rotgen::Index s=0;s<a.size();++s)
TTS_EQUAL(a(s), i++) << a;
i = 1;
for(rotgen::Index r=0;r<a.rows()*a.cols();++r)
TTS_EQUAL(a(r), i++) << a;
for(rotgen::Index s=0;s<a.size();++s)
TTS_EQUAL(a[s], i++) << a;
T& ref = a(2);
ref = 999.5;
TTS_EQUAL(a(2), 999.5);
T& bref = a[3];
bref = 42.5;
TTS_EQUAL(a[3], 42.5);
};