Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- #include <windows.h>
- #include <vector>
- template <class T>
- class spvec
- {
- public:
- T *data;
- int size;
- int reserved;
- int reservstep;
- float reservstep_grow;
- spvec()
- {
- size = 0;
- reserved = 0;
- reservstep = 1;
- data = 0;
- reservstep_grow = 1.5f;
- }
- spvec(int reservstep)
- {
- size = 0;
- reserved = 0;
- reservstep = reservstep;
- data = 0;
- reservstep_grow = 0.0f;
- }
- spvec(T *indata, int insize)
- {
- size = insize;
- reserved = 0;
- reservstep = 1;
- data = indata;
- reservstep_grow = 1.5f;
- }
- spvec(T *indata, int insize, int reservstep)
- {
- size = insize;
- reserved = 0;
- reservstep = reservstep;
- data = indata;
- reservstep_grow = 0.0f;
- }
- ~spvec()
- {
- if( size || reserved ) delete [] data;
- }
- void destroy()
- {
- size = 0;
- reserved = 0;
- delete [] data;
- }
- void makeroom(int howmuch)
- {
- if (reservstep_grow > 1.0) reservstep = int((float)size * reservstep_grow);
- T *p = new T[size+reserved+howmuch+reservstep];
- for( int a = 0; a < size; a++ ) p[a] = data[a];
- if(size) delete [] data;
- data = p;
- reserved += howmuch + reservstep;
- }
- void takeroom( int howmuch )
- {
- int needs = reserved - howmuch;
- if( needs < 0 ) makeroom(needs / -1);
- size += howmuch;
- reserved -= howmuch;
- }
- void trim()
- {
- T *p = new T[size+reservstep];
- for( int a = 0; a < size; a++ ) p[a] = data[a];
- if(size) delete [] data;
- data = p;
- reserved = reservstep;
- }
- void remove(int pos)
- {
- reserved++;
- for( int a = pos; a < size - 1; a++ ) data[a] = data[a+1];
- size--;
- }
- void remove(int pos, int howmuch)
- {
- reserved += howmuch;
- for( int a = pos; a < size - 1; a++ ) data[a] = data[a+howmuch];
- size -= howmuch;
- }
- void reverse()
- {
- T temp;
- int v = 0;
- for( int a = 0; a < size / 2; a++ )
- {
- v = size - (a + 1);
- temp = data[a];
- data[a] = data[v];
- data[v] = temp;
- }
- }
- void clear()
- {
- reserved += size;
- size = 0;
- }
- void resize(int newsize)
- {
- if (newsize == 0)
- {
- clear();
- return;
- }
- T * t = new T[newsize + reservstep];
- int b = size;
- if (newsize < size) b = newsize;
- if (b) for (int a = 0; a < b; a++) t[a] = data[a];
- if (size) delete[] data;
- size = b;
- data = t;
- }
- void move( int what, int to )
- {
- int a; T temp;
- if( what > to )
- {
- temp = data[what];
- for( a = what; a > to; a-- ) data[a] = data[a-1];
- data[to] = temp;
- return;
- }
- temp = data[what];
- for( a = what; a < to; a++ )
- {
- data[a] = data[a+1];
- }
- data[to] = temp;
- }
- void move( int what, int howmuch, int to )
- {
- howmuch--;
- if( what > to )
- {
- for( int a = 0; a <= howmuch; a++ ) move(what + howmuch, to);
- return;
- }
- for( int a = 0; a <= howmuch; a++ ) move(what, to + howmuch);
- }
- void push( T in )
- {
- takeroom( 1 );
- data[size - 1] = in;
- }
- void push( int pos, T in )
- {
- takeroom(1);
- for( int a = size - 2; a >= pos; a-- ) data[a + 1] = data[a];
- data[pos] = in;
- }
- void push(T *in, int insize)
- {
- int i = size;
- takeroom(insize);
- for (int a = 0; a < insize; a++) data[i + a] = in[a];
- }
- void push(int pos, T *in, int insize)
- {
- int i = size;
- takeroom(insize);
- int a;
- for (a = size - 2; a >= pos; a--) data[a + insize] = data[a];
- for (a = pos; a < insize; a++) data[a] = in[a - pos];
- }
- void swap(int what, int with)
- {
- T t;
- t = data[what];
- data[what] = data[with];
- data[with] = t;
- }
- void swap(int start, int end, int to)
- {
- for (int a = 0; a < end - start; a++) swap(start + a, to + a);
- }
- const spvec &operator+=(const spvec <T> in)
- {
- if (!in.size) return (*this);
- this->push(in.data, in.size);
- return (*this);
- }
- const spvec &operator-=(const spvec <T> in)
- {
- if (!in.size) return (*this);
- this->push(0, in.data, in.size);
- return (*this);
- }
- const spvec &operator+=(const T in)
- {
- this->push(in);
- return (*this);
- }
- const spvec &operator%=(int in)
- {
- this->resize(in);
- return (*this);
- }
- const spvec &operator-=(const int in)
- {
- this->push(0, in);
- return (*this);
- }
- const spvec &operator=(int in)
- {
- this->resize(in);
- return (*this);
- }
- T &operator[](int in) const
- {
- return (*(this->data + in));
- }
- spvec(spvec <T> &in)
- {
- cout << "here1" << endl;
- if (this == &in)
- {
- cout << "this == &in" << endl;
- return;
- }
- this->size = in.size;
- this->reserved = in.reserved;
- this->reservstep = in.reservstep;
- if (in.size || in.reserved)
- {
- this->data = new T[in.size + in.reserved];
- for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];
- }
- }
- const spvec <T> &operator=(const spvec <T> &in)
- {
- cout << "here2" << endl;
- if (this == &in)
- {
- cout << "this == &in" << endl;
- return (*this);
- }
- if (this->size)
- {
- cout << "clear old" << endl;
- delete[] this->data;
- }
- this->size = in.size;
- this->reserved = in.reserved;
- this->reservstep = in.reservstep;
- if (in.size || in.reserved)
- {
- this->data = new T[in.size + in.reserved];
- for (int a = 0; a < in.size; a++) this->data[a] = in.data[a];
- }
- return (*this);
- }
- };
- LARGE_INTEGER t_start_counter, t_current_counter;
- void tstart()
- {
- QueryPerformanceCounter(&t_start_counter);
- }
- double tend()
- {
- QueryPerformanceCounter(&t_current_counter);
- double out;
- out = (double)(t_current_counter.QuadPart - t_start_counter.QuadPart);
- out *= 0.000001;
- return out;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int a = 0, b = 0;
- spvec<int> v;
- vector<int> i;
- double t1 = 0.0, t2 = 0.0;
- v.reservstep_grow = 3.0f;
- tstart();
- for (a = 0; a < 16777216; a++) v += a + 100;
- t1 = tend();
- tstart();
- for (a = 0; a < 16777216; a++) i.push_back(a + 100);
- t2 = tend();
- cout << "myvector/defaultvector" << endl;
- cout << "reserved:" << v.reservstep << "/" << i.capacity() << endl;
- cout << "size:" << v.size << "/" << i.size() << endl;
- cout << "times:" << t1 << "/" << t2 << " ration:" << t2 / t1 << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement