From d2357d45a6618e3fce7e1bf05daa1c99f1080044 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Fri, 28 May 2021 12:43:27 +0200 Subject: [PATCH] Library 5 --- librerie/exercise5/matrix/csr/matrixcsr.cpp | 90 ++++++++++++++++++++- librerie/exercise5/matrix/csr/matrixcsr.hpp | 70 ++++++++-------- librerie/exercise5/matrix/vec/matrixvec.cpp | 23 ++---- 3 files changed, 133 insertions(+), 50 deletions(-) diff --git a/librerie/exercise5/matrix/csr/matrixcsr.cpp b/librerie/exercise5/matrix/csr/matrixcsr.cpp index d374ceb..9a91c0c 100644 --- a/librerie/exercise5/matrix/csr/matrixcsr.cpp +++ b/librerie/exercise5/matrix/csr/matrixcsr.cpp @@ -3,7 +3,95 @@ namespace lasd { /* ************************************************************************** */ -// ... +template +MatrixCSR::MatrixCSR(){ + R[0] = &head; +} + +template +MatrixCSR::MatrixCSR(const ulong& r, const ulong& c){ + rows = r; + columns = c; + R.Resize(rows+1); + for(ulong i=0 ; i +MatrixCSR::MatrixCSR(const MatrixCSR& toCopy) : MatrixCSR(toCopy.rows, toCopy.columns) { + /* + For each element in the "R" vector, insert it in the matrix (represented as a list) + in this way: + The row index is represented by "i" (the variable that iterates over the R array), + meanwhile the column index is represented by the second element in the pair of the node. + The actual element is stored in the first element in the pair of the node. + The update of the newely created R array is left to operator()(). + */ + for(ulong i=0 ; i < (toCopy.R.Size()-1) ; ++i){ + for(Node** ptr = toCopy.R[i] ; ptr!=toCopy.R[i+1] ; ptr = &( (*(*ptr)).next ) ){ + (*this)(i , ((*ptr)->value).second) = (*ptr)->value).first; + } + } +} + +template +MatrixCSR::MatrixCSR(MatrixCSR&& toMove) : MatrixCSR() noexcept{ // controllare se chiamare MatrixCSR() + std::swap(rows, toMove.rows); + std::swap(columns, toMove.columns); + std::swap(size, toMove.size); + std::swap(head, toMove.head); + std::swap(R, toMove.R); + + toMove.R[0] = &toMove.head; + + Node** oldHead = R[0]; + for(ulong i=0 ; i +MatrixCSR::~MatrixCSR(){ + Clear(); +} + +template +MatrixCSR& MatrixCSR::operator=(const MatrixCSR& toCopy){ + MatrixCSR tmp(toCopy); + std::swap(*this, tmp); + delete tmp; + return *this; +} + +template +MatrixCSR& MatrixCSR::operator=(MatrixCSR&& toMove) noexcept{ + MatrixCSR tmp(std::move(toMove)); + std::swap(*this, tmp); + delete tmp; + return *this; +} + +template +bool MatrixCSR::operator==(const MatrixCSR& toCompare) const noexcept{ + +} + +template +bool MatrixCSR::operator!=(const MatrixCSR& toCompare) const noexcept{ + return !(*this == toCompare); +} + +template +void RowResize(const ulong& new_row_size){ + R.Resize(new_row_size+1); + for(ulong i=rows ; i -class MatrixCSR { // Must extend Matrix - -private: - - // ... +class MatrixCSR : virtual public List, + virtual public Matrix{ // Must extend Matrix protected: - // using Matrix::???; + using Matrix::rows; + using Matrix::columns; + using List>::size; - // ... + Vector>::Node**> R(1); + + using List>::head; + using struct List>::Node; + +/* + Node + | pair + | | Data (actual element - first) + | | ulong (column index - second) + | next +*/ public: // Default constructor - // MatrixCSR() specifiers; + MatrixCSR(); /* ************************************************************************ */ // Specific constructors - // MatrixCSR(argument) specifiers; // A matrix of some specified dimension + MatrixCSR(const ulong&, const ulong&); // A matrix of some specified dimension /* ************************************************************************ */ // Copy constructor - // MatrixCSR(argument) specifiers; + MatrixCSR(const MatrixCSR&); // Move constructor - // MatrixCSR(argument) specifiers; + MatrixCSR(MatrixCSR&&) noexcept; /* ************************************************************************ */ // Destructor - // ~MatrixCSR() specifiers; + virtual ~MatrixCSR(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + MatrixCSR& operator=(const MatrixCSR&); // Move assignment - // type operator=(argument) specifiers; + MatrixCSR& operator=(MatrixCSR&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const MatrixCSR&) const noexcept; + bool operator!=(const MatrixCSR&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Matrix) - // type RowResize() specifiers; // Override Matrix member - // type ColumnResize() specifiers; // Override Matrix member + void RowResize(const ulong&) override; // Override Matrix member + void ColumnResize(const ulong&) override; // Override Matrix member - // type ExistsCell() specifiers; // Override Matrix member (should not throw exceptions) + bool ExistsCell(const ulong&, const ulong&) override noexcept; // Override Matrix member (should not throw exceptions) - // type operator()() specifiers; // Override Matrix member (mutable access to the element; throw out_of_range when out of range) - // type operator()() specifiers; // 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) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member /* ************************************************************************ */ // Specific member functions (inherited from MappableContainer) - // type MapPreOrder(arguments) specifiers; // Override MappableContainer member - // type MapPostOrder(arguments) specifiers; // Override MappableContainer member + virtual void MapPreOrder(const typename MappableContainer::MapFunctor, void*) override; // Override MappableContainer member + virtual void MapPostOrder(const typename MappableContainer::MapFunctor, void*) override; // Override MappableContainer member /* ************************************************************************ */ // Specific member functions (inherited from FoldableContainer) - // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member - // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member + 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 }; diff --git a/librerie/exercise5/matrix/vec/matrixvec.cpp b/librerie/exercise5/matrix/vec/matrixvec.cpp index 21b0d4a..1eaa023 100644 --- a/librerie/exercise5/matrix/vec/matrixvec.cpp +++ b/librerie/exercise5/matrix/vec/matrixvec.cpp @@ -13,7 +13,7 @@ template MatrixVec::MatrixVec(const MatrixVec& toCopy){ rows = toCopy.rows; columns = toCopy.columns; - size = toCopy.rows * toCopy.columns; + size = toCopy.size; Elements = new Data[size]{}; for(ulong i=0 ; i::operator=(const MatrixVec& toCopy){ Clear(); rows = toCopy.rows; columns = toCopy.columns; - size = toCopy.rows * toCopy.columns; + size = toCopy.size; Elements = new Data[size]{}; for(ulong i=0 ; i::RowResize(const ulong newdim){ template void MatrixVec::ColumnResize(const ulong& new_column_dim){ - if(new_column_dim == 0){ Clear(); } else if(new_column_dim != columns){ - + ulong limit = (new_column_dim < columns)? new_column_dim : columns; Data* tmp = new Data[rows * new_column_dim]{}; - - if(new_column_dim > columns){ - for(ulong i=0 ; i