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

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

base: add Array::Swap() method and the corresponding unit test.

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