Library 5

This commit is contained in:
Alessandro Ferro 2021-06-09 08:13:58 +02:00
parent d15173a276
commit 07cdae9a03
3 changed files with 158 additions and 114 deletions

View File

@ -45,7 +45,7 @@ MatrixCSR<Data>::MatrixCSR(MatrixCSR&& toMove) noexcept{
std::swap(head, toMove.head);
std::swap(R, toMove.R);
toMove.R.Resize(1);
toMove.R.Resize(1); // the moved matrix must be empty
toMove.R[0] = &toMove.head;
Node** oldHead = R[0];

View File

@ -50,7 +50,7 @@ Implementation ChooseImplementation(){
do{
std::cout<<"\nChoose an implementation for the binary tree:"<<std::endl;
std::cout<<" 1. Vector"<<std::endl;
std::cout<<" 2. YALE format (Compressed Sparse Row) "<<std::endl;
std::cout<<" 2. YALE (Compressed Sparse Row) "<<std::endl;
cout<<endl<<" -> ";
std::cin>>std::ws;
std::cin>>choice;
@ -103,6 +103,7 @@ void UseChosenType(Implementation chosenImplementation, DataType chosenDataType)
}
}
/* ----- integer functions ----- */
template <typename T>
void IntegerFunctions(T& mtx){
@ -152,6 +153,38 @@ void IntegerFunctions(T& mtx){
}while(choice!=8 && choice!=9);
}
template <typename T>
void ProductsElementsLessThan(T& mtx){
int n, acc=1;
void (*func)(const int&, const void*, void*) = AccumulateProduct;
cout<<" Multipy all elements of the matrix whose value is less than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&acc);
cout<<"\n The result is "<<acc<<endl<<endl;
}
void AccumulateProduct(const int& data, const void* par, void* acc){
if(data < (*(int*)par)){
*(int*)acc *= data;
}
}
template <typename T>
void Double(T& mtx){
void (*fun)(int&, void*) = MultiplyAnElement;
int par = 2;
mtx.MapPreOrder(fun,(void*)&par);
cout<<" Done.\n";
}
void MultiplyAnElement(int& data, void* par){
data *= *(int*)par;
}
/* ----- float functions ----- */
template <typename T>
void FloatFunctions(T& mtx){
unsigned short int choice;
@ -200,6 +233,38 @@ void FloatFunctions(T& mtx){
}while(choice!=8 && choice!=9);
}
template <typename T>
void SumElementsGreaterThan(T& mtx){
float n, acc = 0;
void (*func)(const float&, const void*, void*) = AccumulateSum;
cout<<" Sum all elements of the matrix whose value is greater than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&acc);
cout<<"\n The result is "<<acc<<endl<<endl;
}
void AccumulateSum(const float& data, const void* par, void* acc){
if(data > (*(float*)par)){
*(float*)acc += data;
}
}
template <typename T>
void CubeElements(T& mtx){
void (*fun)(float&, void*) = Exponentiation;
float par = 3;
mtx.MapPreOrder(fun,(void*)&par);
cout<<" Done.\n";
}
void Exponentiation(float& data, void* par){
data = (-1.0F) * pow(data , *(float*)par );
}
/* ----- string functions ----- */
template <typename T>
void StringFunctions(T& mtx){
unsigned short int choice;
@ -248,6 +313,44 @@ void StringFunctions(T& mtx){
}while(choice!=8 && choice!=9);
}
template <typename T>
void ConcatLessThan(T& mtx){
int n;
string concatenated = "";
void (*func)(const string&, const void*, void*) = ConcatAString;
cout<<" Concatenate all elements of the tree whose length is less or equal than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&concatenated);
cout<<"\n The result is "<< concatenated << endl << endl;
}
void ConcatAString(const string& data, const void* par, void* acc){
if( ((int)data.length()) <= ((*(int*)par)) ){
*(string*)acc = *(string*)acc + "-" + data;
}
}
template <typename T>
void HeadConcat(T& mtx){
void (*fun)(string&, void*) = HeadConcatMapAux;
string par;
cout<<" Concatenate in front the following string: ";
cin>>ws;
cin>>par;
mtx.MapPreOrder(fun,(void*)&par);
cout<<" Done.\n";
}
void HeadConcatMapAux(string& data, void* par){
data = *(string*)par + data;
}
/* ----- shared functions ----- */
template <typename T>
void Resize(T& mtx){
ulong rows, columns;
@ -297,99 +400,6 @@ void Insert(T& mtx){
}
}
template <typename T>
void ConcatLessThan(T& mtx){
int n;
string concatenated = "";
void (*func)(const string&, const void*, void*) = ConcatAString;
cout<<" Concatenate all elements of the tree whose length is less or equal than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&concatenated);
cout<<"\n The result is "<< concatenated << endl << endl;
}
void ConcatAString(const string& data, const void* par, void* acc){
if( ((int)data.length()) <= ((*(int*)par)) ){
*(string*)acc = *(string*)acc + "-" + data;
}
}
template <typename T>
void HeadConcat(T& mtx){
void (*fun)(string&, void*) = HeadConcatMapAux;
string par;
cout<<" Concatenate in front the following string: ";
cin>>ws;
cin>>par;
mtx.MapPreOrder(fun,(void*)&par);
cout<<" Done.\n";
}
void HeadConcatMapAux(string& data, void* par){
data = *(string*)par + data;
}
template <typename T>
void SumElementsGreaterThan(T& mtx){
float n, acc = 0;
void (*func)(const float&, const void*, void*) = AccumulateSum;
cout<<" Sum all elements of the matrix whose value is greater than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&acc);
cout<<"\n The result is "<<acc<<endl<<endl;
}
void AccumulateSum(const float& data, const void* par, void* acc){
if(data > (*(float*)par)){
*(float*)acc += data;
}
}
template <typename T>
void CubeElements(T& mtx){
void (*fun)(float&, void*) = Exponentiation;
float par = 3;
mtx.MapPreOrder(fun,(void*)&par);
cout<<" Done.\n";
}
void Exponentiation(float& data, void* par){
data = (-1.0F) * pow(data , *(float*)par );
}
template <typename T>
void Double(T& mtx){
void (*fun)(int&, void*) = MultiplyAnElement;
int par = 2;
mtx.MapPreOrder(fun,(void*)&par);
cout<<" Done.\n";
}
void MultiplyAnElement(int& data, void* par){
data *= *(int*)par;
}
template <typename T>
void ProductsElementsLessThan(T& mtx){
int n, acc=1;
void (*func)(const int&, const void*, void*) = AccumulateProduct;
cout<<" Multipy all elements of the matrix whose value is less than ";
cin>>ws>>n;
mtx.FoldPreOrder(func, (void*)&n, (void*)&acc);
cout<<"\n The result is "<<acc<<endl<<endl;
}
void AccumulateProduct(const int& data, const void* par, void* acc){
if(data < (*(int*)par)){
*(int*)acc *= data;
}
}
template <template <typename...> class Matrix, typename Data>
void CheckExistence(Matrix<Data>& mtx){
Data elementToLookFor;
@ -446,7 +456,6 @@ void Print(T& mat){
template <typename T>
T GenerateIntegerMat(T& mat){
default_random_engine gen(random_device{}());
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
@ -472,9 +481,7 @@ T GenerateIntegerMat(T& mat){
matrix(row,column) = dist(gen);
}
return matrix;
}
template <typename T>

View File

@ -13,55 +13,92 @@ enum class DataType{integer,ffloat,sstring};
enum class Implementation{vector,yale};
void menu();
template <typename T>
void Print(T&);
DataType ChooseDataType(); //choose data type
Implementation ChooseImplementation();
/* ----- integer functions ----- */
template <typename T>
void IntegerFunctions(T&);
template <template <typename...> class Matrix, typename Data>
void PrintElements(Matrix<Data>& mtx);
template <typename Data>
void PrintSingleElement(Data&, void*);
template <template <typename...> class Matrix, typename Data>
void CheckExistence(Matrix<Data>&);
template <typename T>
void ProductsElementsLessThan(T&);
void AccumulateProduct(const int&, const void*, void*);
template <typename T>
void Double(T&);
void MultiplyAnElement(int&, void*);
template <typename T>
void Insert(T&);
template <typename T>
void Read(const T&);
template <typename T>
void Resize(T& mtx);
/* ----- float functions ----- */
template <typename T>
void FloatFunctions(T&);
template <typename T>
void SumElementsGreaterThan(T&);
void AccumulateSum(const float&, const void*, void*);
template <typename T>
void CubeElements(T&);
void Exponentiation(float&, void*);
/* ----- string functions ----- */
template <typename T>
void StringFunctions(T&);
template <typename T>
void ConcatLessThan(T&);
void ConcatAString(const string&, const void*, void*);
template <typename T>
void HeadConcat(T&);
void HeadConcatMapAux(string&, void*);
/* ----- shared functions ----- */
template <typename T>
void Print(T&);
template <template <typename...> class Matrix, typename Data>
void PrintElements(Matrix<Data>& mtx);
template <typename Data>
void PrintSingleElement(Data&, void*);
template <template <typename...> class Matrix, typename Data>
void CheckExistence(Matrix<Data>&);
template <typename T>
void Insert(T&);
template <typename T>
void Read(const T&);
template <typename T>
void Resize(T& mtx);
/* ----- generator functions ----- */
DataType ChooseDataType(); //choose data type
Implementation ChooseImplementation();
void UseChosenType(Implementation, DataType);
template <typename T>
T GenerateIntegerMat(T&);
template <typename T>
T GenerateFloatMat(T&);
template <typename T>
T GenerateStringsMat(T&);
std::string generateRandomString(ulong);
#endif