From e079220d7809a64566b7ec42cc5dc380f1d5dcc7 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Thu, 15 Apr 2021 12:34:16 +0200 Subject: [PATCH] Library 2 queuevec --- librerie/exercise2/queue/vec/queuevec.cpp | 133 +++++++++++++++++++++- librerie/exercise2/queue/vec/queuevec.hpp | 2 +- librerie/exercise2/vector/vector.hpp | 2 +- 3 files changed, 134 insertions(+), 3 deletions(-) diff --git a/librerie/exercise2/queue/vec/queuevec.cpp b/librerie/exercise2/queue/vec/queuevec.cpp index 0340872..69a540b 100755 --- a/librerie/exercise2/queue/vec/queuevec.cpp +++ b/librerie/exercise2/queue/vec/queuevec.cpp @@ -5,18 +5,149 @@ namespace lasd { template QueueVec::QueueVec(){ size = 4; + rear = 0; + front = 0; Elements = new Data[size]; } template QueueVec(const LinearContainer& linear){ size = linear.Size(); - Elements = new Data[size]; + Elements = new Data[size+1]; //forse da espandere for(ulong i=0 ; i +QueueVec::QueueVec(const QueueVec& toCopy){ + size = toCopy.Size(); + ulong index_of_the_element = toCopy.front , i=0; + Elements = new Data[size]; + + while(index_of_the_element != toCopy.rear){ + Elements[i] = toCopy[index_of_the_element]; + ++i; + index_of_the_element = (index_of_the_element+1)%size; + } + front = 0; + rear = toCopy.rear; +} + +template +QueueVec::QueueVec(QueueVec&& toMove) noexcept{ + std::swap(Elements, toMove.Elements); + std::swap(rear, toMove.rear); + std::swap(front, toMove.front); + std::swap(size, toMove.size); +} + +template +virtual QueueVec::~QueueVec(){ + //vector destructor will be called + // TEST IT! +} + +template +QueueVec& QueueVec::operator=(const QueueVec& toCopy){ + QueueVec* tmpQueue = new QueueVec(toCopy); + std::swap(*tmpQueue, *this); + delete tmpQueue; + return *this; +} + +template +QueueVec& QueueVec::operator=(QueueVec&& toMove) noexcept{ + std::swap(Elements, toMove.Elements); + std::swap(size, toMove.size); + std::swap(rear, toMove.rear); + std::swap(front, toMove.front); + return *this; +} + +template +bool QueueVec::operator==(const QueueVec& toCompare) const noexcept{ + if(Size() == toCompare.Size()){ + ulong indexToCompare = toCompare.front; + ulong index = front; + while(indexToCompare != toCompare.rear){ + if(Elements[index]!=toCompare[indexToCompare]){ + return false; + } + index = (index+1)%size; + indexToCompare = (indexToCompare+1)%size; + } + return true; + }else{ + return false; + } +} + +template +bool QueueVec::operator!=(const QueueVec& toCompare) const noexcept{ + return !(*this == toCompare); +} + +template +void QueueVec::Enqueue(const Data& data){ + if((rear+1)%size == front){ + Expand(); + } + Elements[rear] = data; + rear = (rear + 1) % size; +} + +template +void QueueVec::Enqueue(Data&& data){ + if((rear+1)%size == front){ + Expand(); + } + std::swap(Elements[rear],data); + rear = (rear + 1) % size; +} + +template +Data& QueueVec::Head() const{ + if(Size()<=0){ + throw std::length_error("Queuevec is empty!"); + } + return Elements[front]; +} + +template +void QueueVec::Dequeue(){ + if(Size() == 0){ + throw std::length_error("Queuevec is empty!"); + } + front = (front + 1) % size; + if(Size() < size/4){ + Reduce(); + } +} + +template +Data QueueVec::HeadNDequeue(){ + Data tmp = Head(); + Dequeue(); + return tmp; +} + +template +bool QueueVec::Empty() const noexcept{ + return (front == rear); +} + +template +ulong Size() const noexcept{ + return ((rear + size) - front) % size; +} + +template +void Clear(){ + +} /* ************************************************************************** */ } diff --git a/librerie/exercise2/queue/vec/queuevec.hpp b/librerie/exercise2/queue/vec/queuevec.hpp index 2559c72..d7369d6 100755 --- a/librerie/exercise2/queue/vec/queuevec.hpp +++ b/librerie/exercise2/queue/vec/queuevec.hpp @@ -79,7 +79,7 @@ public: bool Empty() const noexcept override; // Override Container member - unsigned long Size() const noexcept override; // Override Container member + ulong Size() const noexcept override; // Override Container member void Clear() override; // Override Container member diff --git a/librerie/exercise2/vector/vector.hpp b/librerie/exercise2/vector/vector.hpp index b5fcbe5..4ca2084 100644 --- a/librerie/exercise2/vector/vector.hpp +++ b/librerie/exercise2/vector/vector.hpp @@ -56,7 +56,7 @@ public: /* ************************************************************************ */ // Copy assignment - Vector& operator=(const Vector&); + Vector& operator=(const Vector&); // Move assignment Vector& operator=(Vector&&) noexcept;