add basic vector operation, test passed.

This commit is contained in:
Shine wOng
2019-05-08 20:43:35 +08:00
parent 75ae7e0800
commit 1467421e9c
3 changed files with 249 additions and 78 deletions

View File

@@ -1,55 +1,8 @@
#include "Vector.h"
#include <iostream>
//protected methods
template <typename T>
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 <typename T>
void Vector::copyfrom(Vector<T>& const V, int lo, int hi){
copyfrom(V._elem, lo, hi);
}
//constructors
template<typename T>
Vector::Vector() {
_capacity = DEFAULT_CAPACITY;
_size = 0;
_elem = new T[_capacity];
}
template<typename T>
Vector::Vector(int capacity) {
_capacity = capacity;
_size = 0;
_elem = new T[_capacity];
}
template<typename T>
Vector::Vector(T* const A, int lo, int hi){
_capacity = MAX(DEFAULT_CAPACITY, (hi - lo) >> 1);
_size = hi - lo;
copyfrom(A, lo, hi);
}
template<typename T>
Vector(Vectort<T>& const V){
_size = V.getSize();
_capacity = _size >> 1;
copyfrom(V._elem, 0, _size);
}
template<typename T>
Vector::Vector(Vector<T>& const V, int lo, int hi){
_capacity = MAX(DEFAULT_CAPACITY, (hi - lo) >> 1);
_size = hi - lo;
copyfrom(V, lo, hi);
}
//read-only interfaces
template<typename T>
Vector::print(){
}

View File

@@ -2,8 +2,8 @@
#define VECTOR_H_
#include <iostream>
#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<T>& const V, int lo, int hi);
void copyfrom(Vector<T> 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<T>& const V);
Vector(Vector<T>& 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<T> const &V) { copyfrom(V._elem, 0, V._size); }
Vector(Vector<T> 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<T>& operator=(Vector<T> 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 <typename T>
void Vector<T>::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 <typename T>
void Vector<T>::copyfrom(Vector<T> const &V, int lo, int hi) {
copyfrom(V._elem, lo, hi);
}
template<typename T>
void Vector<T>::expand(){
if (_size < _capacity) return;
//else double capacity
T* tmp = _elem;
delete[]_elem;
copyfrom(tmp, 0, _size);
}
//constructors
template<typename T>
Vector<T>::Vector() {
_capacity = DEFAULT_CAPACITY;
_size = 0;
_elem = new T[_capacity];
}
template<typename T>
Vector<T>::Vector(int capacity) {
_capacity = capacity;
_size = 0;
_elem = new T[_capacity];
}
//read-only interfaces
template<typename T>
void Vector<T>::print() {
//pass
}
template<typename T>
int Vector<T>::find(T const &elem){
int pos = _size;
while (--pos >= 0 && _elem[pos] != elem);
return pos;
}
template<typename T>
int Vector<T>::find(T const &elem, int lo, int hi){
int pos = hi;
while (--pos >= lo && _elem[pos] != elem);
return pos;
}
// writable interfaces
template<typename T>
Vector<T>& Vector<T>::operator=(Vector<T> const &V) {
if (_elem) delete[]_elem;
copyfrom(V, 0, V._size);
return *this;
}
template<typename T>
void Vector<T>::push_back(T const &elem){
expand();
_elem[_size++] = elem;
}
//insert before pos
template<typename T>
void Vector<T>::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<typename T>
T Vector<T>::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<typename T>
T Vector<T>::pop(int lo, int hi){
T res = _elem[hi - 1];
while (hi < _size) _elem[lo++] = _elem[hi++];
_size -= hi - lo;
return res;
}
template<typename T>
int Vector<T>::remove(T const &elem){
int pos = _size;
while (--pos >= 0 && _elem[pos] != elem);
if(pos != -1) pop(pos);
return pos;
}
#endif

View File

@@ -1,15 +1,122 @@
#include <cassert>
#include <time.h>
#include "Vector.h"
#include <cassert>
#include <iostream>
void test_constructor(){
cout << "--------------run test_constructo--------------" << endl;
Vector<int> 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<int> V;
assert(V.empty());
assert(V.getCapacity() == DEFAULT_CAPACITY);
Vector<int> V1(10);
assert(V1.empty());
assert(V1.getCapacity() == 10);
int A[10] = { 2,3,4,6,1,0,9,8,7,5 };
Vector<int> V(A, 10);
}
Vector<int> V2(A, 10);
assert(V2.getSize() == 10);
assert(V2.getCapacity() == 20);
assert(!V2.empty());
Vector<int> V3(A, 3, 7);
assert(V3.getSize() == 4);
assert(V3.getCapacity() == 8);
Vector<int> V4(V3);
assert(V4.getSize() == 4);
assert(V4.getCapacity() == 8);
Vector<int> 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<int> v(A, 10);
//test Vector<T>::get method
for (int ix = 0; ix != 10; ++ix)
assert(v.get(ix) == A[ix]);
//test Vector<T>::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<int> 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<int> 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<int> 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;
}