edits
[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         void removeIndex(uint i) {
46                 for (i++; i < fldsize; i++) {
47                         array[i - 1] = array[i];
48                 }
49                 fldsize--;
50         }
51
52         type last() const {
53                 return array[fldsize - 1];
54         }
55
56         void setSize(uint _size) {
57                 if (_size <= fldsize) {
58                         fldsize = _size;
59                         return;
60                 } else if (_size > capacity) {
61                         array = (type *)ourrealloc(array, _size * sizeof(type));
62                         capacity = _size;
63                 }
64                 bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
65                 fldsize = _size;
66         }
67
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));
72         }
73
74         void removeAll(Vector<type> *v) {
75                 uint vsize = v->size();
76                 for (uint i = 0; i < vsize; i++)
77                         remove(v->get(i));
78         }
79
80         void add(type item) {
81                 if (fldsize >= capacity) {
82                         uint newcap = capacity << 1;
83                         array = (type *)ourrealloc(array, newcap * sizeof(type));
84                         capacity = newcap;
85                 }
86                 array[fldsize++] = item;
87         }
88
89         type lastElement() {
90                 return get(size() - 1);
91         }
92
93         type firstElement() {
94                 return get(0);
95         }
96
97         type get(uint index) const {
98                 return array[index];
99         }
100
101         type setExpand(uint index, type item) {
102                 type retval = (type) 0;
103                 if (index >= fldsize)
104                         setSize(index + 1);
105                 else
106                         retval = array[index];
107                 set(index, item);
108                 return retval;
109         }
110
111         void set(uint index, type item) {
112                 array[index] = item;
113         }
114
115         uint size() const {
116                 return fldsize;
117         }
118
119         bool isEmpty() const {
120                 return fldsize == 0;
121         }
122
123         ~Vector() {
124                 ourfree(array);
125         }
126
127         void clear() {
128                 fldsize = 0;
129         }
130
131         type *expose() {
132                 return array;
133         }
134         CMEMALLOC;
135 private:
136         uint fldsize;
137         uint capacity;
138         type *array;
139 };
140 #endif