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

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

base: add safety asserts to the Array and String classes.

  • Property svn:keywords set to Id
File size: 16.7 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+=(ARRAY 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            *this << that[i];
104        return *this;
105    }
106
107    ARRAY operator+(ARRAY const &that) const
108    {
109        /* FIXME: upon return, this makes a copy of the temporary object;
110         * use either C++11 move semantics, or add a special flag to the
111         * object indicating we're a temporary about to be destroyed */
112        ARRAY ret;
113        ret.Reserve(m_count + that.m_count);
114        for (int i = 0; i < m_count; i++)
115            ret << (*this)[i];
116        for (int i = 0; i < that.m_count; i++)
117            ret << that[i];
118        return ret;
119    }
120
121    inline Element& operator[](int n)
122    {
123        /* Allow array[0] even if size is zero so that people can
124         * always use &array[0] to get a pointer to the data. */
125        ASSERT(n >= 0);
126        ASSERT(n < m_count || (!n && !m_count));
127        return m_data[n];
128    }
129
130    inline Element const& operator[](int n) const
131    {
132        ASSERT(n >= 0);
133        ASSERT(n < m_count || (!n && !m_count));
134        return m_data[n];
135    }
136
137    inline Element& Last()
138    {
139        ASSERT(m_count > 0);
140        return m_data[m_count - 1];
141    }
142
143    inline Element const& Last() const
144    {
145        ASSERT(m_count > 0);
146        return m_data[m_count - 1];
147    }
148
149    inline ArrayBase& operator<<(T const &x)
150    {
151        if (m_count >= m_reserved)
152        {
153            T tmp = x;
154            Reserve(m_count * 13 / 8 + 8);
155            new (&m_data[m_count++]) Element(tmp);
156        }
157        else
158        {
159            new (&m_data[m_count++]) Element(x);
160        }
161        return *this;
162    }
163
164    inline void Push(T const &x)
165    {
166        *this << x;
167    }
168
169    inline void Pop()
170    {
171        ASSERT(m_count > 0);
172        Remove(m_count - 1, 1);
173    }
174
175    void Remove(int pos, int todelete = 1)
176    {
177        ASSERT(pos >= 0);
178        ASSERT(pos + todelete <= m_count);
179        for (int i = pos; i + todelete < m_count; i++)
180            m_data[i] = m_data[i + todelete];
181        for (int i = m_count - todelete; i < m_count; i++)
182            m_data[i].~Element();
183        m_count -= todelete;
184    }
185
186    void Resize(int count, Element e = Element())
187    {
188        ASSERT(count > 0);
189        Reserve(count);
190
191        /* Too many elements? Remove them. */
192        for (int i = count; i < m_count; ++i)
193            m_data[i].~Element();
194
195        /* Not enough elements? Add some. */
196        for (int i = m_count; i < count; ++i)
197            new(&m_data[i]) Element(e);
198
199        m_count = count;
200    }
201
202    inline void Empty()
203    {
204        Remove(0, m_count);
205    }
206
207    void Reserve(int toreserve)
208    {
209        if (toreserve <= (int)m_reserved)
210            return;
211
212        /* This cast is not very nice, because we kill any alignment
213         * information we could have. But until C++ gives us the proper
214         * tools to deal with it, we assume new uint8_t[] returns properly
215         * aligned data. */
216        Element *tmp = reinterpret_cast<Element *>(reinterpret_cast<uintptr_t>
217                               (new uint8_t[sizeof(Element) * toreserve]));
218        for (int i = 0; i < m_count; i++)
219        {
220            new(&tmp[i]) Element(m_data[i]);
221            m_data[i].~Element();
222        }
223        if (m_data)
224            delete[] reinterpret_cast<uint8_t *>(m_data);
225        m_data = tmp;
226        m_reserved = toreserve;
227    }
228
229    inline int Count() const { return m_count; }
230    inline int Bytes() const { return m_count * sizeof(Element); }
231
232protected:
233    Element *m_data;
234    int m_count, m_reserved;
235};
236
237/*
238 * Element types
239 */
240
241template<typename T1, typename T2, typename T3 = void, typename T4 = void,
242         typename T5 = void, typename T6 = void, typename T7 = void,
243         typename T8 = void>
244class ArrayElement
245{
246public:
247    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7; T8 m8;
248};
249
250template<typename T1, typename T2, typename T3, typename T4, typename T5,
251         typename T6, typename T7>
252class ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>
253{
254public:
255    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6; T7 m7;
256};
257
258template<typename T1, typename T2, typename T3, typename T4, typename T5,
259         typename T6>
260class ArrayElement<T1, T2, T3, T4, T5, T6, void, void>
261{
262public:
263    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5; T6 m6;
264};
265
266template<typename T1, typename T2, typename T3, typename T4, typename T5>
267class ArrayElement<T1, T2, T3, T4, T5, void, void, void>
268{
269public:
270    T1 m1; T2 m2; T3 m3; T4 m4; T5 m5;
271};
272
273template<typename T1, typename T2, typename T3, typename T4>
274class ArrayElement<T1, T2, T3, T4, void, void, void, void>
275{
276public:
277    T1 m1; T2 m2; T3 m3; T4 m4;
278};
279
280template<typename T1, typename T2, typename T3>
281class ArrayElement<T1, T2, T3, void, void, void, void, void>
282{
283public:
284    T1 m1; T2 m2; T3 m3;
285};
286
287template<typename T1, typename T2>
288class ArrayElement<T1, T2, void, void, void, void, void, void>
289{
290public:
291    T1 m1; T2 m2;
292};
293
294/*
295 * Array specialisations implementing specific setters
296 */
297
298template<typename T1, typename T2 = void, typename T3 = void,
299         typename T4 = void, typename T5 = void, typename T6 = void,
300         typename T7 = void, typename T8 = void>
301class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8>,
302                               Array<T1, T2, T3, T4, T5, T6, T7, T8> >
303{
304public:
305    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
306                     T5 const &m5, T6 const &m6, T7 const &m7, T8 const &m8)
307    {
308        if (this->m_count >= this->m_reserved)
309        {
310            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
311            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7; T8 tmp8 = m8;
312            this->Reserve(this->m_count * 13 / 8 + 8);
313            new (&this->m_data[this->m_count].m1) T1(tmp1);
314            new (&this->m_data[this->m_count].m2) T2(tmp2);
315            new (&this->m_data[this->m_count].m3) T3(tmp3);
316            new (&this->m_data[this->m_count].m4) T4(tmp4);
317            new (&this->m_data[this->m_count].m5) T5(tmp5);
318            new (&this->m_data[this->m_count].m6) T6(tmp6);
319            new (&this->m_data[this->m_count].m7) T7(tmp7);
320            new (&this->m_data[this->m_count].m8) T8(tmp8);
321        }
322        else
323        {
324            new (&this->m_data[this->m_count].m1) T1(m1);
325            new (&this->m_data[this->m_count].m2) T2(m2);
326            new (&this->m_data[this->m_count].m3) T3(m3);
327            new (&this->m_data[this->m_count].m4) T4(m4);
328            new (&this->m_data[this->m_count].m5) T5(m5);
329            new (&this->m_data[this->m_count].m6) T6(m6);
330            new (&this->m_data[this->m_count].m7) T7(m7);
331            new (&this->m_data[this->m_count].m8) T8(m8);
332        }
333        ++this->m_count;
334    }
335};
336
337template<typename T1, typename T2, typename T3, typename T4, typename T5,
338         typename T6, typename T7>
339class Array<T1, T2, T3, T4, T5, T6, T7, void>
340  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>,
341                     Array<T1, T2, T3, T4, T5, T6, T7> >
342{
343public:
344    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
345                     T5 const &m5, T6 const &m6, T7 const &m7)
346    {
347        if (this->m_count >= this->m_reserved)
348        {
349            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
350            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7;
351            this->Reserve(this->m_count * 13 / 8 + 8);
352            new (&this->m_data[this->m_count].m1) T1(tmp1);
353            new (&this->m_data[this->m_count].m2) T2(tmp2);
354            new (&this->m_data[this->m_count].m3) T3(tmp3);
355            new (&this->m_data[this->m_count].m4) T4(tmp4);
356            new (&this->m_data[this->m_count].m5) T5(tmp5);
357            new (&this->m_data[this->m_count].m6) T6(tmp6);
358            new (&this->m_data[this->m_count].m7) T7(tmp7);
359        }
360        else
361        {
362            new (&this->m_data[this->m_count].m1) T1(m1);
363            new (&this->m_data[this->m_count].m2) T2(m2);
364            new (&this->m_data[this->m_count].m3) T3(m3);
365            new (&this->m_data[this->m_count].m4) T4(m4);
366            new (&this->m_data[this->m_count].m5) T5(m5);
367            new (&this->m_data[this->m_count].m6) T6(m6);
368            new (&this->m_data[this->m_count].m7) T7(m7);
369        }
370        ++this->m_count;
371    }
372};
373
374template<typename T1, typename T2, typename T3, typename T4, typename T5,
375         typename T6>
376class Array<T1, T2, T3, T4, T5, T6, void, void>
377  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void>,
378                     Array<T1, T2, T3, T4, T5, T6> >
379{
380public:
381    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
382                     T5 const &m5, T6 const &m6)
383    {
384        if (this->m_count >= this->m_reserved)
385        {
386            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
387            T5 tmp5 = m5; T6 tmp6 = m6;
388            this->Reserve(this->m_count * 13 / 8 + 8);
389            new (&this->m_data[this->m_count].m1) T1(tmp1);
390            new (&this->m_data[this->m_count].m2) T2(tmp2);
391            new (&this->m_data[this->m_count].m3) T3(tmp3);
392            new (&this->m_data[this->m_count].m4) T4(tmp4);
393            new (&this->m_data[this->m_count].m5) T5(tmp5);
394            new (&this->m_data[this->m_count].m6) T6(tmp6);
395        }
396        else
397        {
398            new (&this->m_data[this->m_count].m1) T1(m1);
399            new (&this->m_data[this->m_count].m2) T2(m2);
400            new (&this->m_data[this->m_count].m3) T3(m3);
401            new (&this->m_data[this->m_count].m4) T4(m4);
402            new (&this->m_data[this->m_count].m5) T5(m5);
403            new (&this->m_data[this->m_count].m6) T6(m6);
404        }
405        ++this->m_count;
406    }
407};
408
409template<typename T1, typename T2, typename T3, typename T4, typename T5>
410class Array<T1, T2, T3, T4, T5, void, void, void>
411  : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void>,
412                     Array<T1, T2, T3, T4, T5> >
413{
414public:
415    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
416                     T5 const &m5)
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;
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        }
429        else
430        {
431            new (&this->m_data[this->m_count].m1) T1(m1);
432            new (&this->m_data[this->m_count].m2) T2(m2);
433            new (&this->m_data[this->m_count].m3) T3(m3);
434            new (&this->m_data[this->m_count].m4) T4(m4);
435            new (&this->m_data[this->m_count].m5) T5(m5);
436        }
437        ++this->m_count;
438    }
439};
440
441template<typename T1, typename T2, typename T3, typename T4>
442class Array<T1, T2, T3, T4, void, void, void, void>
443  : public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void>,
444                     Array<T1, T2, T3, T4> >
445{
446public:
447    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4)
448    {
449        if (this->m_count >= this->m_reserved)
450        {
451            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
452            this->Reserve(this->m_count * 13 / 8 + 8);
453            new (&this->m_data[this->m_count].m1) T1(tmp1);
454            new (&this->m_data[this->m_count].m2) T2(tmp2);
455            new (&this->m_data[this->m_count].m3) T3(tmp3);
456            new (&this->m_data[this->m_count].m4) T4(tmp4);
457        }
458        else
459        {
460            new (&this->m_data[this->m_count].m1) T1(m1);
461            new (&this->m_data[this->m_count].m2) T2(m2);
462            new (&this->m_data[this->m_count].m3) T3(m3);
463            new (&this->m_data[this->m_count].m4) T4(m4);
464        }
465        ++this->m_count;
466    }
467};
468
469template<typename T1, typename T2, typename T3>
470class Array<T1, T2, T3, void, void, void, void, void>
471  : public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void>,
472                     Array<T1, T2, T3> >
473{
474public:
475    inline void Push(T1 const &m1, T2 const &m2, T3 const &m3)
476    {
477        if (this->m_count >= this->m_reserved)
478        {
479            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3;
480            this->Reserve(this->m_count * 13 / 8 + 8);
481            new (&this->m_data[this->m_count].m1) T1(tmp1);
482            new (&this->m_data[this->m_count].m2) T2(tmp2);
483            new (&this->m_data[this->m_count].m3) T3(tmp3);
484        }
485        else
486        {
487            new (&this->m_data[this->m_count].m1) T1(m1);
488            new (&this->m_data[this->m_count].m2) T2(m2);
489            new (&this->m_data[this->m_count].m3) T3(m3);
490        }
491        ++this->m_count;
492    }
493};
494
495template<typename T1, typename T2>
496class Array<T1, T2, void, void, void, void, void, void>
497  : public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void>,
498                     Array<T1, T2> >
499{
500public:
501    inline void Push(T1 const &m1, T2 const &m2)
502    {
503        if (this->m_count >= this->m_reserved)
504        {
505            T1 tmp1 = m1; T2 tmp2 = m2;
506            this->Reserve(this->m_count * 13 / 8 + 8);
507            new (&this->m_data[this->m_count].m1) T1(tmp1);
508            new (&this->m_data[this->m_count].m2) T2(tmp2);
509        }
510        else
511        {
512            new (&this->m_data[this->m_count].m1) T1(m1);
513            new (&this->m_data[this->m_count].m2) T2(m2);
514        }
515        ++this->m_count;
516    }
517};
518
519template<typename T>
520class Array<T, void, void, void, void, void, void, void>
521  : public ArrayBase<T,
522                     Array<T> >
523{
524};
525
526} /* namespace lol */
527
528#endif // __LOL_BASE_ARRAY_H__
529
Note: See TracBrowser for help on using the repository browser.