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 void removeIndex(uint i) {
47 for (i++; i < fldsize; i++) {
48 array[i - 1] = array[i];
54 return array[fldsize - 1];
57 void setSize(uint _size) {
58 if (_size <= fldsize) {
61 } else if (_size > capacity) {
62 array = (type *)ourrealloc(array, _size * sizeof(type));
65 bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
69 void addAll(Vector<type> *v) {
70 int oldsize = fldsize;
71 setSize(fldsize + v->fldsize);
72 memcpy(&array[fldsize], v->array, v->fldsize * sizeof(type));
75 void removeAll(Vector<type> *v) {
76 uint vsize = v->size();
77 for (uint i = 0; i < vsize; i++)
82 if (fldsize >= capacity) {
83 uint newcap = capacity << 1;
84 array = (type *)ourrealloc(array, newcap * sizeof(type));
87 array[fldsize++] = item;
91 return get(size() - 1);
98 type get(uint index) const {
102 type setExpand(uint index, type item) {
103 type retval = (type) 0;
104 if (index >= fldsize)
107 retval = array[index];
112 void set(uint index, type item) {
120 bool isEmpty() const {