home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtvec.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  3.1 KB  |  134 lines

  1. // CmTVec.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Dynamic array template implementation.
  7. // -----------------------------------------------------------------
  8.  
  9.  
  10. // "CmTVector" is the default vector constructor.
  11. //
  12. template <class T> CmTVector<T>::CmTVector(unsigned sz)
  13. {
  14.   _array = (sz > 0) ? new T[sz] : NULL;
  15.   _size  = (_array) ? sz : 0;
  16. }
  17.  
  18.  
  19. // "CmTVector" is the vector copy constructor.
  20. //
  21. template <class T> CmTVector<T>::CmTVector(const CmTVector<T>& aVec)
  22. {
  23.   _size  = 0;
  24.   _array = NULL;
  25.   copy(aVec);
  26. }
  27.  
  28.  
  29. // "~CmTVector" is the vector destructor.
  30. //
  31. template <class T> CmTVector<T>::~CmTVector()
  32. {
  33.   clear();
  34. }
  35.  
  36.  
  37. // "=" assignment operator copies the contents from an input array into
  38. // this array.
  39. //
  40. template <class T>
  41. CmTVector<T>& CmTVector<T>::operator=(const CmTVector<T>& aVec)
  42. {
  43.   if (&aVec != this) copy(aVec);
  44.   return *this;
  45. }
  46.  
  47.  
  48. // "size" returns the vector size.
  49. //
  50. template <class T> unsigned CmTVector<T>::size() const
  51. {
  52.   return _size;
  53. }
  54.  
  55.  
  56. // "array" returns the array pointer in the vector.
  57. //
  58. template <class T> T* CmTVector<T>::array() const
  59. {
  60.   return _array;
  61. }
  62.  
  63.  
  64. // "[]" operator returns the entry at the specified index in the array.
  65. //
  66. template <class T> T& CmTVector<T>::operator[](int idx)
  67. {
  68.   if (idx < 0) idx = 0;
  69.   if (idx >= _size)
  70.     if (!resize(idx + 1)) idx = _size;
  71.   return _array[idx];
  72. }
  73.  
  74.  
  75. // "[]" operator returns the entry at the specified index in the array.
  76. //
  77. template <class T> const T& CmTVector<T>::operator[](int idx) const
  78. {
  79.   if (idx < 0) idx = 0;
  80.   if (idx >= _size)
  81.     if (!((CmTVector<T>*) this)->resize(idx + 1)) idx = _size;
  82.   return _array[idx];
  83. }
  84.  
  85.  
  86. // "clear" clears out this array resetting the size to zero.
  87. //
  88. template <class T> void CmTVector<T>::clear()
  89. {
  90.   delete[] _array;
  91.   _size  = 0;
  92.   _array = NULL;
  93. }
  94.  
  95.  
  96. // "resize" resizes the array to match a specified size.
  97. //
  98. template <class T> Bool CmTVector<T>::resize(unsigned NewSize)
  99. {
  100.   if (NewSize == 0)                    // Zero size specified.
  101.     clear();
  102.   else                                 // Specified size is greater than zero.
  103.   {                                    // Create new list and copy old one
  104.     T *newentries = new T[NewSize];    // into it.  Delete the old.
  105.     if (!newentries) return FALSE;
  106.     if (_size > 0)
  107.     {
  108.       unsigned limit = (_size > NewSize) ? NewSize : _size;
  109.       for (int ii = 0; ii < limit; ii++)
  110.         newentries[ii] = _array[ii];
  111.       delete[] _array;
  112.     }
  113.     _size  = NewSize;
  114.     _array = newentries;
  115.   }
  116.   return TRUE;
  117. }
  118.  
  119.  
  120. // "copy" copies the contents of an input vector into this vector.
  121. //
  122. template <class T> void CmTVector<T>::copy(const CmTVector<T>& aVec)
  123. {
  124.   if (_array) delete[] _array;
  125.   _array = NULL;
  126.   _size  = aVec._size;
  127.   if (aVec._array)
  128.   {
  129.     _array = new T[_size];
  130.     for (int ii = 0; ii < _size; ii++)
  131.       _array[ii] = aVec._array[ii];
  132.   }
  133. }
  134.