mirror of https://github.com/xfarrow/lasd.git
parent
fe51a1b547
commit
4aa82d6108
|
@ -1,7 +1,7 @@
|
|||
|
||||
#! /bin/bash
|
||||
|
||||
g++ -O3 -o main \
|
||||
g++-10 -o main \
|
||||
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
|
||||
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
|
||||
zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \
|
||||
|
|
|
@ -29,7 +29,7 @@ protected:
|
|||
Node* next = nullptr;
|
||||
|
||||
/* ********************************************************************** */
|
||||
|
||||
Node() = default;
|
||||
// Specific constructors
|
||||
Node(const Data&);
|
||||
|
||||
|
|
|
@ -1,15 +1,22 @@
|
|||
|
||||
namespace lasd {
|
||||
|
||||
/*
|
||||
|
||||
CONTROLLARE COLUMN RESIZE
|
||||
MOVE CONSTRUCTOR
|
||||
|
||||
*/
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
MatrixCSR<Data>::MatrixCSR(){
|
||||
R.Resize(1);
|
||||
R[0] = &head;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixCSR<Data>::MatrixCSR(const ulong& r, const ulong& c){
|
||||
MatrixCSR<Data>::MatrixCSR(const ulong r, const ulong c){
|
||||
rows = r;
|
||||
columns = c;
|
||||
R.Resize(rows+1);
|
||||
|
@ -31,19 +38,21 @@ MatrixCSR<Data>::MatrixCSR(const MatrixCSR& toCopy) : MatrixCSR(toCopy.rows, toC
|
|||
*/
|
||||
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;
|
||||
(*this)(i , ((*ptr)->value).second) = ((*ptr)->value).first;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixCSR<Data>::MatrixCSR(MatrixCSR&& toMove) noexcept{
|
||||
|
||||
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.Resize(1);
|
||||
toMove.R[0] = &toMove.head;
|
||||
|
||||
Node** oldHead = R[0];
|
||||
|
@ -58,30 +67,36 @@ MatrixCSR<Data>::~MatrixCSR(){
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixCSR<Data>& MatrixCSR::operator=(const MatrixCSR& toCopy){
|
||||
MatrixCSR<Data> tmp(toCopy);
|
||||
std::swap(*this, tmp);
|
||||
MatrixCSR<Data>& MatrixCSR<Data>::operator=(const MatrixCSR& toCopy){
|
||||
MatrixCSR<Data>* tmp = new MatrixCSR<Data>(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);
|
||||
MatrixCSR<Data>& MatrixCSR<Data>::operator=(MatrixCSR&& toMove) noexcept{
|
||||
MatrixCSR<Data>* tmp = new MatrixCSR<Data>(std::move(toMove));
|
||||
std::swap(*this, *tmp);
|
||||
delete tmp;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool MatrixCSR<Data>::operator==(const MatrixCSR& toCompare) const noexcept{
|
||||
/*
|
||||
* Check the # of rows, of columns and of elements.
|
||||
* For each row, check if the nodes in that row have the same value (data and column).
|
||||
* If one row ends before the other one, return false.
|
||||
*/
|
||||
|
||||
if(columns == toCompare.columns && rows == toCompare.rows && size==toCompare.size){
|
||||
for(ulong i=0 ; i<(R.Size()-1) ; ++i){
|
||||
for(Node** ptr1 = R[i], ptr2 = toCopy.R[i] ; ptr1!=R[i+1] && ptr2!=toCopy.R[i+1] ; ptr1 = &( (*(*ptr1)).next ) , ptr2 = &( (*(*ptr2)).next ) ){
|
||||
for(Node** ptr1 = R[i] , **ptr2 = (toCompare.R)[i] ; ptr1!=R[i+1] && ptr2!=toCompare.R[i+1] ; ptr1 = &( (*(*ptr1)).next ) , ptr2 = &( (*(*ptr2)).next ) ){
|
||||
|
||||
if( ( ((*ptr1)->value).first != ((*ptr2)->value).first ) || ( ((*ptr1)->value).second != ((*ptr2)->value).second )) return false;
|
||||
|
||||
if( (&((*(*ptr1)).next)==R[i+1] && &((*(*ptr2)).next) != toCopy.R.[i+1]) || (&((*(*ptr1)).next)!=R[i+1] && &((*(*ptr2)).next) == toCopy.R.[i+1])){
|
||||
if( (&((*(*ptr1)).next)==R[i+1] && &((*(*ptr2)).next) != toCompare.R[i+1]) || (&((*(*ptr1)).next)!=R[i+1] && &((*(*ptr2)).next) == toCompare.R[i+1])){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -103,14 +118,15 @@ void MatrixCSR<Data>::RowResize(const ulong& new_row_size){
|
|||
if(new_row_size == 0){
|
||||
Clear();
|
||||
}
|
||||
else if(new_row_size > row){
|
||||
else if(new_row_size > rows){
|
||||
R.Resize(new_row_size+1);
|
||||
for(ulong i=rows ; i<new_row_size+1 ; ++i){
|
||||
R[i] = R[rows];
|
||||
}
|
||||
rows = new_row_size;
|
||||
}else if(new_row_size < row){
|
||||
Node* toDelete,tmp;
|
||||
}else if(new_row_size < rows){
|
||||
Node* toDelete;
|
||||
Node* tmp;
|
||||
toDelete = *R[new_row_size];
|
||||
while(toDelete!=nullptr){
|
||||
tmp = toDelete->next;
|
||||
|
@ -133,21 +149,34 @@ void MatrixCSR<Data>::ColumnResize(const ulong& new_column_size){
|
|||
columns = new_column_size;
|
||||
}
|
||||
else if(new_column_size < columns){
|
||||
Node** add;
|
||||
Node** last;
|
||||
Node** last_not_deleted;
|
||||
Node* toDelete;
|
||||
for(ulong i=0 ; i<R.Size()-1 ; ++i){
|
||||
last = R[i+1];
|
||||
last_not_deleted = R[i];
|
||||
for(Node** ptr = R[i] ; ptr!=R[i+1] ; ptr = &( (*(*ptr)).next ) ){
|
||||
if(((*ptr)->value).second) >= new_row_size) break;
|
||||
if(((*ptr)->value).second < new_column_size){
|
||||
last_not_deleted = &( (*(*ptr)).next );
|
||||
}else{
|
||||
toDelete = *ptr;
|
||||
*ptr = (*(*ptr)).next;
|
||||
delete toDelete;
|
||||
--size;
|
||||
}
|
||||
}
|
||||
for(ulong j=i+1 ; j<R.Size() ; ++j){
|
||||
if(R[j] == last){
|
||||
R[j] = last_not_deleted;
|
||||
}
|
||||
if(ptr!=R[i+1]){
|
||||
//?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool MatrixCSR<Data>::ExistsCell(const ulong& r, const ulong& c) noexcept{
|
||||
if(r>=row || c>=column) return false;
|
||||
bool MatrixCSR<Data>::ExistsCell(const ulong& r, const ulong& c) const noexcept{
|
||||
if(r>=rows || c>=columns) return false;
|
||||
Node** ptr = R[r];
|
||||
while(ptr != R[r+1]){
|
||||
if( (**ptr).value.second == c ) return true;
|
||||
|
@ -158,7 +187,7 @@ bool MatrixCSR<Data>::ExistsCell(const ulong& r, const ulong& c) noexcept{
|
|||
|
||||
template <typename Data>
|
||||
const Data& MatrixCSR<Data>::operator()(const ulong& r, const ulong& c) const{
|
||||
if(r>=row || c>=column) throw std::out_of_range("Tried to access position ["<<r<<"]["<<c<<"] in a ["<<rows<<"]["<<columns<<"] matrix!");
|
||||
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]){
|
||||
|
@ -171,24 +200,76 @@ const Data& MatrixCSR<Data>::operator()(const ulong& r, const ulong& c) const{
|
|||
|
||||
template <typename Data>
|
||||
Data& MatrixCSR<Data>::operator()(const ulong& r, const ulong& c){
|
||||
if(r>=row || c>=column) throw std::out_of_range("Tried to access position ["<<r<<"]["<<c<<"] in a ["<<rows<<"]["<<columns<<"] matrix!");
|
||||
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]){
|
||||
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;
|
||||
}
|
||||
else if( (**ptr).value.second < c ){
|
||||
ptr = &((**ptr).next);
|
||||
}
|
||||
else if((**ptr).value.second > c){
|
||||
Node** tmp = *ptr;
|
||||
//?
|
||||
}
|
||||
|
||||
struct List<std::pair<Data,ulong>>::Node* newNode = new Node;
|
||||
struct List<std::pair<Data,ulong>>::Node* nextNode = *ptr;
|
||||
*ptr = newNode;
|
||||
newNode->next = nextNode;
|
||||
|
||||
if(last == ptr){ // the newely inserted element is the last one in its row
|
||||
for(ulong i=r+1 ; i<R.Size() ; ++i){ // then for each next row
|
||||
if(R[r+1] == last){ // check if it pointed to last (it was empty)
|
||||
R[r+1] = &(newNode->next); // assign the address of the pointer of the next node
|
||||
}
|
||||
else break;
|
||||
}
|
||||
}
|
||||
return (newNode->value).first;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixCSR<Data>::Clear(){
|
||||
List<std::pair<Data,ulong>>::Clear();
|
||||
columns = 0;
|
||||
rows = 0;
|
||||
size = 0;
|
||||
R.Resize(1);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixCSR<Data>::MapPreOrder(const typename MappableContainer<Data>::MapFunctor fun, void* par){
|
||||
List<std::pair<Data,ulong>>::MapPreOrder(
|
||||
[&fun](std::pair<Data,ulong>& datx, void* parx){fun(datx.first, parx); }
|
||||
, par);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixCSR<Data>::MapPostOrder(const typename MappableContainer<Data>::MapFunctor fun, void* par){
|
||||
List<std::pair<Data,ulong>>::MapPostOrder(
|
||||
[&fun](std::pair<Data,ulong>& datx, void* parx){fun(datx.first, parx); }
|
||||
, par);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixCSR<Data>::FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor fun, const void* par, void* acc) const{
|
||||
List<std::pair<Data,ulong>>::FoldPreOrder(
|
||||
[&fun](const std::pair<Data,ulong>& datx, const void* parx, void* accx) {fun(datx.first, parx, accx); }
|
||||
, par , acc);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixCSR<Data>::FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor fun, const void* par, void* acc) const{
|
||||
List<std::pair<Data,ulong>>::FoldPostOrder(
|
||||
[&fun](const std::pair<Data,ulong>& datx, const void* parx, void* accx) {fun(datx.first, parx, accx); }
|
||||
, par , acc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
class MatrixCSR : virtual public List<Data>,
|
||||
class MatrixCSR : virtual public List<std::pair<Data,ulong>>,
|
||||
virtual public Matrix<Data>{ // Must extend Matrix<Data>
|
||||
|
||||
protected:
|
||||
|
@ -20,11 +20,10 @@ protected:
|
|||
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);
|
||||
Vector<struct List<std::pair<Data,ulong>>::Node**> R;
|
||||
|
||||
using List<std::pair<Data,ulong>>::head;
|
||||
using struct List<std::pair<Data,ulong>>::Node;
|
||||
using typename List<std::pair<Data,ulong>>::Node;
|
||||
|
||||
/*
|
||||
Node
|
||||
|
@ -42,7 +41,7 @@ public:
|
|||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructors
|
||||
MatrixCSR(const ulong&, const ulong&); // A matrix of some specified dimension
|
||||
MatrixCSR(const ulong, const ulong); // A matrix of some specified dimension
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
|
@ -78,7 +77,7 @@ public:
|
|||
void RowResize(const ulong&) override; // Override Matrix member
|
||||
void ColumnResize(const ulong&) override; // Override Matrix member
|
||||
|
||||
bool ExistsCell(const ulong&, const ulong&) override noexcept; // Override Matrix member (should not throw exceptions)
|
||||
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)
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
ulong Matrix<Data>::RowNumber() noexcept{
|
||||
ulong Matrix<Data>::RowNumber() const noexcept{
|
||||
return rows;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
ulong Matrix<Data>::ColumnNumber() noexcept{
|
||||
ulong Matrix<Data>::ColumnNumber() const noexcept{
|
||||
return columns;
|
||||
}
|
||||
|
||||
|
|
|
@ -47,13 +47,13 @@ public:
|
|||
|
||||
// Specific member functions
|
||||
|
||||
ulong RowNumber() noexcept; // (concrete function should not throw exceptions)
|
||||
ulong ColumnNumber() noexcept; // (concrete function should not throw exceptions)
|
||||
ulong RowNumber() const noexcept; // (concrete function should not throw exceptions)
|
||||
ulong ColumnNumber() const noexcept; // (concrete function should not throw exceptions)
|
||||
|
||||
virtual void RowResize(const ulong&) = 0;
|
||||
virtual void ColumnResize(const ulong&) = 0;
|
||||
|
||||
virtual bool ExistsCell(const ulong&, const ulong&) noexcept = 0; // (concrete function should not throw exceptions)
|
||||
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)
|
||||
|
|
|
@ -21,7 +21,7 @@ MatrixVec<Data>::MatrixVec(const MatrixVec& toCopy){
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec<Data>::MatrixVec(MatrixVec&& toMove) noexcept{
|
||||
MatrixVec<Data>::MatrixVec(MatrixVec<Data>&& toMove) noexcept{
|
||||
std::swap(rows, toMove.rows);
|
||||
std::swap(columns, toMove.columns);
|
||||
std::swap(size, toMove.size);
|
||||
|
@ -34,7 +34,7 @@ MatrixVec<Data>::~MatrixVec(){
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec& MatrixVec<Data>::operator=(const MatrixVec& toCopy){
|
||||
MatrixVec<Data>& MatrixVec<Data>::operator=(const MatrixVec& toCopy){
|
||||
Clear();
|
||||
rows = toCopy.rows;
|
||||
columns = toCopy.columns;
|
||||
|
@ -47,7 +47,7 @@ MatrixVec& MatrixVec<Data>::operator=(const MatrixVec& toCopy){
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec& MatrixVec<Data>::operator=(MatrixVec&& toMove) noexcept{
|
||||
MatrixVec<Data>& MatrixVec<Data>::operator=(MatrixVec&& toMove) noexcept{
|
||||
Clear();
|
||||
std::swap(rows, toMove.rows);
|
||||
std::swap(columns, toMove.columns);
|
||||
|
@ -71,7 +71,7 @@ bool MatrixVec<Data>::operator!=(const MatrixVec& toCompare) const noexcept{
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixVec<Data>::RowResize(const ulong newdim){
|
||||
void MatrixVec<Data>::RowResize(const ulong& newdim){
|
||||
Vector<Data>::Resize(newdim);
|
||||
}
|
||||
|
||||
|
@ -83,7 +83,7 @@ void MatrixVec<Data>::ColumnResize(const ulong& new_column_dim){
|
|||
else if(new_column_dim != columns){
|
||||
ulong limit = (new_column_dim < columns)? new_column_dim : columns;
|
||||
Data* tmp = new Data[rows * new_column_dim]{};
|
||||
for(ulong i=0 ; i<r ; ++i){
|
||||
for(ulong i=0 ; i<rows ; ++i){
|
||||
for(ulong j=0 ; j<limit ; ++j){
|
||||
tmp[(i*new_column_dim)+j] = (*this)(i,j);
|
||||
}
|
||||
|
@ -96,20 +96,20 @@ void MatrixVec<Data>::ColumnResize(const ulong& new_column_dim){
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
bool MatrixVec<Data>::ExistsCell(const ulong& r, const ulong& c) noexcept override{
|
||||
bool MatrixVec<Data>::ExistsCell(const ulong& r, const ulong& c) const noexcept{
|
||||
return (r<rows && c<columns);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& MatrixVec<Data>::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 position ["<<r<<"]["<<c<<"] in a ["<<rows<<"]["<<columns<<"] matrix!");
|
||||
else throw std::out_of_range("Tried to access an invalid position!");
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& MatrixVec<Data>::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 position ["<<r<<"]["<<c<<"] in a ["<<rows<<"]["<<columns<<"] matrix!");
|
||||
else throw std::out_of_range("Tried to access an invalid position!");
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
|
|
|
@ -16,12 +16,13 @@ protected:
|
|||
using Matrix<Data>::rows;
|
||||
using Matrix<Data>::columns;
|
||||
using Vector<Data>::size;
|
||||
using Vector<Data>::Elements;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
MatrixVec() = default;
|
||||
MatrixVec(ulong&, ulong&); // A matrix of some specified dimension
|
||||
MatrixVec(ulong, ulong); // A matrix of some specified dimension
|
||||
MatrixVec(const MatrixVec&);
|
||||
MatrixVec(MatrixVec&&) noexcept;
|
||||
|
||||
|
@ -37,7 +38,7 @@ public:
|
|||
// Specific member functions (inherited from Matrix)
|
||||
void RowResize(const ulong&) override; // Override Matrix member
|
||||
void ColumnResize(const ulong&) override; // Override Matrix member
|
||||
bool ExistsCell(const ulong&, const ulong&) noexcept override; // Override Matrix member (should not throw exceptions)
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue