From 3921c579e1750265277865100d13a30837f77b94 Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Sat, 12 Jul 2014 14:08:50 +0000 Subject: [PATCH] arraynd: specializing arraynd for dimension 1 --- src/lol/math/arraynd.h | 70 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/lol/math/arraynd.h b/src/lol/math/arraynd.h index 433dbc06..cac48e41 100644 --- a/src/lol/math/arraynd.h +++ b/src/lol/math/arraynd.h @@ -11,7 +11,7 @@ // // -// The arrayNd class +// The arraynd class // ----------------- // A N-Dimensional array class allowing var[i][j][k]... indexing, // @@ -184,7 +184,7 @@ public: } /* Access elements directly using index position */ - inline proxy operator [](size_t pos) const + inline proxy operator[](size_t pos) const { return proxy(this, m_sizes, pos, m_sizes[0]); } @@ -221,6 +221,72 @@ private: vec_t m_sizes; }; + +template +class arraynd<1, T...> : protected array +{ +public: + typedef array super; + typedef typename super::element_t element_t; + + inline arraynd() : + super(), + m_sizes() + { + } + + inline arraynd(vec_t sizes, element_t e = element_t()) : + super(), + m_sizes(sizes) + { + SetSize(m_sizes, e); + } + + inline arraynd(std::initializer_list initializer) : + super(), + m_sizes() + { + m_sizes[0] = initializer.size(); + SetSize(m_sizes[0]); + + size_t pos = 0; + for (auto element : initializer) + (*this)[pos++] = element; + } + + /* Access elements directly using index position */ + inline element_t & operator[](size_t pos) const + { + return super::operator[](pos); + } + + inline size_t ComputeTotalSize(vec_t sizes) + { + return m_sizes[0]; + } + + /* Resize the array. + * FIXME: data gets scrambled; should we care? */ + inline void SetSize(vec_t sizes, element_t e = element_t()) + { + this->Resize(ComputeTotalSize(sizes), e); + } + + inline vec_t GetSize() const + { + return ComputeTotalSize(this->m_sizes); + } + +public: + inline element_t *Data() { return super::Data(); } + inline element_t const *Data() const { return super::Data(); } + inline int Count() const { return super::Count(); } + inline int Bytes() const { return super::Bytes(); } + +private: + vec_t m_sizes; +}; + } /* namespace lol */ #endif // __LOL_MATH_ARRAYND_H__