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];
45 void removeIndex(uint i) {
46 for (i++; i < fldsize; i++) {
47 array[i - 1] = array[i];
53 return array[fldsize - 1];
56 void setSize(uint _size) {
57 if (_size <= fldsize) {
60 } else if (_size > capacity) {
61 array = (type *)ourrealloc(array, _size * sizeof(type));
64 bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
68 void addAll(Vector<type> *v) {
69 int oldsize = fldsize;
70 setSize(fldsize + v->fldsize);
71 memcpy(&array[fldsize], v->array, v->fldsize * sizeof(type));
74 void removeAll(Vector<type> *v) {
75 uint vsize = v->size();
76 for(uint i = 0; i < vsize; i++)
81 if (fldsize >= capacity) {
82 uint newcap = capacity << 1;
83 array = (type *)ourrealloc(array, newcap * sizeof(type));
86 array[fldsize++] = item;
90 return get(size() - 1);
97 type get(uint index) const {
101 type setExpand(uint index, type item) {
102 type retval = (type) 0;
103 if (index >= fldsize)
106 retval = array[index];
111 void set(uint index, type item) {
119 bool isEmpty() const {