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