Advertisement
Guest User

Untitled

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