daeArray.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
  5. * file except in compliance with the License. You may obtain a copy of the License at:
  6. * http://research.scea.com/scea_shared_source_license.html
  7. *
  8. * Unless required by applicable law or agreed to in writing, software distributed under the License
  9. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing permissions and limitations under the
  11. * License.
  12. */
  13. #ifndef __DAE_ARRAY_H__
  14. #define __DAE_ARRAY_H__
  15. #include <new>
  16. #include <dae/daeMemorySystem.h>
  17. class daeAtomicType;
  18. /**
  19. * COLLADA C++ class that implements storage for resizable array containers.
  20. */
  21. class daeArray
  22. {
  23. protected:
  24. size_t _count;
  25. size_t _capacity;
  26. daeMemoryRef _data;
  27. size_t _elementSize;
  28. daeAtomicType* _type;
  29. public:
  30. /**
  31. * Constructor
  32. */
  33. DLLSPEC daeArray();
  34. /**
  35. * Destructor
  36. */
  37. virtual DLLSPEC ~daeArray();
  38. /**
  39. * Clears the contents of the array. Do not use this function if the array contains @c daeSmartRef objects and the
  40. * @c dom* class the array belongs to has a @c _contents member.
  41. *
  42. * Many @c dom* objects have a @c _contents member that stores the original creation order of the @c daeElements
  43. * that are their children. If you use @c clear() on a @c daeArray of @c daeSmartRef derived objects, these
  44. * objects will not be removed from @c _contents, which can cause problems when you
  45. * save the data. We recommended that @c clear() not be used on arrays that are part of a @c dom* object.
  46. */
  47. virtual DLLSPEC void clear() = 0;
  48. /**
  49. * Sets the size of an element in the array. This clears and reinitializes the array.
  50. * @param elementSize Size of an element in the array.
  51. */
  52. DLLSPEC void setElementSize(size_t elementSize);
  53. /**
  54. * Gets the size of an element in this array.
  55. * @return Returns the size of an element in this array.
  56. */
  57. size_t getElementSize() const {return _elementSize;}
  58. /**
  59. * Grows the array to the specified size and sets the @c daeArray to that size.
  60. * @param cnt Size to grow the array to.
  61. */
  62. virtual void setCount(size_t cnt) = 0;
  63. /**
  64. * Gets the number of items stored in this @c daeArray.
  65. * @return Returns the number of items stored in this @c daeArray.
  66. */
  67. size_t getCount() const {return _count;}
  68. /**
  69. * Increases the capacity of the @c daeArray.
  70. * @param minCapacity The minimum array capacity (the actual resulting capacity may be higher).
  71. */
  72. virtual void grow(size_t minCapacity) = 0;
  73. /**
  74. * Gets the current capacity of the array, the biggest it can get without incurring a realloc.
  75. * @return Returns the capacity of the array.
  76. */
  77. size_t getCapacity() const {return _capacity;}
  78. /**
  79. * Gets a pointer to the raw memory of a particular element.
  80. * @return Returns a pointer to the memory for the raw data.
  81. */
  82. daeMemoryRef getRaw(size_t index) const {return _data + index*_elementSize;}
  83. /**
  84. * Removes an item at a specific index in the @c daeArray.
  85. * @param index Index number of the item to delete.
  86. * @return Returns DAE_OK if success, a negative value defined in daeError.h otherwise.
  87. * @note The @c daeElement objects sometimes list
  88. * objects in two places, the class member and the <i> @c _contents </i> array, when you remove something from the
  89. * dom, you must remove it from both places.
  90. */
  91. virtual daeInt removeIndex(size_t index) = 0;
  92. // Provided for backward compatibility only. Don't use these.
  93. void setRawCount(size_t cnt) { setCount(cnt); } // Use setCount instead
  94. daeMemoryRef getRawData() const {return _data;} // Use getRaw instead
  95. };
  96. /**
  97. * COLLADA C++ templated version of @c daeArray for storing items of various types.
  98. */
  99. template <class T>
  100. class daeTArray : public daeArray
  101. {
  102. protected:
  103. T* prototype;
  104. public:
  105. /**
  106. * Constructor.
  107. */
  108. daeTArray() {
  109. _elementSize = sizeof( T );
  110. prototype = NULL;
  111. }
  112. /**
  113. * Constructor.
  114. */
  115. explicit daeTArray(T* prototype) : prototype(prototype) {
  116. _elementSize = sizeof( T );
  117. }
  118. /**
  119. * Copy Constructor
  120. */
  121. daeTArray( const daeTArray<T> &cpy ) : daeArray() {
  122. prototype = NULL;
  123. *this = cpy;
  124. }
  125. /**
  126. * Constructor that takes one element and turns into an array
  127. */
  128. explicit daeTArray( const T &el ) {
  129. _elementSize = sizeof(T);
  130. prototype = NULL;
  131. append( el );
  132. }
  133. /**
  134. * Destructor.
  135. */
  136. virtual ~daeTArray() {
  137. clear();
  138. delete prototype;
  139. }
  140. /**
  141. * Frees the memory in this array and resets it to it's initial state.
  142. */
  143. virtual void clear()
  144. {
  145. for(size_t i=0;i<_count;i++)
  146. ((T*)_data + i)->~T();
  147. free(_data);
  148. _count = 0;
  149. _capacity = 0;
  150. _data = NULL;
  151. }
  152. /**
  153. * Increases the capacity of the @c daeArray.
  154. * @param minCapacity The minimum array capacity (the actual resulting capacity may be higher).
  155. */
  156. void grow(size_t minCapacity) {
  157. if (minCapacity <= _capacity)
  158. return;
  159. size_t newCapacity = _capacity == 0 ? 1 : _capacity;
  160. while(newCapacity < minCapacity)
  161. newCapacity *= 2;
  162. T* newData = (T*)malloc(newCapacity*_elementSize);
  163. for (size_t i = 0; i < _count; i++) {
  164. new (&newData[i]) T(get(i));
  165. ((T*)_data + i)->~T();
  166. }
  167. if (_data != NULL)
  168. free(_data);
  169. _data = (daeMemoryRef)newData;
  170. _capacity = newCapacity;
  171. }
  172. /**
  173. * Removes an item at a specific index in the @c daeArray.
  174. * @param index Index number of the item to delete.
  175. * @return Returns DAE_OK if success, a negative value defined in daeError.h otherwise.
  176. * @note The @c daeElement objects sometimes list
  177. * objects in two places, the class member and the <i> @c _contents </i> array, when you remove something from the
  178. * dom, you must remove it from both places.
  179. */
  180. virtual daeInt removeIndex(size_t index)
  181. {
  182. if (index >= _count)
  183. return(DAE_ERR_INVALID_CALL);
  184. for (size_t i = index; i < _count-1; i++)
  185. *((T*)_data+i) = *((T*)_data+i+1);
  186. ((T*)_data+(_count-1))->~T();
  187. _count--;
  188. return DAE_OK;
  189. }
  190. /**
  191. * Resets the number of elements in the array. If the array increases in size, the new
  192. * elements will be initialized to the specified value.
  193. * @param nElements The new size of the array.
  194. * @param value The value new elements will be initialized to.
  195. * @note Shrinking the array does NOT free up memory.
  196. */
  197. void setCount(size_t nElements, const T& value)
  198. {
  199. grow(nElements);
  200. // Destruct the elements that are being chopped off
  201. for (size_t i = nElements; i < _count; i++)
  202. ((T*)_data+i)->~T();
  203. // Use value to initialize the new elements
  204. for (size_t i = _count; i < nElements; i++)
  205. new ((void*)((T*)_data+i)) T(value);
  206. _count = nElements;
  207. }
  208. /**
  209. * Resets the number of elements in the array. If the array increases in size, the new
  210. * elements will be initialized with a default constructor.
  211. * @param nElements The new size of the array.
  212. * @note Shrinking the array does NOT free up memory.
  213. */
  214. virtual void setCount(size_t nElements) {
  215. if (prototype)
  216. setCount(nElements, *prototype);
  217. else
  218. setCount(nElements, T());
  219. }
  220. /**
  221. * Sets a specific index in the @c daeArray, growing the array if necessary.
  222. * @param index Index of the object to set, asserts if the index is out of bounds.
  223. * @param value Value to store at index in the array.
  224. */
  225. void set(size_t index, const T& value) {
  226. if (index >= _count)
  227. setCount(index+1);
  228. ((T*)_data)[index] = value;
  229. }
  230. /**
  231. * Gets the object at a specific index in the @c daeArray.
  232. * @param index Index of the object to get, asserts if the index is out of bounds.
  233. * @return Returns the object at index.
  234. */
  235. T& get(size_t index) {
  236. assert(index < _count);
  237. return ((T*)_data)[index]; }
  238. /**
  239. * Gets the object at a specific index in the @c daeArray.
  240. * @param index Index of the object to get, asserts if the index is out of bounds.
  241. * @return Returns the object at index.
  242. */
  243. const T& get(size_t index) const {
  244. assert(index < _count);
  245. return ((T*)_data)[index]; }
  246. /**
  247. * Appends a new object to the end of the @c daeArray.
  248. * @param value Value of the object to append.
  249. * @return Returns the index of the new object.
  250. */
  251. size_t append(const T& value) {
  252. set(_count, value);
  253. return _count-1;
  254. }
  255. /**
  256. * Appends a unique object to the end of the @c daeArray.
  257. * Functions the same as @c append(), but does nothing if the value is already in the @c daeArray.
  258. * @param value Value of the object to append.
  259. * @return Returns the index where this value was appended. If the value already exists in the array,
  260. * returns the index in this array where the value was found.
  261. */
  262. size_t appendUnique(const T& value) {
  263. size_t ret;
  264. if (find(value,ret) != DAE_OK)
  265. return append(value);
  266. else
  267. return ret;
  268. }
  269. /**
  270. * Adds a new item to the front of the @c daeArray.
  271. * @param value Item to be added.
  272. */
  273. void prepend(const T& value) {
  274. insertAt(0, value);
  275. }
  276. /**
  277. * Removes an item from the @c daeArray.
  278. * @param value A reference to the item to delete.
  279. * @return Returns DAE_OK if success, a negative value defined in daeError.h otherwise.
  280. * @note The @c daeElement objects sometimes list
  281. * objects in two places, the class member and the <i> @c _contents </i> array, when you remove something from the
  282. * do, you must remove it from both places.
  283. */
  284. daeInt remove(const T& value, size_t *idx = NULL )
  285. {
  286. size_t index;
  287. if(find(value,index) == DAE_OK)
  288. {
  289. if ( idx != NULL ) {
  290. *idx = index;
  291. }
  292. return(removeIndex( index ));
  293. }
  294. else
  295. {
  296. return(DAE_ERR_INVALID_CALL);
  297. }
  298. }
  299. /**
  300. * Finds an item from the @c daeArray.
  301. * @param value A reference to the item to find.
  302. * @param index If the function returns DAE_OK, this is set to the index where the value appears in the array.
  303. * @return Returns DAE_OK if no error or DAE_ERR_QUERY_NO_MATCH if the value was not found.
  304. */
  305. daeInt find(const T& value, size_t &index) const
  306. {
  307. size_t i;
  308. for(i=0;i<_count;i++)
  309. {
  310. if (((T*)_data)[i] == value)
  311. {
  312. index = i;
  313. return DAE_OK;
  314. }
  315. }
  316. return DAE_ERR_QUERY_NO_MATCH;
  317. }
  318. /**
  319. * Just like the previous function, but has a more reasonable interface.
  320. * @param value The value to find.
  321. * @return Returns a pointer to the value if found, null otherwise.
  322. */
  323. T* find(const T& value) const {
  324. size_t i;
  325. if (find(value, i) == DAE_OK)
  326. return get(i);
  327. return NULL;
  328. }
  329. /**
  330. * Gets the object at a specific index in the @c daeArray.
  331. * @param index Index of the object to get, asserts if the index is out of bounds.
  332. * @return Returns the object at @c index.
  333. */
  334. T& operator[](size_t index) {
  335. assert(index < _count);
  336. return ((T*)_data)[index]; }
  337. /**
  338. * Gets the object at a specific index in the @c daeArray.
  339. * @param index Index of the object to get, asserts if the index is out of bounds.
  340. * @return Returns the object at @c index.
  341. */
  342. const T& operator[](size_t index) const {
  343. assert(index < _count);
  344. return ((T*)_data)[index]; }
  345. /**
  346. * Inserts the specified number of elements at a specific location in the array.
  347. * @param index Index into the array where the elements will be inserted
  348. * @param n The number of elements to insert
  349. * @param val The value to insert
  350. */
  351. void insert(size_t index, size_t n, const T& val = T()) {
  352. if (index >= _count) {
  353. // Append to the end of the array
  354. size_t oldCount = _count;
  355. setCount(index + n);
  356. for (size_t i = oldCount; i < _count; i++)
  357. get(i) = val;
  358. }
  359. else {
  360. setCount(_count + n);
  361. for (size_t i = _count-1; i >= index+n; i--)
  362. get(i) = get(i-n);
  363. for (size_t i = index; i < index+n; i++)
  364. get(i) = val;
  365. }
  366. }
  367. /**
  368. * Inserts an object at a specific index in the daeArray, growing the array if neccessary
  369. * @param index Index into the array for where to place the object
  370. * @param value The object to append
  371. */
  372. void insertAt(size_t index, const T& value) {
  373. insert(index, 1);
  374. get(index) = value;
  375. }
  376. /**
  377. * Overloaded assignment operator.
  378. * @param other A reference to the array to copy
  379. * @return A reference to this object.
  380. */
  381. daeTArray<T> &operator=( const daeTArray<T> &other ) {
  382. if (this != &other) {
  383. clear();
  384. _elementSize = other._elementSize;
  385. _type = other._type;
  386. grow(other._count);
  387. for(size_t i=0;i<other._count;i++)
  388. append(other[i]);
  389. }
  390. return *this;
  391. }
  392. /**
  393. * Overloaded equality operator
  394. * @param other A reference to the other array.
  395. * @return true if the arrays are equal, false otherwise.
  396. */
  397. bool operator==(const daeTArray<T>& other) {
  398. if (getCount() != other.getCount())
  399. return false;
  400. for (size_t i = 0; i < getCount(); i++)
  401. if (get(i) != other.get(i))
  402. return false;
  403. return true;
  404. }
  405. //some helpers
  406. /**
  407. * Sets the array to the contain the two values specified.
  408. * @param one The first value.
  409. * @param two The second value.
  410. */
  411. void set2( const T &one, const T &two )
  412. {
  413. setCount( 2 );
  414. set( 0, one );
  415. set( 1, two );
  416. }
  417. /**
  418. * Sets the array to the contain the three values specified.
  419. * @param one The first value.
  420. * @param two The second value.
  421. * @param three The third value.
  422. */
  423. void set3( const T &one, const T &two, const T &three )
  424. {
  425. setCount( 3 );
  426. set( 0, one );
  427. set( 1, two );
  428. set( 2, three );
  429. }
  430. /**
  431. * Sets the array to the contain the four values specified.
  432. * @param one The first value.
  433. * @param two The second value.
  434. * @param three The third value.
  435. * @param four The fourth value.
  436. */
  437. void set4( const T &one, const T &two, const T &three, const T &four )
  438. {
  439. setCount( 4 );
  440. set( 0, one );
  441. set( 1, two );
  442. set( 2, three );
  443. set( 3, four );
  444. }
  445. /**
  446. * Sets the values in the array at the specified location to the contain the two
  447. * values specified. This function will grow the array if needed.
  448. * @param index The position in the array to start setting.
  449. * @param one The first value.
  450. * @param two The second value.
  451. */
  452. void set2at( size_t index, const T &one, const T &two )
  453. {
  454. set( index, one );
  455. set( index+1, two );
  456. }
  457. /**
  458. * Sets the values in the array at the specified location to the contain the three
  459. * values specified. This function will grow the array if needed.
  460. * @param index The position in the array to start setting.
  461. * @param one The first value.
  462. * @param two The second value.
  463. * @param three The third value.
  464. */
  465. void set3at( size_t index, const T &one, const T &two, const T &three )
  466. {
  467. set( index, one );
  468. set( index+1, two );
  469. set( index+2, three );
  470. }
  471. /**
  472. * Sets the values in the array at the specified location to the contain the four
  473. * values specified. This function will grow the array if needed.
  474. * @param index The position in the array to start setting.
  475. * @param one The first value.
  476. * @param two The second value.
  477. * @param three The third value.
  478. * @param four The fourth value.
  479. */
  480. void set4at( size_t index, const T &one, const T &two, const T &three, const T &four )
  481. {
  482. set( index, one );
  483. set( index+1, two );
  484. set( index+2, three );
  485. set( index+3, four );
  486. }
  487. /**
  488. * Appends two values to the array.
  489. * @param one The first value.
  490. * @param two The second value.
  491. */
  492. void append2( const T &one, const T &two )
  493. {
  494. append( one );
  495. append( two );
  496. }
  497. /**
  498. * Appends three values to the array.
  499. * @param one The first value.
  500. * @param two The second value.
  501. * @param three The third value.
  502. */
  503. void append3( const T &one, const T &two, const T &three )
  504. {
  505. append( one );
  506. append( two );
  507. append( three );
  508. }
  509. /**
  510. * Appends four values to the array.
  511. * @param one The first value.
  512. * @param two The second value.
  513. * @param three The third value.
  514. * @param four The fourth value.
  515. */
  516. void append4( const T &one, const T &two, const T &three, const T &four )
  517. {
  518. append( one );
  519. append( two );
  520. append( three );
  521. append( four );
  522. }
  523. /**
  524. * Inserts two values into the array at the specified location.
  525. * @param index The position in the array to start inserting.
  526. * @param one The first value.
  527. * @param two The second value.
  528. */
  529. void insert2at( size_t index, const T &one, const T &two )
  530. {
  531. insert(index, 2);
  532. set(index, one);
  533. set(index+1, two);
  534. }
  535. /**
  536. * Inserts three values into the array at the specified location.
  537. * @param index The position in the array to start inserting.
  538. * @param one The first value.
  539. * @param two The second value.
  540. * @param three The third value.
  541. */
  542. void insert3at( size_t index, const T &one, const T &two, const T &three )
  543. {
  544. insert(index, 3);
  545. set( index, one );
  546. set( index+1, two );
  547. set( index+2, three );
  548. }
  549. /**
  550. * Inserts four values into the array at the specified location.
  551. * @param index The position in the array to start inserting.
  552. * @param one The first value.
  553. * @param two The second value.
  554. * @param three The third value.
  555. * @param four The fourth value.
  556. */
  557. void insert4at( size_t index, const T &one, const T &two, const T &three, const T &four )
  558. {
  559. insert(index, 4);
  560. set( index, one );
  561. set( index+1, two );
  562. set( index+2, three );
  563. set( index+4, four );
  564. }
  565. /**
  566. * Gets two values from the array at the specified location.
  567. * @param index The position in the array to start getting.
  568. * @param one Variable to store the first value.
  569. * @param two Variable to store the second value.
  570. * @return Returns The number of elements retrieved.
  571. */
  572. daeInt get2at( size_t index, T &one, T &two )
  573. {
  574. daeInt retVal = 0;
  575. if ( index < _count )
  576. {
  577. one = get(index);
  578. retVal++;
  579. }
  580. if ( index+1 < _count )
  581. {
  582. two = get(index+1);
  583. retVal++;
  584. }
  585. return retVal;
  586. }
  587. /**
  588. * Gets three values from the array at the specified location.
  589. * @param index The position in the array to start getting.
  590. * @param one Variable to store the first value.
  591. * @param two Variable to store the second value.
  592. * @param three Variable to store the third value.
  593. * @return Returns The number of elements retrieved.
  594. */
  595. daeInt get3at( size_t index, T &one, T &two, T &three )
  596. {
  597. daeInt retVal = 0;
  598. if ( index < _count )
  599. {
  600. one = get(index);
  601. retVal++;
  602. }
  603. if ( index+1 < _count )
  604. {
  605. two = get(index+1);
  606. retVal++;
  607. }
  608. if ( index+2 < _count )
  609. {
  610. three = get(index+2);
  611. retVal++;
  612. }
  613. return retVal;
  614. }
  615. /**
  616. * Gets four values from the array at the specified location.
  617. * @param index The position in the array to start getting.
  618. * @param one Variable to store the first value.
  619. * @param two Variable to store the second value.
  620. * @param three Variable to store the third value.
  621. * @param four Variable to store the fourth value.
  622. * @return Returns The number of elements retrieved.
  623. */
  624. daeInt get4at( size_t index, T &one, T &two, T &three, T &four )
  625. {
  626. daeInt retVal = 0;
  627. if ( index < _count )
  628. {
  629. one = get(index);
  630. retVal++;
  631. }
  632. if ( index+1 < _count )
  633. {
  634. two = get(index+1);
  635. retVal++;
  636. }
  637. if ( index+2 < _count )
  638. {
  639. three = get(index+2);
  640. retVal++;
  641. }
  642. if ( index+3 < _count )
  643. {
  644. four = get(index+3);
  645. retVal++;
  646. }
  647. return retVal;
  648. }
  649. /**
  650. * Appends a number of elements to this array from a C native array.
  651. * @param num The number of elements to append.
  652. * @param array The C native array that contains the values to append.
  653. */
  654. void appendArray( size_t num, T *array )
  655. {
  656. if ( array == NULL )
  657. return;
  658. for ( size_t i = 0; i < num; i++ )
  659. append( array[i] );
  660. }
  661. /**
  662. * Appends a number of elements to this array from another daeTArray.
  663. * @param array The daeTArray that contains the values to append.
  664. */
  665. void appendArray( const daeTArray<T> &array ){
  666. size_t num = array.getCount();
  667. for ( size_t i = 0; i < num; i++ )
  668. append( array[i] );
  669. }
  670. };
  671. #endif //__DAE_ARRAY_H__