Bug fixes + tabbing
[iotcloud.git] / version2 / src / C / vector.h
index 5a7770d2b725cb959ad5679b84b101c589aa374d..e1378c900e0c447b7b70413f3130024eddc6e4f8 100644 (file)
@@ -7,80 +7,117 @@ 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) :
-               size(v->size),
+               fldsize(v->fldsize),
                capacity(v->capacity),
                array((type *) ourmalloc(sizeof(type) * v->capacity)) {
                memcpy(array, v->array, capacity * sizeof(type));
        }
 
        void pop() {
-               size--;
+               fldsize--;
+       }
+
+       void remove(type t) {
+               for (uint i = 0; i < fldsize; i++) {
+                       if (array[i] == t) {
+                               for (i++; i < fldsize; i++) {
+                                       array[i - 1] = array[i];
+                               }
+                               fldsize--;
+                               break;
+                       }
+               }
+       }
+
+       void removeIndex(uint i) {
+               for (i++; i < fldsize; i++) {
+                       array[i - 1] = array[i];
+               }
+               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 addAll(Vector<type> *v) {
-               int oldsize = size;
-               setSize(size + v->size);
-               memcpy(&array[size], v->array, v->size * sizeof(type));
+               int oldsize = fldsize;
+               setSize(fldsize + v->fldsize);
+               memcpy(&array[fldsize], v->array, v->fldsize * sizeof(type));
+       }
+
+       void removeAll(Vector<type> *v) {
+               uint vsize = v->size();
+               for (uint i = 0; i < vsize; i++)
+                       remove(v->get(i));
        }
 
        void add(type item) {
-               if (size >= capacity) {
+               if (fldsize >= capacity) {
                        uint newcap = capacity << 1;
                        array = (type *)ourrealloc(array, newcap * sizeof(type));
                        capacity = newcap;
                }
-               array[size++] = item;
+               array[fldsize++] = item;
+       }
+
+       type lastElement() {
+               return get(size() - 1);
+       }
+
+       type firstElement() {
+               return get(0);
        }
 
        type get(uint index) const {
                return array[index];
        }
 
-       void setExpand(uint index, type item) {
-               if (index >= size)
+       type setExpand(uint index, type item) {
+               type retval = (type) 0;
+               if (index >= fldsize)
                        setSize(index + 1);
+               else
+                       retval = array[index];
                set(index, item);
+               return retval;
        }
 
        void set(uint index, type item) {
                array[index] = item;
        }
 
-       uint getSize() const {
-               return size;
+       uint size() const {
+               return fldsize;
        }
 
        bool isEmpty() const {
-               return size == 0;
+               return fldsize == 0;
        }
 
        ~Vector() {
@@ -88,7 +125,7 @@ public:
        }
 
        void clear() {
-               size = 0;
+               fldsize = 0;
        }
 
        type *expose() {
@@ -96,7 +133,7 @@ public:
        }
        CMEMALLOC;
 private:
-       uint size;
+       uint fldsize;
        uint capacity;
        type *array;
 };