1 /* see copyright notice in squirrel.h */
\r
5 #define sq_new(__ptr,__type) {__ptr=(__type *)sq_vm_malloc(sizeof(__type));new (__ptr) __type;}
\r
6 #define sq_delete(__ptr,__type) {__ptr->~__type();sq_vm_free(__ptr,sizeof(__type));}
\r
7 #define SQ_MALLOC(__size) sq_vm_malloc(__size);
\r
8 #define SQ_FREE(__ptr,__size) sq_vm_free(__ptr,__size);
\r
9 #define SQ_REALLOC(__ptr,__oldsize,__size) sq_vm_realloc(__ptr,__oldsize,__size);
\r
11 //sqvector mini vector class, supports objects by value
\r
12 template<typename T> class sqvector
\r
21 sqvector(const sqvector<T>& v)
\r
25 void copy(const sqvector<T>& v)
\r
28 for(unsigned int i = 0; i < v._size; i++) {
\r
29 new ((void *)&_vals[i]) T(v._vals[i]);
\r
36 for(unsigned int i = 0; i < _size; i++)
\r
38 SQ_FREE(_vals, (_allocated * sizeof(T)));
\r
41 void reserve(unsigned int newsize) { _realloc(newsize); }
\r
42 void resize(unsigned int newsize, const T& fill = T())
\r
44 if(newsize > _allocated)
\r
46 if(newsize > _size) {
\r
47 while(_size < newsize) {
\r
48 new ((void *)&_vals[_size]) T(fill);
\r
53 for(unsigned int i = newsize; i < _size; i++) {
\r
59 void shrinktofit() { if(_size > 4) { _realloc(_size); } }
\r
60 T& top() const { return _vals[_size - 1]; }
\r
61 inline unsigned int size() const { return _size; }
\r
62 bool empty() const { return (_size <= 0); }
\r
63 inline void push_back(const T& val = T())
\r
65 if(_allocated <= _size)
\r
66 _realloc(_size * 2);
\r
67 new ((void *)&_vals[_size++]) T(val);
\r
69 inline void pop_back()
\r
71 _size--; _vals[_size].~T();
\r
73 void insert(unsigned int idx, const T& val)
\r
76 for(unsigned int i = _size - 1; i > idx; i--) {
\r
77 _vals[i] = _vals[i - 1];
\r
81 void remove(unsigned int idx)
\r
84 if(idx < (_size - 1)) {
\r
85 memcpy(&_vals[idx], &_vals[idx+1], sizeof(T) * (_size - idx - 1));
\r
89 unsigned int capacity() { return _allocated; }
\r
90 inline T &back() const { return _vals[_size - 1]; }
\r
91 T& operator[](unsigned int pos) const{ return _vals[pos]; }
\r
94 void _realloc(unsigned int newsize)
\r
96 newsize = (newsize > 0)?newsize:4;
\r
97 _vals = (T*)SQ_REALLOC(_vals, _allocated * sizeof(T), newsize * sizeof(T));
\r
98 _allocated = newsize;
\r
100 unsigned int _size;
\r
101 unsigned int _allocated;
\r
104 #endif //_SQUTILS_H_
\r