Browse Source

arraynd: implementation/correction of the initializer_list constructor

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 10 years ago
parent
commit
a28832a53b
2 changed files with 23 additions and 5 deletions
  1. +13
    -5
      src/lol/math/arraynd.h
  2. +10
    -0
      test/unit/arraynd.cpp

+ 13
- 5
src/lol/math/arraynd.h View File

@@ -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<element_t, N> initializer)
inline arraynd(std::initializer_list<arraynd_initializer<element_t, N - 1> > 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 */


+ 10
- 0
test/unit/arraynd.cpp View File

@@ -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)


Loading…
Cancel
Save