tc3d_array.h
来自「自己写的一些基本的3d引擎的基础的代码」· C头文件 代码 · 共 654 行 · 第 1/2 页
H
654 行
/**
* Comet 3D Engine file (c) 2007 - 2008 Tianjie Wei, THSystems Research Group
*
* Released under BSD license, please refer to license.txt for more information
*/
#ifndef _TC3D_ARRAY_H_
#define _TC3D_ARRAY_H_
#include "TC3D_Allocator.h"
namespace C3D
{
namespace Util
{
const TC3D_U32 CC3D_ARRAY_INIT_SIZE = 8;
/**
* Variable sized array similar to the STL's vector class
*
* @Author Tianjie (James) Wei
* @Version 3.0
*/
template < class T, class TAlloc = TC3D_Allocator<T> >
class TC3D_Array
{
private:
friend class CC3D_Iterator;
T *pData;
TC3D_U32 tSize;
TC3D_U32 tAlloc;
TC3D_Bool tSorted;
TAlloc tAllocator;
public:
/**
* Array iterator class
*
* @Author Tianjie (James) Wei
* @Version 3.0
*/
class CC3D_Iterator
{
private:
friend class TC3D_Array<T, TAlloc>;
T *pData;
TC3D_Array<T, TAlloc> *pBase;
/** Iterator constructor */
inline CC3D_Iterator(T *current, TC3D_Array<T, TAlloc> *base)
{
pData = current;
pBase = base;
}
public:
/** Default constructor */
inline CC3D_Iterator()
{
pData = DC3D_NULL;
pBase = DC3D_NULL;
}
/** Copy constructor */
inline CC3D_Iterator(const CC3D_Iterator &src)
{
pData = src.pData;
pBase = src.pBase;
}
/** Assignment operator */
inline CC3D_Iterator &operator = (const CC3D_Iterator &src)
{
if(this != &src)
{
pData = src.pData;
pBase = src.pBase;
}
return *this;
}
/** Gets the constant pointer */
inline const T *Pointer_Const() const
{
return pData;
}
/** Gets the pointer */
inline T *Pointer() const
{
return pData;
}
/** Dereferences the data */
inline T &operator * () const
{
MC3D_DEBUG_ASSERT((pData - pBase->pData) < (TC3D_S32)pBase->tSize);
return *pData;
}
/** Member accessing */
inline T *operator -> () const
{
MC3D_DEBUG_ASSERT((pData - pBase->pData) < (TC3D_S32)pBase->tSize);
return pData;
}
/** Pre increment pointer */
CC3D_Iterator &operator ++ ()
{
MC3D_DEBUG_ASSERT(pData < (pBase->pData + pBase->tSize));
pData++;
return *this;
}
/** Pre decrement pointer */
CC3D_Iterator &operator -- ()
{
MC3D_DEBUG_ASSERT(pData > pBase->pData);
pData--;
return *this;
}
/** Post increment pointer */
CC3D_Iterator operator ++ (TC3D_S32 dummy)
{
MC3D_DEBUG_ASSERT(pData < (pBase->pData + pBase->tSize));
CC3D_Iterator temp(pData, pBase);
pData++;
return temp;
}
/** Post decrement pointer */
CC3D_Iterator operator -- (TC3D_S32 dummy)
{
MC3D_DEBUG_ASSERT(pData > pBase->pData);
CC3D_Iterator temp(pData, pBase);
pData--;
return temp;
}
/** Addition operator */
CC3D_Iterator operator + (TC3D_S32 value) const
{
CC3D_Iterator temp(pData, pBase);
if(value >= 0)
{
while(value--)
++temp;
}
else
{
while(value++)
--temp;
}
return temp;
}
/** Subtraction operator */
CC3D_Iterator operator - (TC3D_S32 value) const
{
CC3D_Iterator temp(pData, pBase);
temp += (-value);
return temp;
}
/** Subtraction operator */
inline TC3D_S32 operator - (const CC3D_Iterator &src) const
{
return (TC3D_S32)(pData - src.pData);
}
/** Addition and assignment operator */
CC3D_Iterator &operator += (TC3D_S32 value)
{
if(value >= 0)
{
while(value--)
++(*this);
}
else
{
while(value++)
--(*this);
}
return *this;
}
/** Subtraction and assignment operator */
CC3D_Iterator &operator -= (TC3D_S32 value) const
{
*this += (-value);
return *this;
}
/** Iterator comparison */
TC3D_Bool operator == (const CC3D_Iterator &src) const
{
return (pData == src.pData);
}
/** Iterator comparison */
inline TC3D_Bool operator != (const CC3D_Iterator &src) const
{
return (pData != src.pData);
}
/** Iterator comparison */
inline TC3D_Bool operator >= (const CC3D_Iterator &src) const
{
return (pData >= src.pData);
}
/** Iterator comparison */
inline TC3D_Bool operator <= (const CC3D_Iterator &src) const
{
return (pData <= src.pData);
}
/** Iterator comparison */
inline TC3D_Bool operator > (const CC3D_Iterator &src) const
{
return (pData > src.pData);
}
/** Iterator comparison */
inline TC3D_Bool operator < (const CC3D_Iterator &src) const
{
return (pData < src.pData);
}
};
public:
/** Construct the array */
inline TC3D_Array()
{
pData = NULL;
tSize = 0;
tAlloc = 0;
tSorted = DC3D_FALSE;
}
/** Construct the array with initial an size */
explicit TC3D_Array(TC3D_U32 size)
{
pData = NULL;
tSize = 0;
tAlloc = 0;
tSorted = DC3D_FALSE;
Adjust(size);
}
/** Copy constructor */
TC3D_Array(const TC3D_Array<T, TAlloc> &src)
{
pData = NULL;
tSize = 0;
tAlloc = 0;
Adjust(src.tAlloc);
tSize = src.tSize;
tSorted = src.tSorted;
for(TC3D_U32 i = 0; i < src.tSize; i++)
tAllocator.Construct(pData + i, src.pData[i]);
}
/** Destruct the array */
virtual ~TC3D_Array()
{
Clear();
}
/**
* Resize the array so that the allocated components
* are count as used. This can speed up some importers
* since the memeory will only be allocated once at
* the beginning.
*
* @param size size to set
* @return none
*/
virtual TC3D_Void Resize(TC3D_U32 size)
{
Adjust(size);
tSize = tAlloc;
}
/**
* Gets the element at a particular position
*
* @param index element index
* @return reference to the element
*/
inline virtual const T &At(TC3D_U32 index) const
{
MC3D_DEBUG_ASSERT(index < tSize);
return pData[index];
}
/**
* Assigns an element at a particular position
*
* @param index element index
* @data data datat to be assigned
* @return none
*/
inline virtual TC3D_Void Assign(TC3D_U32 index, const T &item)
{
MC3D_DEBUG_ASSERT(index < tSize);
pData[index] = item;
}
/**
* Expand or shrink the array as needed, this function
* will adjust the buffer size accordingly, if the size
* needed is greater, additional memory is allocated
* and the original data is copied over. If the needed
* size is less, then the additional memory is freed.
*
* @param size size to adjust the array to
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?