mirror of https://github.com/OpenTTD/OpenTTD
(svn r18823) -Codechange: Some uints for unsigned ints. (skidd13)
parent
c7dafb9a26
commit
085e9251c5
|
@ -17,10 +17,9 @@
|
||||||
|
|
||||||
/** Flexible array with size limit. Implemented as fixed size
|
/** Flexible array with size limit. Implemented as fixed size
|
||||||
* array of fixed size arrays */
|
* array of fixed size arrays */
|
||||||
template <class T, int B = 1024, int N = B>
|
template <class T, uint B = 1024, uint N = B>
|
||||||
class SmallArray {
|
class SmallArray {
|
||||||
public:
|
public:
|
||||||
typedef T Titem; ///< Titem is now visible from outside
|
|
||||||
typedef FixedSizeArray<T, B> SubArray; ///< inner array
|
typedef FixedSizeArray<T, B> SubArray; ///< inner array
|
||||||
typedef FixedSizeArray<SubArray, N> SuperArray; ///< outer array
|
typedef FixedSizeArray<SubArray, N> SuperArray; ///< outer array
|
||||||
|
|
||||||
|
@ -28,30 +27,28 @@ protected:
|
||||||
SuperArray data; ///< array of arrays of items
|
SuperArray data; ///< array of arrays of items
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const int Tblock_size = B; ///< block size is now visible from outside
|
static const uint Tcapacity = B * N; ///< total max number of items
|
||||||
static const int Tnum_blocks = N; ///< number of blocks is now visible from outside
|
|
||||||
static const int Tcapacity = B * N; ///< total max number of items
|
|
||||||
|
|
||||||
/** implicit constructor */
|
/** implicit constructor */
|
||||||
FORCEINLINE SmallArray() { }
|
FORCEINLINE SmallArray() { }
|
||||||
/** Clear (destroy) all items */
|
/** Clear (destroy) all items */
|
||||||
FORCEINLINE void Clear() {data.Clear();}
|
FORCEINLINE void Clear() {data.Clear();}
|
||||||
/** Return actual number of items */
|
/** Return actual number of items */
|
||||||
FORCEINLINE int Length() const
|
FORCEINLINE uint Length() const
|
||||||
{
|
{
|
||||||
int super_size = data.Length();
|
uint super_size = data.Length();
|
||||||
if (super_size == 0) return 0;
|
if (super_size == 0) return 0;
|
||||||
int sub_size = data[super_size - 1].Length();
|
uint sub_size = data[super_size - 1].Length();
|
||||||
return (super_size - 1) * Tblock_size + sub_size;
|
return (super_size - 1) * B + sub_size;
|
||||||
}
|
}
|
||||||
/** return true if array is empty */
|
/** return true if array is empty */
|
||||||
FORCEINLINE bool IsEmpty() { return data.IsEmpty(); }
|
FORCEINLINE bool IsEmpty() { return data.IsEmpty(); }
|
||||||
/** return true if array is full */
|
/** return true if array is full */
|
||||||
FORCEINLINE bool IsFull() { return data.IsFull() && data[Tnum_blocks - 1].IsFull(); }
|
FORCEINLINE bool IsFull() { return data.IsFull() && data[N - 1].IsFull(); }
|
||||||
/** return first sub-array with free space for new item */
|
/** return first sub-array with free space for new item */
|
||||||
FORCEINLINE SubArray& FirstFreeSubArray()
|
FORCEINLINE SubArray& FirstFreeSubArray()
|
||||||
{
|
{
|
||||||
int super_size = data.Length();
|
uint super_size = data.Length();
|
||||||
if (super_size > 0) {
|
if (super_size > 0) {
|
||||||
SubArray& s = data[super_size - 1];
|
SubArray& s = data[super_size - 1];
|
||||||
if (!s.IsFull()) return s;
|
if (!s.IsFull()) return s;
|
||||||
|
@ -63,28 +60,28 @@ public:
|
||||||
/** allocate and construct new item */
|
/** allocate and construct new item */
|
||||||
FORCEINLINE T& AppendC() { return FirstFreeSubArray().AppendC(); }
|
FORCEINLINE T& AppendC() { return FirstFreeSubArray().AppendC(); }
|
||||||
/** indexed access (non-const) */
|
/** indexed access (non-const) */
|
||||||
FORCEINLINE Titem& operator [] (int index)
|
FORCEINLINE T& operator [] (uint index)
|
||||||
{
|
{
|
||||||
SubArray& s = data[index / Tblock_size];
|
const SubArray& s = data[index / B];
|
||||||
Titem& item = s[index % Tblock_size];
|
T& item = s[index % B];
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
/** indexed access (const) */
|
/** indexed access (const) */
|
||||||
FORCEINLINE const Titem& operator [] (int index) const
|
FORCEINLINE const T& operator [] (uint index) const
|
||||||
{
|
{
|
||||||
const SubArray& s = data[index / Tblock_size];
|
const SubArray& s = data[index / B];
|
||||||
const Titem& item = s[index % Tblock_size];
|
const T& item = s[index % B];
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename D> void Dump(D &dmp) const
|
template <typename D> void Dump(D &dmp) const
|
||||||
{
|
{
|
||||||
dmp.WriteLine("capacity = %d", Tcapacity);
|
dmp.WriteLine("capacity = %d", Tcapacity);
|
||||||
int num_items = Length();
|
uint num_items = Length();
|
||||||
dmp.WriteLine("num_items = %d", num_items);
|
dmp.WriteLine("num_items = %d", num_items);
|
||||||
CStrA name;
|
CStrA name;
|
||||||
for (int i = 0; i < num_items; i++) {
|
for (uint i = 0; i < num_items; i++) {
|
||||||
const Titem& item = (*this)[i];
|
const T& item = (*this)[i];
|
||||||
name.Format("item[%d]", i);
|
name.Format("item[%d]", i);
|
||||||
dmp.WriteStructT(name.Data(), &item);
|
dmp.WriteStructT(name.Data(), &item);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
* Upon construction it preallocates fixed size block of memory
|
* Upon construction it preallocates fixed size block of memory
|
||||||
* for all items, but doesn't construct them. Item's construction
|
* for all items, but doesn't construct them. Item's construction
|
||||||
* is delayed. */
|
* is delayed. */
|
||||||
template <class T, int C>
|
template <class T, uint C>
|
||||||
struct FixedSizeArray {
|
struct FixedSizeArray {
|
||||||
/** the only member of fixed size array is pointer to the block
|
/** 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). */
|
* of C array of items. Header can be found on the offset -sizeof(ArrayHeader). */
|
||||||
|
@ -27,22 +27,19 @@ struct FixedSizeArray {
|
||||||
/** header for fixed size array */
|
/** header for fixed size array */
|
||||||
struct ArrayHeader
|
struct ArrayHeader
|
||||||
{
|
{
|
||||||
int items; ///< number of items in the array
|
uint items; ///< number of items in the array
|
||||||
int reference_count; ///< block reference counter (used by copy constructor and by destructor)
|
uint reference_count; ///< block reference counter (used by copy constructor and by destructor)
|
||||||
};
|
};
|
||||||
|
|
||||||
/* make types and constants visible from outside */
|
/* make constants visible from outside */
|
||||||
typedef T Titem; // type of array item
|
static const uint Tsize = sizeof(T); // size of item
|
||||||
|
static const uint HeaderSize = sizeof(ArrayHeader); // 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. */
|
/** Default constructor. Preallocate space for items and header, then initialize header. */
|
||||||
FixedSizeArray()
|
FixedSizeArray()
|
||||||
{
|
{
|
||||||
/* allocate block for header + items (don't construct items) */
|
/* allocate block for header + items (don't construct items) */
|
||||||
data = (Titem*)((MallocT<int8>(HeaderSize + Tcapacity * Tsize)) + HeaderSize);
|
data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
|
||||||
SizeRef() = 0; // initial number of items
|
SizeRef() = 0; // initial number of items
|
||||||
RefCnt() = 1; // initial reference counter
|
RefCnt() = 1; // initial reference counter
|
||||||
}
|
}
|
||||||
|
@ -63,7 +60,7 @@ struct FixedSizeArray {
|
||||||
|
|
||||||
Clear();
|
Clear();
|
||||||
/* free the memory block occupied by items */
|
/* free the memory block occupied by items */
|
||||||
free(((int8*)data) - HeaderSize);
|
free(((byte*)data) - HeaderSize);
|
||||||
data = NULL;
|
data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +68,7 @@ struct FixedSizeArray {
|
||||||
FORCEINLINE void Clear()
|
FORCEINLINE void Clear()
|
||||||
{
|
{
|
||||||
/* walk through all allocated items backward and destroy them */
|
/* walk through all allocated items backward and destroy them */
|
||||||
for (Titem *pItem = &data[Length() - 1]; pItem >= data; pItem--) {
|
for (T *pItem = &data[Length() - 1]; pItem >= data; pItem--) {
|
||||||
pItem->~T();
|
pItem->~T();
|
||||||
}
|
}
|
||||||
/* number of items become zero */
|
/* number of items become zero */
|
||||||
|
@ -80,30 +77,30 @@ struct FixedSizeArray {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** return reference to the array header (non-const) */
|
/** return reference to the array header (non-const) */
|
||||||
FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((int8*)data) - HeaderSize); }
|
FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
|
||||||
/** return reference to the array header (const) */
|
/** return reference to the array header (const) */
|
||||||
FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((int8*)data) - HeaderSize); }
|
FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
|
||||||
/** return reference to the block reference counter */
|
/** return reference to the block reference counter */
|
||||||
FORCEINLINE int& RefCnt() { return Hdr().reference_count; }
|
FORCEINLINE uint& RefCnt() { return Hdr().reference_count; }
|
||||||
/** return reference to number of used items */
|
/** return reference to number of used items */
|
||||||
FORCEINLINE int& SizeRef() { return Hdr().items; }
|
FORCEINLINE uint& SizeRef() { return Hdr().items; }
|
||||||
public:
|
public:
|
||||||
/** return number of used items */
|
/** return number of used items */
|
||||||
FORCEINLINE int Length() const { return Hdr().items; }
|
FORCEINLINE uint Length() const { return Hdr().items; }
|
||||||
/** return true if array is full */
|
/** return true if array is full */
|
||||||
FORCEINLINE bool IsFull() const { return Length() >= Tcapacity; };
|
FORCEINLINE bool IsFull() const { return Length() >= C; };
|
||||||
/** return true if array is empty */
|
/** return true if array is empty */
|
||||||
FORCEINLINE bool IsEmpty() const { return Length() <= 0; };
|
FORCEINLINE bool IsEmpty() const { return Length() <= 0; };
|
||||||
/** index validation */
|
/** index validation */
|
||||||
FORCEINLINE void CheckIdx(int index) const { assert(index >= 0); assert(index < Length()); }
|
FORCEINLINE void CheckIdx(uint index) const { assert(index < Length()); }
|
||||||
/** add (allocate), but don't construct item */
|
/** add (allocate), but don't construct item */
|
||||||
FORCEINLINE Titem& Append() { assert(!IsFull()); return data[SizeRef()++]; }
|
FORCEINLINE T& Append() { assert(!IsFull()); return data[SizeRef()++]; }
|
||||||
/** add and construct item using default constructor */
|
/** add and construct item using default constructor */
|
||||||
FORCEINLINE Titem& AppendC() { Titem& item = Append(); new(&item)Titem; return item; }
|
FORCEINLINE T& AppendC() { T& item = Append(); new(&item)T; return item; }
|
||||||
/** return item by index (non-const version) */
|
/** return item by index (non-const version) */
|
||||||
FORCEINLINE Titem& operator [] (int index) { CheckIdx(index); return data[index]; }
|
FORCEINLINE T& operator [] (uint index) { CheckIdx(index); return data[index]; }
|
||||||
/** return item by index (const version) */
|
/** return item by index (const version) */
|
||||||
FORCEINLINE const Titem& operator [] (int index) const { CheckIdx(index); return data[index]; }
|
FORCEINLINE const T& operator [] (uint index) const { CheckIdx(index); return data[index]; }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* FIXEDSIZEARRAY_HPP */
|
#endif /* FIXEDSIZEARRAY_HPP */
|
||||||
|
|
Loading…
Reference in New Issue