summaryrefslogtreecommitdiff
path: root/thirdparty/bullet/BulletCollision/Gimpact/gim_array.h
blob: cda51a5fcecf10c534b7496e43fa99e502d75510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#ifndef GIM_ARRAY_H_INCLUDED
#define GIM_ARRAY_H_INCLUDED
/*! \file gim_array.h
\author Francisco Leon Najera
*/
/*
-----------------------------------------------------------------------------
This source file is part of GIMPACT Library.

For the latest info, see http://gimpact.sourceforge.net/

Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
email: projectileman@yahoo.com

 This library is free software; you can redistribute it and/or
 modify it under the terms of EITHER:
   (1) The GNU Lesser General Public License as published by the Free
       Software Foundation; either version 2.1 of the License, or (at
       your option) any later version. The text of the GNU Lesser
       General Public License is included with this library in the
       file GIMPACT-LICENSE-LGPL.TXT.
   (2) The BSD-style license that is included with this library in
       the file GIMPACT-LICENSE-BSD.TXT.
   (3) The zlib/libpng license that is included with this library in
       the file GIMPACT-LICENSE-ZLIB.TXT.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.

-----------------------------------------------------------------------------
*/

#include "gim_memory.h"


#define GIM_ARRAY_GROW_INCREMENT 2
#define GIM_ARRAY_GROW_FACTOR 2

//!	Very simple array container with fast access and simd memory
template<typename T>
class gim_array
{
public:
//! properties
//!@{
    T *m_data;
    GUINT m_size;
    GUINT m_allocated_size;
//!@}
//! protected operations
//!@{

    inline void destroyData()
	{
	    m_allocated_size = 0;
		if(m_data==NULL) return;
		gim_free(m_data);
		m_data = NULL;
	}

	inline bool resizeData(GUINT newsize)
	{
		if(newsize==0)
		{
			destroyData();
			return true;
		}

		if(m_size>0)
		{
            m_data = (T*)gim_realloc(m_data,m_size*sizeof(T),newsize*sizeof(T));
		}
		else
		{
		    m_data = (T*)gim_alloc(newsize*sizeof(T));
		}
		m_allocated_size = newsize;
		return true;
	}

	inline bool growingCheck()
	{
		if(m_allocated_size<=m_size)
		{
		    GUINT requestsize = m_size;
		    m_size = m_allocated_size;
			if(resizeData((requestsize+GIM_ARRAY_GROW_INCREMENT)*GIM_ARRAY_GROW_FACTOR)==false) return false;
		}
		return true;
	}

//!@}
//! public operations
//!@{
    inline  bool reserve(GUINT size)
    {
        if(m_allocated_size>=size) return false;
        return resizeData(size);
    }

    inline void clear_range(GUINT start_range)
    {
        while(m_size>start_range)
        {
            m_data[--m_size].~T();
        }
    }

    inline void clear()
    {
        if(m_size==0)return;
        clear_range(0);
    }

    inline void clear_memory()
    {
        clear();
        destroyData();
    }

    gim_array()
    {
        m_data = 0;
        m_size = 0;
        m_allocated_size = 0;
    }

    gim_array(GUINT reservesize)
    {
        m_data = 0;
        m_size = 0;

        m_allocated_size = 0;
        reserve(reservesize);
    }

    ~gim_array()
    {
        clear_memory();
    }

    inline GUINT size() const
    {
        return m_size;
    }

    inline GUINT max_size() const
    {
        return m_allocated_size;
    }

    inline T & operator[](size_t i)
	{
		return m_data[i];
	}
	inline  const T & operator[](size_t i) const
	{
		return m_data[i];
	}

    inline T * pointer(){ return m_data;}
    inline const T * pointer() const
    { return m_data;}


    inline T * get_pointer_at(GUINT i)
	{
		return m_data + i;
	}

	inline const T * get_pointer_at(GUINT i) const
	{
		return m_data + i;
	}

	inline T & at(GUINT i)
	{
		return m_data[i];
	}

	inline const T & at(GUINT i) const
	{
		return m_data[i];
	}

	inline T & front()
	{
		return *m_data;
	}

	inline const T & front() const
	{
		return *m_data;
	}

	inline T & back()
	{
		return m_data[m_size-1];
	}

	inline const T & back() const
	{
		return m_data[m_size-1];
	}


	inline void swap(GUINT i, GUINT j)
	{
	    gim_swap_elements(m_data,i,j);
	}

	inline void push_back(const T & obj)
	{
	    this->growingCheck();
	    m_data[m_size] = obj;
	    m_size++;
	}

	//!Simply increase the m_size, doesn't call the new element constructor
	inline void push_back_mem()
	{
	    this->growingCheck();
	    m_size++;
	}

	inline void push_back_memcpy(const T & obj)
	{
	    this->growingCheck();
	    gim_simd_memcpy(&m_data[m_size],&obj,sizeof(T));
	    m_size++;
	}

	inline void pop_back()
	{
	    m_size--;
        m_data[m_size].~T();
	}

	//!Simply decrease the m_size, doesn't call the deleted element destructor
	inline void pop_back_mem()
	{
	    m_size--;
	}

    //! fast erase
	inline void erase(GUINT index)
	{
	    if(index<m_size-1)
	    {
	        swap(index,m_size-1);
	    }
	    pop_back();
	}

	inline void erase_sorted_mem(GUINT index)
	{
	    m_size--;
	    for(GUINT i = index;i<m_size;i++)
	    {
	        gim_simd_memcpy(m_data+i,m_data+i+1,sizeof(T));
	    }
	}

	inline void erase_sorted(GUINT index)
	{
	    m_data[index].~T();
	    erase_sorted_mem(index);
	}

	inline void insert_mem(GUINT index)
	{
	    this->growingCheck();
	    for(GUINT i = m_size;i>index;i--)
	    {
	        gim_simd_memcpy(m_data+i,m_data+i-1,sizeof(T));
	    }
	    m_size++;
	}

	inline void insert(const T & obj,GUINT index)
	{
	    insert_mem(index);
	    m_data[index] = obj;
	}

	inline void resize(GUINT size, bool call_constructor = true, const T& fillData=T())
	{
	    if(size>m_size)
	    {
            reserve(size);
            if(call_constructor)
            {
                while(m_size<size)
                {
                    m_data[m_size] = fillData;
                    m_size++;
                }
            }
            else
            {
            	m_size = size;
            }
	    }
	    else if(size<m_size)
	    {
	        if(call_constructor) clear_range(size);
	        m_size = size;
	    }
	}

	inline void refit()
	{
	    resizeData(m_size);
	}

};





#endif // GIM_CONTAINERS_H_INCLUDED