diff --git a/librerie/exercise2/list/list.cpp b/librerie/exercise2/list/list.cpp index 7f5b3b4..cb7682f 100644 --- a/librerie/exercise2/list/list.cpp +++ b/librerie/exercise2/list/list.cpp @@ -56,7 +56,7 @@ template List::List(const List& copyFrom){ for(ulong i = 0; i +QueueLst(const LinearContainer& linear){ + for(ulong i=0; i +QueueLst(const QueueLst& copyFrom){ + for(ulong i=0; i +QueueLst(QueueLst&& moveFrom) noexcept{ + std::swap(head,moveFrom.head); + std::swap(tail,moveFrom.tail); + std::swap(size,moveFrom.size); +} + +~QueueLst(){ + Clear(); +} + +template +QueueLst& operator=(const QueueLst& toCopy){ + QueueLst* tmp = new QueueLst(toCopy); + std::swap(*this, *tmp); + delete tmp; + return *this; + /* + List::operator=(copyFrom); + */ +} + +QueueLst& operator=(QueueLst&& toMove) noexcept{ + if(*this != toMove){ + std::swap(head, toMove.head); + std::swap(head, toMove.tail); + std::swap(size, toMove.size); + return *this; + } + + bool operator==(const QueueLst& queuelist) const noexcept{ + return List::operator==(queuelist); + } + + bool operator==(const QueueLst& queuelist) const noexcept{ + return List::operator!=(queuelist); + } + +template + void Enqueue(const Data& data){ + List::InsertAtBack(data); + } + +template + void Enqueue(Data&& data){ + List::InsertAtBack(data); + } + +template + Data& Head() const{ + return List::Front(); + } + +template + void Dequeue(){ + List::RemoveFromFront(); + } +} +template +Data HeadNDequeue(){ + return List::FrontNRemove(); +} /* ************************************************************************** */ diff --git a/librerie/exercise2/queue/lst/queuelst.hpp b/librerie/exercise2/queue/lst/queuelst.hpp index 4f1954c..92ea410 100755 --- a/librerie/exercise2/queue/lst/queuelst.hpp +++ b/librerie/exercise2/queue/lst/queuelst.hpp @@ -14,70 +14,70 @@ namespace lasd { /* ************************************************************************** */ template -class QueueLst { // Must extend Queue and List +class QueueLst : virtual public Queue, + virtual protected List{ // Must extend Queue and List private: - // ... - protected: - // using List::???; - - // ... + using List::head; + using List::tail; + using List::size; + using typename List::Node; public: // Default constructor - // QueueLst() specifier; + QueueLst() = default; /* ************************************************************************ */ // Specific constructor - // QueueLst(argument) specifiers; // A queue obtained from a LinearContainer + QueueLst(const LinearContainer&); // A queue obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // QueueLst(argument); + QueueLst(const QueueLst&); // Move constructor - // QueueLst(argument); + QueueLst(QueueLst&&) noexcept; /* ************************************************************************ */ // Destructor - // ~QueueLst() specifier; + ~QueueLst(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + QueueLst& operator=(const QueueLst&); // Move assignment - // type operator=(argument); + QueueLst& operator=(QueueLst&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const QueueLst&) const noexcept; + bool operator!=(const QueueLst&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Queue) - // type Enqueue(argument) specifiers; // Override Queue member (copy of the value) - // type Enqueue(argument) specifiers; // Override Queue member (move of the value) - // type Head() specifiers; // Override Queue member (must throw std::length_error when empty) - // type Dequeue() specifiers; // Override Queue member (must throw std::length_error when empty) - // type HeadNDequeue() specifiers; // Override Queue member (must throw std::length_error when empty) + void Enqueue(const Data&) override; // Override Queue member (copy of the value) + void Enqueue(Data&&) override; // Override Queue member (move of the value) + Data& Head() const override; // Override Queue member (must throw std::length_error when empty) + void Dequeue() override; // Override Queue member (must throw std::length_error when empty) + Data HeadNDequeue() override; // Override Queue member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member }; diff --git a/librerie/exercise2/queue/queue.hpp b/librerie/exercise2/queue/queue.hpp index 6b612e4..ba810d8 100755 --- a/librerie/exercise2/queue/queue.hpp +++ b/librerie/exercise2/queue/queue.hpp @@ -13,44 +13,40 @@ namespace lasd { /* ************************************************************************** */ template -class Queue { // Must extend Container +class Queue : virtual public Container{ // Must extend Container private: - // ... - protected: - // ... - public: // Destructor - // ~Queue() specifiers + ~Queue() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Queue& operator=(const Queue&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Queue& operator=(Queue&&) = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const Queue&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const Queue&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Enqueue(argument) specifiers; // Copy of the value - // type Enqueue(argument) specifiers; // Move of the value - // type Head() specifiers; // (concrete function must throw std::length_error when empty) - // type Dequeue() specifiers; // (concrete function must throw std::length_error when empty) - // type HeadNDequeue() specifiers; // (concrete function must throw std::length_error when empty) + virtual void Enqueue(const Data&) = 0; // Copy of the value + virtual void Enqueue(Data&&) = 0; // Move of the value + virtual Data& Head() const = 0; // (concrete function must throw std::length_error when empty) + virtual void Dequeue() = 0; // (concrete function must throw std::length_error when empty) + virtual Data HeadNDequeue() = 0; // (concrete function must throw std::length_error when empty) }; diff --git a/librerie/exercise2/queue/vec/queuevec.cpp b/librerie/exercise2/queue/vec/queuevec.cpp index d374ceb..0340872 100755 --- a/librerie/exercise2/queue/vec/queuevec.cpp +++ b/librerie/exercise2/queue/vec/queuevec.cpp @@ -2,8 +2,20 @@ namespace lasd { /* ************************************************************************** */ +template +QueueVec::QueueVec(){ + size = 4; + Elements = new Data[size]; +} -// ... +template +QueueVec(const LinearContainer& linear){ + size = linear.Size(); + Elements = new Data[size]; + for(ulong i=0 ; i -class QueueVec { // Must extend Queue and Vector +class QueueVec : virtual public Queue, + virtual protected Vector{ // Must extend Queue and Vector private: - // ... - protected: - // using Vector::???; - - // ... + using Vector::Elements; + using Vector::size; + ulong front = 0; + ulong rear = 0; public: // Default constructor - // QueueVec() specifier; + QueueVec(); /* ************************************************************************ */ // Specific constructor - // QueueVec(argument) specifiers; // A queue obtained from a LinearContainer + QueueVec(const LinearContainer&); // A queue obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // QueueVec(argument); + QueueVec(const QueueVec&); // Move constructor - // QueueVec(argument); + QueueVec(QueueVec&&) noexcept; /* ************************************************************************ */ // Destructor - // ~QueueVec() specifier; + virtual ~QueueVec(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + QueueVec& operator=(const QueueVec&); // Move assignment - // type operator=(argument); + QueueVec& operator=(QueueVec&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const QueueVec&) const noexcept; + bool operator!=(const QueueVec&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Queue) - // type Enqueue(argument) specifiers; // Override Queue member (copy of the value) - // type Enqueue(argument) specifiers; // Override Queue member (move of the value) - // type Head() specifiers; // Override Queue member (must throw std::length_error when empty) - // type Dequeue() specifiers; // Override Queue member (must throw std::length_error when empty) - // type HeadNDequeue() specifiers; // Override Queue member (must throw std::length_error when empty) + void Enqueue(const Data&) override; // Override Queue member (copy of the value) + void Enqueue(Data&&) override; // Override Queue member (move of the value) + Data& Head() const override; // Override Queue member (must throw std::length_error when empty) + void Dequeue() override; // Override Queue member (must throw std::length_error when empty) + Data HeadNDequeue() override; // Override Queue member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Empty() specifiers; // Override Container member + bool Empty() const noexcept override; // Override Container member - // type Size() specifiers; // Override Container member + unsigned long Size() const noexcept override; // Override Container member - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member protected: // Auxiliary member functions - // type Expand() specifiers; - // type Reduce() specifiers; - // type SwapVectors(arguments) specifiers; + void Expand(); + void Reduce(); + //void SwapVectors(arguments) specifiers; }; diff --git a/librerie/exercise2/stack/lst/stacklst.cpp b/librerie/exercise2/stack/lst/stacklst.cpp index 0bb6885..05dabca 100755 --- a/librerie/exercise2/stack/lst/stacklst.cpp +++ b/librerie/exercise2/stack/lst/stacklst.cpp @@ -10,12 +10,23 @@ StackLst(const LinearContainer& linear){ Push(linear[i]); } } +/* +PROVARE +template +StackLst(const LinearContainer& linear) : List(linear){ +return; +} +*/ StackLst(const StackLst& stcklist){ for(ulong i=stcklist.Size()-1 ; i>=0 ; --i){ Push(stcklist[i]); } } +/* +PROVARE +StackLst(const StackLst& stcklist) : List(stcklist){} +*/ template StackLst::StackLst(StackLst&& stcklist) noexcept{ diff --git a/librerie/exercise2/stack/vec/stackvec.cpp b/librerie/exercise2/stack/vec/stackvec.cpp index d374ceb..cca91ea 100755 --- a/librerie/exercise2/stack/vec/stackvec.cpp +++ b/librerie/exercise2/stack/vec/stackvec.cpp @@ -2,8 +2,134 @@ namespace lasd { /* ************************************************************************** */ +// constructors +template +StackVec::StackVec(){ + size = 4; // default vector is instantiated with 4 cells + Elements = new Data[size]; +} -// ... +template +StackVec::StackVec(const LinearContainer& linear){ // si può richiamare il costruttore della classe Vector + Elements = new Data[linear.Size()]; // espandere di un po' forse + for(ulong i=0 ; i +StackVec::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(copyFrom) +*/ + +template +StackVec::StackVec(StackVec&& toMove) noexcept{ + std::swap(Elements, toMove.Elements); + std::swap(size, toMove.size); + std::swap(stackSize, toMove.stackSize); +} + +StackVec::~StackVec(){ + // Vector destructor will be called automatically + // (TEST IT) +} + +template +StackVec& StackVec::operator=(const StackVec& copyFrom){ + Vector::operator=(copyFrom); // espandere di un po' forse + stackSize = copyFrom.Size(); + return *this; +} + +template +StackVec& StackVec::operator=(StackVec&& moveFrom) noexcept{ + Vector::operator=(std::move(moveFrom)); // espandere di un po' forse + stackSize = copyFrom.Size(); +} + +template +bool StackVec::operator==(const StackVec& toCompare) const noexcept{ + Vector::operator==(toCompare); +} + +template +bool StackVec::operator!=(const StackVec& toCompare) const noexcept{ + Vector::operator!=(toCompare); +} + +// Specific member functions (inherited from Stack) +template +void StackVec::Push(const Data& data){ + if(size == stackSize){ + Expand(); + } + Elements[stackSize] = data; + ++stackSize; +} + +template +void StackVec::Push(Data&& data){ + if(size == stackSize){ + Expand(); + } + std::swap(Elements[stackSize], data); +} + +template +Data& StackVec::Top() const{ + if(stackSize == 0){ + throw std::length_error("Empty Stack!"); + } + return Elements[stackSize-1] +} + +template +void StackVec::Pop(){ + if(stackSize<=0){ + throw std::length_error("Empty Stack!"); + } + --stackSize; + if(stackSize < (int)(size/4)){ + Reduce(); + } +} +template +Data StackVec::TopNPop(){ + Data data = Top(); + Pop(); + return data; +} + +template +bool StackVec::Empty() const noexcept{ + return (stackSize == 0); +} + +template +ulong StackVec::Size() const noexcept{ + return stackSize; +} + +template +void StackVec::Expand(){ + Vector::Resize(size * 2); +} + +template +void StackVec::Reduce(){ + if(size <= 4) return; // we're not going to have vectors with less than 4 cells + Vector::Resize((ulong)size/2); +} /* ************************************************************************** */ diff --git a/librerie/exercise2/stack/vec/stackvec.hpp b/librerie/exercise2/stack/vec/stackvec.hpp index b4211dc..26555ab 100755 --- a/librerie/exercise2/stack/vec/stackvec.hpp +++ b/librerie/exercise2/stack/vec/stackvec.hpp @@ -21,7 +21,7 @@ private: protected: - ulong stackSize = 0; // Actual stack dimension + ulong stackSize = 0; // first empty cell using Vector::Elements; using Vector::size; @@ -52,7 +52,7 @@ public: /* ************************************************************************ */ // Copy assignment - StackVec& operator=(const StackVec&) + StackVec& operator=(const StackVec&); // Move assignment StackVec& operator=(StackVec&&) noexcept; @@ -79,7 +79,7 @@ public: bool Empty() const noexcept override; // Override Container member - ulong Size() const noexcept override; specifiers; // Override Container member + ulong Size() const noexcept override; // Override Container member void Clear() override;// Override Container member