source: trunk/src/lol/base/array.h @ 2402

Last change on this file since 2402 was 2402, checked in by sam, 7 years ago

base: work around an annoying GCC warning we very well know about.

  • Property svn:keywords set to Id
File size: 16.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The Array class
13// ---------------
14// A very simple Array class not unlike the std::vector, with some nice
15// additional features, eg. Array<int,float> for automatic arrays of structs.
16//
17
18#if !defined __LOL_BASE_ARRAY_H__
19#define __LOL_BASE_ARRAY_H__
20
21#include <lol/base/assert.h>
22
23#include <new>
24#include <stdint.h>
25
26namespace lol
27{
28
29/*
30 * The base array type.
31 *
32 * Contains an m_data memory array of Elements, of which only the first
33 * m_count are allocated. The rest is uninitialised memory.
34 */
35
36template<typename T, typename ARRAY> class ArrayBase
37{
38public:
39    typedef T Element;
40
41    inline ArrayBase() : m_data(0), m_count(0), m_reserved(0)
42    {
43    }
44
45    inline ~ArrayBase()
46    {
47        for (int i = 0; i < m_count; i++)
48            m_data[i].~Element();
49        delete[] reinterpret_cast<uint8_t *>(m_data);
50    }
51
52    ArrayBase(ArrayBase const& that) : m_data(0), m_count(0), m_reserved(0)
53    {
54        /* Reserve the exact number of values instead of what the other
55         * array had reserved. Just a method for not wasting too much. */
56        Reserve(that.m_count);
57        for (int i = 0; i < that.m_count; i++)
58            new(&m_data[i]) Element(that[i]);
59        m_count = that.m_count;
60    }
61
62    ArrayBase& operator=(ArrayBase const& that)
63    {
64        if ((uintptr_t)this != (uintptr_t)&that)
65        {
66            /* FIXME: there is an opportunity for optimisation here if we
67             * find a way to ask Reserve not to create new elements, since
68             * we're going to overwrite them anyway. */
69            if (m_reserved < that.m_count)
70            {
71                /* If not enough space, reserve memory, overwrite the first
72                 * elements, then use placement new directly for the
73                 * remaining elements. */
74                Reserve(that.m_count);
75                for (int i = 0; i < m_count && i < that.m_count; i++)
76                    m_data[i] = Element(that[i]);
77                for (int i = m_count; i < that.m_count; i++)
78                    new(&m_data[i]) Element(that[i]);
79            }
80            else
81            {
82                /* If enough space, overwrite the common elements, then
83                 * use placement new for the elements in the other array
84                 * that we do not have, and finally destroy the remaining
85                 * elements. */
86                for (int i = 0; i < m_count && i < that.m_count; i++)
87                    m_data[i] = Element(that[i]);
88                for (int i = m_count; i < that.m_count; i++)
89                    new(&m_data[i]) Element(that[i]);
90                for (int i = that.m_count; i < m_count; i++)
91                    m_data[i].~Element();
92            }
93            m_count = that.m_count;
94        }
95        return *this;
96    }
97
98    ArrayBase& operator+=(ArrayBase const &that)
99    {
100        int todo = that.m_count;
101        Reserve(m_count + that.m_count);
102        for (int i = 0; i < todo; i++)
103            new(&m_data[m_count + i]) Element(that[i]);
104        m_count += todo;
105        return *this;
106    }
107
108    ARRAY operator+(ARRAY const &that) const
109    {
110        ARRAY ret;
111        ret.Reserve(m_count + that.m_count);
112        ret += *this;
113        ret += that;
114        return ret;
115    }
116
117    inline Element& operator[](int n)
118    {
119        /* Allow array[0] even if size is zero so that people can
120         * always use &array[0] to get a pointer to the data. */
121        ASSERT(n >= 0);
122        ASSERT((unsigned)n < (unsigned)m_count || (!n && !m_count));
123        return m_data[n];
124    }
125
126    inline Element const& operator[](int n) const
127    {
128        ASSERT(n >= 0);
129        ASSERT(n < m_count || (!n && !m_count));
130        return m_data[n];
131    }
132
133    inline Element& Last()
134    {
135        ASSERT(m_count > 0);
136        return m_data[m_count - 1];
137    }
138
139    inline Element const& Last() const
140    {
141        ASSERT(m_count > 0);
142        return m_data[m_count - 1];
143    }
144
145    inline ArrayBase& operator<<(T const &x)
146    {
147        if (m_count >= m_reserved)
148        {
149            T tmp = x;
150            Reserve(m_count * 13 / 8 + 8);
151            new (&m_data[m_count++]) Element(tmp);
152        }
153        else
154        {
155            new (&m_data[m_count++]) Element(x);
156        }
157        return *this;
158    }
159
160    inline void Push(T const &x)
161    {
162        *this << x;
163    }
164
165    inline void Pop()
166    {
167        ASSERT(m_count > 0);
168        Remove(m_count - 1, 1);
169    }
170
171    void Remove(int pos, int todelete = 1)
172    {
173        ASSERT(pos >= 0);
174        ASSERT(todelete >= 0);
175        ASSERT(pos + todelete <= m_count);
176        for (int i = pos; i + todelete < m_count; i++)
177            m_data[i] = m_data[i + todelete];
178        for (int i = m_count - todelete; i < m_count; i++)
179            m_data[i].~Element();
180        m_count -= todelete;
181    }
182
183    void Resize(int count, Element e = Element())
184    {
185        ASSERT(count > 0);
186        Reserve(count);
187
188        /* Too many elements? Remove them. */
189        for (int i = count; i < m_count; ++i)
190            m_data[i].~Element();
191
192        /* Not enough elements? Add some. */
193        for (int i = m_count; i < count; ++i)
194            new(&m_data[i]) Element(e);
195
196        m_count = count;
197    }
198
199    inline void Empty()
200    {
201        Remove(0, m_count);
202    }
203
204    void Reserve(int toreserve)
205    {
206        if (toreserve <= (int)m_reserved)
207            return;
208
209        /* This cast is not very nice, because we kill any alignment
210         * information we could have. But until C++ gives us the proper
211         * tools to deal with it, we assume new uint8_t[] returns properly
212         * aligned data. */
213        Element *tmp = reinterpret_cast<Element *>(reinterpret_cast<uintptr_t>
214                               (new uint8_t[sizeof(Element) * toreserve]));
215        for (int i = 0; i < m_count; i++)
216        {
217            new(&tmp[i]) Element(m_data[i]);
218            m_data[i].~Element();
219        }
220        if (m_data)
221            delete[] reinterpret_cast<uint8_t *>(m_data);
222        m_data = tmp;
223        m_reserved = toreserve;
224    }
225
226    inline int Count() const { return m_count; }
227    inline int Bytes() const { return m_count * sizeof(Element); }
228
229protected:
230    Element *m_data;
231    int m_count, m_reserved;
232};
233
234/*
235 * Element types
236 */
237
238template<typename T1, typename T2, typename T3 = void, typename T4 = void,
239         typename T5 = void, typename T6 = void, typename T7 = void,
240         typename T8 = void>
241class ArrayElement
242{
243public:
244    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; T8 m8;
245};
246
247template<typename T1, typename T2, typename T3, typename T4, typename T5,
248         typename T6, typename T7>
249class ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>
250{
251public:
252    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7;
253};
254
255template<typename T1, typename T2, typename T3, typename T4, typename T5,
256         typename T6>
257class ArrayElement<T1, T2, T3, T4, T5, T6, void, void>
258{
259public:
260    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6;
261};
262
263template<typename T1, typename T2, typename T3, typename T4, typename T5>
264class ArrayElement<T1, T2, T3, T4, T5, void, void, void>
265{
266public:
267    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5;
268};
269
270template<typename T1, typename T2, typename T3, typename T4>
271class ArrayElement<T1, T2, T3, T4, void, void, void, void>
272{
273public:
274    T1 m1; T2 m2; T3 m3; T4 m4;
275};
276
277template<typename T1, typename T2, typename T3>
278class ArrayElement<T1, T2, T3, void, void, void, void, void>
279{
280public:
281    T1 m1; T2 m2; T3 m3;
282};
283
284template<typename T1, typename T2>
285class ArrayElement<T1, T2, void, void, void, void, void, void>
286{
287public:
288    T1 m1; T2 m2;
289};
290
291/*
292 * Array specialisations implementing specific setters
293 */
294
295template<typename T1, typename T2 = void, typename T3 = void,
296         typename T4 = void, typename T5 = void, typename T6 = void,
297         typename T7 = void, typename T8 = void>
298class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8>,
299                               Array<T1, T2, T3, T4, T5, T6, T7, T8> >
300{
301public:
302    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
303                     T5 const &m5, T6 const &m6, T7 const &m7, T8 const &m8)
304    {
305        if (this->m_count >= this->m_reserved)
306        {
307            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
308            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7; T8 tmp8 = m8;
309            this->Reserve(this->m_count * 13 / 8 + 8);
310            new (&this->m_data[this->m_count].m1) T1(tmp1);
311            new (&this->m_data[this->m_count].m2) T2(tmp2);
312            new (&this->m_data[this->m_count].m3) T3(tmp3);
313            new (&this->m_data[this->m_count].m4) T4(tmp4);
314            new (&this->m_data[this->m_count].m5) T5(tmp5);
315            new (&this->m_data[this->m_count].m6) T6(tmp6);
316            new (&this->m_data[this->m_count].m7) T7(tmp7);
317            new (&this->m_data[this->m_count].m8) T8(tmp8);
318        }
319        else
320        {
321            new (&this->m_data[this->m_count].m1) T1(m1);
322            new (&this->m_data[this->m_count].m2) T2(m2);
323            new (&this->m_data[this->m_count].m3) T3(m3);
324            new (&this->m_data[this->m_count].m4) T4(m4);
325            new (&this->m_data[this->m_count].m5) T5(m5);
326            new (&this->m_data[this->m_count].m6) T6(m6);
327            new (&this->m_data[this->m_count].m7) T7(m7);
328            new (&this->m_data[this->m_count].m8) T8(m8);
329        }
330        ++this->m_count;
331    }
332};
333
334template<typename T1, typename T2, typename T3, typename T4, typename T5,
335         typename T6, typename T7>
336class Array<T1, T2, T3, T4, T5, T6, T7, void>
337  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>,
338                     Array<T1, T2, T3, T4, T5, T6, T7> >
339{
340public:
341    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
342                     T5 const &m5, T6 const &m6, T7 const &m7)
343    {
344        if (this->m_count >= this->m_reserved)
345        {
346            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
347            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7;
348            this->Reserve(this->m_count * 13 / 8 + 8);
349            new (&this->m_data[this->m_count].m1) T1(tmp1);
350            new (&this->m_data[this->m_count].m2) T2(tmp2);
351            new (&this->m_data[this->m_count].m3) T3(tmp3);
352            new (&this->m_data[this->m_count].m4) T4(tmp4);
353            new (&this->m_data[this->m_count].m5) T5(tmp5);
354            new (&this->m_data[this->m_count].m6) T6(tmp6);
355            new (&this->m_data[this->m_count].m7) T7(tmp7);
356        }
357        else
358        {
359            new (&this->m_data[this->m_count].m1) T1(m1);
360            new (&this->m_data[this->m_count].m2) T2(m2);
361            new (&this->m_data[this->m_count].m3) T3(m3);
362            new (&this->m_data[this->m_count].m4) T4(m4);
363            new (&this->m_data[this->m_count].m5) T5(m5);
364            new (&this->m_data[this->m_count].m6) T6(m6);
365            new (&this->m_data[this->m_count].m7) T7(m7);
366        }
367        ++this->m_count;
368    }
369};
370
371template<typename T1, typename T2, typename T3, typename T4, typename T5,
372         typename T6>
373class Array<T1, T2, T3, T4, T5, T6, void, void>
374  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void>,
375                     Array<T1, T2, T3, T4, T5, T6> >
376{
377public:
378    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
379                     T5 const &m5, T6 const &m6)
380    {
381        if (this->m_count >= this->m_reserved)
382        {
383            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
384            T5 tmp5 = m5; T6 tmp6 = m6;
385            this->Reserve(this->m_count * 13 / 8 + 8);
386            new (&this->m_data[this->m_count].m1) T1(tmp1);
387            new (&this->m_data[this->m_count].m2) T2(tmp2);
388            new (&this->m_data[this->m_count].m3) T3(tmp3);
389            new (&this->m_data[this->m_count].m4) T4(tmp4);
390            new (&this->m_data[this->m_count].m5) T5(tmp5);
391            new (&this->m_data[this->m_count].m6) T6(tmp6);
392        }
393        else
394        {
395            new (&this->m_data[this->m_count].m1) T1(m1);
396            new (&this->m_data[this->m_count].m2) T2(m2);
397            new (&this->m_data[this->m_count].m3) T3(m3);
398            new (&this->m_data[this->m_count].m4) T4(m4);
399            new (&this->m_data[this->m_count].m5) T5(m5);
400            new (&this->m_data[this->m_count].m6) T6(m6);
401        }
402        ++this->m_count;
403    }
404};
405
406template<typename T1, typename T2, typename T3, typename T4, typename T5>
407class Array<T1, T2, T3, T4, T5, void, void, void>
408  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void>,
409                     Array<T1, T2, T3, T4, T5> >
410{
411public:
412    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
413                     T5 const &m5)
414    {
415        if (this->m_count >= this->m_reserved)
416        {
417            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
418            T5 tmp5 = m5;
419            this->Reserve(this->m_count * 13 / 8 + 8);
420            new (&this->m_data[this->m_count].m1) T1(tmp1);
421            new (&this->m_data[this->m_count].m2) T2(tmp2);
422            new (&this->m_data[this->m_count].m3) T3(tmp3);
423            new (&this->m_data[this->m_count].m4) T4(tmp4);
424            new (&this->m_data[this->m_count].m5) T5(tmp5);
425        }
426        else
427        {
428            new (&this->m_data[this->m_count].m1) T1(m1);
429            new (&this->m_data[this->m_count].m2) T2(m2);
430            new (&this->m_data[this->m_count].m3) T3(m3);
431            new (&this->m_data[this->m_count].m4) T4(m4);
432            new (&this->m_data[this->m_count].m5) T5(m5);
433        }
434        ++this->m_count;
435    }
436};
437
438template<typename T1, typename T2, typename T3, typename T4>
439class Array<T1, T2, T3, T4, void, void, void, void>
440  : public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void>,
441                     Array<T1, T2, T3, T4> >
442{
443public:
444    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4)
445    {
446        if (this->m_count >= this->m_reserved)
447        {
448            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
449            this->Reserve(this->m_count * 13 / 8 + 8);
450            new (&this->m_data[this->m_count].m1) T1(tmp1);
451            new (&this->m_data[this->m_count].m2) T2(tmp2);
452            new (&this->m_data[this->m_count].m3) T3(tmp3);
453            new (&this->m_data[this->m_count].m4) T4(tmp4);
454        }
455        else
456        {
457            new (&this->m_data[this->m_count].m1) T1(m1);
458            new (&this->m_data[this->m_count].m2) T2(m2);
459            new (&this->m_data[this->m_count].m3) T3(m3);
460            new (&this->m_data[this->m_count].m4) T4(m4);
461        }
462        ++this->m_count;
463    }
464};
465
466template<typename T1, typename T2, typename T3>
467class Array<T1, T2, T3, void, void, void, void, void>
468  : public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void>,
469                     Array<T1, T2, T3> >
470{
471public:
472    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3)
473    {
474        if (this->m_count >= this->m_reserved)
475        {
476            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3;
477            this->Reserve(this->m_count * 13 / 8 + 8);
478            new (&this->m_data[this->m_count].m1) T1(tmp1);
479            new (&this->m_data[this->m_count].m2) T2(tmp2);
480            new (&this->m_data[this->m_count].m3) T3(tmp3);
481        }
482        else
483        {
484            new (&this->m_data[this->m_count].m1) T1(m1);
485            new (&this->m_data[this->m_count].m2) T2(m2);
486            new (&this->m_data[this->m_count].m3) T3(m3);
487        }
488        ++this->m_count;
489    }
490};
491
492template<typename T1, typename T2>
493class Array<T1, T2, void, void, void, void, void, void>
494  : public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void>,
495                     Array<T1, T2> >
496{
497public:
498    inline void Push(T1 const &m1, T2 const &m2)
499    {
500        if (this->m_count >= this->m_reserved)
501        {
502            T1 tmp1 = m1; T2 tmp2 = m2;
503            this->Reserve(this->m_count * 13 / 8 + 8);
504            new (&this->m_data[this->m_count].m1) T1(tmp1);
505            new (&this->m_data[this->m_count].m2) T2(tmp2);
506        }
507        else
508        {
509            new (&this->m_data[this->m_count].m1) T1(m1);
510            new (&this->m_data[this->m_count].m2) T2(m2);
511        }
512        ++this->m_count;
513    }
514};
515
516template<typename T>
517class Array<T, void, void, void, void, void, void, void>
518  : public ArrayBase<T,
519                     Array<T> >
520{
521};
522
523} /* namespace lol */
524
525#endif // __LOL_BASE_ARRAY_H__
526
Note: See TracBrowser for help on using the repository browser.