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.

202 lines
3.4 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. return 0;
  63. }
  64. template <class T> int CIVector<T>::iCount(void)
  65. {
  66. ASSERT( this );
  67. return m_iCount;
  68. }
  69. template <class T> int CIVector<T>::bAdd(T * t)
  70. {
  71. ASSERT( this );
  72. #ifdef _DEBUG
  73. if( !m_pData )
  74. {
  75. OutputDebugString( TEXT( "CIVector::bAdd() m_pData not allocated" ) );
  76. DebugBreak();
  77. }
  78. if( ! t )
  79. DebugBreak();
  80. #endif
  81. if ( m_iCount < m_iSize )
  82. {
  83. m_pData[ m_iCount++ ] = t;
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. template <class T> T * CIVector<T>::poObjectAt(int idx)
  89. {
  90. ASSERT( this );
  91. if( idx >= 0 && idx < m_iCount )
  92. {
  93. ASSERT( m_pData[ idx ] );
  94. return m_pData[ idx ];
  95. }
  96. return 0;
  97. }
  98. template <class T> T * CIVector<T>::poDetach(int idx)
  99. {
  100. ASSERT( this );
  101. if( idx >= 0 && idx < m_iCount )
  102. {
  103. ASSERT( m_pData );
  104. T * pID = m_pData[ idx ];
  105. //
  106. // Move the last one down
  107. //
  108. m_pData[ idx ] = m_pData[ --m_iCount ];
  109. #ifdef _DEBUG
  110. //
  111. // When debugging, put a 0 in the slot.
  112. //
  113. m_pData[ m_iCount ] = (T *) 0;
  114. #endif
  115. ASSERT ( pID );
  116. return pID;
  117. }
  118. return (T *)0;
  119. }
  120. template <class T> int CIVector<T>::bDelete( int idx )
  121. {
  122. ASSERT( this );
  123. T * pID = poDetach( idx );
  124. if ( pID )
  125. {
  126. delete pID;
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. template <class T> int CIVector<T>::bDelete( T * t )
  132. {
  133. ASSERT( this );
  134. return bDelete( iFind( t ) );
  135. }
  136. template <class T> void CIVector<T>::vDeleteAll()
  137. {
  138. ASSERT( this );
  139. while( m_iCount )
  140. bDelete( 0 );
  141. }
  142. template <class T> int CIVector<T>::iFind( T * t )
  143. {
  144. ASSERT( this );
  145. for( int i = 0; i < m_iCount; i++ )
  146. {
  147. if( m_pData[ i ] == t )
  148. return i;
  149. }
  150. return kNotFound;
  151. }
  152. #endif // __VECTTMPL_H__
  153.