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         type last() const {
34                 return array[fldsize - 1];
35         }
36
37         void setSize(uint _size) {
38                 if (_size <= fldsize) {
39                         fldsize = _size;
40                         return;
41                 } else if (_size > capacity) {
42                         array = (type *)ourrealloc(array, _size * sizeof(type));
43                         capacity = _size;
44                 }
45                 bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
46                 fldsize = _size;
47         }
48
49         void addAll(Vector<type> *v) {
50                 int oldsize = fldsize;
51                 setSize(fldsize + v->fldsize);
52                 memcpy(&array[fldsize], v->array, v->fldsize * sizeof(type));
53         }
54
55         void add(type item) {
56                 if (fldsize >= capacity) {
57                         uint newcap = capacity << 1;
58                         array = (type *)ourrealloc(array, newcap * sizeof(type));
59                         capacity = newcap;
60                 }
61                 array[fldsize++] = item;
62         }
63
64         type get(uint index) const {
65                 return array[index];
66         }
67
68         void setExpand(uint index, type item) {
69                 if (index >= fldsize)
70                         setSize(index + 1);
71                 set(index, item);
72         }
73
74         void set(uint index, type item) {
75                 array[index] = item;
76         }
77
78         uint size() const {
79                 return fldsize;
80         }
81
82         bool isEmpty() const {
83                 return fldsize == 0;
84         }
85
86         ~Vector() {
87                 ourfree(array);
88         }
89
90         void clear() {
91                 fldsize = 0;
92         }
93
94         type *expose() {
95                 return array;
96         }
97         CMEMALLOC;
98 private:
99         uint fldsize;
100         uint capacity;
101         type *array;
102 };
103 #endif