diff --git a/thu_dsa/chp2/Vector.cpp b/thu_dsa/chp2/Vector.cpp index d8e6349..1b61f33 100644 --- a/thu_dsa/chp2/Vector.cpp +++ b/thu_dsa/chp2/Vector.cpp @@ -1,55 +1,8 @@ #include "Vector.h" +#include -//protected methods -template -void Vector::copyfrom(T* const A, int lo, int hi){ - int ix = 0; - while (lo != hi) _elem[ix++] = A[lo++]; -} +using std::cout; +using std::endl; -template -void Vector::copyfrom(Vector& const V, int lo, int hi){ - copyfrom(V._elem, lo, hi); -} -//constructors -template -Vector::Vector() { - _capacity = DEFAULT_CAPACITY; - _size = 0; - _elem = new T[_capacity]; -} -template -Vector::Vector(int capacity) { - _capacity = capacity; - _size = 0; - _elem = new T[_capacity]; -} - -template -Vector::Vector(T* const A, int lo, int hi){ - _capacity = MAX(DEFAULT_CAPACITY, (hi - lo) >> 1); - _size = hi - lo; - copyfrom(A, lo, hi); -} - -template -Vector(Vectort& const V){ - _size = V.getSize(); - _capacity = _size >> 1; - copyfrom(V._elem, 0, _size); -} - -template -Vector::Vector(Vector& const V, int lo, int hi){ - _capacity = MAX(DEFAULT_CAPACITY, (hi - lo) >> 1); - _size = hi - lo; - copyfrom(V, lo, hi); -} - -//read-only interfaces -template -Vector::print(){ - -} diff --git a/thu_dsa/chp2/Vector.h b/thu_dsa/chp2/Vector.h index bca22e3..9829fab 100644 --- a/thu_dsa/chp2/Vector.h +++ b/thu_dsa/chp2/Vector.h @@ -2,8 +2,8 @@ #define VECTOR_H_ #include -#define DEFAULT_CAPACITY 3 +#define DEFAULT_CAPACITY 3 #define MAX(X,Y) ((X)>(Y)?(X):(Y)) #define MIN(X,Y) ((X)<(Y)?(X):(Y)) @@ -19,39 +19,150 @@ protected: //protected methods void copyfrom(T* const A, int lo, int hi); - void copyfrom(Vector& const V, int lo, int hi); + void copyfrom(Vector const &V, int lo, int hi); void expand(); - void shrink(); + //void shrink(); public: //constructors Vector(); Vector(int capacity); - Vector(T* const A, int n); - Vector(T* const A, int lo, int hi); - Vector(Vectort& const V); - Vector(Vector& const V, int lo, int hi); + Vector(T* const A, int n) { copyfrom(A, 0, n); } + Vector(T* const A, int lo, int hi) { copyfrom(A, lo, hi); } + Vector(Vector const &V) { copyfrom(V._elem, 0, V._size); } + Vector(Vector const &V, int lo, int hi) { copyfrom(V._elem, lo, hi); } //deconstructor - ~Vector(); + ~Vector() { delete[]_elem; } //read-only interfaces void print(void); - int find(T& const elem); - int find(T& const elem, int lo, int hi); + int find(T const &elem); + int find(T const &elem, int lo, int hi); int getSize() { return _size; } - int getCapacity(){return _capacity} + int getCapacity() { return _capacity; } + bool empty(){ return _size == 0;} //writable interfaces - T& get(int index); - T& put(int index, T& elem); - T& operator[](int index) const; - int push_back(T& const elem); - int insert(T& const elem); - T pop_back(void); + T& get(int index) { return _elem[index]; } + void replace(int index, T const &elem) { _elem[index] = elem; } + T& operator[](int index) { return get(index); } + Vector& operator=(Vector const &V); + void push_back(T const &elem); + void insert(int pos, T const &elem); + T pop_back(void) { return _elem[--_size]; } T pop(int index); - T pop(int hi, int lo);//return the last element popped - int remove(T& const elem); + T pop(int lo, int hi);//return the last element popped + int remove(T const &elem);//remove first element matched, return index, -1 if not found }; +//protected methods +template +void Vector::copyfrom(T* const A, int lo, int hi) { + _capacity = (hi - lo) << 1; + _size = 0; + _elem = new T[_capacity]; + while (lo != hi) _elem[_size++] = A[lo++]; +} + +template +void Vector::copyfrom(Vector const &V, int lo, int hi) { + copyfrom(V._elem, lo, hi); +} + +template +void Vector::expand(){ + if (_size < _capacity) return; + //else double capacity + T* tmp = _elem; + delete[]_elem; + copyfrom(tmp, 0, _size); +} + +//constructors +template +Vector::Vector() { + _capacity = DEFAULT_CAPACITY; + _size = 0; + _elem = new T[_capacity]; +} + +template +Vector::Vector(int capacity) { + _capacity = capacity; + _size = 0; + _elem = new T[_capacity]; +} + +//read-only interfaces +template +void Vector::print() { + //pass +} + +template +int Vector::find(T const &elem){ + int pos = _size; + while (--pos >= 0 && _elem[pos] != elem); + return pos; +} + +template +int Vector::find(T const &elem, int lo, int hi){ + int pos = hi; + while (--pos >= lo && _elem[pos] != elem); + return pos; +} + + +// writable interfaces +template +Vector& Vector::operator=(Vector const &V) { + if (_elem) delete[]_elem; + copyfrom(V, 0, V._size); + return *this; +} +template +void Vector::push_back(T const &elem){ + expand(); + _elem[_size++] = elem; +} + +//insert before pos +template +void Vector::insert(int pos, T const &elem){ + expand(); + int ix; + for (ix = _size; ix != pos; --ix) + _elem[ix] = _elem[ix - 1]; + ++_size; + _elem[ix] = elem; +} + +template +T Vector::pop(int index){ + T res = _elem[index]; + int ix; + for (ix = index; ix != _size - 1; ++ix) + _elem[ix] = _elem[ix + 1]; + --_size; + return res; +} + +template +T Vector::pop(int lo, int hi){ + T res = _elem[hi - 1]; + while (hi < _size) _elem[lo++] = _elem[hi++]; + _size -= hi - lo; + return res; +} + +template +int Vector::remove(T const &elem){ + int pos = _size; + while (--pos >= 0 && _elem[pos] != elem); + if(pos != -1) pop(pos); + return pos; +} + #endif diff --git a/thu_dsa/chp2/testVector.cpp b/thu_dsa/chp2/testVector.cpp index 77f7dd3..0ccc6ed 100644 --- a/thu_dsa/chp2/testVector.cpp +++ b/thu_dsa/chp2/testVector.cpp @@ -1,15 +1,122 @@ -#include -#include #include "Vector.h" +#include +#include -void test_constructor(){ - cout << "--------------run test_constructo--------------" << endl; - Vector V(); - assert(V.getSize() == 0); +using std::cout; +using std::endl; + +void test_constructor(); +void test_access(); +void test_insert(); +void test_delete(); +void test_find(); + +int main(){ + test_constructor(); + test_access(); + test_insert(); + test_delete(); + test_find(); + + system("pause"); + return 0; +} + +void test_constructor() { + cout << "run test_constructor..."; + Vector V; + assert(V.empty()); assert(V.getCapacity() == DEFAULT_CAPACITY); - + Vector V1(10); + assert(V1.empty()); + assert(V1.getCapacity() == 10); int A[10] = { 2,3,4,6,1,0,9,8,7,5 }; - Vector V(A, 10); -} \ No newline at end of file + Vector V2(A, 10); + assert(V2.getSize() == 10); + assert(V2.getCapacity() == 20); + assert(!V2.empty()); + + Vector V3(A, 3, 7); + assert(V3.getSize() == 4); + assert(V3.getCapacity() == 8); + + Vector V4(V3); + assert(V4.getSize() == 4); + assert(V4.getCapacity() == 8); + + Vector V5(V4, 2, 4); + assert(V5.getSize() == 2); + assert(V5.getCapacity() == 4); + + cout << "test passed." << endl; +} + +void test_access(){ + cout << "run test_access..."; + int A[] = { 2,3,4,6,1,0,9,8,7,5 }; + Vector v(A, 10); + + //test Vector::get method + for (int ix = 0; ix != 10; ++ix) + assert(v.get(ix) == A[ix]); + + //test Vector::replace method + int val = 12; + v.replace(0, 12); + v.replace(5, 12); + assert(v[0] == 12); + assert(v[5] == 12); + + //test [] + v[1] = val; + v[2] = val; + assert(v[1] == val); + assert(v[2] == val); + + cout << "test passed." << endl; +} + +void test_insert(){ + cout << "run test_insert..."; + int A[] = { 2,3,4,6,1,0,9,8,7,5 }; + Vector v(A, 10); + v.push_back(12); + v.insert(2, 15); + assert(v.getSize() == 12); + assert(v.getCapacity() == 20); + assert(v[11] == 12); + assert(v[2] == 15); + + cout << "test passed." << endl; +} + +void test_delete(){ + cout << "run test_delete..."; + int A[] = { 2,3,4,6,1,0,9,8,7,5 }; + Vector v(A, 10); + + assert(v.pop_back() == 5); + assert(v.pop(5) == 0); + assert(v.pop(4, 6) == 9); + assert(v.getSize() == 6); + assert(v.remove(8) == 4); + assert(v.remove(12) == -1); + + cout << "test passed." << endl; +} + +void test_find(){ + cout << "run test_find..."; + int A[] = { 2,3,4,6,1,0,9,8,7,5 }; + Vector v(A, 10); + + assert(v.find(4) == 2); + assert(v.find(5) == 9); + assert(v.find(11) == -1); + assert(v.find(4, 2, 8) == 2); + assert(v.find(4, 5, 8) == 4); + + cout << "test passed." << endl; +}