1 #ifndef GIM_ARRAY_H_INCLUDED
2 #define GIM_ARRAY_H_INCLUDED
4 \author Francisco Leon Najera
7 -----------------------------------------------------------------------------
8 This source file is part of GIMPACT Library.
10 For the latest info, see http://gimpact.sourceforge.net/
12 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of EITHER:
17 (1) The GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 2.1 of the License, or (at
19 your option) any later version. The text of the GNU Lesser
20 General Public License is included with this library in the
21 file GIMPACT-LICENSE-LGPL.TXT.
22 (2) The BSD-style license that is included with this library in
23 the file GIMPACT-LICENSE-BSD.TXT.
24 (3) The zlib/libpng license that is included with this library in
25 the file GIMPACT-LICENSE-ZLIB.TXT.
27 This library is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
30 GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
32 -----------------------------------------------------------------------------
35 #include "gim_memory.h"
38 #define GIM_ARRAY_GROW_INCREMENT 2
39 #define GIM_ARRAY_GROW_FACTOR 2
41 //! Very simple array container with fast access and simd memory
50 GUINT m_allocated_size;
52 //! protected operations
55 inline void destroyData()
58 if(m_data==NULL) return;
63 inline bool resizeData(GUINT newsize)
73 m_data = (T*)gim_realloc(m_data,m_size*sizeof(T),newsize*sizeof(T));
77 m_data = (T*)gim_alloc(newsize*sizeof(T));
79 m_allocated_size = newsize;
83 inline bool growingCheck()
85 if(m_allocated_size<=m_size)
87 GUINT requestsize = m_size;
88 m_size = m_allocated_size;
89 if(resizeData((requestsize+GIM_ARRAY_GROW_INCREMENT)*GIM_ARRAY_GROW_FACTOR)==false) return false;
97 inline bool reserve(GUINT size)
99 if(m_allocated_size>=size) return false;
100 return resizeData(size);
103 inline void clear_range(GUINT start_range)
105 while(m_size>start_range)
107 m_data[--m_size].~T();
117 inline void clear_memory()
127 m_allocated_size = 0;
130 gim_array(GUINT reservesize)
135 m_allocated_size = 0;
136 reserve(reservesize);
144 inline GUINT size() const
149 inline GUINT max_size() const
151 return m_allocated_size;
154 inline T & operator[](size_t i)
158 inline const T & operator[](size_t i) const
163 inline T * pointer(){ return m_data;}
164 inline const T * pointer() const
168 inline T * get_pointer_at(GUINT i)
173 inline const T * get_pointer_at(GUINT i) const
178 inline T & at(GUINT i)
183 inline const T & at(GUINT i) const
193 inline const T & front() const
200 return m_data[m_size-1];
203 inline const T & back() const
205 return m_data[m_size-1];
209 inline void swap(GUINT i, GUINT j)
211 gim_swap_elements(m_data,i,j);
214 inline void push_back(const T & obj)
216 this->growingCheck();
217 m_data[m_size] = obj;
221 //!Simply increase the m_size, doesn't call the new element constructor
222 inline void push_back_mem()
224 this->growingCheck();
228 inline void push_back_memcpy(const T & obj)
230 this->growingCheck();
231 irr_simd_memcpy(&m_data[m_size],&obj,sizeof(T));
235 inline void pop_back()
241 //!Simply decrease the m_size, doesn't call the deleted element destructor
242 inline void pop_back_mem()
248 inline void erase(GUINT index)
252 swap(index,m_size-1);
257 inline void erase_sorted_mem(GUINT index)
260 for(GUINT i = index;i<m_size;i++)
262 gim_simd_memcpy(m_data+i,m_data+i+1,sizeof(T));
266 inline void erase_sorted(GUINT index)
269 erase_sorted_mem(index);
272 inline void insert_mem(GUINT index)
274 this->growingCheck();
275 for(GUINT i = m_size;i>index;i--)
277 gim_simd_memcpy(m_data+i,m_data+i-1,sizeof(T));
282 inline void insert(const T & obj,GUINT index)
288 inline void resize(GUINT size, bool call_constructor = true)
299 m_data[m_size] = obj;
310 if(call_constructor) clear_range(size);
326 #endif // GIM_CONTAINERS_H_INCLUDED