diff --git a/src/lol/math/all.h b/src/lol/math/all.h index 028fa0e5..20b48a50 100644 --- a/src/lol/math/all.h +++ b/src/lol/math/all.h @@ -20,8 +20,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/src/lol/math/array2d.h b/src/lol/math/array2d.h deleted file mode 100644 index 96fa7373..00000000 --- a/src/lol/math/array2d.h +++ /dev/null @@ -1,178 +0,0 @@ -// -// Lol Engine -// -// Copyright: (c) 2010-2014 Sam Hocevar -// (c) 2013-2014 Benjamin "Touky" Huet -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. -// - -// -// The array2d class -// ----------------- -// A very simple 2D array class allowing var[i][j] indexing, with some nice -// additional features, eg. array2d for automatic arrays of tuples. -// - -// -// FIXME: This file is in lol/math/ instead of lol/base/ because it -// uses ivec2. -// - -#if !defined __LOL_MATH_ARRAY2D_H__ -#define __LOL_MATH_ARRAY2D_H__ - -#include -#include - -namespace lol -{ - -template -class array2d : protected array -{ -public: - typedef array super; - typedef typename super::element_t element_t; - - inline array2d() - : m_size(0, 0) - { - } - - inline array2d(std::initializer_list< - std::initializer_list> const &list) - : m_size(list.size() ? (int)(*list.begin()).size() : 0, - (int)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)); - } - - inline array2d(ivec2 size) - { - SetSize(size); - } - - /* Access elements directly using an ivec2 index */ - inline element_t const &operator [](ivec2 pos) const - { - ASSERT(pos.x >= 0); - ASSERT(pos.y >= 0); - ASSERT(pos.x < m_size.x); - ASSERT(pos.y < m_size.y); - - ptrdiff_t n = pos.y * m_size.x + pos.x; - ASSERT(n >= 0); - ASSERT(n < this->m_count); - - return this->m_data[n]; - } - - inline element_t &operator [](ivec2 pos) - { - ASSERT(pos.x >= 0); - ASSERT(pos.y >= 0); - ASSERT(pos.x < m_size.x); - ASSERT(pos.y < m_size.y); - - ptrdiff_t n = pos.y * m_size.x + pos.x; - ASSERT(n >= 0); - ASSERT(n < this->m_count); - - return this->m_data[n]; - } - - class Column - { - public: - inline Column(array2d &array, ptrdiff_t i) - : m_array(array), - m_column(i) - { - } - - inline element_t &operator [](ptrdiff_t j) - { - ASSERT(j >= 0); - ASSERT(j < m_array.m_size.y); - return m_array[ivec2(m_column, j)]; - } - - private: - array2d &m_array; - ptrdiff_t m_column; - }; - - class ConstColumn - { - public: - inline ConstColumn(array2d const &array, ptrdiff_t i) - : m_array(array), - m_column(i) - { - } - - inline element_t const &operator [](ptrdiff_t j) const - { - ASSERT(j >= 0); - ASSERT(j < m_array.m_size.y); - return m_array[ivec2(m_column, j)]; - } - - private: - array2d const &m_array; - ptrdiff_t m_column; - }; - - /* Access addressable columns, allowing for array[i][j] syntax. */ - inline class Column operator [](ptrdiff_t i) - { - ASSERT(i >= 0); - ASSERT(i < m_size.x); - return Column(*this, i); - } - - inline class ConstColumn operator [](ptrdiff_t i) const - { - ASSERT(i >= 0); - ASSERT(i < m_size.x); - return ConstColumn(*this, i); - } - - /* Resize the array. - * FIXME: data gets scrambled; should we care? */ - inline void SetSize(ivec2 size, element_t e = element_t()) - { - this->Resize(size.x * size.y, e); - m_size = size; - } - - inline ivec2 GetSize() const - { - return m_size; - } - -public: - inline element_t *Data() { return super::Data(); } - inline element_t const *Data() const { return super::Data(); } - inline ptrdiff_t Count() const { return super::Count(); } - inline ptrdiff_t Bytes() const { return super::Bytes(); } - -private: - ivec2 m_size; -}; - -} /* namespace lol */ - -#endif // __LOL_MATH_ARRAY2D_H__ - diff --git a/src/lol/math/array3d.h b/src/lol/math/array3d.h deleted file mode 100644 index 783314f8..00000000 --- a/src/lol/math/array3d.h +++ /dev/null @@ -1,228 +0,0 @@ -// -// Lol Engine -// -// Copyright: (c) 2010-2014 Sam Hocevar -// (c) 2013-2014 Benjamin "Touky" Huet -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. -// - -// -// The array3d class -// ----------------- -// A very simple 3D array class allowing var[i][j][k] indexing, with some nice -// additional features, eg. array3d for automatic arrays of tuples. -// - -// -// FIXME: This file is in lol/math/ instead of lol/base/ because it -// uses ivec3. -// - -#if !defined __LOL_MATH_ARRAY3D_H__ -#define __LOL_MATH_ARRAY3D_H__ - -#include -#include - -namespace lol -{ - -template -class array3d : protected array -{ -public: - typedef array super; - typedef typename super::element_t element_t; - - inline array3d() - : m_size(0, 0, 0) - { - } - - inline array3d(std::initializer_list< - std::initializer_list< - std::initializer_list>> const &list) - : m_size(list.size() && (*list.begin()).size() ? - (int)(*(*list.begin()).begin()).size() : 0, - list.size() ? (int)(*list.begin()).size() : 0, - (int)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)); - } - - inline array3d(ivec3 size) - { - SetSize(size); - } - - /* Access elements directly using an ivec3 index */ - inline element_t const &operator [](ivec3 pos) const - { - ASSERT(pos.x >= 0); - ASSERT(pos.y >= 0); - ASSERT(pos.z >= 0); - ASSERT(pos.x < m_size.x); - ASSERT(pos.y < m_size.y); - ASSERT(pos.z < m_size.z); - - ptrdiff_t n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x; - ASSERT(n >= 0); - ASSERT(n < this->m_count); - - return this->m_data[n]; - } - - inline element_t &operator [](ivec3 pos) - { - ASSERT(pos.x >= 0); - ASSERT(pos.y >= 0); - ASSERT(pos.z >= 0); - ASSERT(pos.x < m_size.x); - ASSERT(pos.y < m_size.y); - ASSERT(pos.z < m_size.z); - - ptrdiff_t n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x; - ASSERT(n >= 0); - ASSERT(n < this->m_count); - - return this->m_data[n]; - } - - class Slice - { - public: - inline Slice(array3d &array, ptrdiff_t i) - : m_array(array), - m_slice(i) - { - } - - class Line - { - public: - inline Line(array3d &array, ivec2 ij) - : m_array(array), - m_line(ij) - { - } - - inline element_t &operator [](ptrdiff_t k) - { - ASSERT(k >= 0); - ASSERT(k < m_array.m_size.z); - return m_array[ivec3(m_line, k)]; - } - - private: - array3d &m_array; - ivec2 m_line; - }; - - inline class Line operator [](ptrdiff_t j) - { - ASSERT(j >= 0); - ASSERT(j < m_array.m_size.y); - return Line(m_array, ivec2(m_slice, j)); - } - - private: - array3d &m_array; - ptrdiff_t m_slice; - }; - - class ConstSlice - { - public: - inline ConstSlice(array3d const &array, ptrdiff_t i) - : m_array(array), - m_slice(i) - { - } - - class Line - { - public: - inline Line(array3d const &array, ivec2 ij) - : m_array(array), - m_line(ij) - { - } - - inline element_t const &operator [](ptrdiff_t k) const - { - ASSERT(k >= 0); - ASSERT(k < m_array.m_size.z); - return m_array[ivec3(m_line, k)]; - } - - private: - array3d const &m_array; - ivec2 m_line; - }; - - inline class Line operator [](ptrdiff_t j) const - { - ASSERT(j >= 0); - ASSERT(j < m_array.m_size.y); - return Line(m_array, ivec2(m_slice, j)); - } - - private: - array3d const &m_array; - ptrdiff_t m_slice; - }; - - /* Access addressable slices, allowing for array[i][j][k] syntax. */ - inline class Slice operator [](ptrdiff_t i) - { - ASSERT(i >= 0); - ASSERT(i < m_size.x); - return Slice(*this, i); - } - - inline class ConstSlice operator [](ptrdiff_t i) const - { - ASSERT(i >= 0); - ASSERT(i < m_size.x); - return ConstSlice(*this, i); - } - - /* Resize the array. - * FIXME: data gets scrambled; should we care? */ - inline void SetSize(ivec3 size, element_t e = element_t()) - { - this->Resize(size.x * size.y * size.z, e); - m_size = size; - } - - inline ivec3 GetSize() const - { - return m_size; - } - -public: - inline element_t *Data() { return super::Data(); } - inline element_t const *Data() const { return super::Data(); } - inline ptrdiff_t Count() const { return super::Count(); } - inline ptrdiff_t Bytes() const { return super::Bytes(); } - -private: - ivec3 m_size; -}; - -} /* namespace lol */ - -#endif // __LOL_MATH_ARRAY3D_H__ - diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 8dc7273f..9e16a4c7 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -43,15 +43,15 @@ public: { } - void FillSizes(ptrdiff_t * sizes) + void FillSizes(int * sizes) { - *sizes = std::max(*sizes, (ptrdiff_t)(m_initializers.size())); + *sizes = std::max((ptrdiff_t)*sizes, (ptrdiff_t)(m_initializers.size())); for (auto subinitializer : m_initializers) subinitializer.FillSizes(sizes - 1); } - void FillValues(T * origin, ptrdiff_t prev, ptrdiff_t * sizes) + void FillValues(T * origin, ptrdiff_t prev, int * sizes) { ptrdiff_t pos = 0; @@ -75,12 +75,12 @@ public: { } - void FillSizes(ptrdiff_t * sizes) + void FillSizes(int * sizes) { - *sizes = std::max(*sizes, (ptrdiff_t)(m_initializers.size())); + *sizes = std::max((ptrdiff_t)*sizes, (ptrdiff_t)(m_initializers.size())); } - void FillValues(T * origin, ptrdiff_t prev, ptrdiff_t * sizes) + void FillValues(T * origin, ptrdiff_t prev, int * sizes) { UNUSED(sizes); @@ -107,7 +107,7 @@ public: { } - inline arraynd(vec_t sizes, element_t e = element_t()) + inline arraynd(vec_t sizes, element_t e = element_t()) : m_sizes(sizes) { FixSizes(e); @@ -137,7 +137,7 @@ public: return super::operator[](n); } - inline element_t & operator[](vec_t const &pos) + inline element_t & operator[](vec_t const &pos) { return const_cast( const_cast const&>(*this)[pos]); @@ -207,13 +207,13 @@ public: /* Resize the array. * FIXME: data gets scrambled; should we care? */ - inline void SetSize(vec_t sizes, element_t e = element_t()) + inline void SetSize(vec_t sizes, element_t e = element_t()) { m_sizes = sizes; FixSizes(e); } - inline vec_t GetSize() const + inline vec_t GetSize() const { return this->m_sizes; } @@ -235,9 +235,12 @@ private: this->Resize(total_size, e); } - vec_t m_sizes; + vec_t m_sizes; }; +template using array2d = arraynd<2, T...>; +template using array3d = arraynd<3, T...>; + } /* namespace lol */ #endif // __LOL_MATH_ARRAYND_H__ diff --git a/test/unit/arraynd.cpp b/test/unit/arraynd.cpp index 1ff57b95..d7bdc418 100644 --- a/test/unit/arraynd.cpp +++ b/test/unit/arraynd.cpp @@ -29,7 +29,7 @@ LOLUNIT_FIXTURE(ArrayNDTest) LOLUNIT_TEST(Array2D) { arraynd<2, int> a; - a.SetSize(vec_t(2, 2)); + a.SetSize(vec_t(2, 2)); /* Non-const accessors */ a[0][0] = 1; @@ -98,7 +98,7 @@ LOLUNIT_FIXTURE(ArrayNDTest) LOLUNIT_TEST(ArrayNDInit) { int const NDIM = 8; - vec_t size; + vec_t size; for (int i = 0; i < NDIM; ++i) size[i] = 5;