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.

114 lines
2.2 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. #include "stdafx.h"
  3. #include"tarray.h"
  4. //------------------------------------------------------------------------
  5. template< class T > CArrayT< T >::CArrayT( )
  6. {
  7. m_pT = NULL;
  8. m_nMaxSize = 0;
  9. m_idx = 0;
  10. }
  11. //------------------------------------------------------------------------
  12. // destroy the list
  13. template< class T > CArrayT< T >::~CArrayT( )
  14. {
  15. if( m_pT != NULL )
  16. {
  17. delete[] m_pT;
  18. }
  19. }
  20. //------------------------------------------------------------------------
  21. // increases array size, returns zero if the operation failed
  22. template< class T > int CArrayT< T >::GrowBy( int iSize )
  23. {
  24. if( iSize == 0 )
  25. {
  26. //
  27. //Grow by # number of items
  28. //
  29. iSize = 4;
  30. }
  31. if( m_pT == NULL )
  32. {
  33. m_pT = ( T * )new T[ iSize ];
  34. if( m_pT == NULL )
  35. {
  36. return 0;
  37. }
  38. m_nMaxSize = iSize;
  39. m_idx = 0;
  40. }
  41. else
  42. {
  43. T *pT;
  44. m_nMaxSize += iSize;
  45. pT = ( T * )new T[ m_nMaxSize ];
  46. if( pT == NULL )
  47. {
  48. return 0;
  49. }
  50. ZeroMemory( ( PVOID )pT , sizeof( T ) * m_nMaxSize );
  51. CopyMemory( pT , m_pT , sizeof( T ) * ( m_idx ) );
  52. if( m_pT != NULL )
  53. {
  54. delete[] m_pT;
  55. }
  56. m_pT = pT;
  57. }
  58. return m_nMaxSize;
  59. }
  60. //------------------------------------------------------------------------
  61. // Simply put, increase the array size if empty, and place item at the
  62. // end of the list
  63. template< class T > int CArrayT< T >::Insert( T tItem )
  64. {
  65. if( m_pT == NULL || ( m_idx ) >= m_nMaxSize )
  66. {
  67. if( GrowBy( 0 ) == 0 )
  68. {
  69. return 0;
  70. }
  71. }
  72. m_pT[ m_idx ] = tItem;
  73. m_idx++;
  74. return m_idx;
  75. }
  76. //------------------------------------------------------------------------
  77. // exposes the array for direct reference
  78. template< class T > T* CArrayT< T >::ExposeArray( )
  79. {
  80. return &m_pT[0];
  81. }
  82. //------------------------------------------------------------------------
  83. // Returns the number of valid entries in the array
  84. template< class T > int CArrayT< T >::GetSize( ) const
  85. {
  86. return ( m_idx );
  87. }