gim_array.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #ifndef GIM_ARRAY_H_INCLUDED
  2. #define GIM_ARRAY_H_INCLUDED
  3. /*! \file gim_array.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. -----------------------------------------------------------------------------
  8. This source file is part of GIMPACT Library.
  9. For the latest info, see http://gimpact.sourceforge.net/
  10. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  11. email: projectileman@yahoo.com
  12. This library is free software; you can redistribute it and/or
  13. modify it under the terms of EITHER:
  14. (1) The GNU Lesser General Public License as published by the Free
  15. Software Foundation; either version 2.1 of the License, or (at
  16. your option) any later version. The text of the GNU Lesser
  17. General Public License is included with this library in the
  18. file GIMPACT-LICENSE-LGPL.TXT.
  19. (2) The BSD-style license that is included with this library in
  20. the file GIMPACT-LICENSE-BSD.TXT.
  21. (3) The zlib/libpng license that is included with this library in
  22. the file GIMPACT-LICENSE-ZLIB.TXT.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  26. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  27. -----------------------------------------------------------------------------
  28. */
  29. #include "gim_memory.h"
  30. #define GIM_ARRAY_GROW_INCREMENT 2
  31. #define GIM_ARRAY_GROW_FACTOR 2
  32. //! Very simple array container with fast access and simd memory
  33. template<typename T>
  34. class gim_array
  35. {
  36. public:
  37. //! properties
  38. //!@{
  39. T *m_data;
  40. GUINT m_size;
  41. GUINT m_allocated_size;
  42. //!@}
  43. //! protected operations
  44. //!@{
  45. inline void destroyData()
  46. {
  47. m_allocated_size = 0;
  48. if(m_data==NULL) return;
  49. gim_free(m_data);
  50. m_data = NULL;
  51. }
  52. inline bool resizeData(GUINT newsize)
  53. {
  54. if(newsize==0)
  55. {
  56. destroyData();
  57. return true;
  58. }
  59. if(m_size>0)
  60. {
  61. m_data = (T*)gim_realloc(m_data,m_size*sizeof(T),newsize*sizeof(T));
  62. }
  63. else
  64. {
  65. m_data = (T*)gim_alloc(newsize*sizeof(T));
  66. }
  67. m_allocated_size = newsize;
  68. return true;
  69. }
  70. inline bool growingCheck()
  71. {
  72. if(m_allocated_size<=m_size)
  73. {
  74. GUINT requestsize = m_size;
  75. m_size = m_allocated_size;
  76. if(resizeData((requestsize+GIM_ARRAY_GROW_INCREMENT)*GIM_ARRAY_GROW_FACTOR)==false) return false;
  77. }
  78. return true;
  79. }
  80. //!@}
  81. //! public operations
  82. //!@{
  83. inline bool reserve(GUINT size)
  84. {
  85. if(m_allocated_size>=size) return false;
  86. return resizeData(size);
  87. }
  88. inline void clear_range(GUINT start_range)
  89. {
  90. while(m_size>start_range)
  91. {
  92. m_data[--m_size].~T();
  93. }
  94. }
  95. inline void clear()
  96. {
  97. if(m_size==0)return;
  98. clear_range(0);
  99. }
  100. inline void clear_memory()
  101. {
  102. clear();
  103. destroyData();
  104. }
  105. gim_array()
  106. {
  107. m_data = 0;
  108. m_size = 0;
  109. m_allocated_size = 0;
  110. }
  111. gim_array(GUINT reservesize)
  112. {
  113. m_data = 0;
  114. m_size = 0;
  115. m_allocated_size = 0;
  116. reserve(reservesize);
  117. }
  118. ~gim_array()
  119. {
  120. clear_memory();
  121. }
  122. inline GUINT size() const
  123. {
  124. return m_size;
  125. }
  126. inline GUINT max_size() const
  127. {
  128. return m_allocated_size;
  129. }
  130. inline T & operator[](size_t i)
  131. {
  132. return m_data[i];
  133. }
  134. inline const T & operator[](size_t i) const
  135. {
  136. return m_data[i];
  137. }
  138. inline T * pointer(){ return m_data;}
  139. inline const T * pointer() const
  140. { return m_data;}
  141. inline T * get_pointer_at(GUINT i)
  142. {
  143. return m_data + i;
  144. }
  145. inline const T * get_pointer_at(GUINT i) const
  146. {
  147. return m_data + i;
  148. }
  149. inline T & at(GUINT i)
  150. {
  151. return m_data[i];
  152. }
  153. inline const T & at(GUINT i) const
  154. {
  155. return m_data[i];
  156. }
  157. inline T & front()
  158. {
  159. return *m_data;
  160. }
  161. inline const T & front() const
  162. {
  163. return *m_data;
  164. }
  165. inline T & back()
  166. {
  167. return m_data[m_size-1];
  168. }
  169. inline const T & back() const
  170. {
  171. return m_data[m_size-1];
  172. }
  173. inline void swap(GUINT i, GUINT j)
  174. {
  175. gim_swap_elements(m_data,i,j);
  176. }
  177. inline void push_back(const T & obj)
  178. {
  179. this->growingCheck();
  180. m_data[m_size] = obj;
  181. m_size++;
  182. }
  183. //!Simply increase the m_size, doesn't call the new element constructor
  184. inline void push_back_mem()
  185. {
  186. this->growingCheck();
  187. m_size++;
  188. }
  189. inline void push_back_memcpy(const T & obj)
  190. {
  191. this->growingCheck();
  192. irr_simd_memcpy(&m_data[m_size],&obj,sizeof(T));
  193. m_size++;
  194. }
  195. inline void pop_back()
  196. {
  197. m_size--;
  198. m_data[m_size].~T();
  199. }
  200. //!Simply decrease the m_size, doesn't call the deleted element destructor
  201. inline void pop_back_mem()
  202. {
  203. m_size--;
  204. }
  205. //! fast erase
  206. inline void erase(GUINT index)
  207. {
  208. if(index<m_size-1)
  209. {
  210. swap(index,m_size-1);
  211. }
  212. pop_back();
  213. }
  214. inline void erase_sorted_mem(GUINT index)
  215. {
  216. m_size--;
  217. for(GUINT i = index;i<m_size;i++)
  218. {
  219. gim_simd_memcpy(m_data+i,m_data+i+1,sizeof(T));
  220. }
  221. }
  222. inline void erase_sorted(GUINT index)
  223. {
  224. m_data[index].~T();
  225. erase_sorted_mem(index);
  226. }
  227. inline void insert_mem(GUINT index)
  228. {
  229. this->growingCheck();
  230. for(GUINT i = m_size;i>index;i--)
  231. {
  232. gim_simd_memcpy(m_data+i,m_data+i-1,sizeof(T));
  233. }
  234. m_size++;
  235. }
  236. inline void insert(const T & obj,GUINT index)
  237. {
  238. insert_mem(index);
  239. m_data[index] = obj;
  240. }
  241. inline void resize(GUINT size, bool call_constructor = true, const T& fillData=T())
  242. {
  243. if(size>m_size)
  244. {
  245. reserve(size);
  246. if(call_constructor)
  247. {
  248. while(m_size<size)
  249. {
  250. m_data[m_size] = fillData;
  251. m_size++;
  252. }
  253. }
  254. else
  255. {
  256. m_size = size;
  257. }
  258. }
  259. else if(size<m_size)
  260. {
  261. if(call_constructor) clear_range(size);
  262. m_size = size;
  263. }
  264. }
  265. inline void refit()
  266. {
  267. resizeData(m_size);
  268. }
  269. };
  270. #endif // GIM_CONTAINERS_H_INCLUDED