OpenTTD
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 "str.hpp"
15 
20 template <class T, uint B = 1024, uint N = B>
21 class SmallArray {
22 protected:
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.WriteLine("capacity = %d", Tcapacity);
107  uint num_items = Length();
108  dmp.WriteLine("num_items = %d", num_items);
109  CStrA name;
110  for (uint i = 0; i < num_items; i++) {
111  const T &item = (*this)[i];
112  name.Format("item[%d]", i);
113  dmp.WriteStructT(name.Data(), &item);
114  }
115  }
116 };
117 
118 #endif /* ARRAY_HPP */
bool IsEmpty() const
return true if array is empty
T * Append()
add (allocate), but don&#39;t construct item
bool IsFull()
return true if array is full
Definition: array.hpp:68
SubArray & FirstFreeSubArray()
return first sub-array with free space for new item
Definition: array.hpp:31
T * Append()
allocate but not construct new item
Definition: array.hpp:74
void Dump(D &dmp) const
Helper for creating a human readable output of this data.
Definition: array.hpp:104
bool IsFull() const
return true if array is full
void Clear()
Clear (destroy) all items.
Definition: array.hpp:48
A fixed size array that doesn&#39;t create items until needed.
FixedSizeArray< T, B > SubArray
inner array
Definition: array.hpp:23
uint Length() const
Return actual number of items.
Definition: array.hpp:54
Blob based case sensitive ANSI/UTF-8 string.
Definition: str.hpp:20
Flexible array with size limit.
Definition: array.hpp:21
String formatting?
T * AppendC()
allocate and construct new item
Definition: array.hpp:80
SuperArray data
array of arrays of items
Definition: array.hpp:28
static const uint Tcapacity
total max number of items
Definition: array.hpp:26
uint Length() const
return number of used items
fixed size array Upon construction it preallocates fixed size block of memory for all items...
void Clear()
Clear (destroy) all items.
T & operator[](uint index)
indexed access (non-const)
Definition: array.hpp:86
T * Data()
Return pointer to the first data item - non-const version.
Definition: blob.hpp:356
T * AppendC()
add and construct item using default constructor
SmallArray()
implicit constructor
Definition: array.hpp:43
FixedSizeArray< SubArray, N > SuperArray
outer array
Definition: array.hpp:24
bool IsEmpty()
return true if array is empty
Definition: array.hpp:62