tabbing
[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                 size(0),
11                 capacity(_capacity),
12                 array((type *) ourmalloc(sizeof(type) * _capacity)) {
13         }
14
15         Vector(uint _capacity, type *_array)  :
16                 size(_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                 size(v->size),
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                 size--;
31         }
32
33         type last() const {
34                 return array[size - 1];
35         }
36
37         void setSize(uint _size) {
38                 if (_size <= size) {
39                         size = _size;
40                         return;
41                 } else if (_size > capacity) {
42                         array = (type *)ourrealloc(array, _size * sizeof(type));
43                         capacity = _size;
44                 }
45                 bzero(&array[size], (_size - size) * sizeof(type));
46                 size = _size;
47         }
48
49         void addAll(Vector<type> *v) {
50                 int oldsize = size;
51                 setSize(size + v->size);
52                 memcpy(&array[size], v->array, v->size * sizeof(type));
53         }
54
55         void add(type item) {
56                 if (size >= capacity) {
57                         uint newcap = capacity << 1;
58                         array = (type *)ourrealloc(array, newcap * sizeof(type));
59                         capacity = newcap;
60                 }
61                 array[size++] = 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 >= size)
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 getSize() const {
79                 return size;
80         }
81
82         bool isEmpty() const {
83                 return size == 0;
84         }
85
86         ~Vector() {
87                 ourfree(array);
88         }
89
90         void clear() {
91                 size = 0;
92         }
93
94         type *expose() {
95                 return array;
96         }
97         CMEMALLOC;
98 private:
99         uint size;
100         uint capacity;
101         type *array;
102 };
103 #endif