Array3d.h
Upload User: hcfgz168
Upload Date: 2011-09-11
Package Size: 116k
Code Size: 5k
Category:

OpenGL program

Development Platform:

WINDOWS

  1. //********************************************
  2. // Array3d.h
  3. // class CArray3d :
  4. //********************************************
  5. // Careful : this array stores pointers
  6. // on elements, but does not free memory
  7. // in destructors, you must call array.Free()
  8. // to do this.
  9. //********************************************
  10. // pierre.alliez@cnet.francetelecom.fr
  11. // Created : 30/01/98
  12. // Modified : 18/02/98
  13. //********************************************
  14. #ifndef _ARRAY_3D_
  15. #define _ARRAY_3D_
  16. template<class T>
  17. class CArray3d
  18. {
  19. // Implementation
  20. private:
  21. T** m_pData;     // the actual array of data
  22. int m_nSize;     // # of elements (upperBound - 1)
  23. int m_nMaxSize;  // max allocated
  24. int m_nGrowBy;   // grow amount
  25. public:
  26. // Construction
  27. CArray3d()
  28. {
  29. m_pData = NULL;
  30. m_nSize = 0;
  31. m_nMaxSize = 0;
  32. m_nGrowBy = 0;
  33. }
  34. // Destruction
  35. ~CArray3d()
  36. {
  37. delete [] (BYTE*)m_pData;
  38. }
  39. // Attributes
  40. int GetSize() { return m_nSize; }
  41. int GetUpperBound() { return m_nSize-1; }
  42. // Operations
  43. // Really clean up
  44. void Free()
  45. {
  46. for(int i=0;i<m_nSize;i++)
  47. if(m_pData[i] != NULL)
  48. {
  49. delete m_pData[i];
  50.   m_pData[i] = NULL;
  51. }
  52. RemoveAll();
  53. }
  54. // Clean up pointers array
  55. void FreeExtra();
  56. // Clean up pointers array
  57. void RemoveAll() { SetSize(0); }
  58. // Accessing elements
  59. T* GetAt(int nIndex)
  60. ASSERT(nIndex >= 0 && nIndex < m_nSize);
  61. return m_pData[nIndex]; 
  62. }
  63. // SetAt
  64. void SetAt(int nIndex, T* newElement)
  65. ASSERT(nIndex >= 0 && nIndex < m_nSize);
  66. m_pData[nIndex] = newElement; 
  67. }
  68. // Has
  69. int Has(T* pElement)
  70. {
  71. int size = GetSize();
  72. for(int i=0;i<size;i++)
  73. if(GetAt(i) == pElement)
  74. return 1;
  75. return 0;
  76. }
  77. // Direct Access to the element data (may return NULL)
  78. T** GetData() { return m_pData; }
  79. // Add
  80. int Add(T* newElement)
  81. int nIndex = m_nSize;
  82. SetAtGrow(nIndex, newElement);
  83. return nIndex; 
  84. }
  85. // overloaded operator helpers
  86. T* operator[](int nIndex) { return GetAt(nIndex); }
  87. // Potentially growing the array
  88. void SetAtGrow(int nIndex, T* newElement)
  89. {
  90. ASSERT(nIndex >= 0);
  91. if (nIndex >= m_nSize)
  92. SetSize(nIndex+1);
  93. m_pData[nIndex] = newElement;
  94. }
  95. // Operations that move elements around
  96. void InsertAt(int nIndex, T* newElement, int nCount = 1)
  97. {
  98. ASSERT(nIndex >= 0);    // will expand to meet need
  99. ASSERT(nCount > 0);     // zero or negative size not allowed
  100. if (nIndex >= m_nSize)
  101. {
  102. // adding after the end of the array
  103. SetSize(nIndex + nCount);  // grow so nIndex is valid
  104. }
  105. else
  106. {
  107. // inserting in the middle of the array
  108. int nOldSize = m_nSize;
  109. SetSize(m_nSize + nCount);  // grow it to new size
  110. // shift old data up to fill gap
  111. memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
  112. (nOldSize-nIndex) * sizeof(T*));
  113. // re-init slots we copied from
  114. memset(&m_pData[nIndex], 0, nCount * sizeof(T*));
  115. }
  116. // insert new value in the gap
  117. ASSERT(nIndex + nCount <= m_nSize);
  118. while (nCount--)
  119. m_pData[nIndex++] = newElement;
  120. }
  121. void RemoveAt(int nIndex, int nCount = 1)
  122. {
  123. ASSERT(nIndex >= 0);
  124. ASSERT(nCount >= 0);
  125. ASSERT(nIndex + nCount <= m_nSize);
  126. // just remove a range
  127. int nMoveCount = m_nSize - (nIndex + nCount);
  128. if (nMoveCount)
  129. memcpy(&m_pData[nIndex], &m_pData[nIndex + nCount],
  130. nMoveCount * sizeof(T*));
  131. m_nSize -= nCount;
  132. }
  133. // IndexFrom
  134. int IndexFrom(T* pElement)
  135. {
  136. for(int i=0;i<m_nSize;i++)
  137. if(m_pData[i] == pElement)
  138. return i;
  139. TRACE("IndexFrom : no elementn");
  140. return -1;
  141. }
  142. // SetSize
  143. void SetSize(int nNewSize, int nGrowBy = -1)
  144. {
  145. ASSERT(nNewSize >= 0);
  146. if(nGrowBy != -1)
  147. m_nGrowBy = nGrowBy;  // set new size
  148. // shrink to nothing
  149. if(nNewSize == 0)
  150. {
  151. delete[] (BYTE*)m_pData;
  152. m_pData = NULL;
  153. m_nSize = 0;
  154. m_nMaxSize = 0;
  155. }
  156. else 
  157. // create one with exact size
  158. if(m_pData == NULL)
  159. {
  160. m_pData = (T**) new BYTE[nNewSize * sizeof(T*)];
  161. memset(m_pData, 0, nNewSize * sizeof(T*));  // zero fill
  162. m_nSize = nNewSize;
  163. m_nMaxSize = nNewSize;
  164. }
  165. else 
  166. if(nNewSize <= m_nMaxSize)
  167. {
  168. // it fits
  169. if (nNewSize > m_nSize)
  170. {
  171. // initialize the new elements
  172. memset(&m_pData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(T*));
  173. }
  174. m_nSize = nNewSize;
  175. }
  176. else
  177. {
  178. // otherwise, grow array
  179. int nGrowBy = m_nGrowBy;
  180. if (nGrowBy == 0)
  181. {
  182. // heuristically determine growth when nGrowBy == 0
  183. //  (this avoids heap fragmentation in many situations)
  184. nGrowBy = min(1024, max(4, m_nSize / 8));
  185. }
  186. int nNewMax;
  187. if (nNewSize < m_nMaxSize + nGrowBy)
  188. nNewMax = m_nMaxSize + nGrowBy;  // granularity
  189. else
  190. nNewMax = nNewSize;  // no slush
  191. ASSERT(nNewMax >= m_nMaxSize);  // no wrap around
  192. T** pNewData = (T**) new BYTE[nNewMax * sizeof(T*)];
  193. // copy new data from old
  194. memcpy(pNewData, m_pData, m_nSize * sizeof(T*));
  195. // construct remaining elements
  196. ASSERT(nNewSize > m_nSize);
  197. memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(T*));
  198. // get rid of old stuff (note: no destructors called)
  199. delete[] (BYTE*)m_pData;
  200. m_pData = pNewData;
  201. m_nSize = nNewSize;
  202. m_nMaxSize = nNewMax;
  203. }
  204. }
  205. };
  206. #endif // _ARRAY_3D_