mirror of https://github.com/xfarrow/lasd.git
parent
980fce4cb7
commit
828ec16447
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
|
||||
namespace lasd {
|
||||
|
||||
/*
|
||||
|
||||
CONTROLLARE COLUMN RESIZE
|
||||
MOVE CONSTRUCTOR
|
||||
|
||||
*/
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
|
@ -120,7 +114,7 @@ void MatrixCSR<Data>::RowResize(const ulong& new_row_size){
|
|||
}
|
||||
else if(new_row_size > rows){
|
||||
R.Resize(new_row_size+1);
|
||||
for(ulong i=rows ; i<new_row_size+1 ; ++i){
|
||||
for(ulong i=rows+1 ; i<R.Size(); ++i){
|
||||
R[i] = R[rows];
|
||||
}
|
||||
rows = new_row_size;
|
||||
|
@ -145,14 +139,11 @@ void MatrixCSR<Data>::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<R.Size()-1 ; ++i){
|
||||
for(ulong i=0 ; i<R.Size()-1 ; ++i){ // iterate over the R array
|
||||
last = R[i+1];
|
||||
last_not_deleted = R[i];
|
||||
for(Node** ptr = R[i] ; ptr!=R[i+1] ; ptr = &( (*(*ptr)).next ) ){
|
||||
|
@ -163,15 +154,19 @@ void MatrixCSR<Data>::ColumnResize(const ulong& new_column_size){
|
|||
*ptr = (*(*ptr)).next;
|
||||
delete toDelete;
|
||||
--size;
|
||||
}
|
||||
}
|
||||
for(ulong j=i+1 ; j<R.Size() ; ++j){
|
||||
if(R[j] == last){
|
||||
R[j] = last_not_deleted;
|
||||
|
||||
for(ulong j=i+1 ; j<R.Size() ; ++j){
|
||||
if(R[j] == last){
|
||||
R[j] = last_not_deleted;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
if(ptr == R[i+1]) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
columns = new_column_size;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
|
@ -186,12 +181,14 @@ bool MatrixCSR<Data>::ExistsCell(const ulong& r, const ulong& c) const noexcept{
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& MatrixCSR<Data>::operator()(const ulong& r, const ulong& c) const{
|
||||
const Data& MatrixCSR<Data>::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<Data>::operator()(const ulong& r, const ulong& c) const{
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& MatrixCSR<Data>::operator()(const ulong& r, const ulong& c){
|
||||
Data& MatrixCSR<Data>::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<Data>::operator()(const ulong& r, const ulong& c){
|
|||
}
|
||||
}
|
||||
|
||||
struct List<std::pair<Data,ulong>>::Node* newNode = new Node;
|
||||
struct List<std::pair<Data,ulong>>::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 ; 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
|
||||
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<Data>::Clear(){
|
|||
rows = 0;
|
||||
size = 0;
|
||||
R.Resize(1);
|
||||
R[0] = &head;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
|
@ -269,7 +267,38 @@ void MatrixCSR<Data>::FoldPostOrder(const typename FoldableContainer<Data>::Fold
|
|||
, par , acc);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixCSR<Data>::debug(){
|
||||
std::cout<<std::endl;
|
||||
std::cout<<"rows: "<<rows<<" columns: "<<columns<<" size: "<<size<<"\n\n";
|
||||
Node* tmp = head;
|
||||
|
||||
while(tmp!=nullptr){
|
||||
std::cout<<(tmp->value).first<<"|"<< (tmp->value).second;
|
||||
std::cout<<std::endl;
|
||||
std::cout<<&(tmp->next);
|
||||
|
||||
std::cout<<std::endl;
|
||||
std::cout<<std::endl;
|
||||
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
||||
std::cout << "R VECTOR:" << '\n';
|
||||
for(ulong i=0; i<R.Size();++i){
|
||||
std::cout << R[i] << '\n';
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
std::cout<<std::endl;
|
||||
|
||||
//// print
|
||||
// for(int i=0; i<rows; ++i){
|
||||
// for(int j=0; j<columns ; ++j){
|
||||
// std::cout<<(*this)(i,j)<<" ";
|
||||
// }
|
||||
// std::cout<<std::endl;
|
||||
// }
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
@ -9,13 +9,15 @@
|
|||
#include "../../list/list.hpp"
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
class MatrixCSR : virtual public List<std::pair<Data,ulong>>,
|
||||
virtual public Matrix<Data>{ // Must extend Matrix<Data>
|
||||
|
||||
protected:
|
||||
public: //CAMBIARE A PROTETTO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
using Matrix<Data>::rows;
|
||||
using Matrix<Data>::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<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
|
||||
|
||||
void debug();
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
|
|
@ -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)
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -107,13 +107,13 @@ bool MatrixVec<Data>::ExistsCell(const ulong& r, const ulong& c) const noexcept{
|
|||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& MatrixVec<Data>::operator()(const ulong& r, const ulong& c) const{
|
||||
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 an invalid position!");
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& MatrixVec<Data>::operator()(const ulong& r, const ulong& c){
|
||||
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 an invalid position!");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ void stestMatrixInt(Mat<long>& 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<long>& mat, uint& testnum, uint& testerr) {
|
|||
SetColumnNumber(loctestnum, loctesterr, mat, true, 5); //31
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 1, 4, 8);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 3, 4, 9);
|
||||
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 3);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 3, 4, 9); //33
|
||||
//mat.debug();
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 3); //34
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 4);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
@ -131,7 +131,7 @@ void stestMatrixInt(Mat<long>& mat, uint& testnum, uint& testerr) {
|
|||
|
||||
NonEqualMatrix(loctestnum, loctesterr, mat, copmat);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 0, 3, 4);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 0, 3, 4); //63
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 2, 3, 5);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
@ -143,7 +143,7 @@ void stestMatrixInt(Mat<long>& mat, uint& testnum, uint& testerr) {
|
|||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 40320);
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 40320); //70
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapDecrement<long>, 0);
|
||||
|
||||
|
@ -154,21 +154,21 @@ void stestMatrixInt(Mat<long>& mat, uint& testnum, uint& testerr) {
|
|||
|
||||
Mat<long> 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<long>(loctestnum, loctesterr, movmat, false, 8);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, movmat, true, 4, 2, 8);
|
||||
SetCell<long>(loctestnum, loctesterr, movmat, true, 4, 2, 8); //79
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, movmat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, movmat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, movmat, true, &FoldAdd<long>, 0, 0, 36);
|
||||
|
||||
Exists<long>(loctestnum, loctesterr, movmat, true, 8);
|
||||
Exists<long>(loctestnum, loctesterr, movmat, true, 8); //83
|
||||
|
||||
SetRowNumber(loctestnum, loctesterr, movmat, true, 4);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, movmat, true, &MapPrint<long>, 0);
|
||||
|
@ -176,7 +176,7 @@ void stestMatrixInt(Mat<long>& 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();
|
||||
|
|
|
@ -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<iostream>
|
||||
using namespace std;
|
||||
using namespace lasd;
|
||||
void menu(){
|
||||
|
||||
// ...
|
||||
unsigned short int choice;
|
||||
|
||||
do{
|
||||
std::cout<<std::endl;
|
||||
std::cout<<" 1. Use your tests (to be used by the professor)"<<std::endl;
|
||||
std::cout<<" 2. Use the library demo"<<std::endl;
|
||||
cout<<endl<<" -> ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
}while(choice!=1 && choice!=2);
|
||||
if(choice==1){
|
||||
lasdtest();
|
||||
}else{
|
||||
MatrixCSR<long> 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<mat.RowNumber() ; ++i){
|
||||
for(ulong j=0 ; j<mat.ColumnNumber() ; ++j){
|
||||
cout<<mat(i,j)<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
mat(3,0) = 5;
|
||||
mat(3,1) = 6;
|
||||
mat(3,2) = 7;
|
||||
mat(3,3) = 8;
|
||||
|
||||
for(ulong i=0 ; i<mat.RowNumber() ; ++i){
|
||||
for(ulong j=0 ; j<mat.ColumnNumber() ; ++j){
|
||||
cout<<mat(i,j)<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// ...
|
||||
|
||||
void menu();
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue