2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file fixedsizearray.hpp A fixed size array that doesn't create items until needed. */
10 #ifndef FIXEDSIZEARRAY_HPP
11 #define FIXEDSIZEARRAY_HPP
13 #include "../core/alloc_func.hpp"
17 * Upon construction it preallocates fixed size block of memory
18 * for all items, but doesn't construct them. Item's construction
21 template <class T
, uint C
>
22 struct FixedSizeArray
{
24 /** header for fixed size array */
27 uint items
; ///< number of items in the array
28 uint reference_count
; ///< block reference counter (used by copy constructor and by destructor)
31 /* make constants visible from outside */
32 static const uint Tsize
= sizeof(T
); ///< size of item
33 static const uint HeaderSize
= sizeof(ArrayHeader
); ///< size of header
36 * the only member of fixed size array is pointer to the block
37 * of C array of items. Header can be found on the offset -sizeof(ArrayHeader).
41 /** return reference to the array header (non-const) */
42 inline ArrayHeader
&Hdr()
44 return *(ArrayHeader
*)(((byte
*)data
) - HeaderSize
);
47 /** return reference to the array header (const) */
48 inline const ArrayHeader
&Hdr() const
50 return *(ArrayHeader
*)(((byte
*)data
) - HeaderSize
);
53 /** return reference to the block reference counter */
56 return Hdr().reference_count
;
59 /** return reference to number of used items */
60 inline uint
&SizeRef()
66 /** Default constructor. Preallocate space for items and header, then initialize header. */
69 /* Ensure the size won't overflow. */
70 static_assert(C
< (SIZE_MAX
- HeaderSize
) / Tsize
);
72 /* allocate block for header + items (don't construct items) */
73 data
= (T
*)((MallocT
<byte
>(HeaderSize
+ C
* Tsize
)) + HeaderSize
);
74 SizeRef() = 0; // initial number of items
75 RefCnt() = 1; // initial reference counter
78 /** Copy constructor. Preallocate space for items and header, then initialize header. */
79 FixedSizeArray(const FixedSizeArray
<T
, C
> &src
)
81 /* share block (header + items) with the source array */
83 RefCnt()++; // now we share block with the source
86 /** destroy remaining items and free the memory block */
89 /* release one reference to the shared block */
90 if ((--RefCnt()) > 0) return; // and return if there is still some owner
93 /* free the memory block occupied by items */
94 free(((byte
*)data
) - HeaderSize
);
98 /** Clear (destroy) all items */
101 /* Walk through all allocated items backward and destroy them
102 * Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
103 * on some compilers with some architectures. (e.g. gcc with x86) */
104 for (T
*pItem
= this->data
+ this->Length() - 1; pItem
>= this->data
; pItem
--) {
107 /* number of items become zero */
111 /** return number of used items */
112 inline uint
Length() const
117 /** return true if array is full */
118 inline bool IsFull() const
120 return Length() >= C
;
123 /** return true if array is empty */
124 inline bool IsEmpty() const
126 return Length() <= 0;
129 /** add (allocate), but don't construct item */
133 return &data
[SizeRef()++];
136 /** add and construct item using default constructor */
143 /** return item by index (non-const version) */
144 inline T
& operator[](uint index
)
146 assert(index
< Length());
150 /** return item by index (const version) */
151 inline const T
& operator[](uint index
) const
153 assert(index
< Length());
158 #endif /* FIXEDSIZEARRAY_HPP */