OpenTTD Source: src/misc/array.hpp Source File

OpenTTD Source 14.1

array.hpp
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #ifndef ARRAY_HPP
11 #define ARRAY_HPP
12
13 #include "fixedsizearray.hpp"
14 #include "../string_func.h"
15
20 template <class T, uint B = 1024, uint N = B>
21 class SmallArray {
22 protected:
23 typedef FixedSizeArray<T, B> SubArray;
24 typedef FixedSizeArray<SubArray, N> SuperArray;
25
26 static const uint Tcapacity = B * N;
27
28 SuperArray data;
29
31 inline SubArray &FirstFreeSubArray()
32 {
33 uint super_size = data.Length();
34 if (super_size > 0) {
35 SubArray &s = data[super_size - 1];
36 if (!s.IsFull()) return s;
37 }
38 return *data.AppendC();
39 }
40
41 public:
43 inline SmallArray()
44 {
45 }
46
48 inline void Clear()
49 {
50 data.Clear();
51 }
52
54 inline uint Length() const
55 {
56 uint super_size = data.Length();
57 if (super_size == 0) return 0;
58 uint sub_size = data[super_size - 1].Length();
59 return (super_size - 1) * B + sub_size;
60 }
62 inline bool IsEmpty()
63 {
64 return data.IsEmpty();
65 }
66
68 inline bool IsFull()
69 {
70 return data.IsFull() && data[N - 1].IsFull();
71 }
72
74 inline T *Append()
75 {
76 return FirstFreeSubArray().Append();
77 }
78
80 inline T *AppendC()
81 {
82 return FirstFreeSubArray().AppendC();
83 }
84
86 inline T& operator[](uint index)
87 {
88 const SubArray &s = data[index / B];
89 T &item = s[index % B];
90 return item;
91 }
93 inline const T& operator[](uint index) const
94 {
95 const SubArray &s = data[index / B];
96 const T &item = s[index % B];
97 return item;
98 }
99
104 template <typename D> void Dump(D &dmp) const
105 {
106 dmp.WriteValue("capacity", Tcapacity);
107 uint num_items = Length();
108 dmp.WriteValue("num_items", num_items);
109 for (uint i = 0; i < num_items; i++) {
110 const T &item = (*this)[i];
111 dmp.WriteStructT(fmt::format("item[{}]", i), &item);
112 }
113 }
114 };
115
116 #endif /* ARRAY_HPP */
FixedSizeArray::Clear
void Clear()
Clear (destroy) all items.
Definition: fixedsizearray.hpp:99
SmallArray::Dump
void Dump(D &dmp) const
Helper for creating a human readable output of this data.
Definition: array.hpp:104
fixedsizearray.hpp
SmallArray::Append
T * Append()
allocate but not construct new item
Definition: array.hpp:74
SmallArray::Length
uint Length() const
Return actual number of items.
Definition: array.hpp:54
SmallArray::IsFull
bool IsFull()
return true if array is full
Definition: array.hpp:68
SmallArray::FirstFreeSubArray
SubArray & FirstFreeSubArray()
return first sub-array with free space for new item
Definition: array.hpp:31
SmallArray::Clear
void Clear()
Clear (destroy) all items.
Definition: array.hpp:48
FixedSizeArray::IsEmpty
bool IsEmpty() const
return true if array is empty
Definition: fixedsizearray.hpp:124
SmallArray::AppendC
T * AppendC()
allocate and construct new item
Definition: array.hpp:80
SmallArray::SubArray
FixedSizeArray< T, B > SubArray
inner array
Definition: array.hpp:23
SmallArray
Flexible array with size limit.
Definition: array.hpp:21
SmallArray::Tcapacity
static const uint Tcapacity
total max number of items
Definition: array.hpp:26
SmallArray::data
SuperArray data
array of arrays of items
Definition: array.hpp:28
FixedSizeArray::Append
T * Append()
add (allocate), but don't construct item
Definition: fixedsizearray.hpp:130
FixedSizeArray::IsFull
bool IsFull() const
return true if array is full
Definition: fixedsizearray.hpp:118
FixedSizeArray
fixed size array Upon construction it preallocates fixed size block of memory for all items,...
Definition: fixedsizearray.hpp:22
SmallArray::IsEmpty
bool IsEmpty()
return true if array is empty
Definition: array.hpp:62
SmallArray::SuperArray
FixedSizeArray< SubArray, N > SuperArray
outer array
Definition: array.hpp:24
FixedSizeArray::Length
uint Length() const
return number of used items
Definition: fixedsizearray.hpp:112
SmallArray::SmallArray
SmallArray()
implicit constructor
Definition: array.hpp:43
SmallArray::operator[]
T & operator[](uint index)
indexed access (non-const)
Definition: array.hpp:86
FixedSizeArray::AppendC
T * AppendC()
add and construct item using default constructor
Definition: fixedsizearray.hpp:137