Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #ifndef SPARKLYVECTOR_H
- #define SPARKLYVECTOR_H
- #pragma pack(push,_CRT_PACKING)
- #pragma warning(push,3)
- #pragma push_macro("new")
- #undef new
- template <class T>
- class spvec
- {
- public:
- T *data;
- T *last;
- 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;
- last = p + (size - 1);
- 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 _moveitem(T *first, T *last, T *dest)
- {
- for (; first != last; ++dest, ++first) *dest = _STD move(*first);
- }
- void remove(int pos)
- {
- // _Move(data + 1, last, data);
- size--;
- // last--;
- // actionremove(data + pos, size - pos);
- memmove(data + pos, data + pos + 1, sizeof(T) * (size - pos));
- reserved++;
- }
- void remove(int pos, int pos2)
- {
- int v = pos2 - pos;
- size -= v;
- reserved += v;
- memmove(data + pos, data + pos2, sizeof(T) * (size - pos2));
- }
- 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)
- {
- destroy();
- 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);
- }
- };
- #pragma pop_macro("new")
- #pragma warning(pop)
- #pragma pack(pop)
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement