2012-11-15 00:39:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
template<typename T> class Array
|
|
|
|
{
|
|
|
|
u32 m_count;
|
|
|
|
T* m_array;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Array()
|
|
|
|
: m_count(0)
|
|
|
|
, m_array(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Array()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool RemoveAt(const u32 from, const u32 count = 1)
|
|
|
|
{
|
|
|
|
if(!GetCount()) return false;
|
|
|
|
const u32 to = from + count;
|
|
|
|
if(to > GetCount()) return false;
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
for(u32 i=0; i<count; ++i) m_array[from + i].~T();
|
2012-11-15 00:39:56 +01:00
|
|
|
memmove(m_array + from, m_array + to, (m_count-to) * sizeof(T));
|
|
|
|
m_count -= count;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertRoomEnd(const u32 size)
|
|
|
|
{
|
|
|
|
_InsertRoomEnd(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InsertRoom(const u32 pos, const u32 size)
|
|
|
|
{
|
|
|
|
if(pos >= m_count) return false;
|
|
|
|
|
|
|
|
_InsertRoomEnd(size);
|
|
|
|
memmove(m_array + pos + size, m_array + pos, sizeof(T) * (m_count - size - pos));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline bool Move(const u32 pos, T* data)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
if(!InsertRoom(pos, 1)) return false;
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
memcpy(m_array + pos, data, sizeof(T));
|
|
|
|
free(data);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline u32 Move(T* data)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
_InsertRoomEnd(1);
|
|
|
|
|
|
|
|
memcpy(m_array + GetCount() - 1, data, sizeof(T));
|
|
|
|
free(data);
|
|
|
|
|
|
|
|
return m_count - 1;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline bool Add(const u32 pos, T*& data)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
if(!InsertRoom(pos, 1)) return false;
|
|
|
|
|
|
|
|
memcpy(m_array + pos, data, sizeof(T));
|
2013-06-30 10:46:29 +02:00
|
|
|
free(data);
|
|
|
|
data = m_array + pos;
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline u32 Add(T*& data)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
_InsertRoomEnd(1);
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
memcpy(m_array + GetCount() - 1, data, sizeof(T));
|
|
|
|
free(data);
|
|
|
|
data = m_array + GetCount() - 1;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
return m_count - 1;
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline bool AddCpy(const u32 pos, const T* data, u64 count = 1)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
if(!InsertRoom(pos, count)) return false;
|
|
|
|
|
|
|
|
memcpy(m_array + pos, data, sizeof(T) * count);
|
|
|
|
|
|
|
|
return true;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline bool AddCpy(const u32 pos, const T& data)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
return AddCpy(pos, &data);
|
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline u32 AddCpy(const T* data, u64 count = 1)
|
|
|
|
{
|
|
|
|
_InsertRoomEnd(count);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
memcpy(m_array + m_count - count, data, sizeof(T)*count);
|
|
|
|
|
|
|
|
return m_count - count;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline u32 AddCpy(const T& data)
|
|
|
|
{
|
|
|
|
return AddCpy(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Clear()
|
|
|
|
{
|
2013-06-30 10:46:29 +02:00
|
|
|
u32 count = m_count;
|
2012-11-15 00:39:56 +01:00
|
|
|
m_count = 0;
|
2013-06-30 10:46:29 +02:00
|
|
|
for(u32 i=0; i<count; ++i) m_array[i].~T();
|
2012-11-15 00:39:56 +01:00
|
|
|
safe_delete(m_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T& Get(u32 num)
|
|
|
|
{
|
|
|
|
//if(num >= GetCount()) return *new T();
|
|
|
|
return m_array[num];
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 GetCount() const { return m_count; }
|
|
|
|
|
|
|
|
void SetCount(const u32 count, bool memzero = true)
|
|
|
|
{
|
|
|
|
if(GetCount() >= count) return;
|
|
|
|
|
|
|
|
_InsertRoomEnd(count - GetCount());
|
|
|
|
|
|
|
|
if(memzero) memset(m_array + GetCount(), 0, count - GetCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reserve(const u32 count)
|
|
|
|
{
|
|
|
|
SetCount(GetCount() + count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendFrom(const Array<T>& src)
|
|
|
|
{
|
|
|
|
if(!src.GetCount()) return;
|
|
|
|
|
|
|
|
Reserve(src.GetCount());
|
|
|
|
|
|
|
|
memcpy(m_array, &src[0], GetCount() * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyFrom(const Array<T>& src)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
AppendFrom(src);
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
inline T* GetPtr() { return m_array; }
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
T& operator[](u32 num) const { return m_array[num]; }
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
T* operator + (u32 right) const
|
|
|
|
{
|
|
|
|
return m_array + right;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator ->()
|
|
|
|
{
|
|
|
|
return m_array;
|
|
|
|
}
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
protected:
|
|
|
|
void _InsertRoomEnd(const u32 size)
|
|
|
|
{
|
|
|
|
if(!size) return;
|
|
|
|
|
|
|
|
m_array = m_count ? (T*)realloc(m_array, sizeof(T) * (m_count + size)) : (T*)malloc(sizeof(T) * size);
|
|
|
|
m_count += size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct Stack : public Array<T>
|
|
|
|
{
|
|
|
|
Stack() : Array<T>()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Stack()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Push(const T data) { AddCpy(data); }
|
|
|
|
|
|
|
|
T Pop()
|
|
|
|
{
|
|
|
|
const u32 pos = GetCount() - 1;
|
|
|
|
|
|
|
|
const T ret = Get(pos);
|
|
|
|
RemoveAt(pos);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-06 01:49:38 +02:00
|
|
|
template<typename T, size_t size> class SizedStack
|
|
|
|
{
|
|
|
|
T m_ptr[size];
|
|
|
|
uint m_count;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SizedStack()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~SizedStack()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Pop(T& dst)
|
|
|
|
{
|
|
|
|
if(!m_count)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dst = m_ptr[--m_count];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Push(const T& src)
|
|
|
|
{
|
|
|
|
if(m_count + 1 > size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_ptr[m_count++] = src;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetFreeCount() const
|
|
|
|
{
|
|
|
|
return size - m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetCount() const
|
|
|
|
{
|
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetMaxCount() const
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
template<typename T> class ArrayF
|
|
|
|
{
|
|
|
|
u32 m_count;
|
|
|
|
T** m_array;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ArrayF()
|
|
|
|
: m_count(0)
|
|
|
|
, m_array(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
virtual ~ArrayF()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool RemoveFAt(const u32 from, const u32 count = 1)
|
|
|
|
{
|
|
|
|
if(from + count > m_count) return false;
|
|
|
|
|
|
|
|
memmove(&m_array[from], &m_array[from+count], (m_count-(from+count)) * sizeof(T**));
|
|
|
|
|
|
|
|
m_count -= count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool RemoveAt(const u32 from, const u32 count = 1)
|
|
|
|
{
|
|
|
|
if(from + count > m_count) return false;
|
|
|
|
|
|
|
|
for(uint i = from; i < from + count; ++i)
|
|
|
|
{
|
|
|
|
free(m_array[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RemoveFAt(from, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline u32 Add(T& data)
|
|
|
|
{
|
|
|
|
return Add(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline u32 Add(T* data)
|
|
|
|
{
|
|
|
|
if(!m_array)
|
|
|
|
{
|
|
|
|
m_array = (T**)malloc(sizeof(T*));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_array = (T**)realloc(m_array, sizeof(T*) * (m_count + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_array[m_count] = data;
|
|
|
|
return m_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void ClearF()
|
|
|
|
{
|
|
|
|
if(m_count == 0) return;
|
|
|
|
|
|
|
|
m_count = 0;
|
|
|
|
m_array = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Clear()
|
|
|
|
{
|
|
|
|
if(m_count == 0) return;
|
|
|
|
|
|
|
|
m_count = 0;
|
2013-07-01 14:08:58 +02:00
|
|
|
safe_free(m_array);
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline T& Get(const u64 num)
|
|
|
|
{
|
|
|
|
//if(m_count <= num) *m_array[0]; //TODO
|
|
|
|
return *m_array[num];
|
|
|
|
}
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
T** operator + (u32 right) const
|
|
|
|
{
|
|
|
|
return m_array + right;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator ->()
|
|
|
|
{
|
|
|
|
return *m_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T** GetPtr()
|
|
|
|
{
|
|
|
|
return m_array;
|
|
|
|
}
|
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
inline u32 GetCount() const { return m_count; }
|
|
|
|
T& operator[](u32 num) const { return *m_array[num]; }
|
|
|
|
};
|
2013-06-30 10:46:29 +02:00
|
|
|
|
|
|
|
template<typename T> struct ScopedPtr
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
T* m_ptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedPtr() : m_ptr(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedPtr(T* ptr) : m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedPtr()
|
|
|
|
{
|
|
|
|
Swap(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() { return m_ptr; }
|
|
|
|
operator const T*() const { return m_ptr; }
|
|
|
|
|
|
|
|
T* operator ->() { return m_ptr; }
|
|
|
|
const T* operator ->() const { return m_ptr; }
|
|
|
|
|
|
|
|
void Swap(T* ptr)
|
|
|
|
{
|
|
|
|
delete m_ptr;
|
|
|
|
m_ptr = ptr;
|
|
|
|
}
|
|
|
|
};
|