From a28832a53b430d4d13ec448dea81048cf2f182eb Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Tue, 29 Jul 2014 20:47:25 +0000 Subject: [PATCH] arraynd: implementation/correction of the initializer_list constructor --- src/lol/math/arraynd.h | 18 +++++++++++++----- test/unit/arraynd.cpp | 10 ++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 7957489c..0be61fd0 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -45,7 +45,7 @@ public: void FillSizes(ptrdiff_t * sizes) { - *sizes = std::max(*sizes, m_initializers.size()); + *sizes = std::max(*sizes, (ptrdiff_t)(m_initializers.size())); for (auto subinitializer : m_initializers) subinitializer.FillSizes(sizes + 1); @@ -80,7 +80,7 @@ public: void FillSizes(ptrdiff_t * sizes) { - *sizes = std::max(*sizes, m_initializers.size()); + *sizes = std::max(*sizes, (ptrdiff_t)(m_initializers.size())); } void FillValues(T * values, ptrdiff_t * sizes, ptrdiff_t accumulator) @@ -119,11 +119,19 @@ public: FixSizes(e); } - inline arraynd(arraynd_initializer initializer) + inline arraynd(std::initializer_list > initializer) { - initializer.FillSizes(&m_sizes[0]); + m_sizes[0] = initializer.size(); + + for (auto inner_initializer : initializer) + inner_initializer.FillSizes(&m_sizes[1]); + FixSizes(); - initializer.FillValues(&super::operator[](0), &m_sizes[0], 1); + + ptrdiff_t pos = 0; + + for (auto inner_initializer : initializer) + inner_initializer.FillValues(&super::operator[](pos++), &m_sizes[1], m_sizes[0]); } /* Access elements directly using an ivec2, ivec3 etc. index */ diff --git a/test/unit/arraynd.cpp b/test/unit/arraynd.cpp index fe24dbf8..5efe41ae 100644 --- a/test/unit/arraynd.cpp +++ b/test/unit/arraynd.cpp @@ -53,6 +53,16 @@ LOLUNIT_FIXTURE(ArrayNDTest) arraynd<20, float> b; arraynd<30, uint8_t> c; arraynd<40, double> d; + + arraynd<3, double> e = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} }}; + LOLUNIT_ASSERT_EQUAL(e[0][0][0], 1); + LOLUNIT_ASSERT_EQUAL(e[0][0][1], 2); + LOLUNIT_ASSERT_EQUAL(e[0][1][0], 3); + LOLUNIT_ASSERT_EQUAL(e[0][1][1], 4); + LOLUNIT_ASSERT_EQUAL(e[1][0][0], 5); + LOLUNIT_ASSERT_EQUAL(e[1][0][1], 6); + LOLUNIT_ASSERT_EQUAL(e[1][1][0], 7); + LOLUNIT_ASSERT_EQUAL(e[1][1][1], 8); } LOLUNIT_TEST(ArrayNDInit)