From 75ae7e0800b275d445d2491454e4fb6132dc8c6f Mon Sep 17 00:00:00 2001 From: Shine wOng <1551885@tongji.edu.cn> Date: Wed, 8 May 2019 11:00:10 +0800 Subject: [PATCH] add Vector class(unfinished --- thu_dsa/chp2/Vector.cpp | 55 +++++++++++++++++++++++++++++++++++ thu_dsa/chp2/Vector.h | 57 +++++++++++++++++++++++++++++++++++++ thu_dsa/chp2/testVector.cpp | 15 ++++++++++ 3 files changed, 127 insertions(+) create mode 100644 thu_dsa/chp2/Vector.cpp create mode 100644 thu_dsa/chp2/Vector.h create mode 100644 thu_dsa/chp2/testVector.cpp diff --git a/thu_dsa/chp2/Vector.cpp b/thu_dsa/chp2/Vector.cpp new file mode 100644 index 0000000..d8e6349 --- /dev/null +++ b/thu_dsa/chp2/Vector.cpp @@ -0,0 +1,55 @@ +#include "Vector.h" + +//protected methods +template +void Vector::copyfrom(T* const A, int lo, int hi){ + int ix = 0; + while (lo != hi) _elem[ix++] = A[lo++]; +} + +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 new file mode 100644 index 0000000..bca22e3 --- /dev/null +++ b/thu_dsa/chp2/Vector.h @@ -0,0 +1,57 @@ +#ifndef VECTOR_H_ +#define VECTOR_H_ + +#include +#define DEFAULT_CAPACITY 3 + +#define MAX(X,Y) ((X)>(Y)?(X):(Y)) +#define MIN(X,Y) ((X)<(Y)?(X):(Y)) + +using std::cout; +using std::endl; + +template +class Vector{ +protected: + T* _elem; + int _size; + int _capacity; + + //protected methods + void copyfrom(T* const A, int lo, int hi); + void copyfrom(Vector& const V, int lo, int hi); + void expand(); + 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); + + //deconstructor + ~Vector(); + + //read-only interfaces + void print(void); + int find(T& const elem); + int find(T& const elem, int lo, int hi); + int getSize() { return _size; } + int getCapacity(){return _capacity} + + //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 pop(int index); + T pop(int hi, int lo);//return the last element popped + int remove(T& const elem); +}; + +#endif diff --git a/thu_dsa/chp2/testVector.cpp b/thu_dsa/chp2/testVector.cpp new file mode 100644 index 0000000..77f7dd3 --- /dev/null +++ b/thu_dsa/chp2/testVector.cpp @@ -0,0 +1,15 @@ +#include +#include +#include "Vector.h" + +void test_constructor(){ + cout << "--------------run test_constructo--------------" << endl; + Vector V(); + assert(V.getSize() == 0); + assert(V.getCapacity() == DEFAULT_CAPACITY); + + + + int A[10] = { 2,3,4,6,1,0,9,8,7,5 }; + Vector V(A, 10); +} \ No newline at end of file