edits
[iotcloud.git] / version2 / src / C / vector.h
index 2d4cc1956df10579e362966c84371f4f34d5c63f..00ff96281c3d8f965db3c32f72bfebca504973b9 100644 (file)
@@ -1,52 +1,64 @@
 #ifndef CPPVECTOR_H
 #define CPPVECTOR_H
 #include <string.h>
-
 #define VECTOR_DEFCAP 8
 
 template<typename type>
 class Vector {
 public:
        Vector(uint _capacity = VECTOR_DEFCAP) :
-               size(0),
+               fldsize(0),
                capacity(_capacity),
                array((type *) ourmalloc(sizeof(type) * _capacity)) {
        }
 
        Vector(uint _capacity, type *_array)  :
-               size(_capacity),
+               fldsize(_capacity),
                capacity(_capacity),
                array((type *) ourmalloc(sizeof(type) * _capacity)) {
                memcpy(array, _array, capacity * sizeof(type));
        }
 
+       Vector(Vector<type> *v) :
+               fldsize(v->fldsize),
+               capacity(v->capacity),
+               array((type *) ourmalloc(sizeof(type) * v->capacity)) {
+               memcpy(array, v->array, capacity * sizeof(type));
+       }
+
        void pop() {
-               size--;
+               fldsize--;
        }
 
        type last() const {
-               return array[size - 1];
+               return array[fldsize - 1];
        }
 
        void setSize(uint _size) {
-               if (_size <= size) {
-                       size = _size;
+               if (_size <= fldsize) {
+                       fldsize = _size;
                        return;
                } else if (_size > capacity) {
                        array = (type *)ourrealloc(array, _size * sizeof(type));
                        capacity = _size;
                }
-               bzero(&array[size], (_size - size) * sizeof(type));
-               size = _size;
+               bzero(&array[fldsize], (_size - fldsize) * sizeof(type));
+               fldsize = _size;
        }
 
-       void push(type item) {
-               if (size >= capacity) {
+       void addAll(Vector<type> *v) {
+               int oldsize = fldsize;
+               setSize(fldsize + v->fldsize);
+               memcpy(&array[fldsize], v->array, v->fldsize * sizeof(type));
+       }
+
+       void add(type item) {
+               if (fldsize >= capacity) {
                        uint newcap = capacity << 1;
                        array = (type *)ourrealloc(array, newcap * sizeof(type));
                        capacity = newcap;
                }
-               array[size++] = item;
+               array[fldsize++] = item;
        }
 
        type get(uint index) const {
@@ -54,7 +66,7 @@ public:
        }
 
        void setExpand(uint index, type item) {
-               if (index >= size)
+               if (index >= fldsize)
                        setSize(index + 1);
                set(index, item);
        }
@@ -63,8 +75,12 @@ public:
                array[index] = item;
        }
 
-       uint getSize() const {
-               return size;
+       uint size() const {
+               return fldsize;
+       }
+
+       bool isEmpty() const {
+               return fldsize == 0;
        }
 
        ~Vector() {
@@ -72,7 +88,7 @@ public:
        }
 
        void clear() {
-               size = 0;
+               fldsize = 0;
        }
 
        type *expose() {
@@ -80,7 +96,7 @@ public:
        }
        CMEMALLOC;
 private:
-       uint size;
+       uint fldsize;
        uint capacity;
        type *array;
 };