Library 2

This commit is contained in:
Alessandro Ferro 2021-04-19 12:32:33 +02:00
parent e079220d78
commit 15e341a6a0
15 changed files with 231 additions and 81 deletions

View File

@ -1,7 +1,7 @@
#! /bin/bash
g++ -O3 -o main \
g++ -g -O3 -o main \
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
zlasdtest/container/container.cpp \

View File

@ -118,7 +118,6 @@ bool List<Data>::operator!=(const List<Data>& list) const noexcept{
tmp->next = head;
head =tmp;
size++;
if(size == 1){
tail = head;
}
@ -158,7 +157,7 @@ bool List<Data>::operator!=(const List<Data>& list) const noexcept{
template <typename Data>
Data List<Data>::FrontNRemove(){
if(head == nullptr){
throw std::length_error("List is empty!");
throw std::length_error("List is empty! (head=null)");
}
else{
Data value = head->value;

BIN
librerie/exercise2/main Executable file

Binary file not shown.

View File

@ -1,8 +1,16 @@
#include "zlasdtest/test.hpp"
#include "zmytest/test.hpp"
#include"container/container.hpp"
#include"list/list.hpp"
#include"vector/vector.hpp"
#include"queue/queue.hpp"
#include"queue/lst/queuelst.hpp"
#include"queue/vec/queuevec.hpp"
#include"stack/stack.hpp"
#include"stack/lst/stacklst.hpp"
#include"stack/vec/stackvec.hpp"
/* ************************************************************************** */
#include <iostream>
@ -12,5 +20,6 @@
int main() {
std::cout << "Lasd Libraries 2020" << std::endl;
lasdtest(); // To call in the menu of your library test!
//menu();
return 0;
}

View File

@ -3,82 +3,87 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueLst(const LinearContainer<Data>& linear){
QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear){
for(ulong i=0; i<linear.Size() ; ++i){
Enqueue(linear[i]);
}
}
template <typename Data>
QueueLst(const QueueLst& copyFrom){
QueueLst<Data>::QueueLst(const QueueLst& copyFrom){
for(ulong i=0; i<copyFrom.Size() ; ++i){
Enqueue(copyFrom[i]);
}
}
template <typename Data>
QueueLst(QueueLst&& moveFrom) noexcept{
QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept{
std::swap(head,moveFrom.head);
std::swap(tail,moveFrom.tail);
std::swap(size,moveFrom.size);
}
~QueueLst(){
template <typename Data>
QueueLst<Data>::~QueueLst(){
Clear();
}
template <typename 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=(copyFrom);
*/
}
QueueLst<Data>& QueueLst<Data>::operator=(const QueueLst& toCopy){
// QueueLst<Data>* tmp = new QueueLst<Data>(toCopy);
// std::swap(*this, *tmp);
// delete tmp;
// return *this;
QueueLst& operator=(QueueLst&& toMove) noexcept{
if(*this != toMove){
std::swap(head, toMove.head);
std::swap(head, toMove.tail);
std::swap(size, toMove.size);
List<Data>::operator=(toCopy);
return *this;
}
bool operator==(const QueueLst& queuelist) const noexcept{
template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(QueueLst&& toMove) noexcept{
List<Data>::operator=(std::move(toMove));
return *this;
}
template <typename Data>
bool QueueLst<Data>::operator==(const QueueLst& queuelist) const noexcept{
return List<Data>::operator==(queuelist);
}
bool operator==(const QueueLst& queuelist) const noexcept{
template <typename Data>
bool QueueLst<Data>::operator!=(const QueueLst& queuelist) const noexcept{
return List<Data>::operator!=(queuelist);
}
template <typename Data>
void Enqueue(const Data& data){
void QueueLst<Data>::Enqueue(const Data& data){
List<Data>::InsertAtBack(data);
}
template <typename Data>
void Enqueue(Data&& data){
void QueueLst<Data>::Enqueue(Data&& data){
List<Data>::InsertAtBack(data);
}
template <typename Data>
Data& Head() const{
Data& QueueLst<Data>::Head() const{
return List<Data>::Front();
}
template <typename Data>
void Dequeue(){
void QueueLst<Data>::Dequeue(){
List<Data>::RemoveFromFront();
}
}
template <typename Data>
Data HeadNDequeue(){
Data QueueLst<Data>::HeadNDequeue(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void QueueLst<Data>::Clear(){
List<Data>::Clear();
}
/* ************************************************************************** */
}

View File

@ -11,19 +11,19 @@ QueueVec<Data>::QueueVec(){
}
template <typename Data>
QueueVec(const LinearContainer<Data>& linear){
QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){
size = linear.Size();
Elements = new Data[size+1]; //forse da espandere
for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i];
}
front = 0;
rear = size;
rear = size; // the vector will be full
}
template <typename Data>
QueueVec<Data>::QueueVec(const QueueVec& toCopy){
size = toCopy.Size();
size = toCopy.size;
ulong index_of_the_element = toCopy.front , i=0;
Elements = new Data[size];
@ -33,11 +33,16 @@ QueueVec<Data>::QueueVec(const QueueVec& toCopy){
index_of_the_element = (index_of_the_element+1)%size;
}
front = 0;
rear = toCopy.rear;
rear = i;
}
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)
*/
size = 4;
std::swap(Elements, toMove.Elements);
std::swap(rear, toMove.rear);
std::swap(front, toMove.front);
@ -45,9 +50,8 @@ QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{
}
template <typename Data>
virtual QueueVec<Data>::~QueueVec(){
//vector destructor will be called
// TEST IT!
QueueVec<Data>::~QueueVec(){
//vector destructor will be called automatically i hope
}
template <typename Data>
@ -118,7 +122,7 @@ Data& QueueVec<Data>::Head() const{
template <typename Data>
void QueueVec<Data>::Dequeue(){
if(Size() == 0){
if(Size() <= 0){
throw std::length_error("Queuevec is empty!");
}
front = (front + 1) % size;
@ -140,13 +144,53 @@ bool QueueVec<Data>::Empty() const noexcept{
}
template <typename Data>
ulong Size() const noexcept{
ulong QueueVec<Data>::Size() const noexcept{
return ((rear + size) - front) % size;
}
template <typename Data>
void Clear(){
void QueueVec<Data>::Clear(){
if(size!=4){
delete[] Elements;
Elements = new Data[4];
size = 4;
}
front = 0;
rear = 0;
}
template <typename Data>
void QueueVec<Data>::Expand(){
Data* tmp = new Data[size * 2];
ulong current_index = front , i=0;
while(current_index != rear){
tmp[i] = Elements[current_index];
current_index = (current_index+1)%size;
++i;
}
delete[] Elements;
Elements = tmp;
front = 0;
rear = i;
size *= 2;
}
template <typename Data>
void QueueVec<Data>::Reduce(){
if(size<=4) return; // we are not going to have vectors with less than 4 Elements
ulong newsize = (ulong)size/2;
Data* tmp = new Data[newsize];
ulong current_index = front , i=0;
while(i < newsize){
tmp[i] = Elements[current_index];
current_index = (current_index+1)%size;
++i;
}
delete[] Elements;
Elements = tmp;
front = 0;
rear = i;
size *= 2;
}
/* ************************************************************************** */

View File

@ -22,7 +22,7 @@ private:
protected:
using Vector<Data>::Elements;
using Vector<Data>::size;
using Vector<Data>::size; // dimension of the array
ulong front = 0;
ulong rear = 0;
@ -89,6 +89,7 @@ protected:
void Expand();
void Reduce();
//void SwapVectors(arguments) specifiers;
};

View File

@ -5,8 +5,8 @@ namespace lasd {
// Constructors
template <typename Data>
StackLst(const LinearContainer<Data>& linear){
for(ulong i=linear.Size()-1 ; i>=0 ; --i){ // è possibile usare il costruttore di List?
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]);
}
}
@ -17,9 +17,9 @@ StackLst(const LinearContainer<Data>& linear) : List<Data>(linear){
return;
}
*/
StackLst(const StackLst& stcklist){
for(ulong i=stcklist.Size()-1 ; i>=0 ; --i){
template <typename Data>
StackLst<Data>::StackLst(const StackLst& stcklist){
for(long int i=(long int)stcklist.Size()-1 ; i>=0 ; i--){
Push(stcklist[i]);
}
}
@ -35,62 +35,67 @@ StackLst<Data>::StackLst(StackLst&& stcklist) noexcept{
}
// Destructor
virtual ~StackLst(){
template <typename Data>
StackLst<Data>::~StackLst(){
Clear();
}
StackLst& operator=(const StackLst& copyFrom){
template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(const StackLst& copyFrom){
if(*this != copyFrom){
Clear();
for(ulong i=copyFrom.Size()-1 ; i>=0 ; --i){
for(long int i=(long int)copyFrom.Size()-1 ; i>=0 ; --i){
Push(copyFrom[i]);
}
}
return *this;
}
StackLst& operator=(StackLst&& moveFrom){
template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(StackLst&& moveFrom) noexcept{
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
return *this;
}
bool operator==(const StackLst& stcklist) const noexcept{
template <typename Data>
bool StackLst<Data>::operator==(const StackLst& stcklist) const noexcept{
return List<Data>::operator==(stcklist);
}
bool operator!=(const StackLst& stcklist) const noexcept{
template <typename Data>
bool StackLst<Data>::operator!=(const StackLst& stcklist) const noexcept{
return List<Data>::operator!=(stcklist);
}
// Specific member functions (inherited from Stack)
template <typename Data>
void Push(const Data& element){
void StackLst<Data>::Push(const Data& element){
List<Data>::InsertAtFront(element);
}
template <typename Data>
void Push(Data&& element){
void StackLst<Data>::Push(Data&& element) noexcept{
List<Data>::InsertAtFront(element);
}
template <typename Data>
Data& Top() const{
Data& StackLst<Data>::Top() const{
return List<Data>::Front();
}
template <typename Data>
void Pop(){
void StackLst<Data>::Pop(){
List<Data>::RemoveFromFront();
}
template <typename Data>
Data TopNPop(){
Data StackLst<Data>::TopNPop(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void Clear(){
void StackLst<Data>::Clear(){
List<Data>::Clear();
}

View File

@ -6,6 +6,7 @@ namespace lasd {
template <typename Data>
StackVec<Data>::StackVec(){
size = 4; // default vector is instantiated with 4 cells
stackSize = 0;
Elements = new Data[size];
}
@ -21,12 +22,12 @@ StackVec<Data>::StackVec(const LinearContainer<Data>& linear){ // si può richia
template <typename Data>
StackVec<Data>::StackVec(const StackVec& stckvec){// si può richiamare il costruttore della classe Vector
Elements = new Data[stckvec.Size()]; // espandere di un po' forse
for(ulong i=0 ; i<stckvec.Size() ; ++i){
Elements = new Data[stckvec.size]; // espandere di un po' forse
for(ulong i=0 ; i<stckvec.size ; ++i){
Elements[i] = stckvec[i];
}
size = stckvec.Size();
stackSize = stckvec.stackSize;
size = stckvec.size;
stackSize = stckvec.Size();
}
/*
StackVec(const StackVec& stckvec) : Vector<Data>(copyFrom)
@ -39,32 +40,42 @@ StackVec<Data>::StackVec(StackVec&& toMove) noexcept{
std::swap(stackSize, toMove.stackSize);
}
template <typename Data>
StackVec<Data>::~StackVec(){
// Vector destructor will be called automatically
// (TEST IT)
}
template <typename Data>
StackVec& StackVec<Data>::operator=(const StackVec& copyFrom){
StackVec<Data>& StackVec<Data>::operator=(const StackVec<Data>& copyFrom){
Vector<Data>::operator=(copyFrom); // espandere di un po' forse
stackSize = copyFrom.Size();
return *this;
}
template <typename Data>
StackVec& StackVec<Data>::operator=(StackVec&& moveFrom) noexcept{
StackVec<Data>& StackVec<Data>::operator=(StackVec<Data>&& moveFrom) noexcept{
Vector<Data>::operator=(std::move(moveFrom)); // espandere di un po' forse
stackSize = copyFrom.Size();
stackSize = moveFrom.Size();
}
template <typename Data>
bool StackVec<Data>::operator==(const StackVec& toCompare) const noexcept{
Vector<Data>::operator==(toCompare);
bool StackVec<Data>::operator==(const StackVec<Data>& toCompare) const noexcept{
if(stackSize == toCompare.Size()){
for(ulong i=0 ; i<stackSize ; ++i){
if(Elements[i] != toCompare[i]){
return false;
}
}
return true;
}else{
return false;
}
}
template <typename Data>
bool StackVec<Data>::operator!=(const StackVec& toCompare) const noexcept{
Vector<Data>::operator!=(toCompare);
bool StackVec<Data>::operator!=(const StackVec<Data>& toCompare) const noexcept{
return !(*this == toCompare);
}
// Specific member functions (inherited from Stack)
@ -83,20 +94,21 @@ void StackVec<Data>::Push(Data&& data){
Expand();
}
std::swap(Elements[stackSize], data);
++stackSize;
}
template <typename Data>
Data& StackVec<Data>::Top() const{
if(stackSize == 0){
throw std::length_error("Empty Stack!");
throw std::length_error("Empty Stack! (TOP)");
}
return Elements[stackSize-1]
return Elements[stackSize-1];
}
template <typename Data>
void StackVec<Data>::Pop(){
if(stackSize<=0){
throw std::length_error("Empty Stack!");
if(stackSize==0){
throw std::length_error("Empty Stack! (POP)");
}
--stackSize;
if(stackSize < (int)(size/4)){
@ -131,6 +143,13 @@ void StackVec<Data>::Reduce(){
Vector<Data>::Resize((ulong)size/2);
}
template <typename Data>
void StackVec<Data>::Clear(){
delete [] Elements;
size = 4;
stackSize = 0;
Elements = new Data[size];
}
/* ************************************************************************** */
}

View File

@ -23,7 +23,7 @@ protected:
ulong stackSize = 0; // first empty cell
using Vector<Data>::Elements;
using Vector<Data>::size;
using Vector<Data>::size; // dimension of the vector
public:

View File

@ -13,7 +13,7 @@ using namespace std;
void lasdtest() {
cout << endl << "~*~#~*~ Welcome to the LASD Test Suite ~*~#~*~ " << endl;
testSimpleExercise1();
//testSimpleExercise1();
testFullExercise1();
testSimpleExercise2();
testFullExercise2();

View File

@ -1,2 +1,49 @@
#include"test.hpp"
#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();
*/
QueueLst<int> q1;
q1.Enqueue(4);
q1.Enqueue(0);
q1.Enqueue(3);
q1.Enqueue(1);
q1.Enqueue(2);
// QueueLst<int>* tmp = new QueueLst<int>(q1);
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
// (*tmp).HeadNDequeue();
QueueLst<int> q2;
q2 = std::move(q1);
q2.HeadNDequeue();
q2.HeadNDequeue();
q2.HeadNDequeue();
q2.HeadNDequeue();
q2.HeadNDequeue();
}

View File

@ -1,11 +1,19 @@
#ifndef MYTEST_HPP
#define MYTEST_HPP
#include<iostream>
#include"../vector/vector.hpp"
#include"../list/list.hpp"
#include"../queue/queue.hpp"
#include"../queue/lst/queuelst.hpp"
#include"../queue/vec/queuevec.hpp"
#include"../stack/stack.hpp"
#include"../stack/lst/stacklst.hpp"
#include"../stack/vec/stackvec.hpp"
/* ************************************************************************** */
// ...
void menu();
/* ************************************************************************** */
#include"test.cpp"
#endif

BIN
teoria/a.out Executable file

Binary file not shown.

13
teoria/prova.cpp Normal file
View File

@ -0,0 +1,13 @@
#include<iostream>
using namespace std;
ulong Size(){
ulong a = 6;
return a;
}
int main(){
ulong j = Size();
cout<<j<<endl;
for(long int x=(long int)Size() ; x>=0 ; --x){
cout<<x<<endl;
}
}