Library 5

This commit is contained in:
Alessandro Ferro 2021-05-28 12:43:27 +02:00
parent 31f7ae9b35
commit d2357d45a6
3 changed files with 133 additions and 50 deletions

View File

@ -3,7 +3,95 @@ namespace lasd {
/* ************************************************************************** */
// ...
template <typename Data>
MatrixCSR<Data>::MatrixCSR(){
R[0] = &head;
}
template <typename Data>
MatrixCSR<Data>::MatrixCSR(const ulong& r, const ulong& c){
rows = r;
columns = c;
R.Resize(rows+1);
for(ulong i=0 ; i<R.Size() ; ++i){
R[i] = &head;
}
}
// copy constructor
template <typename Data>
MatrixCSR<Data>::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 <typename Data>
MatrixCSR<Data>::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<R.Size() && R[i]==oldHead; ++i){
R[i] = &head;
}
}
template <typename Data>
MatrixCSR<Data>::~MatrixCSR(){
Clear();
}
template <typename Data>
MatrixCSR<Data>& MatrixCSR::operator=(const MatrixCSR& toCopy){
MatrixCSR<Data> tmp(toCopy);
std::swap(*this, tmp);
delete tmp;
return *this;
}
template <typename Data>
MatrixCSR<Data>& MatrixCSR::operator=(MatrixCSR&& toMove) noexcept{
MatrixCSR<Data> tmp(std::move(toMove));
std::swap(*this, tmp);
delete tmp;
return *this;
}
template <typename Data>
bool MatrixCSR<Data>::operator==(const MatrixCSR& toCompare) const noexcept{
}
template <typename Data>
bool MatrixCSR<Data>::operator!=(const MatrixCSR& toCompare) const noexcept{
return !(*this == toCompare);
}
template <typename Data>
void RowResize(const ulong& new_row_size){
R.Resize(new_row_size+1);
for(ulong i=rows ; i<new_row_size+1 ; ++i){
R[i] = R[rows];
}
rows = new_row_size;
size = new_row_size * columns;
}
/* ************************************************************************** */

View File

@ -6,96 +6,102 @@
#include "../matrix.hpp"
// #include "../../list/list.hpp"
// #include "../../vector/vector.hpp"
/* ************************************************************************** */
#include "../../list/list.hpp"
#include "../../vector/vector.hpp"
namespace lasd {
/* ************************************************************************** */
template <typename Data>
class MatrixCSR { // Must extend Matrix<Data>
private:
// ...
class MatrixCSR : virtual public List<Data>,
virtual public Matrix<Data>{ // Must extend Matrix<Data>
protected:
// using Matrix<Data>::???;
using Matrix<Data>::rows;
using Matrix<Data>::columns;
using List<std::pair<Data,ulong>>::size;
// ...
Vector<struct List<std::pair<Data,ulong>>::Node**> R(1);
using List<std::pair<Data,ulong>>::head;
using struct List<std::pair<Data,ulong>>::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<Data>::MapFunctor, void*) override; // Override MappableContainer member
virtual void MapPostOrder(const typename MappableContainer<Data>::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<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
virtual void FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
};

View File

@ -13,7 +13,7 @@ template <typename Data>
MatrixVec<Data>::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<size ; ++i){
Elements[i] = toCopy.Elements[i];
@ -38,7 +38,7 @@ MatrixVec& MatrixVec<Data>::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<size ; ++i){
Elements[i] = toCopy.Elements[i];
@ -77,28 +77,17 @@ void MatrixVec<Data>::RowResize(const ulong newdim){
template <typename Data>
void MatrixVec<Data>::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<r ; ++i){
for(ulong j=0 ; j<new_column_dim ; ++j){
if(ExistsCell(i,j)) tmp[(i*new_column_dim)+j] = (*this)(i,j);
}
}
}else if(new_column_dim < columns){
for(ulong i=0 ; i<r ; ++i){
for(ulong j=0 ; j<new_column_dim ; ++j){
for(ulong j=0 ; j<limit ; ++j){
tmp[(i*new_column_dim)+j] = (*this)(i,j);
}
}
}
size = rows * new_column_dim;
columns = new_column_dim;
std::swap(Elements, tmp);