4 #define VECTOR_DEFCAP 8
6 template<typename type>
9 Vector(uint _capacity = VECTOR_DEFCAP) :
12 array((type *) ourmalloc(sizeof(type) * _capacity)) {
15 Vector(uint _capacity, type *_array) :
18 array((type *) ourmalloc(sizeof(type) * _capacity)) {
19 memcpy(array, _array, capacity * sizeof(type));
22 Vector(Vector<type> *v) :
24 capacity(v->capacity),
25 array((type *) ourmalloc(sizeof(type) * v->capacity)) {
26 memcpy(array, v->array, capacity * sizeof(type));
34 for (uint i=0; i<fldsize; i++) {
36 for (i++; i<fldsize; i++) {
37 array[i - 1] = array[i];
46 return array[fldsize - 1];
49 void setSize(uint _size) {
50 if (_size <= fldsize) {
53 } else if (_size > capacity) {
54 array = (type *)ourrealloc(array, _size * sizeof(type));
57 bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
61 void addAll(Vector<type> *v) {
62 int oldsize = fldsize;
63 setSize(fldsize + v->fldsize);
64 memcpy(&array[fldsize], v->array, v->fldsize * sizeof(type));
68 if (fldsize >= capacity) {
69 uint newcap = capacity << 1;
70 array = (type *)ourrealloc(array, newcap * sizeof(type));
73 array[fldsize++] = item;
76 type get(uint index) const {
80 type setExpand(uint index, type item) {
81 type retval = (type) 0;
85 retval = array[index];
90 void set(uint index, type item) {
98 bool isEmpty() const {