#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 {
}
void setExpand(uint index, type item) {
- if (index >= size)
+ if (index >= fldsize)
setSize(index + 1);
set(index, item);
}
array[index] = item;
}
- uint getSize() const {
- return size;
+ uint size() const {
+ return fldsize;
+ }
+
+ bool isEmpty() const {
+ return fldsize == 0;
}
~Vector() {
}
void clear() {
- size = 0;
+ fldsize = 0;
}
type *expose() {
}
CMEMALLOC;
private:
- uint size;
+ uint fldsize;
uint capacity;
type *array;
};