Vector.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef __DAHUA_INFRA_VECTOR_H__
  2. #define __DAHUA_INFRA_VECTOR_H__
  3. #include "Allocator.h"
  4. #include "Detail/construct.h"
  5. namespace Dahua {
  6. namespace Infra {
  7. template<typename T>
  8. class TVector
  9. {
  10. public:
  11. typedef T value_type;
  12. typedef value_type* iterator;
  13. typedef value_type& reference;
  14. typedef size_t size_type;
  15. iterator begin() const
  16. {
  17. return m_start;
  18. }
  19. iterator end() const
  20. {
  21. return m_end;
  22. }
  23. size_type size() const
  24. {
  25. return size_type(m_end - m_start);
  26. }
  27. size_type capacity() const
  28. {
  29. return size_type(m_capEnd - m_start);
  30. }
  31. bool empty() const
  32. {
  33. return begin() == end();
  34. }
  35. reference operator[](size_type n) const
  36. {
  37. return *(begin() + n);
  38. }
  39. TVector()
  40. : m_start(0)
  41. , m_end(0)
  42. , m_capEnd(0)
  43. {}
  44. TVector(size_type n, const T& value)
  45. {
  46. fill_initialize(n, value);
  47. }
  48. explicit TVector(size_type n)
  49. {
  50. fill_initialize(n, T());
  51. }
  52. TVector(TVector const& r)
  53. {
  54. m_start = (iterator)CAllocator::allocate(r.capacity() * sizeof(T));
  55. for (size_t i = 0; i < r.size(); ++i)
  56. {
  57. Datail::construct(&m_start[i], r.m_start[i]);
  58. }
  59. m_end = m_start + r.size();
  60. m_capEnd = m_start + r.capacity();
  61. }
  62. TVector& operator=(TVector const& r)
  63. {
  64. if (this == &r)
  65. {
  66. return *this;
  67. }
  68. m_start = (iterator)CAllocator::allocate(r.capacity() * sizeof(T));
  69. for (size_t i = 0; i < r.size(); ++i)
  70. {
  71. Datail::construct(&m_start[i], r.m_start[i]);
  72. }
  73. m_end = m_start + r.size();
  74. m_capEnd = m_start + r.capacity();
  75. return *this;
  76. }
  77. ~TVector()
  78. {
  79. for (size_t i = 0; i < size(); ++i)
  80. {
  81. Datail::destruct(m_start[i]);
  82. }
  83. if (m_start != 0)
  84. {
  85. CAllocator::dealocate((void *)m_start, size() * sizeof(T));
  86. }
  87. m_start = m_end = m_capEnd = 0;
  88. }
  89. reference front() const
  90. {
  91. return *m_start;
  92. }
  93. reference back() const
  94. {
  95. return *(m_end - 1);
  96. }
  97. void push_back(const T& value)
  98. {
  99. if (m_end == m_capEnd)
  100. {
  101. realloc();
  102. }
  103. Datail::construct(m_end, value);
  104. ++m_end;
  105. }
  106. void pop_back()
  107. {
  108. --m_end;
  109. Datail::destruct(m_end);
  110. }
  111. iterator erase(iterator pos)
  112. {
  113. if (pos + 1 != end())
  114. {
  115. size_t copy_size = size_t(m_end - pos) - 1;
  116. for (size_t i = 0; i < copy_size; ++i)
  117. pos[i] = pos[i + 1];
  118. }
  119. --m_end;
  120. Datail::destruct(m_end);
  121. return pos;
  122. }
  123. void resize(size_type new_size, const T& x)
  124. {
  125. if (new_size < size())
  126. {
  127. m_end = m_start + new_size;
  128. for (iterator s = m_start; s != m_end; ++s)
  129. {
  130. Datail::destruct(s);
  131. Datail::construct(s, x);
  132. }
  133. return;
  134. }
  135. for ( iterator s = m_start; s != m_end; ++s )
  136. {
  137. Datail::destruct(s);
  138. }
  139. if (new_size > capacity())
  140. {
  141. realloc(new_size);
  142. }
  143. m_end = m_start + new_size;
  144. for ( iterator s = m_start; s != m_end; ++s )
  145. {
  146. Datail::construct(s, x);
  147. }
  148. }
  149. void resize(size_type new_size)
  150. {
  151. resize(new_size, T());
  152. }
  153. void clear()
  154. {
  155. for ( iterator s = m_start; s != m_end; ++s )
  156. {
  157. Datail::destruct(s);
  158. }
  159. m_end = m_start;
  160. }
  161. private:
  162. void realloc()
  163. {
  164. size_t cap = capacity();
  165. if (0 == cap)
  166. {
  167. m_start = (iterator)CAllocator::allocate(sizeof(T));
  168. m_capEnd = m_start + 1;
  169. m_end = m_start;
  170. }
  171. else
  172. {
  173. size_t s = size();
  174. m_start = (T*)CAllocator::reallocate(m_start, cap * sizeof(T), 2 * cap * sizeof(T));
  175. m_capEnd = m_start + 2 * cap;
  176. m_end = m_start + s;
  177. }
  178. }
  179. void realloc(size_t new_size)
  180. {
  181. size_t s = size();
  182. size_t cap = capacity();
  183. if (cap == 0)
  184. {
  185. m_start = (iterator)CAllocator::allocate(sizeof(T) * new_size);
  186. m_capEnd = m_start + new_size;
  187. m_end = m_start;
  188. return;
  189. }
  190. size_t nbr = new_size / cap + (new_size % cap ? 1 : 0);
  191. m_start = (T*)CAllocator::reallocate(m_start, cap * sizeof(T), cap * nbr * sizeof(T));
  192. m_capEnd = m_start + cap * nbr;
  193. m_end = m_start + s;
  194. }
  195. void fill_initialize(size_type n, const T& value)
  196. {
  197. m_start = (iterator)CAllocator::allocate(n * sizeof(T));
  198. for (size_t i = 0; i < n; ++i)
  199. Datail::construct(m_start + i, value);
  200. m_end = m_start + n;
  201. m_capEnd = m_end;
  202. }
  203. iterator m_start;
  204. iterator m_end;
  205. iterator m_capEnd;
  206. };
  207. } // end of Infra
  208. } // end of Dahua
  209. #endif // end of __DAHUA_INFRA_VECTOR_H__