1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
#define __DESTRUCTOR(ty, ptr) (ptr)->~ty()
#define __CONSTRUCTOR(ty, ptr) (*ptr) = ty()
#define _array_start _array_data
#define _vector_capacity (_array_end - _array_start)
#define _vector_size (_current - _array_start)
template <class T>
class tVector
{
public:
inline tVector(int nElements){
_array_data = new T[nElements];
_current = _array_start;
_array_end = (_array_start + nElements);
}
inline tVector(){
_array_data = new T[2];
_current = _array_start;
_array_end = (&_array_data[2]);
}
inline tVector(tVector<T> &obj){
if((_vector_capacity) < (obj._current - obj._array_start))
allocate(obj._current - obj._array_start);
memcpy(_array_data, obj._array_data, (unsigned int)((char*)obj._array_end - (char*)obj._array_start));
_current = _array_start + (obj._array_end - obj._array_start);
}
inline operator= (tVector<T> &obj){
if((_vector_capacity) < (obj._array_end - obj._array_start))
allocate(obj._current - obj._array_start);
memcpy(_array_data, obj._array_data, (unsigned int)((char*)obj._array_end - (char*)obj._array_start));
_current = _array_start + (obj._array_end - obj._array_start);
}
inline ~tVector(){delete []_array_data;}
inline void double_size(){
T *_temp = new T[(unsigned int)(_vector_capacity) * 2];
_current = _temp + (_vector_capacity);
memcpy(_temp, _array_data, (unsigned int)((char*)_array_end - (char*)_array_data));
_array_end = _temp + (_array_end - _array_data) * 2;
delete [] _array_data;_array_data = _temp;
}
inline void push_back(const T &t){
if(_current == _array_end)double_size();*_current++ = t;
}
inline T& push_back_and_access(const T &t){
if(_current == _array_end)double_size();*_current++ = t;return _current[-1];
}
inline void pop_back(){_current -= (_current != _array_start);}
inline void pop_back_and_destroy(){
if(_current != _array_start){_current--;__DESTRUCTOR(T, _current]);}}
inline void resize(size_t size){
if((_vector_size) == size)return;
T *it;
if((_vector_size) < size){
if((_vector_capacity) < size)
{
T *_temp = new T[size];
_current = _temp + (_vector_size);
memcpy(_temp, _array_data, (unsigned int)((char*)_array_end - (char*)_array_start));
_array_end = (_temp + size);
delete [] _array_data;_array_data = _temp;return;
}else
{
it = _current;
_current = (_array_start + size);
for(;it < _current;it++)__CONSTRUCTOR(T, it);
}}
else
{
it = _current;
_current = (_array_start + size);
if(it != _current)
do{__DESTRUCTOR(T, it);}while(it != _current);
}
}
inline void insert(unsigned int pos, const T &val)
{if(pos >= (_vector_size))return;
if(_current == _array_end)double_size();
T *end = _array_data + pos;
for(T *it = _current++;it != end;)*it-- = it[-1];
*end = val;
}
inline void insert(T * pos, const T &val){
if(pos >= _current)return;
unsigned int _pos = (pos - _array_start);
if(_current == _array_end)
{double_size();pos= _array_start + _pos;}
for(T *it = _current++;it != pos;)*it-- = it[-1];
*pos = val;
}
inline void assign(size_t size, const T &val){if(!size)return;
if(_current != _array_start)
for(T *it = _array_data;it < _current;it++)__DESTRUCTOR(T, it);
if((_vector_capacity) < size)allocate(size);
_current = _array_start + size;
for(T *it = _array_start;it < _current;){*it++ = val;}
}
inline void shrink_to_fit(){if(_current == _array_end)return;
if(_current == _array_start)return;
T *_temp = new T[(_vector_size)];
memcpy(_temp, _array_data, (unsigned int)((char*)_current - (char*)_array_start));
_current = _temp + (_vector_size);
delete [] _array_data;_array_data = _temp;_array_end = _current;
}
inline void reverse(int count = 0){
size_t half_size = ((unsigned int)(_vector_size)) / 2;
if(count && count < half_size)half_size = count;
char temp[sizeof(T)]; //avoid constructors
T*_end = _array_end - 1;
for(int i = 0;i < half_size;i++)
{
*((T*)temp) = (_array_start)[i];
(_array_start)[i] = (_end)[-i];
(_end)[-i] = *((T*)temp);
}
}
inline void erase(unsigned int pos){
if(_current == _array_start || pos >= (_vector_size))return;
__DESTRUCTOR(T, &_array_data[pos]);
for(T *it = &_array_data[pos + 1];it < _current;){it[-1] = *it++;}
_current--;
}
inline void erase(T *pos){
if(pos >= _current)return;
__DESTRUCTOR(T, pos);
T *__current = _current--;
for(pos++;pos < __current;){pos[-1] = *pos++;}
}
inline void erase(T *first, T *last){
if(last >= _current)return;
for(T *it = first;it < last;it++)
{__DESTRUCTOR(T, it);}
T *__current = _current;
_current -= (last - first);
for(;it < __current;){*first++ = *it++;}
}
inline void erase(unsigned int first, unsigned int last){
unsigned int range = last - first;
if(first >= last || (_current - range < _array_start) || last >= (_vector_size))return;
T *end = &_array_data[last];
for(T *it = &_array_data[first];it < end;it++)
{__DESTRUCTOR(T, it);}
range = -range;
for(;it < _current;){it[range] = *it++;}
_current += range; //it's negative
}
inline void swap(tVector <T> &obj){
T *__current = _current;
T *__array_start = _array_start;
T *__array_end = _array_end;
_current = obj._current;
_array_start = obj._array_start;
_array_end = obj._array_end;
obj._current = __current;
obj._array_start = __array_start;
obj._array_end = __array_end;
}
inline void clear(bool bDestroy = false){
if(bDestroy)for(T *it = _array_data;it < _current;it++){__DESTRUCTOR(T, it);}
_array_end = _array_start;_current = array_data;}
inline void reset(){_current = _array_start;}
inline size_t size(){return _vector_size;}
inline void reserve(size_t max){if(max > (_vector_capacity))new_size(max);}
inline T& at(unsigned int index){return _array_start[index];}
inline bool empty(){return ((_vector_capacity) <= 0);}
inline T* data(){return _array_start;}
inline T* begin(){return _array_start;}
inline T* end(){return _current;}
inline T& front(){return *_array_start;}
inline T& back(){return _current[-1];}
inline size_t capacity(){return (_vector_capacity);}
inline T& operator[] (unsigned int index){return _array_start[index];}
inline size_t max_size(){allocator<T> obj;return obj.max_size();}
inline void allocate(size_t size){
delete [] _array_data;
_array_data = new T[size];
_current = _array_start;
_array_end = (_array_start + size);
}
_PROTECTED :
T *_array_data, *_current, *_array_end;
private :
inline void new_size(size_t size)
{
T *_temp = new T[size];
_current = _temp + (_vector_size);
memcpy(_temp, _array_data, (unsigned int)((char*)_array_end - (char*)_array_data));
_array_end = _temp + size;
delete [] _array_data;_array_data = _temp;
}
};
#undef __DESTRUCTOR
#undef __CONSTRUCTOR
#undef _array_start
#undef _vector_capacity
#undef _vector_size
|