diff options
Diffstat (limited to 'src/misc/fixedsizearray.hpp')
-rw-r--r-- | src/misc/fixedsizearray.hpp | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/src/misc/fixedsizearray.hpp b/src/misc/fixedsizearray.hpp index 3e39cc4d9..d6c1242f3 100644 --- a/src/misc/fixedsizearray.hpp +++ b/src/misc/fixedsizearray.hpp @@ -18,61 +18,61 @@ * Upon construction it preallocates fixed size block of memory * for all items, but doesn't construct them. Item's construction * is delayed. */ -template <class Titem_, int Tcapacity_> -struct CFixedSizeArrayT { +template <class T, int C> +struct FixedSizeArray { /** the only member of fixed size array is pointer to the block - * of C array of items. Header can be found on the offset -sizeof(CHdr). */ - Titem_ *m_items; + * of C array of items. Header can be found on the offset -sizeof(ArrayHeader). */ + T *data; /** header for fixed size array */ - struct CHdr + struct ArrayHeader { - int m_num_items; ///< number of items in the array - int m_ref_cnt; ///< block reference counter (used by copy constructor and by destructor) + int items; ///< number of items in the array + int reference_count; ///< block reference counter (used by copy constructor and by destructor) }; /* make types and constants visible from outside */ - typedef Titem_ Titem; // type of array item + typedef T Titem; // type of array item - static const int Tcapacity = Tcapacity_; // the array capacity (maximum size) - static const int TitemSize = sizeof(Titem_); // size of item - static const int ThdrSize = sizeof(CHdr); // size of header + static const int Tcapacity = C; // the array capacity (maximum size) + static const int Tsize = sizeof(T); // size of item + static const int HeaderSize = sizeof(ArrayHeader); // size of header /** Default constructor. Preallocate space for items and header, then initialize header. */ - CFixedSizeArrayT() + FixedSizeArray() { /* allocate block for header + items (don't construct items) */ - m_items = (Titem*)((MallocT<int8>(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize); + data = (Titem*)((MallocT<int8>(HeaderSize + Tcapacity * Tsize)) + HeaderSize); SizeRef() = 0; // initial number of items RefCnt() = 1; // initial reference counter } /** Copy constructor. Preallocate space for items and header, then initialize header. */ - CFixedSizeArrayT(const CFixedSizeArrayT<Titem_, Tcapacity_>& src) + FixedSizeArray(const FixedSizeArray<T, C>& src) { /* share block (header + items) with the source array */ - m_items = src.m_items; + data = src.data; RefCnt()++; // now we share block with the source } /** destroy remaining items and free the memory block */ - ~CFixedSizeArrayT() + ~FixedSizeArray() { /* release one reference to the shared block */ if ((--RefCnt()) > 0) return; // and return if there is still some owner Clear(); /* free the memory block occupied by items */ - free(((int8*)m_items) - ThdrSize); - m_items = NULL; + free(((int8*)data) - HeaderSize); + data = NULL; } /** Clear (destroy) all items */ FORCEINLINE void Clear() { /* walk through all allocated items backward and destroy them */ - for (Titem *pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) { - pItem->~Titem_(); + for (Titem *pItem = &data[Length() - 1]; pItem >= data; pItem--) { + pItem->~T(); } /* number of items become zero */ SizeRef() = 0; @@ -80,30 +80,30 @@ struct CFixedSizeArrayT { protected: /** return reference to the array header (non-const) */ - FORCEINLINE CHdr& Hdr() { return *(CHdr*)(((int8*)m_items) - ThdrSize); } + FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((int8*)data) - HeaderSize); } /** return reference to the array header (const) */ - FORCEINLINE const CHdr& Hdr() const { return *(CHdr*)(((int8*)m_items) - ThdrSize); } + FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((int8*)data) - HeaderSize); } /** return reference to the block reference counter */ - FORCEINLINE int& RefCnt() { return Hdr().m_ref_cnt; } + FORCEINLINE int& RefCnt() { return Hdr().reference_count; } /** return reference to number of used items */ - FORCEINLINE int& SizeRef() { return Hdr().m_num_items; } + FORCEINLINE int& SizeRef() { return Hdr().items; } public: /** return number of used items */ - FORCEINLINE int Size() const { return Hdr().m_num_items; } + FORCEINLINE int Length() const { return Hdr().items; } /** return true if array is full */ - FORCEINLINE bool IsFull() const { return Size() >= Tcapacity; }; + FORCEINLINE bool IsFull() const { return Length() >= Tcapacity; }; /** return true if array is empty */ - FORCEINLINE bool IsEmpty() const { return Size() <= 0; }; + FORCEINLINE bool IsEmpty() const { return Length() <= 0; }; /** index validation */ - FORCEINLINE void CheckIdx(int idx) const { assert(idx >= 0); assert(idx < Size()); } + FORCEINLINE void CheckIdx(int index) const { assert(index >= 0); assert(index < Length()); } /** add (allocate), but don't construct item */ - FORCEINLINE Titem& AddNC() { assert(!IsFull()); return m_items[SizeRef()++]; } + FORCEINLINE Titem& Append() { assert(!IsFull()); return data[SizeRef()++]; } /** add and construct item using default constructor */ - FORCEINLINE Titem& Add() { Titem& item = AddNC(); new(&item)Titem; return item; } + FORCEINLINE Titem& AppendC() { Titem& item = Append(); new(&item)Titem; return item; } /** return item by index (non-const version) */ - FORCEINLINE Titem& operator [] (int idx) { CheckIdx(idx); return m_items[idx]; } + FORCEINLINE Titem& operator [] (int index) { CheckIdx(index); return data[index]; } /** return item by index (const version) */ - FORCEINLINE const Titem& operator [] (int idx) const { CheckIdx(idx); return m_items[idx]; } + FORCEINLINE const Titem& operator [] (int index) const { CheckIdx(index); return data[index]; } }; #endif /* FIXEDSIZEARRAY_HPP */ |