diff --git a/librerie/exercise5/main.cpp b/librerie/exercise5/main.cpp index 34faa12..bfc751c 100755 --- a/librerie/exercise5/main.cpp +++ b/librerie/exercise5/main.cpp @@ -11,6 +11,6 @@ int main() { std::cout << "Lasd Libraries 2020" << std::endl; - lasdtest(); // To call in the menu of your library test! + menu(); return 0; } diff --git a/librerie/exercise5/matrix/csr/matrixcsr.cpp b/librerie/exercise5/matrix/csr/matrixcsr.cpp index 8f2a2b2..2848d3e 100755 --- a/librerie/exercise5/matrix/csr/matrixcsr.cpp +++ b/librerie/exercise5/matrix/csr/matrixcsr.cpp @@ -1,12 +1,6 @@ namespace lasd { -/* - - CONTROLLARE COLUMN RESIZE - MOVE CONSTRUCTOR - -*/ /* ************************************************************************** */ template @@ -120,7 +114,7 @@ void MatrixCSR::RowResize(const ulong& new_row_size){ } else if(new_row_size > rows){ R.Resize(new_row_size+1); - for(ulong i=rows ; i::ColumnResize(const ulong& new_column_size){ if(new_column_size == 0){ Clear(); } - else if(new_column_size > columns){ - columns = new_column_size; - } else if(new_column_size < columns){ Node** last; Node** last_not_deleted; Node* toDelete; - for(ulong i=0 ; i::ColumnResize(const ulong& new_column_size){ *ptr = (*(*ptr)).next; delete toDelete; --size; - } - } - for(ulong j=i+1 ; j @@ -186,12 +181,14 @@ bool MatrixCSR::ExistsCell(const ulong& r, const ulong& c) const noexcept{ } template -const Data& MatrixCSR::operator()(const ulong& r, const ulong& c) const{ +const Data& MatrixCSR::operator()(const ulong r, const ulong c) const{ if(r>=rows || c>=columns) throw std::out_of_range("Tried to access an invalid position!"); else{ Node** ptr = R[r]; while(ptr != R[r+1]){ - if( (**ptr).value.second == c ) return (**ptr).value.first; + if( (**ptr).value.second == c ){ + return (**ptr).value.first; + } ptr = &((**ptr).next); } throw std::length_error("The element does not exist!"); @@ -199,12 +196,11 @@ const Data& MatrixCSR::operator()(const ulong& r, const ulong& c) const{ } template -Data& MatrixCSR::operator()(const ulong& r, const ulong& c){ +Data& MatrixCSR::operator()(const ulong r, const ulong c){ if(r>=rows || c>=columns) throw std::out_of_range("Tried to access an invalid position!"); else{ Node** ptr = R[r]; Node** last = R[r+1]; // pointer to the pointer inside the last element of the r-th cell - while(ptr != R[r+1] && ((**ptr).value.second <= c)){ if((**ptr).value.second == c){ return (**ptr).value.first; @@ -214,21 +210,22 @@ Data& MatrixCSR::operator()(const ulong& r, const ulong& c){ } } - struct List>::Node* newNode = new Node; - struct List>::Node* nextNode = *ptr; + Node* newNode = new Node(); + Node* nextNode = *ptr; *ptr = newNode; newNode->next = nextNode; (newNode->value).second = c; + ++size; if(last == ptr){ // the newely inserted element is the last one in its row for(ulong i=r+1 ; inext); // assign the address of the pointer of the next node + if(R[i] == last){ // check if it pointed to last (it was empty) + R[i] = &(newNode->next); // assign the address of the pointer of the next node } else break; } } - return (newNode->value).first; + return (newNode->value).first; } } @@ -239,6 +236,7 @@ void MatrixCSR::Clear(){ rows = 0; size = 0; R.Resize(1); + R[0] = &head; } template @@ -269,7 +267,38 @@ void MatrixCSR::FoldPostOrder(const typename FoldableContainer::Fold , par , acc); } +template +void MatrixCSR::debug(){ + std::cout<value).first<<"|"<< (tmp->value).second; + std::cout<next); + + std::cout<next; + } + + std::cout << "R VECTOR:" << '\n'; + for(ulong i=0; i + namespace lasd { template class MatrixCSR : virtual public List>, virtual public Matrix{ // Must extend Matrix -protected: +public: //CAMBIARE A PROTETTO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! using Matrix::rows; using Matrix::columns; @@ -33,7 +35,7 @@ protected: | next */ -public: +//public: // Default constructor MatrixCSR(); @@ -79,8 +81,8 @@ public: bool ExistsCell(const ulong&, const ulong&) const noexcept override; // Override Matrix member (should not throw exceptions) - Data& operator()(const ulong&, const ulong&) override; // Override Matrix member (mutable access to the element; throw out_of_range when out of range) - const Data& operator()(const ulong&, const ulong&) const override; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present) + Data& operator()(const ulong, const ulong) override; // Override Matrix member (mutable access to the element; throw out_of_range when out of range) + const Data& operator()(const ulong, const ulong) const override; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present) /* ************************************************************************ */ @@ -102,6 +104,7 @@ public: virtual void FoldPreOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member virtual void FoldPostOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member + void debug(); }; /* ************************************************************************** */ diff --git a/librerie/exercise5/matrix/matrix.hpp b/librerie/exercise5/matrix/matrix.hpp index e7d0ae1..3c0ad46 100755 --- a/librerie/exercise5/matrix/matrix.hpp +++ b/librerie/exercise5/matrix/matrix.hpp @@ -55,8 +55,8 @@ public: virtual bool ExistsCell(const ulong&, const ulong&) const noexcept = 0; // (concrete function should not throw exceptions) - virtual Data& operator()(const ulong&, const ulong&) = 0; // Mutable access to the element (concrete function should throw exceptions only when out of range) - virtual const Data& operator()(const ulong&, const ulong&) const = 0; // Immutable access to the element (concrete function should throw exceptions when not present) + virtual Data& operator()(const ulong, const ulong) = 0; // Mutable access to the element (concrete function should throw exceptions only when out of range) + virtual const Data& operator()(const ulong, const ulong) const = 0; // Immutable access to the element (concrete function should throw exceptions when not present) }; diff --git a/librerie/exercise5/matrix/vec/matrixvec.cpp b/librerie/exercise5/matrix/vec/matrixvec.cpp index 0c3cbce..c4075f4 100755 --- a/librerie/exercise5/matrix/vec/matrixvec.cpp +++ b/librerie/exercise5/matrix/vec/matrixvec.cpp @@ -107,13 +107,13 @@ bool MatrixVec::ExistsCell(const ulong& r, const ulong& c) const noexcept{ } template -const Data& MatrixVec::operator()(const ulong& r, const ulong& c) const{ +const Data& MatrixVec::operator()(const ulong r, const ulong c) const{ if(ExistsCell(r,c)) return Elements[(r*columns)+c]; else throw std::out_of_range("Tried to access an invalid position!"); } template -Data& MatrixVec::operator()(const ulong& r, const ulong& c){ +Data& MatrixVec::operator()(const ulong r, const ulong c){ if(ExistsCell(r,c)) return Elements[(r*columns)+c]; else throw std::out_of_range("Tried to access an invalid position!"); } diff --git a/librerie/exercise5/matrix/vec/matrixvec.hpp b/librerie/exercise5/matrix/vec/matrixvec.hpp index ce2fb0b..4e406f5 100755 --- a/librerie/exercise5/matrix/vec/matrixvec.hpp +++ b/librerie/exercise5/matrix/vec/matrixvec.hpp @@ -39,8 +39,8 @@ public: void RowResize(const ulong&) override; // Override Matrix member void ColumnResize(const ulong&) override; // Override Matrix member bool ExistsCell(const ulong&, const ulong&) const noexcept override; // Override Matrix member (should not throw exceptions) - Data& operator()(const ulong&, const ulong&) override; // Override Matrix member (mutable access to the element; throw out_of_range when out of range) - const Data& operator()(const ulong&, const ulong&) const override; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present) + Data& operator()(const ulong, const ulong) override; // Override Matrix member (mutable access to the element; throw out_of_range when out of range) + const Data& operator()(const ulong, const ulong) const override; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present) // Specific member functions (inherited from Container) @@ -56,6 +56,10 @@ public: // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member + void debug(){ + return; + } + }; } diff --git a/librerie/exercise5/zlasdtest/exercise5/simpletest.cpp b/librerie/exercise5/zlasdtest/exercise5/simpletest.cpp index dcb0ee9..232c9e9 100755 --- a/librerie/exercise5/zlasdtest/exercise5/simpletest.cpp +++ b/librerie/exercise5/zlasdtest/exercise5/simpletest.cpp @@ -36,7 +36,7 @@ void stestMatrixInt(Mat& mat, uint& testnum, uint& testerr) { SetRowNumber(loctestnum, loctesterr, mat, true, 1); SetColumnNumber(loctestnum, loctesterr, mat, true, 1); - std::cout<<"\nClearing the matrix\n"; + std::cout<<" Clearing the matrix\n"; mat.Clear(); ExistsCell(loctestnum, loctesterr, mat, false, 0, 0); @@ -77,9 +77,9 @@ void stestMatrixInt(Mat& mat, uint& testnum, uint& testerr) { SetColumnNumber(loctestnum, loctesterr, mat, true, 5); //31 SetCell(loctestnum, loctesterr, mat, true, 1, 4, 8); - SetCell(loctestnum, loctesterr, mat, true, 3, 4, 9); - - SetColumnNumber(loctestnum, loctesterr, mat, true, 3); + SetCell(loctestnum, loctesterr, mat, true, 3, 4, 9); //33 + //mat.debug(); + SetColumnNumber(loctestnum, loctesterr, mat, true, 3); //34 SetColumnNumber(loctestnum, loctesterr, mat, true, 4); MapPreOrder(loctestnum, loctesterr, mat, true, &MapPrint, 0); @@ -131,7 +131,7 @@ void stestMatrixInt(Mat& mat, uint& testnum, uint& testerr) { NonEqualMatrix(loctestnum, loctesterr, mat, copmat); - SetCell(loctestnum, loctesterr, mat, true, 0, 3, 4); + SetCell(loctestnum, loctesterr, mat, true, 0, 3, 4); //63 SetCell(loctestnum, loctesterr, mat, true, 2, 3, 5); MapPreOrder(loctestnum, loctesterr, mat, true, &MapPrint, 0); @@ -143,7 +143,7 @@ void stestMatrixInt(Mat& mat, uint& testnum, uint& testerr) { MapPreOrder(loctestnum, loctesterr, mat, true, &MapPrint, 0); - FoldPreOrder(loctestnum, loctesterr, mat, true, &FoldMultiply, 0, 1, 40320); + FoldPreOrder(loctestnum, loctesterr, mat, true, &FoldMultiply, 0, 1, 40320); //70 MapPostOrder(loctestnum, loctesterr, mat, true, &MapDecrement, 0); @@ -154,21 +154,21 @@ void stestMatrixInt(Mat& mat, uint& testnum, uint& testerr) { Mat movmat(std::move(mat)); - GetRowNumber(loctestnum, loctesterr, mat, true, 0); - GetColumnNumber(loctestnum, loctesterr, mat, true, 0); + GetRowNumber(loctestnum, loctesterr, mat, true, 0); //75 + GetColumnNumber(loctestnum, loctesterr, mat, true, 0); //76 ExistsCell(loctestnum, loctesterr, mat, false, 0, 0); Exists(loctestnum, loctesterr, movmat, false, 8); - SetCell(loctestnum, loctesterr, movmat, true, 4, 2, 8); + SetCell(loctestnum, loctesterr, movmat, true, 4, 2, 8); //79 MapPreOrder(loctestnum, loctesterr, movmat, true, &MapPrint, 0); MapPostOrder(loctestnum, loctesterr, movmat, true, &MapPrint, 0); FoldPreOrder(loctestnum, loctesterr, movmat, true, &FoldAdd, 0, 0, 36); - Exists(loctestnum, loctesterr, movmat, true, 8); + Exists(loctestnum, loctesterr, movmat, true, 8); //83 SetRowNumber(loctestnum, loctesterr, movmat, true, 4); MapPostOrder(loctestnum, loctesterr, movmat, true, &MapPrint, 0); @@ -176,7 +176,7 @@ void stestMatrixInt(Mat& mat, uint& testnum, uint& testerr) { movmat.Clear(); GetRowNumber(loctestnum, loctesterr, movmat, true, 0); - GetColumnNumber(loctestnum, loctesterr, movmat, true, 0); + GetColumnNumber(loctestnum, loctesterr, movmat, true, 0); //87 mat = copmat; copmat.Clear(); diff --git a/librerie/exercise5/zmytest/test.cpp b/librerie/exercise5/zmytest/test.cpp index 07a921d..4a8ebb3 100755 --- a/librerie/exercise5/zmytest/test.cpp +++ b/librerie/exercise5/zmytest/test.cpp @@ -1,2 +1,56 @@ +#include "test.hpp" +#include"../matrix/matrix.hpp" +#include"../matrix/csr/matrixcsr.hpp" +#include"../matrix/vec/matrixvec.hpp" +#include "../zlasdtest/test.hpp" +#include +using namespace std; +using namespace lasd; +void menu(){ -// ... + unsigned short int choice; + + do{ + std::cout< "; + std::cin>>std::ws; + std::cin>>choice; + }while(choice!=1 && choice!=2); + if(choice==1){ + lasdtest(); + }else{ + MatrixCSR mat; + mat.RowResize(5); + mat.ColumnResize(5); + + mat(0,1) = 1; + mat(0,3) = 4; + mat(1,0) = 2; + mat(1,4) = 8; + mat(2,1) = 3; + mat(2,2) = 5; + mat(3,4) = 9; + mat(4,1) = 6; + mat(4,3) = 7; + + for(ulong i=0 ; i