Library 2

Cleaner code
This commit is contained in:
Alessandro Ferro 2021-04-20 18:16:51 +02:00
parent 3fdd2e86cb
commit aa9dcc16cf
8 changed files with 158 additions and 113 deletions

Binary file not shown.

View File

@ -19,7 +19,7 @@
int main() { int main() {
std::cout << "Lasd Libraries 2020" << std::endl; std::cout << "Lasd Libraries 2020" << std::endl;
lasdtest(); // To call in the menu of your library test! lasdtest();
//menu(); menu();
return 0; return 0;
} }

View File

@ -1,27 +1,17 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */
template <typename Data> template <typename Data>
QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear){ QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear)
for(ulong i=0; i<linear.Size() ; ++i){ : List<Data>(linear) {}
Enqueue(linear[i]);
}
}
template <typename Data> template <typename Data>
QueueLst<Data>::QueueLst(const QueueLst& copyFrom){ QueueLst<Data>::QueueLst(const QueueLst& copyFrom)
for(ulong i=0; i<copyFrom.Size() ; ++i){ : List<Data>(copyFrom) {}
Enqueue(copyFrom[i]);
}
}
template <typename Data> template <typename Data>
QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept{ QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept
std::swap(head,moveFrom.head); : List<Data>(std::move(moveFrom)) {}
std::swap(tail,moveFrom.tail);
std::swap(size,moveFrom.size);
}
template <typename Data> template <typename Data>
QueueLst<Data>::~QueueLst(){ QueueLst<Data>::~QueueLst(){
@ -30,11 +20,6 @@ QueueLst<Data>::~QueueLst(){
template <typename Data> template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(const QueueLst& toCopy){ QueueLst<Data>& QueueLst<Data>::operator=(const QueueLst& toCopy){
// QueueLst<Data>* tmp = new QueueLst<Data>(toCopy);
// std::swap(*this, *tmp);
// delete tmp;
// return *this;
List<Data>::operator=(toCopy); List<Data>::operator=(toCopy);
return *this; return *this;
} }
@ -62,7 +47,7 @@ template <typename Data>
template <typename Data> template <typename Data>
void QueueLst<Data>::Enqueue(Data&& data){ void QueueLst<Data>::Enqueue(Data&& data){
List<Data>::InsertAtBack(data); List<Data>::InsertAtBack(std::move(data));
} }
template <typename Data> template <typename Data>
@ -84,6 +69,5 @@ template <typename Data>
void QueueLst<Data>::Clear(){ void QueueLst<Data>::Clear(){
List<Data>::Clear(); List<Data>::Clear();
} }
/* ************************************************************************** */
} }

View File

@ -1,7 +1,6 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */
template <typename Data> template <typename Data>
QueueVec<Data>::QueueVec(){ QueueVec<Data>::QueueVec(){
size = 4; size = 4;
@ -12,7 +11,7 @@ QueueVec<Data>::QueueVec(){
template <typename Data> template <typename Data>
QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){ QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){
size = linear.Size()+1; size = linear.Size()+1; // 1 free cell
Elements = new Data[size]; //forse da espandere Elements = new Data[size]; //forse da espandere
for(ulong i=0 ; i<linear.Size() ; ++i){ for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i]; Elements[i] = linear[i];
@ -38,10 +37,8 @@ QueueVec<Data>::QueueVec(const QueueVec& toCopy){
template <typename Data> template <typename Data>
QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{ QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{
/* /* we initialize size=4 so the swapped vector won't be in an
we initialize size=4 so the swapped vector won't be in an inconsistent state (size=0 can never be acceptable) */
inconsistent state (size=0 can never be accettable)
*/
size = 4; size = 4;
std::swap(Elements, toMove.Elements); std::swap(Elements, toMove.Elements);
std::swap(rear, toMove.rear); std::swap(rear, toMove.rear);
@ -51,7 +48,7 @@ QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{
template <typename Data> template <typename Data>
QueueVec<Data>::~QueueVec(){ QueueVec<Data>::~QueueVec(){
//vector destructor will be called automatically i hope //vector destructor will be automatically called I hope
} }
template <typename Data> template <typename Data>
@ -64,6 +61,8 @@ QueueVec<Data>& QueueVec<Data>::operator=(const QueueVec& toCopy){
template <typename Data> template <typename Data>
QueueVec<Data>& QueueVec<Data>::operator=(QueueVec&& toMove) noexcept{ QueueVec<Data>& QueueVec<Data>::operator=(QueueVec&& toMove) noexcept{
/* here we do not need size=4 since the QueueVec with at least size=4
exists already */
std::swap(Elements, toMove.Elements); std::swap(Elements, toMove.Elements);
std::swap(size, toMove.size); std::swap(size, toMove.size);
std::swap(rear, toMove.rear); std::swap(rear, toMove.rear);
@ -115,7 +114,7 @@ void QueueVec<Data>::Enqueue(Data&& data){
template <typename Data> template <typename Data>
Data& QueueVec<Data>::Head() const{ Data& QueueVec<Data>::Head() const{
if(Size()<=0){ if(Size()<=0){
throw std::length_error("Queuevec is empty!"); throw std::length_error("Queue is empty!");
} }
return Elements[front]; return Elements[front];
} }
@ -123,7 +122,7 @@ Data& QueueVec<Data>::Head() const{
template <typename Data> template <typename Data>
void QueueVec<Data>::Dequeue(){ void QueueVec<Data>::Dequeue(){
if(Size() <= 0){ if(Size() <= 0){
throw std::length_error("Queuevec is empty!"); throw std::length_error("Queue is empty!");
} }
front = (front + 1) % size; front = (front + 1) % size;
if(Size() < size/4){ if(Size() < size/4){
@ -192,6 +191,5 @@ void QueueVec<Data>::Reduce(){
rear = i; rear = i;
size *= 2; size *= 2;
} }
/* ************************************************************************** */
} }

View File

@ -5,34 +5,16 @@ namespace lasd {
// Constructors // Constructors
template <typename Data> template <typename Data>
StackLst<Data>::StackLst(const LinearContainer<Data>& linear){ StackLst<Data>::StackLst(const LinearContainer<Data>& linear)
for(long int i=(long int)linear.Size()-1 ; i>=0 ; --i){ // è possibile usare il costruttore di List? : List<Data>(linear){}
Push(linear[i]);
}
}
/*
PROVARE
template <typename Data>
StackLst(const LinearContainer<Data>& linear) : List<Data>(linear){
return;
}
*/
template <typename Data>
StackLst<Data>::StackLst(const StackLst& stcklist){
for(long int i=(long int)stcklist.Size()-1 ; i>=0 ; i--){
Push(stcklist[i]);
}
}
/*
PROVARE
StackLst(const StackLst& stcklist) : List<Data>(stcklist){}
*/
template <typename Data> template <typename Data>
StackLst<Data>::StackLst(StackLst&& stcklist) noexcept{ StackLst<Data>::StackLst(const StackLst& stcklist)
std::swap(size, stcklist.size); : List<Data>(stcklist){}
std::swap(head, stcklist.head);
} template <typename Data>
StackLst<Data>::StackLst(StackLst&& stcklist) noexcept
: List<Data>(std::move(stcklist)){}
// Destructor // Destructor
template <typename Data> template <typename Data>
@ -42,19 +24,13 @@ StackLst<Data>::~StackLst(){
template <typename Data> template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(const StackLst& copyFrom){ StackLst<Data>& StackLst<Data>::operator=(const StackLst& copyFrom){
if(*this != copyFrom){ List<Data>::operator=(copyFrom);
Clear();
for(long int i=(long int)copyFrom.Size()-1 ; i>=0 ; --i){
Push(copyFrom[i]);
}
}
return *this; return *this;
} }
template <typename Data> template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(StackLst&& moveFrom) noexcept{ StackLst<Data>& StackLst<Data>::operator=(StackLst&& moveFrom) noexcept{
std::swap(size, moveFrom.size); List<Data>::operator=(std::move(moveFrom));
std::swap(head, moveFrom.head);
return *this; return *this;
} }

View File

@ -10,6 +10,12 @@ StackVec<Data>::StackVec(){
Elements = new Data[size]; Elements = new Data[size];
} }
template <typename Data>
StackVec<Data>::StackVec(const LinearContainer<Data>& linear)
: Vector<Data>(linear){
stackSize = linear.Size(); // the array is full
}
/*
template <typename Data> template <typename Data>
StackVec<Data>::StackVec(const LinearContainer<Data>& linear){ // si può richiamare il costruttore della classe Vector StackVec<Data>::StackVec(const LinearContainer<Data>& linear){ // si può richiamare il costruttore della classe Vector
Elements = new Data[linear.Size()]; // espandere di un po' forse Elements = new Data[linear.Size()]; // espandere di un po' forse
@ -19,6 +25,7 @@ StackVec<Data>::StackVec(const LinearContainer<Data>& linear){ // si può richia
size = linear.Size(); size = linear.Size();
stackSize = linear.Size(); stackSize = linear.Size();
} }
*/
template <typename Data> template <typename Data>
StackVec<Data>::StackVec(const StackVec& stckvec){// si può richiamare il costruttore della classe Vector StackVec<Data>::StackVec(const StackVec& stckvec){// si può richiamare il costruttore della classe Vector

View File

@ -2,56 +2,131 @@
#include<iostream> #include<iostream>
#include<random> #include<random>
#include "../zlasdtest/test.hpp" #include "../zlasdtest/test.hpp"
using namespace lasd; using namespace lasd;
void menu(){ void menu(){
/* /*
QueueVec<int> q1; !!! BUG !!!
q1.Enqueue(4);
q1.Enqueue(0);
q1.Enqueue(3);
q1.Enqueue(1);
q1.Enqueue(2);
q1.HeadNDequeue();
q1.Enqueue(5);
QueueVec<int> q2;
q2 = std::move(q1);
QueueVec<int> q3(std::move(q1));
std::cout<<q1.Head();
*/ */
QueueLst<int> q1; /*
q1.Enqueue(4); Vector<int> v(5);
q1.Enqueue(0); v[0] = 0;
q1.Enqueue(3); v[1] = 1;
q1.Enqueue(1); v[2] = 2;
q1.Enqueue(2); v[3] = 3;
// QueueLst<int>* tmp = new QueueLst<int>(q1); StackVec<int> sv;
// (*tmp).HeadNDequeue(); sv.Push(0);
// (*tmp).HeadNDequeue(); sv.Push(1);
// (*tmp).HeadNDequeue(); sv.Push(2);
// (*tmp).HeadNDequeue(); sv.Push(3);
// (*tmp).HeadNDequeue();
StackVec<int> sv2(sv);
if(sv2 == v) std::cout<<"OK";
else std::cout<<"NO";
*/
// QueueLst<int> q2; QueueLst<int> q;
// q2 = std::move(q1); int a =3;
// q.Enqueue(std::move(a));
// q2.HeadNDequeue();
// q2.HeadNDequeue();
// q2.HeadNDequeue();
// q2.HeadNDequeue();
// q2.HeadNDequeue();
QueueVec<int> quevec;
quevec.Enqueue(0);
quevec.Enqueue(1);
QueueVec<int> newquevec = quevec;
if(quevec == newquevec) std::cout<<"uguali";
} }
/*
void menu(){
unsigned short int choice;
DataStructure chosenDataStructure;
DataType chosenDataType;
Implementation chosenImplementation;
do{
std::cout<<"1. Use your tests (to be used by the professor)"<<std::endl;
std::cout<<"2. Use the library demo"<<std::endl;
std::cin>>std::ws;
std::cin>>choice;
}while(choice!=1 && choice!=2);
switch(choice){
case 1:
lasdtest();
break;
case 2:
chosenDataStructure = ChooseDataStructure();
chosenDataType = ChooseDataType();
chosenImplementation = ChooseImplementation();
UseChosenType(chosenDataStructure,chosenDataType, chosenImplementation);
break;
default:
std::cout<<"An error has occurred"<<std::endl;
return;
}
}
DataStructure ChooseDataStructure(){
unsigned short int choice;
do{
std::cout<<"Choose a data structure:"<<std::endl;
std::cout<<"1. Stack"<<std::endl;
std::cout<<"2. Queue"<<std::endl;
std::cin>>std::ws;
std::cin>>choice;
}while(choice!=1 && choice!=2);
if(choice == 1)
return DataStructure::stack;
else if(choice == 2)
return DataStructure::queue;
}
DataType ChooseDataType(){
unsigned short int choice;
do{
std::cout<<"Choose a data type:"<<std::endl;
std::cout<<"1. Integer"<<std::endl;
std::cout<<"2. Float"<<std::endl;
std::cout<<"3. String"<<std::endl;
std::cin>>std::ws;
std::cin>>choice;
}while(!(choice>0 && choice<4));
if(choice==1)
return DataType::integer;
else if(choice==2)
return DataType::ffloat;
else if(choice==3)
return DataType::sstring;
}
Implementation ChooseImplementation(){
unsigned short int choice;
do{
std::cout<<"Choose an implementation:"<<std::endl;
std::cout<<"1. Vector"<<std::endl;
std::cout<<"2. List"<<std::endl;
std::cin>>std::ws;
std::cin>>choice;
}while(!(choice>0 && choice<4));
if(choice==1)
return Implementation::vector;
else if(choice==2)
return Implementation::list;
}
void UseChosenType(DataStructure chosenDataStructure, DataType chosenDataType, Implementation chosenImplementation){
if(chosenDataStructure == DataStructure::stack){
if(chosenImplementation == Implementation::vector){
if(chosenDataType == DataType::integer){
lasd::StackVec<int> myvec;
myvec = generateVectorOfIntegers();
VectorIntegerFunctions(myvec);
}else if(chosenDataType == DataType::ffloat){
lasd::StackVec<float> myvec;
myvec = generateVectorOfFloat();
VectorFloatFunctions(myvec);
}else if(chosenDataType == DataType::sstring){
lasd::StackVec<std::string> myvec;
myvec = generateVectorOfStrings();
VectorStringFunctions(myvec);
}
}
}
}
*/

View File

@ -11,8 +11,13 @@
#include"../stack/lst/stacklst.hpp" #include"../stack/lst/stacklst.hpp"
#include"../stack/vec/stackvec.hpp" #include"../stack/vec/stackvec.hpp"
/* ************************************************************************** */ /* ************************************************************************** */
enum class DataStructure{stack,queue};
enum class DataType{integer,ffloat,sstring};
enum class Implementation{vector,list};
void menu(); void menu();
DataType ChooseDataType(); //choose data type
DataStructure ChooseDataStructure();
void UseChosenType(DataStructure, DataType);
/* ************************************************************************** */ /* ************************************************************************** */
#include"test.cpp" #include"test.cpp"