Source code of Windows XP (NT5)
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.

100 lines
1.8 KiB

  1. /****************************************************************************\
  2. *
  3. * ARRAY.H ---Class declaration for the array structure
  4. *
  5. \****************************************************************************/
  6. #ifndef _ARRAY_H
  7. #define _ARRAY_H
  8. /*Interface-------------------------------------------------------------------*/
  9. template <class T>
  10. class array {
  11. private:
  12. int nLen, nMax;
  13. T *pData;
  14. void Destruct();
  15. public:
  16. array();
  17. ~array();
  18. BOOL Append(T v);
  19. int Length() const;
  20. void ClearAll();
  21. void DeleteAll();
  22. T& operator[](int index);
  23. };
  24. /*definitions of everything*/
  25. #ifndef ARRAY_CXX
  26. #define ARRAY_CXX
  27. /*Implementation------------------------------------------------------------*/
  28. template <class T>
  29. array<T>::array(){
  30. nLen = nMax = 0;
  31. pData = NULL;
  32. }
  33. template <class T>
  34. inline array<T>::~array() {
  35. if (pData) ::MemFree(pData);
  36. pData = NULL;
  37. nMax = nLen = 0;
  38. }
  39. template <class T>
  40. inline int array<T>::Length() const{
  41. return nLen;
  42. }
  43. template <class T>
  44. inline T& array<T>::operator[](int index){
  45. ASSERT(index<Length());
  46. ASSERT(index>=0);
  47. ASSERT(pData);
  48. return pData[index];
  49. }
  50. template <class T>
  51. BOOL array<T>::Append(T v) {
  52. if (nLen == nMax){
  53. nMax = nMax + 8; /* grow by bigger chunks */
  54. T* pNew = (T*)::MemReAlloc(pData, sizeof(T)*nMax);
  55. if (pNew == NULL)
  56. return FALSE;
  57. pData = pNew;
  58. }
  59. ASSERT(pData);
  60. ASSERT(nMax);
  61. pData[nLen++] = v;
  62. return TRUE;
  63. }
  64. template <class T>
  65. void array<T>::Destruct(){
  66. while (nLen){
  67. delete pData[--nLen];
  68. }
  69. }
  70. template <class T>
  71. inline void array<T>::ClearAll() {
  72. nLen = 0;
  73. }
  74. template <class T>
  75. inline void array<T>::DeleteAll() {
  76. Destruct();
  77. }
  78. #endif
  79. /* ARRAY_CXX */
  80. #endif
  81. /* _ARRAY_H */