ccd86498cc635f9943aa6d3ae5a185bbb6488850
[iotcloud.git] / version2 / src / C / vector.h
1 #ifndef CPPVECTOR_H
2 #define CPPVECTOR_H
3 #include <string.h>
4 #define VECTOR_DEFCAP 8
5
6 template<typename type>
7 class Vector {
8 public:
9         Vector(uint _capacity = VECTOR_DEFCAP) :
10                 fldsize(0),
11                 capacity(_capacity),
12                 array((type *) ourmalloc(sizeof(type) * _capacity)) {
13         }
14
15         Vector(uint _capacity, type *_array)  :
16                 fldsize(_capacity),
17                 capacity(_capacity),
18                 array((type *) ourmalloc(sizeof(type) * _capacity)) {
19                 memcpy(array, _array, capacity * sizeof(type));
20         }
21
22         Vector(Vector<type> *v) :
23                 fldsize(v->fldsize),
24                 capacity(v->capacity),
25                 array((type *) ourmalloc(sizeof(type) * v->capacity)) {
26                 memcpy(array, v->array, capacity * sizeof(type));
27         }
28
29         void pop() {
30                 fldsize--;
31         }
32
33         void remove(type t) {
34                 for (uint i=0; i<fldsize; i++) {
35                         if (array[i] == t) {
36                                 for (i++; i<fldsize; i++) {
37                                         array[i - 1] = array[i];
38                                 }
39                                 fldsize--;
40                                 break;
41                         }
42                 }
43         }
44         
45         type last() const {
46                 return array[fldsize - 1];
47         }
48
49         void setSize(uint _size) {
50                 if (_size <= fldsize) {
51                         fldsize = _size;
52                         return;
53                 } else if (_size > capacity) {
54                         array = (type *)ourrealloc(array, _size * sizeof(type));
55                         capacity = _size;
56                 }
57                 bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
58                 fldsize = _size;
59         }
60
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));
65         }
66
67         void add(type item) {
68                 if (fldsize >= capacity) {
69                         uint newcap = capacity << 1;
70                         array = (type *)ourrealloc(array, newcap * sizeof(type));
71                         capacity = newcap;
72                 }
73                 array[fldsize++] = item;
74         }
75
76         type get(uint index) const {
77                 return array[index];
78         }
79
80         type setExpand(uint index, type item) {
81                 type retval = (type) 0;
82                 if (index >= fldsize)
83                         setSize(index + 1);
84                 else
85                         retval = array[index];
86                 set(index, item);
87                 return retval;
88         }
89
90         void set(uint index, type item) {
91                 array[index] = item;
92         }
93
94         uint size() const {
95                 return fldsize;
96         }
97
98         bool isEmpty() const {
99                 return fldsize == 0;
100         }
101
102         ~Vector() {
103                 ourfree(array);
104         }
105
106         void clear() {
107                 fldsize = 0;
108         }
109
110         type *expose() {
111                 return array;
112         }
113         CMEMALLOC;
114 private:
115         uint fldsize;
116         uint capacity;
117         type *array;
118 };
119 #endif