StaticStackArray.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_STATICSTACKARRAY_CPP
  13. #define SE_INCL_STATICSTACKARRAY_CPP
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Templates/StaticStackArray.h>
  18. #include <Engine/Templates/StaticArray.cpp>
  19. /*
  20. * Default constructor.
  21. */
  22. template<class Type>
  23. inline CStaticStackArray<Type>::CStaticStackArray(void) : CStaticArray<Type>() {
  24. sa_UsedCount=0;
  25. sa_ctAllocationStep = 256;
  26. }
  27. /*
  28. * Destructor.
  29. */
  30. template<class Type>
  31. inline CStaticStackArray<Type>::~CStaticStackArray(void) {
  32. };
  33. /* Destroy all objects, and reset the array to initial (empty) state. */
  34. template<class Type>
  35. inline void CStaticStackArray<Type>::Clear(void) {
  36. if (CStaticArray<Type>::Count()!=0) Delete();
  37. }
  38. /*
  39. * Set how many elements to allocate when stack overflows.
  40. */
  41. template<class Type>
  42. inline void CStaticStackArray<Type>::SetAllocationStep(INDEX ctStep)
  43. {
  44. ASSERT(ctStep>0);
  45. sa_ctAllocationStep = ctStep;
  46. };
  47. /*
  48. * Create a given number of objects.
  49. */
  50. template<class Type>
  51. inline void CStaticStackArray<Type>::New(INDEX iCount) {
  52. CStaticArray<Type>::New(iCount);
  53. sa_UsedCount = 0;
  54. };
  55. /*
  56. * Destroy all objects.
  57. */
  58. template<class Type>
  59. inline void CStaticStackArray<Type>::Delete(void) {
  60. CStaticArray<Type>::Delete();
  61. sa_UsedCount = 0;
  62. }
  63. /*
  64. * Add new object(s) on top of stack.
  65. */
  66. template<class Type>
  67. inline Type &CStaticStackArray<Type>::Push(void) {
  68. sa_UsedCount++;
  69. if (sa_UsedCount>CStaticArray<Type>::Count()) {
  70. Expand(CStaticArray<Type>::Count()+sa_ctAllocationStep);
  71. }
  72. ASSERT(sa_UsedCount <= CStaticArray<Type>::Count());
  73. return CStaticArray<Type>::operator[](sa_UsedCount-1);
  74. }
  75. template<class Type>
  76. inline Type *CStaticStackArray<Type>::Push(INDEX ct) {
  77. sa_UsedCount+=ct;
  78. while(sa_UsedCount>CStaticArray<Type>::Count()) {
  79. Expand(CStaticArray<Type>::Count()+sa_ctAllocationStep);
  80. }
  81. ASSERT(sa_UsedCount <= CStaticArray<Type>::Count());
  82. return &CStaticArray<Type>::operator[](sa_UsedCount-ct);
  83. }
  84. /* Remove one object from top of stack and return it. */
  85. template<class Type>
  86. inline Type &CStaticStackArray<Type>::Pop(void)
  87. {
  88. ASSERT(sa_UsedCount>0);
  89. sa_UsedCount--;
  90. return CStaticArray<Type>::operator[](sa_UsedCount);
  91. }
  92. /*
  93. * Remove objects higher than the given index from stack, but keep stack space.
  94. */
  95. template<class Type>
  96. inline void CStaticStackArray<Type>::PopUntil(INDEX iNewTop)
  97. {
  98. ASSERT(iNewTop < sa_UsedCount);
  99. sa_UsedCount = iNewTop+1;
  100. }
  101. /*
  102. * Remove all objects from stack, but keep stack space.
  103. */
  104. template<class Type>
  105. inline void CStaticStackArray<Type>::PopAll(void) {
  106. sa_UsedCount = 0;
  107. }
  108. /*
  109. * Random access operator.
  110. */
  111. template<class Type>
  112. inline Type &CStaticStackArray<Type>::operator[](INDEX i) {
  113. ASSERT(this!=NULL);
  114. ASSERT(i<sa_UsedCount); // check bounds
  115. return CStaticArray<Type>::operator[](i);
  116. }
  117. template<class Type>
  118. inline const Type &CStaticStackArray<Type>::operator[](INDEX i) const {
  119. ASSERT(this!=NULL);
  120. ASSERT(i<sa_UsedCount); // check bounds
  121. return CStaticArray<Type>::operator[](i);
  122. }
  123. /*
  124. * Get number of elements in array.
  125. */
  126. template<class Type>
  127. INDEX CStaticStackArray<Type>::Count(void) const {
  128. ASSERT(this!=NULL);
  129. return sa_UsedCount;
  130. }
  131. /*
  132. * Get index of a member from it's pointer
  133. */
  134. template<class Type>
  135. INDEX CStaticStackArray<Type>::Index(Type *ptMember) {
  136. ASSERT(this!=NULL);
  137. INDEX i = CStaticArray<Type>::Index(ptMember);
  138. ASSERTMSG(i<sa_UsedCount, "CStaticStackArray<>::Index(): Not a member of this array!");
  139. return i;
  140. }
  141. /*
  142. * Assignment operator.
  143. */
  144. template<class Type>
  145. CStaticStackArray<Type> &CStaticStackArray<Type>::operator=(const CStaticStackArray<Type> &arOriginal)
  146. {
  147. ASSERT(this!=NULL);
  148. ASSERT(&arOriginal!=NULL);
  149. ASSERT(this!=&arOriginal);
  150. // copy stack arrays
  151. CStaticArray<Type>::operator=(arOriginal);
  152. // copy used count
  153. sa_UsedCount = arOriginal.sa_UsedCount;
  154. return *this;
  155. }
  156. /* Move all elements of another array into this one. */
  157. template<class Type>
  158. void CStaticStackArray<Type>::MoveArray(CStaticStackArray<Type> &arOther)
  159. {
  160. ASSERT(this!=NULL);
  161. ASSERT(&arOther!=NULL);
  162. ASSERT(this!=&arOther);
  163. // clear previous contents
  164. Clear();
  165. // if the other array has no elements
  166. if (arOther.Count()==0) {
  167. // no assignment
  168. return;
  169. }
  170. // move data from the other array into this one and clear the other one
  171. CStaticArray<Type>::MoveArray(arOther);
  172. sa_UsedCount = arOther.sa_UsedCount ;
  173. sa_ctAllocationStep = arOther.sa_ctAllocationStep ;
  174. arOther.sa_UsedCount = 0;
  175. }
  176. #endif /* include-once check. */