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() {
std::cout << "Lasd Libraries 2020" << std::endl;
lasdtest(); // To call in the menu of your library test!
//menu();
lasdtest();
menu();
return 0;
}

View File

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

View File

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

View File

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

View File

@ -10,6 +10,12 @@ StackVec<Data>::StackVec(){
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>
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
@ -19,6 +25,7 @@ StackVec<Data>::StackVec(const LinearContainer<Data>& linear){ // si può richia
size = linear.Size();
stackSize = linear.Size();
}
*/
template <typename Data>
StackVec<Data>::StackVec(const StackVec& stckvec){// si può richiamare il costruttore della classe Vector

View File

@ -2,56 +2,131 @@
#include<iostream>
#include<random>
#include "../zlasdtest/test.hpp"
using namespace lasd;
void menu(){
/*
QueueVec<int> q1;
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();
/*
!!! BUG !!!
*/
QueueLst<int> q1;
q1.Enqueue(4);
q1.Enqueue(0);
q1.Enqueue(3);
q1.Enqueue(1);
q1.Enqueue(2);
/*
Vector<int> v(5);
v[0] = 0;
v[1] = 1;
v[2] = 2;
v[3] = 3;
// QueueLst<int>* tmp = new QueueLst<int>(q1);
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
StackVec<int> sv;
sv.Push(0);
sv.Push(1);
sv.Push(2);
sv.Push(3);
StackVec<int> sv2(sv);
if(sv2 == v) std::cout<<"OK";
else std::cout<<"NO";
*/
// QueueLst<int> q2;
// q2 = std::move(q1);
//
// 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";
QueueLst<int> q;
int a =3;
q.Enqueue(std::move(a));
}
/*
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/vec/stackvec.hpp"
/* ************************************************************************** */
enum class DataStructure{stack,queue};
enum class DataType{integer,ffloat,sstring};
enum class Implementation{vector,list};
void menu();
DataType ChooseDataType(); //choose data type
DataStructure ChooseDataStructure();
void UseChosenType(DataStructure, DataType);
/* ************************************************************************** */
#include"test.cpp"