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.2 KiB

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