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.

202 lines
4.1 KiB

  1. #pragma once
  2. template<class T> class TArray
  3. {
  4. public:
  5. TArray(unsigned size) : _size(size), _count(0), _aItem( 0 )
  6. {
  7. if ( 0 != size )
  8. {
  9. _aItem = new T [_size];
  10. memset( _aItem, 0, _size * sizeof T );
  11. }
  12. }
  13. TArray() : _size(0), _count(0), _aItem(0) {}
  14. ~TArray()
  15. {
  16. delete [] _aItem;
  17. }
  18. void Add( const T &newItem, unsigned position )
  19. {
  20. if (position >= _count)
  21. {
  22. if (position >= _size)
  23. _GrowToSize( position );
  24. _count = position + 1;
  25. }
  26. _aItem[position] = newItem;
  27. }
  28. void Append( const T &newItem )
  29. {
  30. Add( newItem, Count() );
  31. }
  32. void Insert(const T& newItem, unsigned pos)
  33. {
  34. if (_count == _size)
  35. {
  36. unsigned newsize;
  37. if ( _size == 0 )
  38. newsize = 16;
  39. else
  40. newsize = _size * 2;
  41. T *aNew = new T [newsize];
  42. memcpy( aNew,
  43. _aItem,
  44. pos * sizeof( T ) );
  45. memcpy( aNew + pos + 1,
  46. _aItem + pos,
  47. (_count - pos) * sizeof T);
  48. delete (BYTE *) _aItem;
  49. _aItem = aNew;
  50. _size = newsize;
  51. }
  52. else
  53. {
  54. memmove ( _aItem + pos + 1,
  55. _aItem + pos,
  56. (_count - pos) * sizeof T);
  57. }
  58. _aItem[pos] = newItem;
  59. _count++;
  60. }
  61. void Remove (unsigned position)
  62. {
  63. if (pos < _count - 1)
  64. memmove ( _aItem + pos,
  65. _aItem + pos + 1,
  66. (_count - pos - 1) * sizeof T);
  67. memset( _aItem + _count - 1, 0, sizeof T );
  68. _count--;
  69. if (_count == 0)
  70. {
  71. delete (BYTE*) _aItem;
  72. _aItem = 0;
  73. _size = 0;
  74. }
  75. }
  76. unsigned Size () const { return _size; }
  77. T& Get (unsigned position) const
  78. {
  79. return _aItem[position];
  80. }
  81. void Clear()
  82. {
  83. delete [] _aItem;
  84. _aItem = 0;
  85. _size = 0;
  86. _count = 0;
  87. }
  88. unsigned Count() const { return _count; }
  89. void SetSize(unsigned position)
  90. {
  91. if (position >= _size)
  92. _GrowToSize(position);
  93. }
  94. void Shrink()
  95. {
  96. // make size == count, to save memory
  97. if ( 0 == _count )
  98. {
  99. Clear();
  100. }
  101. else if ( _count != _size )
  102. {
  103. T * p = new T [_count];
  104. _size = _count;
  105. RtlCopyMemory( p, _aItem, _count * sizeof T );
  106. delete (BYTE *) _aItem;
  107. _aItem = p;
  108. }
  109. }
  110. T* Acquire ()
  111. {
  112. T *temp = _aItem;
  113. _aItem = 0;
  114. _count = 0;
  115. _size = 0;
  116. return temp;
  117. }
  118. void Duplicate( TArray<T> & aFrom )
  119. {
  120. Clear();
  121. if ( 0 != aFrom.Count() )
  122. {
  123. _size = _count = aFrom.Count();
  124. _aItem = new T [_size];
  125. memcpy( _aItem, aFrom._aItem, _size * sizeof( T ) );
  126. }
  127. }
  128. T & operator[]( unsigned position )
  129. {
  130. if ( position >= _count )
  131. {
  132. if ( position >= _size )
  133. _GrowToSize( position );
  134. _count = position + 1;
  135. }
  136. return _aItem[position];
  137. }
  138. T & operator[]( unsigned position ) const
  139. {
  140. return _aItem[position];
  141. }
  142. T const * GetPointer() { return _aItem; }
  143. unsigned SizeOfInUse() const { return sizeof T * Count(); }
  144. protected:
  145. void _GrowToSize( unsigned position )
  146. {
  147. unsigned newsize = _size * 2;
  148. if ( newsize == 0 )
  149. newsize = 16;
  150. for( ; position >= newsize; newsize *= 2)
  151. continue;
  152. T *aNew = new T [newsize];
  153. if (_size > 0)
  154. memcpy( aNew,
  155. _aItem,
  156. _size * sizeof( T ) );
  157. memset( aNew + _size,
  158. 0,
  159. (newsize-_size) * sizeof T );
  160. delete (BYTE*) _aItem;
  161. _aItem = aNew;
  162. _size = newsize;
  163. }
  164. T * _aItem;
  165. unsigned _size;
  166. unsigned _count;
  167. };