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.

209 lines
3.3 KiB

  1. /**********************************************************************
  2. * VectTmpl.h
  3. *
  4. * Template for a vector class.
  5. *
  6. **********************************************************************/
  7. #if !defined(__VECTTMPL_H__)
  8. #define __VECTTMPL_H__
  9. #ifndef __DBUTL_H__
  10. #include "dbutl.h"
  11. #endif
  12. const int kNotFound = -1;
  13. //
  14. // The CIVector class is a vector of pointers to objects (The I is for
  15. // Indirect).
  16. //
  17. template <class T>
  18. class CIVector {
  19. public:
  20. CIVector( int iSize );
  21. ~CIVector( );
  22. int bInit( );
  23. //
  24. // The real array functions.
  25. //
  26. int iCount( void );
  27. int bAdd( T * t );
  28. T * poObjectAt( int idx );
  29. T * poDetach( int idx );
  30. int bDelete( int idx );
  31. int bDelete( T * t );
  32. void vDeleteAll( );
  33. int iFind( T * t );
  34. private:
  35. T ** m_pData;
  36. int m_iSize;
  37. int m_iCount;
  38. };
  39. ////////////////////////////////////////////////////////////////////////////
  40. template <class T>
  41. CIVector<T>::CIVector(int iSize)
  42. : m_pData(0),
  43. m_iSize(iSize),
  44. m_iCount(0)
  45. {}
  46. template <class T> CIVector<T>::~CIVector()
  47. {
  48. ASSERT( this );
  49. vDeleteAll( );
  50. delete [] m_pData;
  51. }
  52. template <class T> int CIVector<T>::bInit()
  53. {
  54. ASSERT( this );
  55. int iRet = 0;
  56. if( !m_pData && (m_iSize > 0) )
  57. {
  58. m_pData = new T * [ m_iSize ];
  59. if( m_pData )
  60. return 1;
  61. }
  62. //
  63. // Error. Clean up.
  64. //
  65. delete this;
  66. return 0;
  67. }
  68. template <class T> int CIVector<T>::iCount(void)
  69. {
  70. ASSERT( this );
  71. return m_iCount;
  72. }
  73. template <class T> int CIVector<T>::bAdd(T * t)
  74. {
  75. ASSERT( this );
  76. #ifdef _DEBUG
  77. if( !m_pData )
  78. {
  79. OutputDebugString( TEXT( "CIVector::bAdd() m_pData not allocated" ) );
  80. DebugBreak();
  81. }
  82. if( ! t )
  83. DebugBreak();
  84. #endif
  85. if ( m_iCount < m_iSize )
  86. {
  87. m_pData[ m_iCount++ ] = t;
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. template <class T> T * CIVector<T>::poObjectAt(int idx)
  93. {
  94. ASSERT( this );
  95. if( idx >= 0 && idx < m_iCount )
  96. {
  97. ASSERT( m_pData[ idx ] );
  98. return m_pData[ idx ];
  99. }
  100. return 0;
  101. }
  102. template <class T> T * CIVector<T>::poDetach(int idx)
  103. {
  104. ASSERT( this );
  105. if( idx >= 0 && idx < m_iCount )
  106. {
  107. ASSERT( m_pData );
  108. T * pID = m_pData[ idx ];
  109. //
  110. // Move the last one down
  111. //
  112. m_pData[ idx ] = m_pData[ --m_iCount ];
  113. #ifdef _DEBUG
  114. //
  115. // When debugging, put a 0 in the slot.
  116. //
  117. m_pData[ m_iCount ] = (T *) 0;
  118. #endif
  119. ASSERT ( pID );
  120. return pID;
  121. }
  122. return (T *)0;
  123. }
  124. template <class T> int CIVector<T>::bDelete( int idx )
  125. {
  126. ASSERT( this );
  127. T * pID = poDetach( idx );
  128. if ( pID )
  129. {
  130. delete pID;
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. template <class T> int CIVector<T>::bDelete( T * t )
  136. {
  137. ASSERT( this );
  138. return bDelete( iFind( t ) );
  139. }
  140. template <class T> void CIVector<T>::vDeleteAll()
  141. {
  142. ASSERT( this );
  143. while( m_iCount )
  144. bDelete( 0 );
  145. }
  146. template <class T> int CIVector<T>::iFind( T * t )
  147. {
  148. ASSERT( this );
  149. for( int i = 0; i < m_iCount; i++ )
  150. {
  151. if( m_pData[ i ] == t )
  152. return i;
  153. }
  154. return kNotFound;
  155. }
  156. #endif // __VECTTMPL_H__
  157.