Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. #pragma once
  2. #ifndef SPARKLYVECTOR_H
  3. #define SPARKLYVECTOR_H
  4. #pragma pack(push,_CRT_PACKING)
  5. #pragma warning(push,3)
  6. #pragma push_macro("new")
  7. #undef new
  8.  
  9. template <class T>
  10. class spvec
  11. {
  12. public:
  13. T *data;
  14. T *last;
  15. int size;
  16. int reserved;
  17. int reservstep;
  18. float reservstep_grow;
  19.  
  20. spvec()
  21. {
  22. size = 0;
  23. reserved = 0;
  24. reservstep = 1;
  25. data = 0;
  26. reservstep_grow = 1.5f;
  27. }
  28.  
  29. spvec(int reservstep)
  30. {
  31. size = 0;
  32. reserved = 0;
  33. reservstep = reservstep;
  34. data = 0;
  35. reservstep_grow = 0.0f;
  36. }
  37.  
  38. spvec(T *indata, int insize)
  39. {
  40. size = insize;
  41. reserved = 0;
  42. reservstep = 1;
  43. data = indata;
  44. reservstep_grow = 1.5f;
  45. }
  46.  
  47. spvec(T *indata, int insize, int reservstep)
  48. {
  49. size = insize;
  50. reserved = 0;
  51. reservstep = reservstep;
  52. data = indata;
  53. reservstep_grow = 0.0f;
  54. }
  55.  
  56. ~spvec()
  57. {
  58. if( size || reserved ) delete [] data;
  59. }
  60.  
  61. void destroy()
  62. {
  63. size = 0;
  64. reserved = 0;
  65. delete [] data;
  66. }
  67.  
  68. void makeroom(int howmuch)
  69. {
  70. if (reservstep_grow > 1.0) reservstep = int((float)size * reservstep_grow);
  71. T *p = new T[size+reserved+howmuch+reservstep];
  72. for( int a = 0; a < size; a++ ) p[a] = data[a];
  73. if(size) delete [] data;
  74. data = p;
  75. last = p + (size - 1);
  76. reserved += howmuch + reservstep;
  77. }
  78.  
  79. void takeroom( int howmuch )
  80. {
  81. int needs = reserved - howmuch;
  82. if( needs < 0 ) makeroom(needs / -1);
  83. size += howmuch;
  84. reserved -= howmuch;
  85. }
  86.  
  87. void trim()
  88. {
  89. T *p = new T[size+reservstep];
  90. for( int a = 0; a < size; a++ ) p[a] = data[a];
  91. if(size) delete [] data;
  92. data = p;
  93. reserved = reservstep;
  94. }
  95.  
  96. void _moveitem(T *first, T *last, T *dest)
  97. {
  98. for (; first != last; ++dest, ++first) *dest = _STD move(*first);
  99. }
  100.  
  101. void remove(int pos)
  102. {
  103. // _Move(data + 1, last, data);
  104. size--;
  105. // last--;
  106. // actionremove(data + pos, size - pos);
  107. memmove(data + pos, data + pos + 1, sizeof(T) * (size - pos));
  108. reserved++;
  109. }
  110.  
  111. void remove(int pos, int pos2)
  112. {
  113. int v = pos2 - pos;
  114. size -= v;
  115. reserved += v;
  116. memmove(data + pos, data + pos2, sizeof(T) * (size - pos2));
  117. }
  118.  
  119. void reverse()
  120. {
  121. T temp;
  122. int v = 0;
  123. for( int a = 0; a < size / 2; a++ )
  124. {
  125. v = size - (a + 1);
  126. temp = data[a];
  127. data[a] = data[v];
  128. data[v] = temp;
  129. }
  130. }
  131.  
  132. void clear()
  133. {
  134. reserved += size;
  135. size = 0;
  136. }
  137.  
  138. void resize(int newsize)
  139. {
  140. if (newsize == 0)
  141. {
  142. destroy();
  143. return;
  144. }
  145. T * t = new T[newsize + reservstep];
  146. int b = size;
  147. if (newsize < size) b = newsize;
  148.  
  149. if (b) for (int a = 0; a < b; a++) t[a] = data[a];
  150. if (size) delete[] data;
  151. size = b;
  152. data = t;
  153. }
  154.  
  155. void move( int what, int to )
  156. {
  157. int a; T temp;
  158. if( what > to )
  159. {
  160. temp = data[what];
  161. for( a = what; a > to; a-- ) data[a] = data[a-1];
  162. data[to] = temp;
  163. return;
  164. }
  165.  
  166. temp = data[what];
  167. for( a = what; a < to; a++ )
  168. {
  169. data[a] = data[a+1];
  170. }
  171. data[to] = temp;
  172. }
  173.  
  174. void move( int what, int howmuch, int to )
  175. {
  176. howmuch--;
  177. if( what > to )
  178. {
  179. for( int a = 0; a <= howmuch; a++ ) move(what + howmuch, to);
  180. return;
  181. }
  182. for( int a = 0; a <= howmuch; a++ ) move(what, to + howmuch);
  183. }
  184.  
  185.  
  186. void push( T in )
  187. {
  188. takeroom( 1 );
  189. data[size - 1] = in;
  190. }
  191.  
  192. void push( int pos, T in )
  193. {
  194. takeroom(1);
  195. for( int a = size - 2; a >= pos; a-- ) data[a + 1] = data[a];
  196. data[pos] = in;
  197. }
  198.  
  199. void push(T *in, int insize)
  200. {
  201. int i = size;
  202. takeroom(insize);
  203. for (int a = 0; a < insize; a++) data[i + a] = in[a];
  204. }
  205.  
  206. void push(int pos, T *in, int insize)
  207. {
  208. int i = size;
  209. takeroom(insize);
  210. int a;
  211. for (a = size - 2; a >= pos; a--) data[a + insize] = data[a];
  212. for (a = pos; a < insize; a++) data[a] = in[a - pos];
  213. }
  214.  
  215. void swap(int what, int with)
  216. {
  217. T t;
  218. t = data[what];
  219. data[what] = data[with];
  220. data[with] = t;
  221. }
  222.  
  223. void swap(int start, int end, int to)
  224. {
  225. for (int a = 0; a < end - start; a++) swap(start + a, to + a);
  226. }
  227.  
  228. const spvec &operator+=(const spvec <T> in)
  229. {
  230. if (!in.size) return (*this);
  231. this->push(in.data, in.size);
  232. return (*this);
  233. }
  234.  
  235. const spvec &operator-=(const spvec <T> in)
  236. {
  237. if (!in.size) return (*this);
  238. this->push(0, in.data, in.size);
  239. return (*this);
  240. }
  241.  
  242. const spvec &operator+=(const T in)
  243. {
  244. this->push(in);
  245. return (*this);
  246. }
  247.  
  248. const spvec &operator%=(int in)
  249. {
  250. this->resize(in);
  251. return (*this);
  252. }
  253.  
  254. const spvec &operator-=(const int in)
  255. {
  256. this->push(0, in);
  257. return (*this);
  258. }
  259.  
  260.  
  261. const spvec &operator=(int in)
  262. {
  263. this->resize(in);
  264. return (*this);
  265. }
  266.  
  267.  
  268. T &operator[](int in) const
  269. {
  270. return (*(this->data + in));
  271. }
  272.  
  273.  
  274.  
  275. spvec(spvec <T> &in)
  276. {
  277. cout << "here1" << endl;
  278. if (this == &in)
  279. {
  280. cout << "this == &in" << endl;
  281. return;
  282. }
  283. this->size = in.size;
  284. this->reserved = in.reserved;
  285. this->reservstep = in.reservstep;
  286. if (in.size || in.reserved)
  287. {
  288. this->data = new T[in.size + in.reserved];
  289. for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];
  290. }
  291. }
  292.  
  293. const spvec <T> &operator=(const spvec <T> &in)
  294. {
  295. cout << "here2" << endl;
  296. if (this == &in)
  297. {
  298. cout << "this == &in" << endl;
  299. return (*this);
  300. }
  301.  
  302. if (this->size)
  303. {
  304. cout << "clear old" << endl;
  305. delete[] this->data;
  306. }
  307. this->size = in.size;
  308. this->reserved = in.reserved;
  309. this->reservstep = in.reservstep;
  310. if (in.size || in.reserved)
  311. {
  312. this->data = new T[in.size + in.reserved];
  313. for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];
  314. }
  315. return (*this);
  316. }
  317. };
  318. #pragma pop_macro("new")
  319. #pragma warning(pop)
  320. #pragma pack(pop)
  321. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement