Sfoglia il codice sorgente

core: add a dynamic Array class.

legacy
Sam Hocevar sam 12 anni fa
parent
commit
18cd98fe91
2 ha cambiato i file con 148 aggiunte e 11 eliminazioni
  1. +1
    -1
      src/Makefile.am
  2. +147
    -10
      src/array.h

+ 1
- 1
src/Makefile.am Vedi File

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


+ 147
- 10
src/array.h Vedi File

@@ -22,23 +22,160 @@
namespace lol
{

template<typename T> class Array
template<typename T1, typename T2 = void, typename T3 = void> 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<T1, T2, T3> 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<typename T1, typename T2> class Array<T1, T2, void>
{
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<T1, T2> 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<typename T1> class Array<T1, void, void>
{
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<T> const& operator+=(T const &x)
inline Array<T1> 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 */


Caricamento…
Annulla
Salva