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.

123 lines
2.0 KiB

  1. //#if !defined(__FARRAY_H__)
  2. //#define __FARRAY_H__
  3. //$DECLARE_TEMPLATE
  4. ////////////////////////////////////////////////////////////////////////////
  5. const int kNotFound = -1;
  6. template <class T> class CIVector {
  7. public:
  8. CIVector(int iSize);
  9. ~CIVector();
  10. int bInit();
  11. // The real array functions.
  12. //
  13. int iCount(void);
  14. int bAdd(const T * t);
  15. T * poDetach(int idx);
  16. int bDelete(int idx);
  17. int bDelete(T * t);
  18. void vDeleteAll();
  19. int iFind(const T * t);
  20. private:
  21. T ** m_pData;
  22. int m_iSize;
  23. int m_iCount;
  24. };
  25. //$IMPLEMENT_TEMPLATE_INLINES
  26. //
  27. //$IMPLEMENT_TEMPLATE
  28. ////////////////////////////////////////////////////////////////////////////
  29. template <class T> CIVector<T>::CIVector(int iSize)
  30. : m_pData(0),
  31. m_iSize(iSize),
  32. m_iCount(0)
  33. {}
  34. template <class T> CIVector<T>::~CIVector()
  35. {
  36. vDeleteAll();
  37. delete [] m_pData;
  38. }
  39. template <class T> int CIVector<T>::bInit()
  40. {
  41. int iRet = 0;
  42. if(m_iSize && !m_iCount) {
  43. m_pData = new T * [m_iSize];
  44. if( m_pData )
  45. iRet = 1;
  46. }
  47. return iRet;
  48. }
  49. template <class T> int CIVector<T>::iCount(void)
  50. {
  51. return m_iCount;
  52. }
  53. template <class T> int CIVector<T>::bAdd(const T * t)
  54. {
  55. int iRet = 0;
  56. if ( m_iCount < m_iSize ) {
  57. m_pData[m_iCount++] = t;
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. template <class T> T * CIVector::poDetach(int idx)
  63. {
  64. if( idx >=0 && idx < m_iCount ) {
  65. return m_pData[idx];
  66. }
  67. return (T *)0;
  68. }
  69. template <class T> int CIVector::bDelete(int idx)
  70. {
  71. if (idx >= 0 && idx < m_iCount ) {
  72. delete *m_pData[idx];
  73. // Move the last one down
  74. m_pData[idx] = m_pData[--m_iCount];
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. template <class T> int CIVector::bDelete(T * t)
  80. {
  81. return bDelete( iFind(t) );
  82. }
  83. template <class T> void CIVector::vDeleteAll()
  84. {
  85. while(m_iCount)
  86. bDelete(0);
  87. }
  88. template <class T> int CIVector::iFind(T * t)
  89. {
  90. for( int i = 0; i < m_iCount; i++) {
  91. if( m_pData[i] == t )
  92. return i;
  93. }
  94. return kNotFound;
  95. }
  96. // #endif // __FARRAY_H__