From 4d524d6df2c31d4a68903ad88f619c73c1d0e29b Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 9 Jun 2021 17:35:27 +0200 Subject: [PATCH] Improvements in multiple libraries - Inheritance improvement - library 5 meets all the requirements --- librerie/exercise1/vector/vector.cpp | 2 +- librerie/exercise2/vector/vector.cpp | 2 +- librerie/exercise3/vector/vector.cpp | 2 +- librerie/exercise4/vector/vector.cpp | 2 +- librerie/exercise5/vector/vector.cpp | 2 +- librerie/exercise5/zmytest/test.cpp | 110 ++++++++++++++++----------- librerie/exercise5/zmytest/test.hpp | 8 +- 7 files changed, 77 insertions(+), 51 deletions(-) diff --git a/librerie/exercise1/vector/vector.cpp b/librerie/exercise1/vector/vector.cpp index a7b3d5e..7d02f20 100644 --- a/librerie/exercise1/vector/vector.cpp +++ b/librerie/exercise1/vector/vector.cpp @@ -77,7 +77,7 @@ bool Vector::operator!=(const Vector& vec)const noexcept{ template void Vector::Resize(const ulong newsize){ if(newsize == 0){ - Clear(); + Vector::Clear(); } else if(size != newsize){ ulong limit = (size < newsize) ? size : newsize; diff --git a/librerie/exercise2/vector/vector.cpp b/librerie/exercise2/vector/vector.cpp index a7b3d5e..7d02f20 100644 --- a/librerie/exercise2/vector/vector.cpp +++ b/librerie/exercise2/vector/vector.cpp @@ -77,7 +77,7 @@ bool Vector::operator!=(const Vector& vec)const noexcept{ template void Vector::Resize(const ulong newsize){ if(newsize == 0){ - Clear(); + Vector::Clear(); } else if(size != newsize){ ulong limit = (size < newsize) ? size : newsize; diff --git a/librerie/exercise3/vector/vector.cpp b/librerie/exercise3/vector/vector.cpp index a7b3d5e..7d02f20 100644 --- a/librerie/exercise3/vector/vector.cpp +++ b/librerie/exercise3/vector/vector.cpp @@ -77,7 +77,7 @@ bool Vector::operator!=(const Vector& vec)const noexcept{ template void Vector::Resize(const ulong newsize){ if(newsize == 0){ - Clear(); + Vector::Clear(); } else if(size != newsize){ ulong limit = (size < newsize) ? size : newsize; diff --git a/librerie/exercise4/vector/vector.cpp b/librerie/exercise4/vector/vector.cpp index a7b3d5e..7d02f20 100644 --- a/librerie/exercise4/vector/vector.cpp +++ b/librerie/exercise4/vector/vector.cpp @@ -77,7 +77,7 @@ bool Vector::operator!=(const Vector& vec)const noexcept{ template void Vector::Resize(const ulong newsize){ if(newsize == 0){ - Clear(); + Vector::Clear(); } else if(size != newsize){ ulong limit = (size < newsize) ? size : newsize; diff --git a/librerie/exercise5/vector/vector.cpp b/librerie/exercise5/vector/vector.cpp index a7b3d5e..21404af 100755 --- a/librerie/exercise5/vector/vector.cpp +++ b/librerie/exercise5/vector/vector.cpp @@ -77,7 +77,7 @@ bool Vector::operator!=(const Vector& vec)const noexcept{ template void Vector::Resize(const ulong newsize){ if(newsize == 0){ - Clear(); + Vector::Clear(); //Vector::Clear() instead of Clear() otherwise it'd call the Matrix's implementation. } else if(size != newsize){ ulong limit = (size < newsize) ? size : newsize; diff --git a/librerie/exercise5/zmytest/test.cpp b/librerie/exercise5/zmytest/test.cpp index 50aabc8..6301f4e 100755 --- a/librerie/exercise5/zmytest/test.cpp +++ b/librerie/exercise5/zmytest/test.cpp @@ -65,39 +65,33 @@ void UseChosenType(Implementation chosenImplementation, DataType chosenDataType) if(chosenImplementation == Implementation::vector){ if(chosenDataType == DataType::integer){ MatrixVec mtx; - mtx = GenerateIntegerMat(mtx); - cout<<"\nThe matrix has been randomly generated.\n"; + mtx = GenerateIntegerMat>(); IntegerFunctions(mtx); }else if(chosenDataType == DataType::ffloat){ MatrixVec mtx; - mtx = GenerateFloatMat(mtx); - cout<<"\nThe matrix has been randomly generated.\n"; + mtx = GenerateFloatMat>(); FloatFunctions(mtx); }else if(chosenDataType == DataType::sstring){ MatrixVec mtx; - mtx = GenerateStringsMat(mtx); - cout<<"\nThe matrix has been randomly generated.\n"; + mtx = GenerateStringsMat>(); StringFunctions(mtx); } }else if(chosenImplementation == Implementation::yale){ if(chosenDataType == DataType::integer){ MatrixCSR mtx; - mtx = GenerateIntegerMat(mtx); - cout<<"\nThe matrix has been randomly generated.\n"; + mtx = GenerateIntegerMat>(); IntegerFunctions(mtx); }else if(chosenDataType == DataType::ffloat){ MatrixCSR mtx; - mtx = GenerateFloatMat(mtx); - cout<<"\nThe matrix has been randomly generated.\n"; + mtx = GenerateFloatMat>(); FloatFunctions(mtx); }else if(chosenDataType == DataType::sstring){ MatrixCSR mtx; - mtx = GenerateStringsMat(mtx); - cout<<"\nThe matrix has been randomly generated.\n"; + mtx = GenerateStringsMat>(); StringFunctions(mtx); } } @@ -479,85 +473,108 @@ void Print(T& mat){ /* ----- generator functions ----- */ template -T GenerateIntegerMat(T& mat){ +T GenerateIntegerMat(){ default_random_engine gen(random_device{}()); uniform_int_distribution random_dimension(1,20); // generates the dimensions of the matrix uniform_int_distribution dist(0,100); // generates values inside the matrix ulong n_columns = random_dimension(gen); ulong n_rows = random_dimension(gen); + ulong row, column, elements_to_insert; + T matrix(n_rows, n_columns); + MatrixCSR aux_matrix(n_rows, n_columns); //avoids to insert twice (or more) an element in a position - uniform_int_distribution n_elements_to_insert(0,(n_columns*n_rows)); // generates how many elements to insert - ulong elements_to_insert = n_elements_to_insert(gen); - - ulong row,column; + cout<<" A "< n_rows*n_columns); for(ulong i=1 ; i<=elements_to_insert ; ++i){ do{ - row = random_dimension(gen)-1; - }while(row >= n_rows); + do{ + row = random_dimension(gen)-1; + }while(row >= n_rows); - do{ - 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); - } return matrix; } template -T GenerateFloatMat(T& mat){ +T GenerateFloatMat(){ default_random_engine gen(random_device{}()); uniform_int_distribution random_dimension(1,20); uniform_real_distribution dist(0,9); ulong n_columns = random_dimension(gen); ulong n_rows = random_dimension(gen); + ulong row, column, elements_to_insert; + T matrix(n_rows, n_columns); + MatrixCSR aux_matrix(n_rows, n_columns); //avoids to insert twice (or more) an element in a position - uniform_int_distribution n_elements_to_insert(0,(n_columns*n_rows)); - ulong elements_to_insert = n_elements_to_insert(gen); - - ulong row,column; + cout<<" A "< n_rows*n_columns); for(ulong i=1 ; i<=elements_to_insert ; ++i){ do{ - row = random_dimension(gen)-1; - }while(row >= n_rows); - do{ - column = random_dimension(gen)-1; - }while(column >= n_columns); + do{ + row = random_dimension(gen)-1; + }while(row >= n_rows); + + 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); } return matrix; } template -T GenerateStringsMat(T& mat){ - +T GenerateStringsMat(){ default_random_engine gen(random_device{}()); uniform_int_distribution random_dimension(1,20); uniform_int_distribution dist(1,5); ulong n_columns = random_dimension(gen); ulong n_rows = random_dimension(gen); + ulong row, column, elements_to_insert; + T matrix(n_rows, n_columns); + MatrixCSR aux_matrix(n_rows, n_columns); //avoids to insert twice (or more) an element in a position - uniform_int_distribution n_elements_to_insert(0,(n_columns*n_rows)); - ulong elements_to_insert = n_elements_to_insert(gen); + cout<<" A "< n_rows*n_columns); for(ulong i=1 ; i<=elements_to_insert ; ++i){ do{ - row = random_dimension(gen)-1; - }while(row >= n_rows); - do{ - column = random_dimension(gen)-1; - }while(column >= n_columns); + do{ + row = random_dimension(gen)-1; + }while(row >= n_rows); + + 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)); } return matrix; @@ -574,3 +591,10 @@ string generateRandomString(ulong dim){ newString[dim]='\0'; return newString; } + +ulong getDimension(){ + ulong dimension; + std::cout<<" How many elements you'd like to insert? "; + std::cin>>dimension; + return dimension; +} diff --git a/librerie/exercise5/zmytest/test.hpp b/librerie/exercise5/zmytest/test.hpp index 0b8c7a4..0bb4f46 100755 --- a/librerie/exercise5/zmytest/test.hpp +++ b/librerie/exercise5/zmytest/test.hpp @@ -89,18 +89,20 @@ void Read(const T&); template void Resize(T& mtx); +ulong getDimension(); + /* ----- generator functions ----- */ void UseChosenType(Implementation, DataType); template -T GenerateIntegerMat(T&); +T GenerateIntegerMat(); template -T GenerateFloatMat(T&); +T GenerateFloatMat(); template -T GenerateStringsMat(T&); +T GenerateStringsMat(); std::string generateRandomString(ulong);