2006-05-27 16:12:16 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file fixedsizearray.hpp A fixed size array that doesn't create items until needed. */
|
2007-04-17 20:23:13 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
#ifndef FIXEDSIZEARRAY_HPP
|
|
|
|
#define FIXEDSIZEARRAY_HPP
|
|
|
|
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "../core/alloc_func.hpp"
|
2006-05-27 16:12:16 +00:00
|
|
|
|
|
|
|
/** fixed size array
|
2006-09-04 20:40:33 +00:00
|
|
|
* Upon construction it preallocates fixed size block of memory
|
|
|
|
* for all items, but doesn't construct them. Item's construction
|
|
|
|
* is delayed. */
|
2010-01-16 14:02:22 +00:00
|
|
|
template <class T, uint C>
|
2010-01-16 13:52:24 +00:00
|
|
|
struct FixedSizeArray {
|
2010-01-16 14:12:18 +00:00
|
|
|
protected:
|
2006-05-27 16:12:16 +00:00
|
|
|
/** header for fixed size array */
|
2010-01-16 13:52:24 +00:00
|
|
|
struct ArrayHeader
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-01-16 14:02:22 +00:00
|
|
|
uint items; ///< number of items in the array
|
|
|
|
uint reference_count; ///< block reference counter (used by copy constructor and by destructor)
|
2006-05-27 16:12:16 +00:00
|
|
|
};
|
|
|
|
|
2010-01-16 14:02:22 +00:00
|
|
|
/* make constants visible from outside */
|
|
|
|
static const uint Tsize = sizeof(T); // size of item
|
|
|
|
static const uint HeaderSize = sizeof(ArrayHeader); // size of header
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-01-16 14:12:18 +00:00
|
|
|
/** 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(ArrayHeader). */
|
|
|
|
T *data;
|
|
|
|
|
|
|
|
/** return reference to the array header (non-const) */
|
|
|
|
FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
|
|
|
|
/** return reference to the array header (const) */
|
|
|
|
FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
|
|
|
|
/** return reference to the block reference counter */
|
|
|
|
FORCEINLINE uint& RefCnt() { return Hdr().reference_count; }
|
|
|
|
/** return reference to number of used items */
|
|
|
|
FORCEINLINE uint& SizeRef() { return Hdr().items; }
|
|
|
|
|
|
|
|
public:
|
2006-05-27 16:12:16 +00:00
|
|
|
/** Default constructor. Preallocate space for items and header, then initialize header. */
|
2010-01-16 13:52:24 +00:00
|
|
|
FixedSizeArray()
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* allocate block for header + items (don't construct items) */
|
2010-01-16 14:02:22 +00:00
|
|
|
data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
|
2006-05-27 16:12:16 +00:00
|
|
|
SizeRef() = 0; // initial number of items
|
|
|
|
RefCnt() = 1; // initial reference counter
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy constructor. Preallocate space for items and header, then initialize header. */
|
2010-01-16 13:52:24 +00:00
|
|
|
FixedSizeArray(const FixedSizeArray<T, C>& src)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* share block (header + items) with the source array */
|
2010-01-16 13:52:24 +00:00
|
|
|
data = src.data;
|
2006-05-27 16:12:16 +00:00
|
|
|
RefCnt()++; // now we share block with the source
|
|
|
|
}
|
|
|
|
|
|
|
|
/** destroy remaining items and free the memory block */
|
2010-01-16 13:52:24 +00:00
|
|
|
~FixedSizeArray()
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* release one reference to the shared block */
|
2006-05-27 16:12:16 +00:00
|
|
|
if ((--RefCnt()) > 0) return; // and return if there is still some owner
|
|
|
|
|
2006-11-18 19:20:47 +00:00
|
|
|
Clear();
|
2009-03-15 00:32:18 +00:00
|
|
|
/* free the memory block occupied by items */
|
2010-01-16 14:02:22 +00:00
|
|
|
free(((byte*)data) - HeaderSize);
|
2010-01-16 13:52:24 +00:00
|
|
|
data = NULL;
|
2006-11-18 19:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear (destroy) all items */
|
|
|
|
FORCEINLINE void Clear()
|
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* walk through all allocated items backward and destroy them */
|
2010-01-16 14:02:22 +00:00
|
|
|
for (T *pItem = &data[Length() - 1]; pItem >= data; pItem--) {
|
2010-01-16 13:52:24 +00:00
|
|
|
pItem->~T();
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
2009-03-15 00:32:18 +00:00
|
|
|
/* number of items become zero */
|
2006-11-18 19:20:47 +00:00
|
|
|
SizeRef() = 0;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** return number of used items */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE uint Length() const { return Hdr().items; }
|
2006-05-27 16:12:16 +00:00
|
|
|
/** return true if array is full */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE bool IsFull() const { return Length() >= C; };
|
2006-05-27 16:12:16 +00:00
|
|
|
/** return true if array is empty */
|
2010-01-16 13:52:24 +00:00
|
|
|
FORCEINLINE bool IsEmpty() const { return Length() <= 0; };
|
2006-05-27 16:12:16 +00:00
|
|
|
/** index validation */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE void CheckIdx(uint index) const { assert(index < Length()); }
|
2006-05-27 16:12:16 +00:00
|
|
|
/** add (allocate), but don't construct item */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE T& Append() { assert(!IsFull()); return data[SizeRef()++]; }
|
2006-05-27 16:12:16 +00:00
|
|
|
/** add and construct item using default constructor */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE T& AppendC() { T& item = Append(); new(&item)T; return item; }
|
2006-05-27 16:12:16 +00:00
|
|
|
/** return item by index (non-const version) */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE T& operator [] (uint index) { CheckIdx(index); return data[index]; }
|
2006-05-27 16:12:16 +00:00
|
|
|
/** return item by index (const version) */
|
2010-01-16 14:02:22 +00:00
|
|
|
FORCEINLINE const T& operator [] (uint index) const { CheckIdx(index); return data[index]; }
|
2006-05-27 16:12:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* FIXEDSIZEARRAY_HPP */
|