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.

280 lines
7.3 KiB

  1. #pragma once
  2. #ifndef _MYVECTOR_H_
  3. #define _MYVECTOR_H_
  4. #include <new>
  5. template< class T >
  6. class TVector
  7. {
  8. public:
  9. typedef T& reference;
  10. typedef const T& const_reference;
  11. // iterators
  12. typedef T* iterator;
  13. typedef const T* const_iterator;
  14. class const_reverse_iterator;
  15. class reverse_iterator
  16. {
  17. public:
  18. reverse_iterator() : m_iter(NULL) {}
  19. explicit reverse_iterator( iterator iter ) : m_iter( iter ) {}
  20. reverse_iterator& operator++() { --m_iter; return *this;}
  21. reverse_iterator operator++(int) { return reverse_iterator(m_iter--); }
  22. reverse_iterator& operator--() { ++m_iter; return *this;}
  23. reverse_iterator operator--(int) { return reverse_iterator(m_iter++); }
  24. reverse_iterator operator+(size_t s) const { return reverse_iterator(m_iter - s); }
  25. reverse_iterator operator-(size_t s) const { return reverse_iterator(m_iter + s); }
  26. reverse_iterator& operator+=(size_t s) { m_iter -= s; return *this; }
  27. reverse_iterator& operator-=(size_t s) { m_iter += s; return *this; }
  28. bool operator==( iterator iter ) const { return ( m_iter == iter ); }
  29. bool operator!=( iterator iter ) const { return ( m_iter != iter ); }
  30. reference operator*() const { return *m_iter; }
  31. private:
  32. iterator m_iter;
  33. friend const_reverse_iterator;
  34. };
  35. class const_reverse_iterator
  36. {
  37. public:
  38. const_reverse_iterator() : m_iter(NULL) {}
  39. const_reverse_iterator( const reverse_iterator& riter ) : m_iter( riter.m_iter ) {}
  40. explicit const_reverse_iterator( const_iterator iter ) : m_iter( iter ) {}
  41. const_reverse_iterator& operator++() { --m_iter; return *this;}
  42. const_reverse_iterator operator++(int) { return const_reverse_iterator(m_iter--); }
  43. const_reverse_iterator& operator--() { ++m_iter; return *this;}
  44. const_reverse_iterator operator--(int) { return const_reverse_iterator(m_iter++); }
  45. const_reverse_iterator operator+(size_t s) const { return const_reverse_iterator(m_iter - s); }
  46. const_reverse_iterator operator-(size_t s) const { return const_reverse_iterator(m_iter + s); }
  47. const_reverse_iterator& operator+=(size_t s) { m_iter -= s; return *this; }
  48. const_reverse_iterator& operator-=(size_t s) { m_iter += s; return *this; }
  49. bool operator==( iterator iter ) const { return ( m_iter == iter ); }
  50. bool operator!=( iterator iter ) const { return ( m_iter != iter ); }
  51. const_reference operator*() const { return *m_iter; }
  52. private:
  53. const_iterator m_iter;
  54. };
  55. // constructor / destructor
  56. TVector();
  57. ~TVector();
  58. // iteration
  59. iterator begin(){ return m_pFirst; }
  60. const_iterator begin() const { return m_pFirst; }
  61. iterator end() { return m_pAfterLast; }
  62. const_iterator end() const { return m_pAfterLast; }
  63. reverse_iterator rbegin() { return reverse_iterator(m_pAfterLast-1); }
  64. const_reverse_iterator rbegin() const { return const_reverse_iterator(m_pAfterLast-1); }
  65. reverse_iterator rend() { return reverse_iterator(m_pFirst-1); }
  66. const_reverse_iterator rend() const { return const_reverse_iterator(m_pFirst-1); }
  67. // insertion / deletion
  68. void push_back( const T& );
  69. void pop_back();
  70. iterator insert( iterator, const T& );
  71. void insert( iterator, size_t, const T& );
  72. void insert( iterator, const_iterator, const_iterator );
  73. iterator erase( iterator );
  74. iterator erase( iterator, iterator );
  75. void clear(){ erase( begin(), end() ); }
  76. // element access
  77. reference operator[]( size_t s){ return *(m_pFirst+s); }
  78. const_reference operator[]( size_t s) const { return *(m_pFirst+s); };
  79. reference front() { return *m_pFirst; }
  80. const_reference front() const { return *m_pFirst; }
  81. reference back() { return *(m_pAfterLast-1); }
  82. const_reference back() const { return *(m_pAfterLast-1); }
  83. // size
  84. size_t size() const{ return ( m_pAfterLast - m_pFirst ); }
  85. bool empty() const{ return ( m_pAfterLast == m_pFirst ); }
  86. private:
  87. enum {
  88. defaultSpace = 64
  89. };
  90. void growSpace(size_t s);
  91. void growSpace(iterator&,size_t);
  92. void checkSpace(size_t s=1){if((size()+s)>=m_space) growSpace(s); }
  93. void checkSpace(iterator& iter, size_t s=1){if((size()+s)>=m_space) growSpace(iter,s); }
  94. size_t bytes(size_t s) const { return (s * sizeof(T)); }
  95. size_t m_space;
  96. T* m_pFirst;
  97. T* m_pAfterLast;
  98. };
  99. template< class T >
  100. inline
  101. TVector<T>::TVector()
  102. {
  103. m_space = 0;
  104. m_pFirst = m_pAfterLast = NULL;
  105. }
  106. template< class T >
  107. inline
  108. TVector<T>::~TVector()
  109. {
  110. for( T* p = m_pFirst; p != m_pAfterLast; ++p )
  111. {
  112. p->~T();
  113. }
  114. operator delete( m_pFirst );
  115. }
  116. template< class T >
  117. inline
  118. void
  119. TVector<T>::push_back(
  120. const T& x
  121. )
  122. {
  123. checkSpace();
  124. new(m_pAfterLast) T(x);
  125. ++m_pAfterLast;
  126. }
  127. template< class T >
  128. inline
  129. void
  130. TVector<T>::pop_back()
  131. {
  132. m_pAfterLast -= 1;
  133. m_pAfterLast->~T();
  134. }
  135. template< class T >
  136. inline
  137. __TYPENAME TVector<T>::iterator
  138. TVector<T>::insert(
  139. __TYPENAME TVector<T>::iterator iter,
  140. const T& x
  141. )
  142. {
  143. checkSpace(iter);
  144. if ( iter != m_pAfterLast )
  145. {
  146. ::memmove( iter+1, iter, bytes( m_pAfterLast - iter ) );
  147. }
  148. new(iter) T(x);
  149. ++m_pAfterLast;
  150. return iter;
  151. }
  152. template< class T >
  153. inline
  154. void
  155. TVector<T>::insert(
  156. __TYPENAME TVector<T>::iterator iter,
  157. size_t n,
  158. const T& x
  159. )
  160. {
  161. checkSpace(iter,n);
  162. if ( iter != m_pAfterLast )
  163. {
  164. ::memmove( iter+n, iter, bytes( m_pAfterLast - iter ) );
  165. }
  166. for ( int i = 0; i < n; i++ )
  167. {
  168. new(iter+i) T(x);
  169. }
  170. m_pAfterLast += n;
  171. }
  172. template< class T >
  173. inline
  174. void
  175. TVector<T>::insert(
  176. __TYPENAME TVector<T>::iterator insIter,
  177. __TYPENAME TVector<T>::const_iterator begIter,
  178. __TYPENAME TVector<T>::const_iterator endIter
  179. )
  180. {
  181. size_t n = endIter - begIter;
  182. checkSpace( insIter,n );
  183. if ( insIter != m_pAfterLast )
  184. {
  185. ::memmove( insIter+n, insIter, bytes(m_pAfterLast-insIter) );
  186. }
  187. for( int i = 0; i < n; i++ )
  188. {
  189. new(insIter+i) T(*(begIter+i));
  190. }
  191. m_pAfterLast += n;
  192. }
  193. template< class T >
  194. inline
  195. __TYPENAME TVector<T>::iterator
  196. TVector<T>::erase(
  197. __TYPENAME TVector<T>::iterator iter
  198. )
  199. {
  200. iter->~T();
  201. ::memmove( iter, iter+1, bytes(m_pAfterLast-(iter+1)) );
  202. m_pAfterLast -= 1;
  203. return iter;
  204. }
  205. template< class T >
  206. inline
  207. __TYPENAME TVector<T>::iterator
  208. TVector<T>::erase(
  209. __TYPENAME TVector<T>::iterator begIter,
  210. __TYPENAME TVector<T>::iterator endIter
  211. )
  212. {
  213. size_t n = endIter - begIter;
  214. for ( int i = 0; i < n; i++ )
  215. {
  216. (begIter+i)->~T();
  217. }
  218. ::memmove( begIter, endIter, bytes(m_pAfterLast-(endIter)) );
  219. m_pAfterLast -= n;
  220. return begIter;
  221. }
  222. template< class T >
  223. void
  224. TVector<T>::growSpace(
  225. size_t n
  226. )
  227. {
  228. size_t s = size();
  229. size_t newSpace = (m_space == 0)? defaultSpace : m_space * 2;
  230. while ( newSpace < (n + m_space) )
  231. newSpace *= 2;
  232. T *pBuffer = static_cast<T *>( operator new( bytes( newSpace ) ) );
  233. ::memmove( pBuffer, m_pFirst, bytes(m_pAfterLast-m_pFirst) );
  234. operator delete( m_pFirst );
  235. m_space = newSpace;
  236. m_pFirst = pBuffer;
  237. m_pAfterLast = m_pFirst + s;
  238. }
  239. // this will grow the space as well as fixup the given iterator (since memory is moving)
  240. template< class T >
  241. void
  242. TVector<T>::growSpace(
  243. __TYPENAME TVector<T>::iterator& iter, // fix up this iterator
  244. size_t n
  245. )
  246. {
  247. size_t iterOff = iter - m_pFirst;
  248. growSpace(n);
  249. iter = m_pFirst + iterOff;
  250. }
  251. #endif // !_MYVECTOR_H_