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"
37 #define GIM_ARRAY_GROW_INCREMENT 2
38 #define GIM_ARRAY_GROW_FACTOR 2
40 //! Very simple array container with fast access and simd memory
49 GUINT m_allocated_size;
51 //! protected operations
54 inline void destroyData()
57 if (m_data == NULL) return;
62 inline bool resizeData(GUINT newsize)
72 m_data = (T*)gim_realloc(m_data, m_size * sizeof(T), newsize * sizeof(T));
76 m_data = (T*)gim_alloc(newsize * sizeof(T));
78 m_allocated_size = newsize;
82 inline bool growingCheck()
84 if (m_allocated_size <= m_size)
86 GUINT requestsize = m_size;
87 m_size = m_allocated_size;
88 if (resizeData((requestsize + GIM_ARRAY_GROW_INCREMENT) * GIM_ARRAY_GROW_FACTOR) == false) return false;
96 inline bool reserve(GUINT size)
98 if (m_allocated_size >= size) return false;
99 return resizeData(size);
102 inline void clear_range(GUINT start_range)
104 while (m_size > start_range)
106 m_data[--m_size].~T();
112 if (m_size == 0) return;
116 inline void clear_memory()
126 m_allocated_size = 0;
129 gim_array(GUINT reservesize)
134 m_allocated_size = 0;
135 reserve(reservesize);
143 inline GUINT size() const
148 inline GUINT max_size() const
150 return m_allocated_size;
153 inline T& operator[](size_t i)
157 inline const T& operator[](size_t i) const
162 inline T* pointer() { return m_data; }
163 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];
208 inline void swap(GUINT i, GUINT j)
210 gim_swap_elements(m_data, i, j);
213 inline void push_back(const T& obj)
215 this->growingCheck();
216 m_data[m_size] = obj;
220 //!Simply increase the m_size, doesn't call the new element constructor
221 inline void push_back_mem()
223 this->growingCheck();
227 inline void push_back_memcpy(const T& obj)
229 this->growingCheck();
230 gim_simd_memcpy(&m_data[m_size], &obj, sizeof(T));
234 inline void pop_back()
240 //!Simply decrease the m_size, doesn't call the deleted element destructor
241 inline void pop_back_mem()
247 inline void erase(GUINT index)
249 if (index < m_size - 1)
251 swap(index, m_size - 1);
256 inline void erase_sorted_mem(GUINT index)
259 for (GUINT i = index; i < m_size; i++)
261 gim_simd_memcpy(m_data + i, m_data + i + 1, sizeof(T));
265 inline void erase_sorted(GUINT index)
268 erase_sorted_mem(index);
271 inline void insert_mem(GUINT index)
273 this->growingCheck();
274 for (GUINT i = m_size; i > index; i--)
276 gim_simd_memcpy(m_data + i, m_data + i - 1, sizeof(T));
281 inline void insert(const T& obj, GUINT index)
287 inline void resize(GUINT size, bool call_constructor = true, const T& fillData = T())
292 if (call_constructor)
294 while (m_size < size)
296 m_data[m_size] = fillData;
305 else if (size < m_size)
307 if (call_constructor) clear_range(size);
318 #endif // GIM_CONTAINERS_H_INCLUDED