array.inl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // This code is in the public domain -- Ignacio Castaño <[email protected]>
  2. #ifndef NV_CORE_ARRAY_INL
  3. #define NV_CORE_ARRAY_INL
  4. #include "array.h"
  5. #include "stream.h"
  6. #include "utils.h" // swap
  7. #include <string.h> // memmove
  8. #include <new> // for placement new
  9. namespace nv
  10. {
  11. template <typename T>
  12. NV_FORCEINLINE T & Array<T>::append()
  13. {
  14. uint old_size = m_size;
  15. uint new_size = m_size + 1;
  16. setArraySize(new_size);
  17. construct_range(m_buffer, new_size, old_size);
  18. return m_buffer[old_size]; // Return reference to last element.
  19. }
  20. // Push an element at the end of the vector.
  21. template <typename T>
  22. NV_FORCEINLINE void Array<T>::push_back( const T & val )
  23. {
  24. #if 1
  25. nvDebugCheck(&val < m_buffer || &val >= m_buffer+m_size);
  26. uint old_size = m_size;
  27. uint new_size = m_size + 1;
  28. setArraySize(new_size);
  29. construct_range(m_buffer, new_size, old_size, val);
  30. #else
  31. uint new_size = m_size + 1;
  32. if (new_size > m_capacity)
  33. {
  34. // @@ Is there any way to avoid this copy?
  35. // @@ Can we create a copy without side effects? Ie. without calls to constructor/destructor. Use alloca + memcpy?
  36. // @@ Assert instead of copy?
  37. const T copy(val); // create a copy in case value is inside of this array.
  38. setArraySize(new_size);
  39. new (m_buffer+new_size-1) T(copy);
  40. }
  41. else
  42. {
  43. m_size = new_size;
  44. new(m_buffer+new_size-1) T(val);
  45. }
  46. #endif // 0/1
  47. }
  48. template <typename T>
  49. NV_FORCEINLINE void Array<T>::pushBack( const T & val )
  50. {
  51. push_back(val);
  52. }
  53. template <typename T>
  54. NV_FORCEINLINE Array<T> & Array<T>::append( const T & val )
  55. {
  56. push_back(val);
  57. return *this;
  58. }
  59. // Qt like push operator.
  60. template <typename T>
  61. NV_FORCEINLINE Array<T> & Array<T>::operator<< ( T & t )
  62. {
  63. push_back(t);
  64. return *this;
  65. }
  66. // Pop the element at the end of the vector.
  67. template <typename T>
  68. NV_FORCEINLINE void Array<T>::pop_back()
  69. {
  70. nvDebugCheck( m_size > 0 );
  71. resize( m_size - 1 );
  72. }
  73. template <typename T>
  74. NV_FORCEINLINE void Array<T>::popBack(uint count)
  75. {
  76. nvDebugCheck(m_size >= count);
  77. resize(m_size - count);
  78. }
  79. template <typename T>
  80. NV_FORCEINLINE void Array<T>::popFront(uint count)
  81. {
  82. nvDebugCheck(m_size >= count);
  83. //resize(m_size - count);
  84. if (m_size == count) {
  85. clear();
  86. }
  87. else {
  88. destroy_range(m_buffer, 0, count);
  89. memmove(m_buffer, m_buffer + count, sizeof(T) * (m_size - count));
  90. m_size -= count;
  91. }
  92. }
  93. // Get back element.
  94. template <typename T>
  95. NV_FORCEINLINE const T & Array<T>::back() const
  96. {
  97. nvDebugCheck( m_size > 0 );
  98. return m_buffer[m_size-1];
  99. }
  100. // Get back element.
  101. template <typename T>
  102. NV_FORCEINLINE T & Array<T>::back()
  103. {
  104. nvDebugCheck( m_size > 0 );
  105. return m_buffer[m_size-1];
  106. }
  107. // Get front element.
  108. template <typename T>
  109. NV_FORCEINLINE const T & Array<T>::front() const
  110. {
  111. nvDebugCheck( m_size > 0 );
  112. return m_buffer[0];
  113. }
  114. // Get front element.
  115. template <typename T>
  116. NV_FORCEINLINE T & Array<T>::front()
  117. {
  118. nvDebugCheck( m_size > 0 );
  119. return m_buffer[0];
  120. }
  121. // Check if the given element is contained in the array.
  122. template <typename T>
  123. NV_FORCEINLINE bool Array<T>::contains(const T & e) const
  124. {
  125. return find(e, NULL);
  126. }
  127. // Return true if element found.
  128. template <typename T>
  129. NV_FORCEINLINE bool Array<T>::find(const T & element, uint * indexPtr) const
  130. {
  131. return find(element, 0, m_size, indexPtr);
  132. }
  133. // Return true if element found within the given range.
  134. template <typename T>
  135. NV_FORCEINLINE bool Array<T>::find(const T & element, uint begin, uint end, uint * indexPtr) const
  136. {
  137. return ::nv::find(element, m_buffer, begin, end, indexPtr);
  138. }
  139. // Remove the element at the given index. This is an expensive operation!
  140. template <typename T>
  141. void Array<T>::removeAt(uint index)
  142. {
  143. nvDebugCheck(index >= 0 && index < m_size);
  144. if (m_size == 1) {
  145. clear();
  146. }
  147. else {
  148. m_buffer[index].~T();
  149. memmove(m_buffer+index, m_buffer+index+1, sizeof(T) * (m_size - 1 - index));
  150. m_size--;
  151. }
  152. }
  153. // Remove the first instance of the given element.
  154. template <typename T>
  155. bool Array<T>::remove(const T & element)
  156. {
  157. uint index;
  158. if (find(element, &index)) {
  159. removeAt(index);
  160. return true;
  161. }
  162. return false;
  163. }
  164. // Insert the given element at the given index shifting all the elements up.
  165. template <typename T>
  166. void Array<T>::insertAt(uint index, const T & val/*=T()*/)
  167. {
  168. nvDebugCheck( index >= 0 && index <= m_size );
  169. setArraySize(m_size + 1);
  170. if (index < m_size - 1) {
  171. memmove(m_buffer+index+1, m_buffer+index, sizeof(T) * (m_size - 1 - index));
  172. }
  173. // Copy-construct into the newly opened slot.
  174. new(m_buffer+index) T(val);
  175. }
  176. // Append the given data to our vector.
  177. template <typename T>
  178. NV_FORCEINLINE void Array<T>::append(const Array<T> & other)
  179. {
  180. append(other.m_buffer, other.m_size);
  181. }
  182. // Append the given data to our vector.
  183. template <typename T>
  184. void Array<T>::append(const T other[], uint count)
  185. {
  186. if (count > 0) {
  187. const uint old_size = m_size;
  188. setArraySize(m_size + count);
  189. for (uint i = 0; i < count; i++ ) {
  190. new(m_buffer + old_size + i) T(other[i]);
  191. }
  192. }
  193. }
  194. // Remove the given element by replacing it with the last one.
  195. template <typename T>
  196. void Array<T>::replaceWithLast(uint index)
  197. {
  198. nvDebugCheck( index < m_size );
  199. nv::swap(m_buffer[index], back()); // @@ Is this OK when index == size-1?
  200. (m_buffer+m_size-1)->~T();
  201. m_size--;
  202. }
  203. // Resize the vector preserving existing elements.
  204. template <typename T>
  205. void Array<T>::resize(uint new_size)
  206. {
  207. uint old_size = m_size;
  208. // Destruct old elements (if we're shrinking).
  209. destroy_range(m_buffer, new_size, old_size);
  210. setArraySize(new_size);
  211. // Call default constructors
  212. construct_range(m_buffer, new_size, old_size);
  213. }
  214. // Resize the vector preserving existing elements and initializing the
  215. // new ones with the given value.
  216. template <typename T>
  217. void Array<T>::resize(uint new_size, const T & elem)
  218. {
  219. nvDebugCheck(&elem < m_buffer || &elem > m_buffer+m_size);
  220. uint old_size = m_size;
  221. // Destruct old elements (if we're shrinking).
  222. destroy_range(m_buffer, new_size, old_size);
  223. setArraySize(new_size);
  224. // Call copy constructors
  225. construct_range(m_buffer, new_size, old_size, elem);
  226. }
  227. // Fill array with the given value.
  228. template <typename T>
  229. void Array<T>::fill(const T & elem)
  230. {
  231. fill(m_buffer, m_size, elem);
  232. }
  233. // Clear the buffer.
  234. template <typename T>
  235. NV_FORCEINLINE void Array<T>::clear()
  236. {
  237. nvDebugCheck(isValidPtr(m_buffer));
  238. // Destruct old elements
  239. destroy_range(m_buffer, 0, m_size);
  240. m_size = 0;
  241. }
  242. // Shrink the allocated vector.
  243. template <typename T>
  244. NV_FORCEINLINE void Array<T>::shrink()
  245. {
  246. if (m_size < m_capacity) {
  247. setArrayCapacity(m_size);
  248. }
  249. }
  250. // Preallocate space.
  251. template <typename T>
  252. NV_FORCEINLINE void Array<T>::reserve(uint desired_size)
  253. {
  254. if (desired_size > m_capacity) {
  255. setArrayCapacity(desired_size);
  256. }
  257. }
  258. // Copy elements to this array. Resizes it if needed.
  259. template <typename T>
  260. NV_FORCEINLINE void Array<T>::copy(const T * data, uint count)
  261. {
  262. #if 1 // More simple, but maybe not be as efficient?
  263. destroy_range(m_buffer, 0, m_size);
  264. setArraySize(count);
  265. construct_range(m_buffer, count, 0, data);
  266. #else
  267. const uint old_size = m_size;
  268. destroy_range(m_buffer, count, old_size);
  269. setArraySize(count);
  270. copy_range(m_buffer, data, old_size);
  271. construct_range(m_buffer, count, old_size, data);
  272. #endif
  273. }
  274. // Assignment operator.
  275. template <typename T>
  276. NV_FORCEINLINE Array<T> & Array<T>::operator=( const Array<T> & a )
  277. {
  278. copy(a.m_buffer, a.m_size);
  279. return *this;
  280. }
  281. // Release ownership of allocated memory and returns pointer to it.
  282. template <typename T>
  283. T * Array<T>::release() {
  284. T * tmp = m_buffer;
  285. m_buffer = NULL;
  286. m_capacity = 0;
  287. m_size = 0;
  288. return tmp;
  289. }
  290. // Change array size.
  291. template <typename T>
  292. inline void Array<T>::setArraySize(uint new_size) {
  293. m_size = new_size;
  294. if (new_size > m_capacity) {
  295. uint new_buffer_size;
  296. if (m_capacity == 0) {
  297. // first allocation is exact
  298. new_buffer_size = new_size;
  299. }
  300. else {
  301. // following allocations grow array by 25%
  302. new_buffer_size = new_size + (new_size >> 2);
  303. }
  304. setArrayCapacity( new_buffer_size );
  305. }
  306. }
  307. // Change array capacity.
  308. template <typename T>
  309. inline void Array<T>::setArrayCapacity(uint new_capacity) {
  310. nvDebugCheck(new_capacity >= m_size);
  311. if (new_capacity == 0) {
  312. // free the buffer.
  313. if (m_buffer != NULL) {
  314. free<T>(m_buffer);
  315. m_buffer = NULL;
  316. }
  317. }
  318. else {
  319. // realloc the buffer
  320. m_buffer = realloc<T>(m_buffer, new_capacity);
  321. }
  322. m_capacity = new_capacity;
  323. }
  324. // Array serialization.
  325. template <typename Typ>
  326. inline Stream & operator<< ( Stream & s, Array<Typ> & p )
  327. {
  328. if (s.isLoading()) {
  329. uint size;
  330. s << size;
  331. p.resize( size );
  332. }
  333. else {
  334. s << p.m_size;
  335. }
  336. for (uint i = 0; i < p.m_size; i++) {
  337. s << p.m_buffer[i];
  338. }
  339. return s;
  340. }
  341. // Swap the members of the two given vectors.
  342. template <typename Typ>
  343. inline void swap(Array<Typ> & a, Array<Typ> & b)
  344. {
  345. nv::swap(a.m_buffer, b.m_buffer);
  346. nv::swap(a.m_capacity, b.m_capacity);
  347. nv::swap(a.m_size, b.m_size);
  348. }
  349. } // nv namespace
  350. #endif // NV_CORE_ARRAY_INL