Array.inl 12 KB

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