source: trunk/src/lol/core/array.h @ 2183

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

build: fix the WTFPL site URL in all code comments.

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