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 add(type item) {
75                 if (fldsize >= capacity) {
76                         uint newcap = capacity << 1;
77                         array = (type *)ourrealloc(array, newcap * sizeof(type));
78                         capacity = newcap;
79                 }
80                 array[fldsize++] = item;
81         }
82
83         type lastElement() {
84                 return get(size()-1);
85         }
86
87         type firstElement() {
88                 return get(0);
89         }
90
91         type get(uint index) const {
92                 return array[index];
93         }
94
95         type setExpand(uint index, type item) {
96                 type retval = (type) 0;
97                 if (index >= fldsize)
98                         setSize(index + 1);
99                 else
100                         retval = array[index];
101                 set(index, item);
102                 return retval;
103         }
104
105         void set(uint index, type item) {
106                 array[index] = item;
107         }
108
109         uint size() const {
110                 return fldsize;
111         }
112
113         bool isEmpty() const {
114                 return fldsize == 0;
115         }
116
117         ~Vector() {
118                 ourfree(array);
119         }
120
121         void clear() {
122                 fldsize = 0;
123         }
124
125         type *expose() {
126                 return array;
127         }
128         CMEMALLOC;
129 private:
130         uint fldsize;
131         uint capacity;
132         type *array;
133 };
134 #endif