diff --git a/src/Makefile.am b/src/Makefile.am index 1b07c773..c0196736 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ noinst_LIBRARIES = liblol.a liblol_a_SOURCES = \ - core.h tiler.cpp tiler.h dict.cpp dict.h \ + core.h tiler.cpp tiler.h dict.cpp dict.h array.h \ audio.cpp audio.h scene.cpp scene.h font.cpp font.h layer.cpp layer.h \ map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h log.cpp log.h \ diff --git a/src/array.h b/src/array.h index 3407e85e..66b89c8f 100644 --- a/src/array.h +++ b/src/array.h @@ -22,23 +22,160 @@ namespace lol { -template class Array +template class Array +{ +public: + typedef struct { T1 m1; T2 m2; T3 m3; } Element; + + inline Array() : m_data(0), m_count(0), m_reserved(0) {} + inline ~Array() { delete[] m_data; } + + inline Element& operator[](int n) + { + return m_data[n]; + } + + inline Element const& operator[](int n) const + { + return m_data[n]; + } + + inline Array const& operator+=(Element const &x) + { + Append(x.m1, x.m2, x.m3); + return *this; + } + + inline void Append(T1 const &m1, T2 const &m2, T3 const &m3) + { + if (m_count >= m_reserved) + Reserve(m_count * 13 / 8 + 8); + m_data[m_count].m1 = m1; + m_data[m_count].m2 = m2; + m_data[m_count].m3 = m3; + ++m_count; + } + + void Remove(int pos) + { + memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); + m_count--; + } + + void Remove(int pos, int count) + { + memmove(m_data + pos, m_data + pos + count, m_count - pos - count); + m_count -= count; + } + + void Reserve(int count) + { + if (count <= (int)m_reserved) + return; + + Element *tmp = new Element[count]; + if (m_data) + { + memcpy(tmp, m_data, m_count * sizeof(Element)); + delete[] m_data; + } + m_data = tmp; + m_reserved = count; + } + + inline int Count() const { return m_count; } + inline int Bytes() const { return m_count * sizeof(Element); } + +private: + Element *m_data; + int m_count, m_reserved; +}; + +template class Array +{ +public: + typedef struct { T1 m1; T2 m2; } Element; + + inline Array() : m_data(0), m_count(0), m_reserved(0) {} + inline ~Array() { delete[] m_data; } + + inline Element& operator[](int n) + { + return m_data[n]; + } + + inline Element const& operator[](int n) const + { + return m_data[n]; + } + + inline Array const& operator+=(Element const &x) + { + Append(x.m1, x.m2); + return *this; + } + + inline void Append(T1 const &m1, T2 const &m2) + { + if (m_count >= m_reserved) + Reserve(m_count * 13 / 8 + 8); + m_data[m_count].m1 = m1; + m_data[m_count].m2 = m2; + ++m_count; + } + + void Remove(int pos) + { + memmove(m_data + pos, m_data + pos + 1, m_count - pos - 1); + m_count--; + } + + void Remove(int pos, int count) + { + memmove(m_data + pos, m_data + pos + count, m_count - pos - count); + m_count -= count; + } + + void Reserve(int count) + { + if (count <= (int)m_reserved) + return; + + Element *tmp = new Element[count]; + if (m_data) + { + memcpy(tmp, m_data, m_count * sizeof(Element)); + delete[] m_data; + } + m_data = tmp; + m_reserved = count; + } + + inline int Count() const { return m_count; } + inline int Bytes() const { return m_count * sizeof(Element); } + +private: + Element *m_data; + int m_count, m_reserved; +}; + +template class Array { public: inline Array() : m_data(0), m_count(0), m_reserved(0) {} inline ~Array() { delete[] m_data; } - inline T& operator[](int n) + inline T1& operator[](int n) { return m_data[n]; } - inline T const& operator[](int n) const + inline T1 const& operator[](int n) const { return m_data[n]; } - inline Array const& operator+=(T const &x) + inline Array const& operator+=(T1 const &x) { if (m_count >= m_reserved) Reserve(m_count * 13 / 8 + 8); @@ -63,22 +200,22 @@ public: if (count <= (int)m_reserved) return; - T *tmp = new T[count]; + T1 *tmp = new T1[count]; if (m_data) { - memcpy(tmp, m_data, m_count * sizeof(T)); + memcpy(tmp, m_data, m_count * sizeof(T1)); delete[] m_data; } m_data = tmp; m_reserved = count; } - inline size_t Count() const { return m_count; } - inline size_t Bytes() const { return m_count * sizeof(T); } + inline int Count() const { return m_count; } + inline int Bytes() const { return m_count * sizeof(T1); } private: - T *m_data; - size_t m_count, m_reserved; + T1 *m_data; + int m_count, m_reserved; }; } /* namespace lol */