Improvements in multiple libraries

- Inheritance improvement
- library 5 meets all the requirements
This commit is contained in:
Alessandro Ferro 2021-06-09 17:35:27 +02:00
parent ed5f80bb78
commit 4d524d6df2
7 changed files with 77 additions and 51 deletions

View File

@ -77,7 +77,7 @@ bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
template <typename Data> template <typename Data>
void Vector<Data>::Resize(const ulong newsize){ void Vector<Data>::Resize(const ulong newsize){
if(newsize == 0){ if(newsize == 0){
Clear(); Vector<Data>::Clear();
} }
else if(size != newsize){ else if(size != newsize){
ulong limit = (size < newsize) ? size : newsize; ulong limit = (size < newsize) ? size : newsize;

View File

@ -77,7 +77,7 @@ bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
template <typename Data> template <typename Data>
void Vector<Data>::Resize(const ulong newsize){ void Vector<Data>::Resize(const ulong newsize){
if(newsize == 0){ if(newsize == 0){
Clear(); Vector<Data>::Clear();
} }
else if(size != newsize){ else if(size != newsize){
ulong limit = (size < newsize) ? size : newsize; ulong limit = (size < newsize) ? size : newsize;

View File

@ -77,7 +77,7 @@ bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
template <typename Data> template <typename Data>
void Vector<Data>::Resize(const ulong newsize){ void Vector<Data>::Resize(const ulong newsize){
if(newsize == 0){ if(newsize == 0){
Clear(); Vector<Data>::Clear();
} }
else if(size != newsize){ else if(size != newsize){
ulong limit = (size < newsize) ? size : newsize; ulong limit = (size < newsize) ? size : newsize;

View File

@ -77,7 +77,7 @@ bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
template <typename Data> template <typename Data>
void Vector<Data>::Resize(const ulong newsize){ void Vector<Data>::Resize(const ulong newsize){
if(newsize == 0){ if(newsize == 0){
Clear(); Vector<Data>::Clear();
} }
else if(size != newsize){ else if(size != newsize){
ulong limit = (size < newsize) ? size : newsize; ulong limit = (size < newsize) ? size : newsize;

View File

@ -77,7 +77,7 @@ bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
template <typename Data> template <typename Data>
void Vector<Data>::Resize(const ulong newsize){ void Vector<Data>::Resize(const ulong newsize){
if(newsize == 0){ if(newsize == 0){
Clear(); Vector<Data>::Clear(); //Vector<Data>::Clear() instead of Clear() otherwise it'd call the Matrix's implementation.
} }
else if(size != newsize){ else if(size != newsize){
ulong limit = (size < newsize) ? size : newsize; ulong limit = (size < newsize) ? size : newsize;

View File

@ -65,39 +65,33 @@ void UseChosenType(Implementation chosenImplementation, DataType chosenDataType)
if(chosenImplementation == Implementation::vector){ if(chosenImplementation == Implementation::vector){
if(chosenDataType == DataType::integer){ if(chosenDataType == DataType::integer){
MatrixVec<int> mtx; MatrixVec<int> mtx;
mtx = GenerateIntegerMat(mtx); mtx = GenerateIntegerMat<MatrixVec<int>>();
cout<<"\nThe matrix has been randomly generated.\n";
IntegerFunctions(mtx); IntegerFunctions(mtx);
}else if(chosenDataType == DataType::ffloat){ }else if(chosenDataType == DataType::ffloat){
MatrixVec<float> mtx; MatrixVec<float> mtx;
mtx = GenerateFloatMat(mtx); mtx = GenerateFloatMat<MatrixVec<float>>();
cout<<"\nThe matrix has been randomly generated.\n";
FloatFunctions(mtx); FloatFunctions(mtx);
}else if(chosenDataType == DataType::sstring){ }else if(chosenDataType == DataType::sstring){
MatrixVec<string> mtx; MatrixVec<string> mtx;
mtx = GenerateStringsMat(mtx); mtx = GenerateStringsMat<MatrixVec<string>>();
cout<<"\nThe matrix has been randomly generated.\n";
StringFunctions(mtx); StringFunctions(mtx);
} }
}else if(chosenImplementation == Implementation::yale){ }else if(chosenImplementation == Implementation::yale){
if(chosenDataType == DataType::integer){ if(chosenDataType == DataType::integer){
MatrixCSR<int> mtx; MatrixCSR<int> mtx;
mtx = GenerateIntegerMat(mtx); mtx = GenerateIntegerMat<MatrixCSR<int>>();
cout<<"\nThe matrix has been randomly generated.\n";
IntegerFunctions(mtx); IntegerFunctions(mtx);
}else if(chosenDataType == DataType::ffloat){ }else if(chosenDataType == DataType::ffloat){
MatrixCSR<float> mtx; MatrixCSR<float> mtx;
mtx = GenerateFloatMat(mtx); mtx = GenerateFloatMat<MatrixCSR<float>>();
cout<<"\nThe matrix has been randomly generated.\n";
FloatFunctions(mtx); FloatFunctions(mtx);
}else if(chosenDataType == DataType::sstring){ }else if(chosenDataType == DataType::sstring){
MatrixCSR<string> mtx; MatrixCSR<string> mtx;
mtx = GenerateStringsMat(mtx); mtx = GenerateStringsMat<MatrixCSR<string>>();
cout<<"\nThe matrix has been randomly generated.\n";
StringFunctions(mtx); StringFunctions(mtx);
} }
} }
@ -479,85 +473,108 @@ void Print(T& mat){
/* ----- generator functions ----- */ /* ----- generator functions ----- */
template <typename T> template <typename T>
T GenerateIntegerMat(T& mat){ T GenerateIntegerMat(){
default_random_engine gen(random_device{}()); default_random_engine gen(random_device{}());
uniform_int_distribution<int> random_dimension(1,20); // generates the dimensions of the matrix uniform_int_distribution<int> random_dimension(1,20); // generates the dimensions of the matrix
uniform_int_distribution<int> dist(0,100); // generates values inside the matrix uniform_int_distribution<int> dist(0,100); // generates values inside the matrix
ulong n_columns = random_dimension(gen); ulong n_columns = random_dimension(gen);
ulong n_rows = random_dimension(gen); ulong n_rows = random_dimension(gen);
ulong row, column, elements_to_insert;
T matrix(n_rows, n_columns); T matrix(n_rows, n_columns);
MatrixCSR<bool> aux_matrix(n_rows, n_columns); //avoids to insert twice (or more) an element in a position
uniform_int_distribution<int> n_elements_to_insert(0,(n_columns*n_rows)); // generates how many elements to insert cout<<" A "<<n_rows<<" x "<<n_columns<<" matrix has been generated.\n";
ulong elements_to_insert = n_elements_to_insert(gen); do{
elements_to_insert = getDimension();
ulong row,column; }while(elements_to_insert > n_rows*n_columns);
for(ulong i=1 ; i<=elements_to_insert ; ++i){ for(ulong i=1 ; i<=elements_to_insert ; ++i){
do{ do{
row = random_dimension(gen)-1; do{
}while(row >= n_rows); row = random_dimension(gen)-1;
}while(row >= n_rows);
do{ do{
column = random_dimension(gen)-1; column = random_dimension(gen)-1;
}while(column >= n_columns); }while(column >= n_columns);
}while(aux_matrix.ExistsCell(row,column));
aux_matrix(row,column) = true;
matrix(row,column) = dist(gen); matrix(row,column) = dist(gen);
} }
return matrix; return matrix;
} }
template <typename T> template <typename T>
T GenerateFloatMat(T& mat){ T GenerateFloatMat(){
default_random_engine gen(random_device{}()); default_random_engine gen(random_device{}());
uniform_int_distribution<int> random_dimension(1,20); uniform_int_distribution<int> random_dimension(1,20);
uniform_real_distribution<double> dist(0,9); uniform_real_distribution<double> dist(0,9);
ulong n_columns = random_dimension(gen); ulong n_columns = random_dimension(gen);
ulong n_rows = random_dimension(gen); ulong n_rows = random_dimension(gen);
ulong row, column, elements_to_insert;
T matrix(n_rows, n_columns); T matrix(n_rows, n_columns);
MatrixCSR<bool> aux_matrix(n_rows, n_columns); //avoids to insert twice (or more) an element in a position
uniform_int_distribution<int> n_elements_to_insert(0,(n_columns*n_rows)); cout<<" A "<<n_rows<<" x "<<n_columns<<" matrix has been generated.\n";
ulong elements_to_insert = n_elements_to_insert(gen); do{
elements_to_insert = getDimension();
ulong row,column; }while(elements_to_insert > n_rows*n_columns);
for(ulong i=1 ; i<=elements_to_insert ; ++i){ for(ulong i=1 ; i<=elements_to_insert ; ++i){
do{ do{
row = random_dimension(gen)-1; do{
}while(row >= n_rows); row = random_dimension(gen)-1;
do{ }while(row >= n_rows);
column = random_dimension(gen)-1;
}while(column >= n_columns); do{
column = random_dimension(gen)-1;
}while(column >= n_columns);
}while(aux_matrix.ExistsCell(row,column));
aux_matrix(row,column) = true;
matrix(row,column) = dist(gen); matrix(row,column) = dist(gen);
} }
return matrix; return matrix;
} }
template <typename T> template <typename T>
T GenerateStringsMat(T& mat){ T GenerateStringsMat(){
default_random_engine gen(random_device{}()); default_random_engine gen(random_device{}());
uniform_int_distribution<int> random_dimension(1,20); uniform_int_distribution<int> random_dimension(1,20);
uniform_int_distribution<int> dist(1,5); uniform_int_distribution<int> dist(1,5);
ulong n_columns = random_dimension(gen); ulong n_columns = random_dimension(gen);
ulong n_rows = random_dimension(gen); ulong n_rows = random_dimension(gen);
ulong row, column, elements_to_insert;
T matrix(n_rows, n_columns); T matrix(n_rows, n_columns);
MatrixCSR<bool> aux_matrix(n_rows, n_columns); //avoids to insert twice (or more) an element in a position
uniform_int_distribution<int> n_elements_to_insert(0,(n_columns*n_rows)); cout<<" A "<<n_rows<<" x "<<n_columns<<" matrix has been generated.\n";
ulong elements_to_insert = n_elements_to_insert(gen);
ulong row,column; do{
elements_to_insert = getDimension();
}while(elements_to_insert > n_rows*n_columns);
for(ulong i=1 ; i<=elements_to_insert ; ++i){ for(ulong i=1 ; i<=elements_to_insert ; ++i){
do{ do{
row = random_dimension(gen)-1; do{
}while(row >= n_rows); row = random_dimension(gen)-1;
do{ }while(row >= n_rows);
column = random_dimension(gen)-1;
}while(column >= n_columns); do{
column = random_dimension(gen)-1;
}while(column >= n_columns);
}while(aux_matrix.ExistsCell(row,column));
aux_matrix(row,column) = true;
matrix(row,column) = generateRandomString(dist(gen)); matrix(row,column) = generateRandomString(dist(gen));
} }
return matrix; return matrix;
@ -574,3 +591,10 @@ string generateRandomString(ulong dim){
newString[dim]='\0'; newString[dim]='\0';
return newString; return newString;
} }
ulong getDimension(){
ulong dimension;
std::cout<<" How many elements you'd like to insert? ";
std::cin>>dimension;
return dimension;
}

View File

@ -89,18 +89,20 @@ void Read(const T&);
template <typename T> template <typename T>
void Resize(T& mtx); void Resize(T& mtx);
ulong getDimension();
/* ----- generator functions ----- */ /* ----- generator functions ----- */
void UseChosenType(Implementation, DataType); void UseChosenType(Implementation, DataType);
template <typename T> template <typename T>
T GenerateIntegerMat(T&); T GenerateIntegerMat();
template <typename T> template <typename T>
T GenerateFloatMat(T&); T GenerateFloatMat();
template <typename T> template <typename T>
T GenerateStringsMat(T&); T GenerateStringsMat();
std::string generateRandomString(ulong); std::string generateRandomString(ulong);