home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Predictors / Array.h next >
Encoding:
C/C++ Source or Header  |  2001-09-23  |  5.5 KB  |  188 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    Array.h
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. #ifndef __ARRAY_H_
  10. #define __ARRAY_H_
  11.  
  12. //----------------------------------------------------------------------------------------------
  13. // Include files
  14. //----------------------------------------------------------------------------------------------
  15.  
  16. #include <assert.h>
  17.  
  18. //----------------------------------------------------------------------------------------------
  19. // CArray: A simple, dynamically allocated array template; similar to the one used in MFC.
  20. //----------------------------------------------------------------------------------------------
  21.  
  22. template <class TData>
  23. class CArray
  24. {
  25. public:                    
  26.                         CArray();
  27.                         ~CArray();
  28.  
  29.     inline TData&        operator[](long index) { return Item(index); }
  30.  
  31. public:
  32.  
  33.     long                Add(TData ti);
  34.     TData                RemoveAt(long index);
  35.     void                Remove(TData ti);
  36.     void                RemoveAll();
  37.     TData&                Item(long index);
  38.     void                SetSize(long size);
  39.     long                GetSize() { return m_nSize; }
  40.     CArray <TData> *    Clone();
  41.  
  42. protected:
  43.  
  44.     TData *                m_pItems;
  45.     long                m_nSize;
  46. };
  47.  
  48.  
  49. //----------------------------------------------------------------------------------------------
  50. // CArray: Constructor
  51. //----------------------------------------------------------------------------------------------
  52.  
  53. template <class TData> 
  54. CArray<TData>::CArray()
  55. {
  56.     m_pItems = NULL;
  57.     m_nSize = 0;
  58. }
  59.  
  60. //----------------------------------------------------------------------------------------------
  61. // ~CArray: Destructor
  62. //----------------------------------------------------------------------------------------------
  63.  
  64. template <class TData>
  65. CArray<TData>::~CArray()
  66. {
  67.     if (m_nSize>0) {
  68.         RemoveAll();
  69.     }
  70. }
  71.  
  72. //----------------------------------------------------------------------------------------------
  73. // Add(): Adds a new item to the array; expands the array
  74. //----------------------------------------------------------------------------------------------
  75.  
  76. template <class TData>
  77. long CArray<TData>::Add(TData ti)
  78. {
  79.     TData * pItems = new TData[m_nSize+1];
  80.     memcpy(pItems,m_pItems,sizeof(TData)*m_nSize);
  81.     pItems[m_nSize] = ti;
  82.     delete [] m_pItems;
  83.     m_pItems = pItems;
  84.     return m_nSize++;
  85. }
  86.  
  87. //----------------------------------------------------------------------------------------------
  88. // RemoveAt(): Removes item at specified index from the array; array is shrinked
  89. //----------------------------------------------------------------------------------------------
  90.  
  91. template <class TData>
  92. TData CArray<TData>::RemoveAt(long index)
  93. {
  94.     assert( index >= 0 && index < m_nSize );
  95.  
  96.     TData val = Item(index);
  97.     TData * pItems = new TData[m_nSize-1];
  98.     memcpy(pItems, m_pItems, sizeof(TData)*index);
  99.     memcpy(pItems+index,m_pItems+index+1, sizeof(TData)*(m_nSize-index-1));
  100.     delete [] m_pItems;
  101.     m_pItems = pItems;
  102.     m_nSize--;
  103.     return val;
  104. }
  105.  
  106. //----------------------------------------------------------------------------------------------
  107. // Remove(): Removes the specified item from the array; array is shrinked
  108. //----------------------------------------------------------------------------------------------
  109.  
  110. template <class TData>
  111. void CArray<TData>::Remove(TData ti)
  112. {
  113.     long i = 0;    
  114.     while (m_pItems[i] != ti && i<m_nSize) {
  115.         i++;
  116.     }
  117.     if (i != m_nSize) {
  118.         RemoveAt(i);
  119.     }
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------------
  123. // RemoveAll(): Removes all items from the array; array is emptied
  124. //----------------------------------------------------------------------------------------------
  125.  
  126. template <class TData>
  127. void CArray<TData>::RemoveAll()
  128. {
  129.     if (m_nSize>0)
  130.     {
  131.         delete [] m_pItems;
  132.         m_pItems = NULL;
  133.         m_nSize = 0;
  134.     }
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------------
  138. // Item(): Item at specified index is returned
  139. //----------------------------------------------------------------------------------------------
  140.  
  141. template <class TData>
  142. TData& CArray<TData>::Item(long index)
  143. {
  144.     assert( index >= 0 && index <= m_nSize );
  145.     return m_pItems[index];
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------------
  149. // SetSize(): Sets the size of the array; remains array contents (if they still fit)
  150. //----------------------------------------------------------------------------------------------
  151.  
  152. template <class TData>
  153. void CArray<TData>::SetSize(long size)
  154. {
  155.     TData * pItems;
  156.     pItems = new TData[size];
  157.  
  158.     if (size > m_nSize)
  159.     {
  160.         memcpy(pItems,m_pItems,sizeof(TData)*m_nSize);
  161.     }
  162.     else 
  163.     {
  164.         memcpy(pItems,m_pItems,sizeof(TData)*size);
  165.     }
  166.  
  167.     delete [] m_pItems;
  168.     m_pItems = pItems;
  169.     m_nSize = size;
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------------
  173. // Clone(): Duplicates the array
  174. //----------------------------------------------------------------------------------------------
  175.  
  176. template <class TData>
  177. CArray<TData>* CArray<TData>::Clone() 
  178. {
  179.     CArray<TData>* copy = new CArray<TData>;
  180.     copy.m_pItems = new TData[m_nSize];
  181.     memcpy(copy.m_pItems, m_pItems, sizeof(TData)*m_nSize);
  182.     copy.m_nSize = m_nSize;
  183.     return copy;
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------------
  187. #endif // __ARRAY_H_
  188.