diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 0be61fd0..8dc7273f 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -48,18 +48,15 @@ public: *sizes = std::max(*sizes, (ptrdiff_t)(m_initializers.size())); for (auto subinitializer : m_initializers) - subinitializer.FillSizes(sizes + 1); + subinitializer.FillSizes(sizes - 1); } - void FillValues(T * values, ptrdiff_t * sizes, ptrdiff_t accumulator) + void FillValues(T * origin, ptrdiff_t prev, ptrdiff_t * sizes) { ptrdiff_t pos = 0; for (auto subinitializer : m_initializers) - { - subinitializer.FillValues(values + pos * accumulator, sizes + 1, accumulator * *sizes); - ++pos; - } + subinitializer.FillValues(origin, pos++ + prev * *sizes, sizes - 1); } private: @@ -83,17 +80,14 @@ public: *sizes = std::max(*sizes, (ptrdiff_t)(m_initializers.size())); } - void FillValues(T * values, ptrdiff_t * sizes, ptrdiff_t accumulator) + void FillValues(T * origin, ptrdiff_t prev, ptrdiff_t * sizes) { UNUSED(sizes); ptrdiff_t pos = 0; for (auto value : m_initializers) - { - *(values + pos * accumulator) = value; - ++pos; - } + *(origin + prev * *sizes + pos++) = value; } private: @@ -121,17 +115,17 @@ public: inline arraynd(std::initializer_list > initializer) { - m_sizes[0] = initializer.size(); + m_sizes[N - 1] = initializer.size(); for (auto inner_initializer : initializer) - inner_initializer.FillSizes(&m_sizes[1]); + inner_initializer.FillSizes(&m_sizes[N - 2]); FixSizes(); ptrdiff_t pos = 0; for (auto inner_initializer : initializer) - inner_initializer.FillValues(&super::operator[](pos++), &m_sizes[1], m_sizes[0]); + inner_initializer.FillValues(&super::operator[](0), pos++, &m_sizes[N - 2]); } /* Access elements directly using an ivec2, ivec3 etc. index */ diff --git a/test/unit/arraynd.cpp b/test/unit/arraynd.cpp index 5efe41ae..1b018395 100644 --- a/test/unit/arraynd.cpp +++ b/test/unit/arraynd.cpp @@ -54,14 +54,14 @@ LOLUNIT_FIXTURE(ArrayNDTest) arraynd<30, uint8_t> c; arraynd<40, double> d; - arraynd<3, double> e = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} }}; + 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[1][0][0], 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][1][0], 4); + LOLUNIT_ASSERT_EQUAL(e[0][0][1], 5); LOLUNIT_ASSERT_EQUAL(e[1][0][1], 6); - LOLUNIT_ASSERT_EQUAL(e[1][1][0], 7); + LOLUNIT_ASSERT_EQUAL(e[0][1][1], 7); LOLUNIT_ASSERT_EQUAL(e[1][1][1], 8); }