6template <
typename T>
class Vec :
public O2obj {
12 Vec(
int siz) { array = NULL; init(siz); }
15 Vec(
int siz,
bool z) { init(siz, z); }
18 Vec(
Vec &v) { allocated = v.allocated;
19 length = v.length; array = v.array;
20 v.array = NULL; v.allocated = 0; v.length = 0; }
23 Vec() { array = NULL; init(0); }
28 void init(
int siz) { init(siz,
false); }
32 void init(
int siz,
bool z) {
34 array = (siz > 0 ? O2_MALLOCNT(siz, T) : NULL);
46 if (array) O2_FREE(array);
51 void zero() { memset((
void *) array, 0,
sizeof(T) * allocated); }
53 T &operator[](
int index) {
return array[index]; }
56 T &last() {
return array[length - 1]; }
59 void clear() { length = 0; }
63 void remove(
int index) {
66 array[index] = array[length];
71 void push_back(T data) { *append_space(1) = data; }
74 void append(
const T *data,
int count) {
75 memcpy(append_space(count), data, count *
sizeof(T));
80 T *append_space(
int count) {
81 if (length + count > allocated) {
82 expand_array(length + count);
85 return &array[length - count];
90 void erase(
int first,
int last) {
91 if (first >= 0 && first <= last && last <= length) {
93 memmove(array + first, array + last, (length - last) *
sizeof(T));
99 void erase(
int i) { erase(i, i + 1); }
103 void drop_front(
int n) { erase(0, n); }
105 void insert(
int i, T data) {
106 if (i >= 0 && i <= length) {
108 memmove(array + i, array + i + 1, (length - i) *
sizeof(T));
114 void retrieve(T *data) { memcpy(data, array, length *
sizeof(T)); }
117 T pop_back() {
return array[length-- - 1]; }
120 bool bounds_check(
int i) {
return i >= 0 && i < length; }
123 int size() {
return length; }
130 int get_allocated() {
return allocated; }
133 void expand_array(
int new_size);
140 if (allocated > 0) allocated *= 2;
142 if (allocated < newsize) allocated = newsize;
143 T *bigger = O2_MALLOCNT(allocated, T);
144 memcpy(bigger, array, length *
sizeof(T));
145 if (array) O2_FREE(array);