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

Last change on this file since 2809 was 2809, checked in by sam, 8 years ago

base: Array::Remove(int) now accepts a negative index to remove elements
from the end. Also added Array::RemoveSwap() for faster removes at the
expense of element ordering.

  • Property svn:keywords set to Id
File size: 17.4 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 T Pop()
166    {
167        ASSERT(m_count > 0);
168        Element tmp = Last();
169        Remove(m_count - 1, 1);
170        return tmp;
171    }
172
173    void Swap(int pos1, int pos2)
174    {
175        ASSERT(pos1 >= 0);
176        ASSERT(pos2 >= 0);
177        ASSERT(pos1 < m_count);
178        ASSERT(pos2 < m_count);
179
180        if (pos1 != pos2)
181        {
182            Element tmp = (*this)[pos1];
183            (*this)[pos1] = (*this)[pos2];
184            (*this)[pos2] = tmp;
185        }
186    }
187
188    void Remove(int pos, int todelete = 1)
189    {
190        ASSERT(todelete >= 0);
191        ASSERT(pos - todelete >= -m_count - 1);
192        ASSERT(pos + todelete <= m_count);
193        if (pos < 0)
194            pos = m_count + pos;
195
196        for (int i = pos; i + todelete < m_count; i++)
197            m_data[i] = m_data[i + todelete];
198        for (int i = m_count - todelete; i < m_count; i++)
199            m_data[i].~Element();
200        m_count -= todelete;
201    }
202
203    void RemoveSwap(int pos, int todelete = 1)
204    {
205        ASSERT(todelete >= 0);
206        ASSERT(pos - todelete >= -m_count - 1);
207        ASSERT(pos + todelete <= m_count);
208        if (pos < 0)
209            pos = m_count + pos;
210
211        for (int i = 0; i < todelete; i++)
212        {
213            if (pos + i < m_count - 1 - i)
214                m_data[pos + i] = m_data[m_count - 1 - i];
215            m_data[m_count - 1 - i].~Element();
216        }
217        m_count -= todelete;
218    }
219
220    void Resize(int count, Element e = Element())
221    {
222        ASSERT(count > 0);
223        Reserve(count);
224
225        /* Too many elements? Remove them. */
226        for (int i = count; i < m_count; ++i)
227            m_data[i].~Element();
228
229        /* Not enough elements? Add some. */
230        for (int i = m_count; i < count; ++i)
231            new(&m_data[i]) Element(e);
232
233        m_count = count;
234    }
235
236    inline void Empty()
237    {
238        Remove(0, m_count);
239    }
240
241    void Reserve(int toreserve)
242    {
243        if (toreserve <= (int)m_reserved)
244            return;
245
246        /* This cast is not very nice, because we kill any alignment
247         * information we could have. But until C++ gives us the proper
248         * tools to deal with it, we assume new uint8_t[] returns properly
249         * aligned data. */
250        Element *tmp = reinterpret_cast<Element *>(reinterpret_cast<uintptr_t>
251                               (new uint8_t[sizeof(Element) * toreserve]));
252        for (int i = 0; i < m_count; i++)
253        {
254            new(&tmp[i]) Element(m_data[i]);
255            m_data[i].~Element();
256        }
257        if (m_data)
258            delete[] reinterpret_cast<uint8_t *>(m_data);
259        m_data = tmp;
260        m_reserved = toreserve;
261    }
262
263    inline int Count() const { return m_count; }
264    inline int Bytes() const { return m_count * sizeof(Element); }
265
266protected:
267    Element *m_data;
268    int m_count, m_reserved;
269};
270
271/*
272 * Element types
273 */
274
275template<typename T1, typename T2, typename T3 = void, typename T4 = void,
276         typename T5 = void, typename T6 = void, typename T7 = void,
277         typename T8 = void>
278class ArrayElement
279{
280public:
281    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; T8 m8;
282};
283
284template<typename T1, typename T2, typename T3, typename T4, typename T5,
285         typename T6, typename T7>
286class ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>
287{
288public:
289    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7;
290};
291
292template<typename T1, typename T2, typename T3, typename T4, typename T5,
293         typename T6>
294class ArrayElement<T1, T2, T3, T4, T5, T6, void, void>
295{
296public:
297    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6;
298};
299
300template<typename T1, typename T2, typename T3, typename T4, typename T5>
301class ArrayElement<T1, T2, T3, T4, T5, void, void, void>
302{
303public:
304    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5;
305};
306
307template<typename T1, typename T2, typename T3, typename T4>
308class ArrayElement<T1, T2, T3, T4, void, void, void, void>
309{
310public:
311    T1 m1; T2 m2; T3 m3; T4 m4;
312};
313
314template<typename T1, typename T2, typename T3>
315class ArrayElement<T1, T2, T3, void, void, void, void, void>
316{
317public:
318    T1 m1; T2 m2; T3 m3;
319};
320
321template<typename T1, typename T2>
322class ArrayElement<T1, T2, void, void, void, void, void, void>
323{
324public:
325    T1 m1; T2 m2;
326};
327
328/*
329 * Array specialisations implementing specific setters
330 */
331
332template<typename T1, typename T2 = void, typename T3 = void,
333         typename T4 = void, typename T5 = void, typename T6 = void,
334         typename T7 = void, typename T8 = void>
335class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8>,
336                               Array<T1, T2, T3, T4, T5, T6, T7, T8> >
337{
338public:
339    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
340                     T5 const &m5, T6 const &m6, T7 const &m7, T8 const &m8)
341    {
342        if (this->m_count >= this->m_reserved)
343        {
344            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
345            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7; T8 tmp8 = m8;
346            this->Reserve(this->m_count * 13 / 8 + 8);
347            new (&this->m_data[this->m_count].m1) T1(tmp1);
348            new (&this->m_data[this->m_count].m2) T2(tmp2);
349            new (&this->m_data[this->m_count].m3) T3(tmp3);
350            new (&this->m_data[this->m_count].m4) T4(tmp4);
351            new (&this->m_data[this->m_count].m5) T5(tmp5);
352            new (&this->m_data[this->m_count].m6) T6(tmp6);
353            new (&this->m_data[this->m_count].m7) T7(tmp7);
354            new (&this->m_data[this->m_count].m8) T8(tmp8);
355        }
356        else
357        {
358            new (&this->m_data[this->m_count].m1) T1(m1);
359            new (&this->m_data[this->m_count].m2) T2(m2);
360            new (&this->m_data[this->m_count].m3) T3(m3);
361            new (&this->m_data[this->m_count].m4) T4(m4);
362            new (&this->m_data[this->m_count].m5) T5(m5);
363            new (&this->m_data[this->m_count].m6) T6(m6);
364            new (&this->m_data[this->m_count].m7) T7(m7);
365            new (&this->m_data[this->m_count].m8) T8(m8);
366        }
367        ++this->m_count;
368    }
369};
370
371template<typename T1, typename T2, typename T3, typename T4, typename T5,
372         typename T6, typename T7>
373class Array<T1, T2, T3, T4, T5, T6, T7, void>
374  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>,
375                     Array<T1, T2, T3, T4, T5, T6, T7> >
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, T7 const &m7)
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; T7 tmp7 = m7;
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            new (&this->m_data[this->m_count].m7) T7(tmp7);
393        }
394        else
395        {
396            new (&this->m_data[this->m_count].m1) T1(m1);
397            new (&this->m_data[this->m_count].m2) T2(m2);
398            new (&this->m_data[this->m_count].m3) T3(m3);
399            new (&this->m_data[this->m_count].m4) T4(m4);
400            new (&this->m_data[this->m_count].m5) T5(m5);
401            new (&this->m_data[this->m_count].m6) T6(m6);
402            new (&this->m_data[this->m_count].m7) T7(m7);
403        }
404        ++this->m_count;
405    }
406};
407
408template<typename T1, typename T2, typename T3, typename T4, typename T5,
409         typename T6>
410class Array<T1, T2, T3, T4, T5, T6, void, void>
411  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void>,
412                     Array<T1, T2, T3, T4, T5, T6> >
413{
414public:
415    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
416                     T5 const &m5, T6 const &m6)
417    {
418        if (this->m_count >= this->m_reserved)
419        {
420            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
421            T5 tmp5 = m5; T6 tmp6 = m6;
422            this->Reserve(this->m_count * 13 / 8 + 8);
423            new (&this->m_data[this->m_count].m1) T1(tmp1);
424            new (&this->m_data[this->m_count].m2) T2(tmp2);
425            new (&this->m_data[this->m_count].m3) T3(tmp3);
426            new (&this->m_data[this->m_count].m4) T4(tmp4);
427            new (&this->m_data[this->m_count].m5) T5(tmp5);
428            new (&this->m_data[this->m_count].m6) T6(tmp6);
429        }
430        else
431        {
432            new (&this->m_data[this->m_count].m1) T1(m1);
433            new (&this->m_data[this->m_count].m2) T2(m2);
434            new (&this->m_data[this->m_count].m3) T3(m3);
435            new (&this->m_data[this->m_count].m4) T4(m4);
436            new (&this->m_data[this->m_count].m5) T5(m5);
437            new (&this->m_data[this->m_count].m6) T6(m6);
438        }
439        ++this->m_count;
440    }
441};
442
443template<typename T1, typename T2, typename T3, typename T4, typename T5>
444class Array<T1, T2, T3, T4, T5, void, void, void>
445  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void>,
446                     Array<T1, T2, T3, T4, T5> >
447{
448public:
449    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
450                     T5 const &m5)
451    {
452        if (this->m_count >= this->m_reserved)
453        {
454            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
455            T5 tmp5 = m5;
456            this->Reserve(this->m_count * 13 / 8 + 8);
457            new (&this->m_data[this->m_count].m1) T1(tmp1);
458            new (&this->m_data[this->m_count].m2) T2(tmp2);
459            new (&this->m_data[this->m_count].m3) T3(tmp3);
460            new (&this->m_data[this->m_count].m4) T4(tmp4);
461            new (&this->m_data[this->m_count].m5) T5(tmp5);
462        }
463        else
464        {
465            new (&this->m_data[this->m_count].m1) T1(m1);
466            new (&this->m_data[this->m_count].m2) T2(m2);
467            new (&this->m_data[this->m_count].m3) T3(m3);
468            new (&this->m_data[this->m_count].m4) T4(m4);
469            new (&this->m_data[this->m_count].m5) T5(m5);
470        }
471        ++this->m_count;
472    }
473};
474
475template<typename T1, typename T2, typename T3, typename T4>
476class Array<T1, T2, T3, T4, void, void, void, void>
477  : public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void>,
478                     Array<T1, T2, T3, T4> >
479{
480public:
481    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4)
482    {
483        if (this->m_count >= this->m_reserved)
484        {
485            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
486            this->Reserve(this->m_count * 13 / 8 + 8);
487            new (&this->m_data[this->m_count].m1) T1(tmp1);
488            new (&this->m_data[this->m_count].m2) T2(tmp2);
489            new (&this->m_data[this->m_count].m3) T3(tmp3);
490            new (&this->m_data[this->m_count].m4) T4(tmp4);
491        }
492        else
493        {
494            new (&this->m_data[this->m_count].m1) T1(m1);
495            new (&this->m_data[this->m_count].m2) T2(m2);
496            new (&this->m_data[this->m_count].m3) T3(m3);
497            new (&this->m_data[this->m_count].m4) T4(m4);
498        }
499        ++this->m_count;
500    }
501};
502
503template<typename T1, typename T2, typename T3>
504class Array<T1, T2, T3, void, void, void, void, void>
505  : public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void>,
506                     Array<T1, T2, T3> >
507{
508public:
509    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3)
510    {
511        if (this->m_count >= this->m_reserved)
512        {
513            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3;
514            this->Reserve(this->m_count * 13 / 8 + 8);
515            new (&this->m_data[this->m_count].m1) T1(tmp1);
516            new (&this->m_data[this->m_count].m2) T2(tmp2);
517            new (&this->m_data[this->m_count].m3) T3(tmp3);
518        }
519        else
520        {
521            new (&this->m_data[this->m_count].m1) T1(m1);
522            new (&this->m_data[this->m_count].m2) T2(m2);
523            new (&this->m_data[this->m_count].m3) T3(m3);
524        }
525        ++this->m_count;
526    }
527};
528
529template<typename T1, typename T2>
530class Array<T1, T2, void, void, void, void, void, void>
531  : public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void>,
532                     Array<T1, T2> >
533{
534public:
535    inline void Push(T1 const &m1, T2 const &m2)
536    {
537        if (this->m_count >= this->m_reserved)
538        {
539            T1 tmp1 = m1; T2 tmp2 = m2;
540            this->Reserve(this->m_count * 13 / 8 + 8);
541            new (&this->m_data[this->m_count].m1) T1(tmp1);
542            new (&this->m_data[this->m_count].m2) T2(tmp2);
543        }
544        else
545        {
546            new (&this->m_data[this->m_count].m1) T1(m1);
547            new (&this->m_data[this->m_count].m2) T2(m2);
548        }
549        ++this->m_count;
550    }
551};
552
553template<typename T>
554class Array<T, void, void, void, void, void, void, void>
555  : public ArrayBase<T,
556                     Array<T> >
557{
558};
559
560} /* namespace lol */
561
562#endif // __LOL_BASE_ARRAY_H__
563
Note: See TracBrowser for help on using the repository browser.