Files
@ r28627:3ac57f81c4f2
Branch filter:
Location: cpp/openttd-patchpack/source/src/misc/array.hpp - annotation
r28627:3ac57f81c4f2
2.9 KiB
text/x-c++hdr
Change: make -dnet=9 give traces of the details of the network protocol (#11931)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r6481:534c9f0317ec r16850:82c7acc31833 r16850:82c7acc31833 r5633:33223f089b79 r5633:33223f089b79 r25113:f0d6acc5740e r5633:33223f089b79 r15610:623a23fb6560 r15610:623a23fb6560 r15613:193c12018337 r15613:193c12018337 r14261:734469f01cfe r14260:f3bc3a5d8acc r14262:62859bc0fbdc r14260:f3bc3a5d8acc r14260:f3bc3a5d8acc r5633:33223f089b79 r14262:62859bc0fbdc r14262:62859bc0fbdc r14260:f3bc3a5d8acc r5633:33223f089b79 r14262:62859bc0fbdc r28389:d1118c144a7d r14262:62859bc0fbdc r14262:62859bc0fbdc r14262:62859bc0fbdc r22182:16fb91fde98a r14262:62859bc0fbdc r14262:62859bc0fbdc r14264:cdaf19f1061f r14262:62859bc0fbdc r14262:62859bc0fbdc r5633:33223f089b79 r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r18782:6453522c2154 r5633:33223f089b79 r14261:734469f01cfe r5633:33223f089b79 r14261:734469f01cfe r14261:734469f01cfe r5633:33223f089b79 r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22182:16fb91fde98a r5633:33223f089b79 r22182:16fb91fde98a r22182:16fb91fde98a r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r22182:16fb91fde98a r5633:33223f089b79 r22182:16fb91fde98a r22182:16fb91fde98a r5633:33223f089b79 r5633:33223f089b79 r7119:a3a17b8fbfec r17630:7d818445376d r17630:7d818445376d r17630:7d818445376d r17630:7d818445376d r7119:a3a17b8fbfec r7119:a3a17b8fbfec r25113:f0d6acc5740e r14261:734469f01cfe r25113:f0d6acc5740e r14261:734469f01cfe r22182:16fb91fde98a r27496:01d86348b958 r7119:a3a17b8fbfec r7119:a3a17b8fbfec r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 | /*
* 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/>.
*/
/** @file array.hpp Array without an explicit maximum size. */
#ifndef ARRAY_HPP
#define ARRAY_HPP
#include "fixedsizearray.hpp"
#include "../string_func.h"
/**
* Flexible array with size limit. Implemented as fixed size
* array of fixed size arrays
*/
template <class T, uint B = 1024, uint N = B>
class SmallArray {
protected:
typedef FixedSizeArray<T, B> SubArray; ///< inner array
typedef FixedSizeArray<SubArray, N> SuperArray; ///< outer array
static const uint Tcapacity = B * N; ///< total max number of items
SuperArray data; ///< array of arrays of items
/** return first sub-array with free space for new item */
inline SubArray &FirstFreeSubArray()
{
uint super_size = data.Length();
if (super_size > 0) {
SubArray &s = data[super_size - 1];
if (!s.IsFull()) return s;
}
return *data.AppendC();
}
public:
/** implicit constructor */
inline SmallArray()
{
}
/** Clear (destroy) all items */
inline void Clear()
{
data.Clear();
}
/** Return actual number of items */
inline uint Length() const
{
uint super_size = data.Length();
if (super_size == 0) return 0;
uint sub_size = data[super_size - 1].Length();
return (super_size - 1) * B + sub_size;
}
/** return true if array is empty */
inline bool IsEmpty()
{
return data.IsEmpty();
}
/** return true if array is full */
inline bool IsFull()
{
return data.IsFull() && data[N - 1].IsFull();
}
/** allocate but not construct new item */
inline T *Append()
{
return FirstFreeSubArray().Append();
}
/** allocate and construct new item */
inline T *AppendC()
{
return FirstFreeSubArray().AppendC();
}
/** indexed access (non-const) */
inline T& operator[](uint index)
{
const SubArray &s = data[index / B];
T &item = s[index % B];
return item;
}
/** indexed access (const) */
inline const T& operator[](uint index) const
{
const SubArray &s = data[index / B];
const T &item = s[index % B];
return item;
}
/**
* Helper for creating a human readable output of this data.
* @param dmp The location to dump to.
*/
template <typename D> void Dump(D &dmp) const
{
dmp.WriteValue("capacity", Tcapacity);
uint num_items = Length();
dmp.WriteValue("num_items", num_items);
for (uint i = 0; i < num_items; i++) {
const T &item = (*this)[i];
dmp.WriteStructT(fmt::format("item[{}]", i), &item);
}
}
};
#endif /* ARRAY_HPP */
|