Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.3 KiB

  1. // array.h
  2. #pragma once
  3. /////////////////////////////////////////////////////////////////////////////
  4. // CArray template
  5. #define DEF_SIZE 10
  6. #define DEF_GROW_BY 10
  7. template<class TYPE, class REF = TYPE> class CArray
  8. {
  9. public:
  10. CArray(int iSize = DEF_SIZE, int iGrowBy = DEF_GROW_BY) :
  11. m_nSize(0),
  12. m_nCount(0),
  13. m_nGrowBy(iGrowBy),
  14. m_pVals(NULL)
  15. {
  16. if (m_nGrowBy <= 0)
  17. m_nGrowBy = DEF_GROW_BY;
  18. if (iSize)
  19. Init(iSize);
  20. }
  21. CArray(const CArray& other)
  22. {
  23. *this = other;
  24. }
  25. const CArray& operator= (const CArray<TYPE, REF>& other)
  26. {
  27. Init(other.m_nCount);
  28. m_nCount = other.m_nCount;
  29. for (int i = 0; i < m_nCount; i++)
  30. m_pVals[i] = other.m_pVals[i];
  31. return *this;
  32. }
  33. ~CArray()
  34. {
  35. if (m_pVals)
  36. delete [] m_pVals;
  37. }
  38. BOOL AddVal(REF val)
  39. {
  40. if (m_nCount >= m_nSize)
  41. {
  42. TYPE *pTemp = new TYPE[m_nSize + m_nGrowBy];
  43. if (!pTemp)
  44. return FALSE;
  45. m_nSize += m_nGrowBy;
  46. for (int i = 0; i < m_nCount; i++)
  47. pTemp[i] = m_pVals[i];
  48. delete [] m_pVals;
  49. m_pVals = pTemp;
  50. }
  51. m_pVals[m_nCount++] = val;
  52. return TRUE;
  53. }
  54. BOOL Init(int iSize)
  55. {
  56. //if (iSize < DEF_SIZE)
  57. // iSize = DEF_SIZE;
  58. if (iSize != m_nSize)
  59. {
  60. if (m_pVals)
  61. delete [] m_pVals;
  62. m_pVals = new TYPE[iSize];
  63. }
  64. m_nSize = iSize;
  65. m_nCount = 0;
  66. return m_pVals != NULL;
  67. }
  68. TYPE operator[] (int iIndex) const
  69. {
  70. return m_pVals[iIndex];
  71. }
  72. TYPE& operator[] (int iIndex)
  73. {
  74. return m_pVals[iIndex];
  75. }
  76. int GetCount() { return m_nCount; }
  77. void SetCount(int iCount)
  78. {
  79. //_ASSERT(iCount < m_nSize);
  80. m_nCount = iCount;
  81. }
  82. int GetSize() { return m_nSize; }
  83. void SetGrowBy(int iVal) { m_nGrowBy = iVal; }
  84. TYPE *GetData() { return m_pVals; }
  85. protected:
  86. TYPE *m_pVals;
  87. int m_nCount,
  88. m_nSize,
  89. m_nGrowBy;
  90. };