Guest User

Untitled

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