/*                      This is the implementation of the CLASS.   Return to // vector.                    */

// vector.cpp

// Robert Simpson

// unit1

// 12 feb. 1998

 

// this file is the implementation of the rational number

// class 'vector'. all the functions for the vector.h

// file are in this file.

#include "vector.h"

#include <assert.h>

void arraycopy (double*,const double*,int);

// friend function

void swap(vector & v1,vector & v2)

{

int temp=v1.currentsize;

v1.currentsize=v2.currentsize;

v2.currentsize=temp;

double *tempstr=v1.array;

v1.array=v2.array;

v2.array=tempstr;

int tempmax=v1.maxsize;

v1.maxsize=v2.maxsize;

v2.maxsize=tempmax;

}

// default constructor

vector::vector ()

{

currentsize=0;

maxsize=default_capacity;

array=new double[maxsize];

}

// constructor

vector::vector (int n,double x)

{

currentsize=n ;

maxsize=(currentsize);

array=new double[maxsize];

for (int count=0;count < currentsize;count++)

array[count]=x;

}

// copy constructor

vector::vector (const vector & right)

{

currentsize=right.currentsize;

new double [currentsize];

arraycopy(array,right.array,currentsize);

}

 

// destructor

vector::~vector()

{

delete [] array;

}

// array size

int vector::size ()const

{

return (currentsize);

}

// isempty

int vector::empty()const

{

return (currentsize==0);

}

// begining of array

double *vector::begin()const

{

return array ;

}

// end of array

double *vector::end()const

{

double *temp=array;

temp=temp+currentsize;

return temp;

}

// operator=

vector & vector::operator = (const vector & right)

{

if (this != & right){

currentsize=right.currentsize;

maxsize=right.maxsize;

delete [] array;

array=new double [currentsize];

arraycopy(array,right.array,currentsize);

}

return *this;

}

 

// double & operator

double & vector::operator [] (int n)

{

return array[n];

}

 

// operator==

int vector::operator == (const vector & v)const

{

if (currentsize!=v.currentsize)

return 0;

for(int x=0;x<currentsize;x++)

if (array[x]!=v.array[x])

return 0;

return 1;

}

// operator<

int vector::operator < (const vector & v)const

{

for(int x=0;x<currentsize;x++)

if (array[x] >= v.array[x])

return 0;

if (currentsize >= v.currentsize)

return 0;

return 1;

}

// front

double & vector::front()

{

assert(! empty());

return array[0];

}

// back

double & vector::back()

{

assert (! empty());

return array[currentsize-1];

}

// push_back

void vector::push_back(double x)

{

if (currentsize=maxsize){

maxsize=maxsize*2;

double *temp;

temp=array;

array=new double[maxsize];

arraycopy(temp,array,currentsize);

currentsize++;

}

array[currentsize]=x;

}

// pop_back

void vector::pop_back()

{

assert(!empty());

currentsize--;

}

// copy function

void arraycopy(double *array,const double *right,int size)

{

while (size >=0){

array[size] = right[size];

size--;

}

}

                                                                      Robert's Home Page