瀏覽代碼

math: initialiser lists for Array2D and Array3D.

undefined
Sam Hocevar 10 年之前
父節點
當前提交
0b97173ee8
共有 5 個文件被更改,包括 96 次插入1 次删除
  1. +1
    -1
      src/lol/base/array.h
  2. +11
    -0
      src/lol/math/array2d.h
  3. +15
    -0
      src/lol/math/array3d.h
  4. +25
    -0
      test/unit/array2d.cpp
  5. +44
    -0
      test/unit/array3d.cpp

+ 1
- 1
src/lol/base/array.h 查看文件

@@ -47,7 +47,7 @@ public:
{
}

inline ArrayBase(std::initializer_list<Element> list)
inline ArrayBase(std::initializer_list<Element> const &list)
: m_data(0),
m_count(0),
m_reserved(0)


+ 11
- 0
src/lol/math/array2d.h 查看文件

@@ -44,6 +44,17 @@ public:
{
}

inline Array2D(std::initializer_list<
std::initializer_list<Element>> const &list)
: m_size(list.size() ? (*list.begin()).size() : 0,
list.size())
{
Super::Reserve(m_size.x * m_size.y);
for (auto l : list)
for (auto elem : l)
Super::Push(elem);
}

inline Array2D(int w, int h)
{
SetSize(ivec2(w, h));


+ 15
- 0
src/lol/math/array3d.h 查看文件

@@ -44,6 +44,21 @@ public:
{
}

inline Array3D(std::initializer_list<
std::initializer_list<
std::initializer_list<Element>>> const &list)
: m_size(list.size() && (*list.begin()).size() ?
(*(*list.begin()).begin()).size() : 0,
list.size() ? (*list.begin()).size() : 0,
list.size())
{
Super::Reserve(m_size.x * m_size.y * m_size.z);
for (auto ll : list)
for (auto l : ll)
for (auto elem : l)
Super::Push(elem);
}

inline Array3D(int w, int h, int d)
{
SetSize(ivec3(w, h, d));


+ 25
- 0
test/unit/array2d.cpp 查看文件

@@ -45,6 +45,31 @@ LOLUNIT_FIXTURE(Array2DTest)
LOLUNIT_ASSERT_EQUAL(b[0][9], 6);
LOLUNIT_ASSERT_EQUAL(b[9][9], 8);
}

LOLUNIT_TEST(Array2DInit)
{
Array2D<int> a = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 8, 7, 6 } };

LOLUNIT_ASSERT_EQUAL(a.GetSize().x, 4);
LOLUNIT_ASSERT_EQUAL(a.GetSize().y, 3);

LOLUNIT_ASSERT_EQUAL(a[0][0], 1);
LOLUNIT_ASSERT_EQUAL(a[1][0], 2);
LOLUNIT_ASSERT_EQUAL(a[2][0], 3);
LOLUNIT_ASSERT_EQUAL(a[3][0], 4);

LOLUNIT_ASSERT_EQUAL(a[0][1], 5);
LOLUNIT_ASSERT_EQUAL(a[1][1], 6);
LOLUNIT_ASSERT_EQUAL(a[2][1], 7);
LOLUNIT_ASSERT_EQUAL(a[3][1], 8);

LOLUNIT_ASSERT_EQUAL(a[0][2], 9);
LOLUNIT_ASSERT_EQUAL(a[1][2], 8);
LOLUNIT_ASSERT_EQUAL(a[2][2], 7);
LOLUNIT_ASSERT_EQUAL(a[3][2], 6);
}
};

} /* namespace lol */


+ 44
- 0
test/unit/array3d.cpp 查看文件

@@ -45,6 +45,50 @@ LOLUNIT_FIXTURE(Array3DTest)
LOLUNIT_ASSERT_EQUAL(b[0][9][9], 6);
LOLUNIT_ASSERT_EQUAL(b[9][9][9], 8);
}

LOLUNIT_TEST(Array3DInit)
{
Array3D<int> a = { { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 8, 7, 6 } },
{ { -1, -2, -3, -4 },
{ -5, -6, -7, -8 },
{ -9, -8, -7, -6 } } };

LOLUNIT_ASSERT_EQUAL(a.GetSize().x, 4);
LOLUNIT_ASSERT_EQUAL(a.GetSize().y, 3);
LOLUNIT_ASSERT_EQUAL(a.GetSize().z, 2);

LOLUNIT_ASSERT_EQUAL(a[0][0][0], 1);
LOLUNIT_ASSERT_EQUAL(a[1][0][0], 2);
LOLUNIT_ASSERT_EQUAL(a[2][0][0], 3);
LOLUNIT_ASSERT_EQUAL(a[3][0][0], 4);

LOLUNIT_ASSERT_EQUAL(a[0][1][0], 5);
LOLUNIT_ASSERT_EQUAL(a[1][1][0], 6);
LOLUNIT_ASSERT_EQUAL(a[2][1][0], 7);
LOLUNIT_ASSERT_EQUAL(a[3][1][0], 8);

LOLUNIT_ASSERT_EQUAL(a[0][2][0], 9);
LOLUNIT_ASSERT_EQUAL(a[1][2][0], 8);
LOLUNIT_ASSERT_EQUAL(a[2][2][0], 7);
LOLUNIT_ASSERT_EQUAL(a[3][2][0], 6);

LOLUNIT_ASSERT_EQUAL(a[0][0][1], -1);
LOLUNIT_ASSERT_EQUAL(a[1][0][1], -2);
LOLUNIT_ASSERT_EQUAL(a[2][0][1], -3);
LOLUNIT_ASSERT_EQUAL(a[3][0][1], -4);

LOLUNIT_ASSERT_EQUAL(a[0][1][1], -5);
LOLUNIT_ASSERT_EQUAL(a[1][1][1], -6);
LOLUNIT_ASSERT_EQUAL(a[2][1][1], -7);
LOLUNIT_ASSERT_EQUAL(a[3][1][1], -8);

LOLUNIT_ASSERT_EQUAL(a[0][2][1], -9);
LOLUNIT_ASSERT_EQUAL(a[1][2][1], -8);
LOLUNIT_ASSERT_EQUAL(a[2][2][1], -7);
LOLUNIT_ASSERT_EQUAL(a[3][2][1], -6);
}
};

} /* namespace lol */


Loading…
取消
儲存