From 97407c2a311118e27546b95a875340c00c320336 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 11 May 2021 06:52:44 +0200 Subject: [PATCH] Library 4 added previous libreries --- librerie/exercise4/binarytree/binarytree.cpp | 602 +++++++++++++++++- librerie/exercise4/binarytree/binarytree.hpp | 424 ++++-------- .../binarytree/lnk/binarytreelnk.cpp | 166 ++++- .../binarytree/lnk/binarytreelnk.hpp | 97 ++- .../binarytree/vec/binarytreevec.cpp | 170 ++++- .../binarytree/vec/binarytreevec.hpp | 108 +--- librerie/exercise4/container/container.cpp | 16 +- librerie/exercise4/container/container.hpp | 402 +++++------- librerie/exercise4/iterator/iterator.hpp | 36 +- librerie/exercise4/list/list.cpp | 263 +++++++- librerie/exercise4/list/list.hpp | 88 +-- librerie/exercise4/queue/lst/queuelst.cpp | 69 +- librerie/exercise4/queue/lst/queuelst.hpp | 42 +- librerie/exercise4/queue/queue.hpp | 26 +- librerie/exercise4/queue/vec/queuevec.cpp | 188 +++++- librerie/exercise4/queue/vec/queuevec.hpp | 53 +- librerie/exercise4/stack/lst/stacklst.cpp | 72 ++- librerie/exercise4/stack/lst/stacklst.hpp | 41 +- librerie/exercise4/stack/stack.hpp | 26 +- librerie/exercise4/stack/vec/stackvec.cpp | 143 ++++- librerie/exercise4/stack/vec/stackvec.hpp | 48 +- librerie/exercise4/vector/vector.cpp | 162 ++++- librerie/exercise4/vector/vector.hpp | 86 +-- 23 files changed, 2366 insertions(+), 962 deletions(-) mode change 100644 => 100755 librerie/exercise4/binarytree/binarytree.cpp mode change 100644 => 100755 librerie/exercise4/binarytree/binarytree.hpp mode change 100644 => 100755 librerie/exercise4/binarytree/lnk/binarytreelnk.cpp mode change 100644 => 100755 librerie/exercise4/binarytree/lnk/binarytreelnk.hpp mode change 100644 => 100755 librerie/exercise4/binarytree/vec/binarytreevec.cpp mode change 100644 => 100755 librerie/exercise4/binarytree/vec/binarytreevec.hpp mode change 100644 => 100755 librerie/exercise4/container/container.cpp mode change 100644 => 100755 librerie/exercise4/container/container.hpp mode change 100644 => 100755 librerie/exercise4/iterator/iterator.hpp mode change 100644 => 100755 librerie/exercise4/list/list.cpp mode change 100644 => 100755 librerie/exercise4/list/list.hpp mode change 100644 => 100755 librerie/exercise4/queue/lst/queuelst.cpp mode change 100644 => 100755 librerie/exercise4/queue/lst/queuelst.hpp mode change 100644 => 100755 librerie/exercise4/queue/queue.hpp mode change 100644 => 100755 librerie/exercise4/queue/vec/queuevec.cpp mode change 100644 => 100755 librerie/exercise4/queue/vec/queuevec.hpp mode change 100644 => 100755 librerie/exercise4/stack/lst/stacklst.cpp mode change 100644 => 100755 librerie/exercise4/stack/lst/stacklst.hpp mode change 100644 => 100755 librerie/exercise4/stack/stack.hpp mode change 100644 => 100755 librerie/exercise4/stack/vec/stackvec.cpp mode change 100644 => 100755 librerie/exercise4/stack/vec/stackvec.hpp mode change 100644 => 100755 librerie/exercise4/vector/vector.cpp mode change 100644 => 100755 librerie/exercise4/vector/vector.hpp diff --git a/librerie/exercise4/binarytree/binarytree.cpp b/librerie/exercise4/binarytree/binarytree.cpp old mode 100644 new mode 100755 index 37c673b..4a8744b --- a/librerie/exercise4/binarytree/binarytree.cpp +++ b/librerie/exercise4/binarytree/binarytree.cpp @@ -1,11 +1,607 @@ -// #include "..." +#include "../queue/queue.hpp" +#include "../queue/vec/queuevec.hpp" +#include "../queue/lst/queuelst.hpp" +#include "../stack/stack.hpp" +#include "../stack/lst/stacklst.hpp" +#include "../stack/vec/stackvec.hpp" +#include namespace lasd { -/* ************************************************************************** */ +/* ----- begin of class BinaryTree ----- */ -// ... +/* ----- begin of struct Node ----- */ + +template +bool BinaryTree::Node::operator==(const Node& toEvaluate) const noexcept{ + return EqualNodes(*this, toEvaluate); +} + +template +bool BinaryTree::Node::operator!=(const Node& toEvaluate) const noexcept{ + return !(*this == toEvaluate); +} + +/* given two nodes, checks if the subtree is the same */ +template +bool BinaryTree::Node::EqualNodes(const Node& n1, const Node& n2) const{ + if(n1.data == n2.data){ + + if( (n1.HasLeftChild() && !n2.HasLeftChild()) || (n1.HasRightChild() && !n2.HasRightChild()) ) return false; + + if(n1.HasLeftChild() && n1.HasRightChild()){ + return( EqualNodes(n1.LeftChild(),n2.LeftChild()) && EqualNodes(n1.RightChild(),n2.RightChild())); + } + else if(n1.HasLeftChild() && !n1.HasRightChild()){ + return( EqualNodes(n1.LeftChild(),n2.LeftChild())); + } + else if(!n1.HasLeftChild() && n1.HasRightChild()){ + return( EqualNodes(n1.RightChild(),n2.RightChild())); + } + else{ //if leaf + return true; + } + + }else{ + return false; + } +} + +template +Data& BinaryTree::Node::Element(){ + return this->data; +} + +template +const Data& BinaryTree::Node::Element() const{ + return this->data; +} + +/* ----- end of struct Node ----- */ + +template +bool BinaryTree::operator==(const BinaryTree& toCompare) const noexcept{ + if(size!=toCompare.size) return false; + return(Root() == toCompare.Root()); +} + +template +bool BinaryTree::operator!=(const BinaryTree& toCompare) const noexcept{ + return !(*this == toCompare); +} + +/* ----- Map and fold functions ----- */ + +template +void BinaryTree::MapPreOrder(const typename MappableContainer::MapFunctor function, void* par){ + if(size == 0) return; + MapPreOrder(function, par, &Root()); +} + +template +void BinaryTree::MapPostOrder(const typename MappableContainer::MapFunctor function, void* par){ + if(size == 0) return; + MapPostOrder(function, par, &Root()); +} + +template +void BinaryTree::MapInOrder(const typename MappableContainer::MapFunctor function, void* par){ + if(size == 0) return; + MapInOrder(function, par, &Root()); +} + +template +void BinaryTree::MapBreadth(const typename MappableContainer::MapFunctor function, void* par){ + if(size == 0) return; + MapBreadth(function, par, &Root()); +} + +template +void BinaryTree::FoldPreOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ + if(size == 0) return; + FoldPreOrder(function, par, acc, &Root()); +} + +template +void BinaryTree::FoldPostOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ + if(size == 0) return; + FoldPostOrder(function, par, acc, &Root()); +} + +template +void BinaryTree::FoldInOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ + if(size == 0) return; + FoldInOrder(function, par, acc, &Root()); +} + +template +void BinaryTree::FoldBreadth(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ + if(size == 0) return; + FoldBreadth(function, par, acc, &Root()); +} + +/* ----- Auxiliary map and fold functions ----- */ + +template +void BinaryTree::MapPreOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ + if(node != nullptr){ + function(node->Element(), par); + if(node->HasLeftChild()){ + MapPreOrder(function, par, &(node->LeftChild())); + } + if(node->HasRightChild()){ + MapPreOrder(function, par, &(node->RightChild())); + } + } +} + +template +void BinaryTree::MapPostOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ + if(node != nullptr){ + if(node->HasLeftChild()){ + MapPostOrder(function, par, &(node->LeftChild())); + } + if(node->HasRightChild()){ + MapPostOrder(function, par, &(node->RightChild())); + } + function(node->Element(), par); + } +} + +template +void BinaryTree::MapInOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ + if(node != nullptr){ + if(node->HasLeftChild()){ + MapInOrder(function, par, &(node->LeftChild())); + } + function(node->Element(), par); + if(node->HasRightChild()){ + MapInOrder(function, par, &(node->RightChild())); + } + } +} + +template +void BinaryTree::MapBreadth(const typename MappableContainer::MapFunctor function, void* par, Node* node){ + QueueLst toVisit; + if(node != nullptr){ + toVisit.Enqueue(node); + while(!toVisit.Empty()){ + function(toVisit.Head()->Element(), par); + + if(toVisit.Head()->HasLeftChild()){ + toVisit.Enqueue(&(toVisit.Head()->LeftChild())); + } + if(toVisit.Head()->HasRightChild()){ + toVisit.Enqueue(&(toVisit.Head()->RightChild())); + } + toVisit.Dequeue(); + } + } +} + +template +void BinaryTree::FoldPreOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, const Node* node) const{ + if(node != nullptr){ + function(node->Element(), par, acc); + if(node->HasLeftChild()){ + FoldPreOrder(function, par, acc, &(node->LeftChild())); + } + if(node->HasRightChild()){ + FoldPreOrder(function, par, acc, &(node->RightChild())); + } + } +} + +template +void BinaryTree::FoldPostOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, const Node* node) const{ + if(node != nullptr){ + if(node->HasLeftChild()){ + FoldPostOrder(function, par, acc, &(node->LeftChild())); + } + if(node->HasRightChild()){ + FoldPostOrder(function, par, acc, &(node->RightChild())); + } + function(node->Element(), par, acc); + } +} + +template +void BinaryTree::FoldInOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, const Node* node) const{ + if(node != nullptr){ + if(node->HasLeftChild()){ + FoldInOrder(function, par, acc, &(node->LeftChild())); + } + function(node->Element(), par, acc); + if(node->HasRightChild()){ + FoldInOrder(function, par, acc, &(node->RightChild())); + } + } +} + +template +void BinaryTree::FoldBreadth(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, Node* node) const{ + QueueLst::Node*> toVisit; + if(node != nullptr){ + toVisit.Enqueue(node); + while(!toVisit.Empty()){ + function(toVisit.Head()->Element(), par, acc); + + if(toVisit.Head()->HasLeftChild()){ + toVisit.Enqueue(&(toVisit.Head()->LeftChild())); + } + if(toVisit.Head()->HasRightChild()){ + toVisit.Enqueue(&(toVisit.Head()->RightChild())); + } + toVisit.Dequeue(); + } + } +} + +/* ----- end of class BinaryTree ----- */ + +/* ----- begin of class BTPreOrderIterator ----- */ + +template +BTPreOrderIterator::BTPreOrderIterator(const BinaryTree& tree){ + if(tree.Size() > 0) + curr = &tree.Root(); + else + curr = nullptr; +} + +template +BTPreOrderIterator::BTPreOrderIterator(const BTPreOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; +} + +template +BTPreOrderIterator::BTPreOrderIterator(BTPreOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); +} + +template +BTPreOrderIterator::~BTPreOrderIterator(){ + stack.Clear(); + curr = nullptr; +} + +template +BTPreOrderIterator& BTPreOrderIterator::operator=(const BTPreOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; + return *this; +} + +template +BTPreOrderIterator& BTPreOrderIterator::operator=(BTPreOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); + return *this; +} + +template +bool BTPreOrderIterator::operator==(const BTPreOrderIterator& itr) const noexcept{ + return ( curr==itr.curr && stack==itr.stack ); +} + +template +bool BTPreOrderIterator::operator!=(const BTPreOrderIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +Data& BTPreOrderIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + return curr->Element(); +} + +template +bool BTPreOrderIterator::Terminated() const noexcept{ + return (curr==nullptr); +} + +template +void BTPreOrderIterator::operator++(){ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + + if(curr->HasLeftChild()){ + + if( curr->HasRightChild() ){ + stack.Push(&(curr->RightChild())); + } + + curr = &(curr->LeftChild()); + + }else if(curr->HasRightChild()){ + curr = &curr->RightChild(); + } + else{ // is leaf + if(stack.Empty()){ + curr = nullptr; + }else{ + curr = stack.TopNPop(); + } + } +} + +/* ----- end of class BTPreOrderIterator ----- */ + +/* ----- begin of class BTPostOrderIterator ----- */ + +template +struct BinaryTree::Node* BTPostOrderIterator::DeepestLeftLeaf(struct BinaryTree::Node* node){ + if(node->HasLeftChild()){ + stack.Push(node); + return DeepestLeftLeaf(&(node->LeftChild())); + } + else if(node->HasRightChild()){ + stack.Push(node); + return DeepestLeftLeaf(&(node->RightChild())); + } + else + return node; +} + +template +BTPostOrderIterator::BTPostOrderIterator(const BinaryTree& tree){ + if(tree.Size() > 0) + curr = DeepestLeftLeaf(&tree.Root()); + else + curr = nullptr; +} + +template +BTPostOrderIterator::BTPostOrderIterator(const BTPostOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; +} + +template +BTPostOrderIterator::BTPostOrderIterator(BTPostOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); +} + +template +BTPostOrderIterator::~BTPostOrderIterator(){ + curr = nullptr; + stack.Clear(); +} + +template +BTPostOrderIterator& BTPostOrderIterator::operator=(const BTPostOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; + return *this; +} + +template +BTPostOrderIterator& BTPostOrderIterator::operator=(BTPostOrderIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(stack, itr.stack); + return *this; +} + +template +bool BTPostOrderIterator::operator==(const BTPostOrderIterator& itr) const noexcept{ + return (curr == itr.curr && stack == itr.stack ); +} + +template +bool BTPostOrderIterator::operator!=(const BTPostOrderIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +Data& BTPostOrderIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + return curr->Element(); +} + +template +bool BTPostOrderIterator::Terminated() const noexcept{ + return (curr == nullptr); +} + +template +void BTPostOrderIterator::operator++(){ + /* + * If we're coming from the left then we have to analyze the tree on the right + * (if existent). Otherwise we just top 'n' pop. + */ + + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + + if(stack.Empty()){ + curr = nullptr; + }else{ + if( curr == &((stack.Top())->LeftChild()) ){ + if( (stack.Top())->HasRightChild() ){ + curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); + }else{ + curr = stack.TopNPop(); + } + }else{ + curr = stack.TopNPop(); + } + } +} + +/* ----- end of class BTPostOrderIterator ----- */ + +/* ----- begin of class BTInOrderIterator ----- */ + +template +struct BinaryTree::Node* BTInOrderIterator::MostLeftNode(struct BinaryTree::Node& root){ + if(root.HasLeftChild()){ + stack.Push(&root); + return MostLeftNode(root.LeftChild()); + }else{ + return &root; + } +} + +template +BTInOrderIterator::BTInOrderIterator(const BinaryTree& tree){ + if(tree.Size() > 0) + curr = MostLeftNode(tree.Root()); + else + curr = nullptr; +} + +template +BTInOrderIterator::BTInOrderIterator(const BTInOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; +} + +template +BTInOrderIterator::BTInOrderIterator(BTInOrderIterator&& toMove) noexcept{ + std::move(curr, toMove.curr); + std::move(stack, toMove.stack); +} + +template +BTInOrderIterator::~BTInOrderIterator(){ + stack.Clear(); + curr = nullptr; + } + +template +BTInOrderIterator& BTInOrderIterator::operator=(const BTInOrderIterator& itr){ + curr = itr.curr; + stack = itr.stack; + return *this; +} + +template +BTInOrderIterator& BTInOrderIterator::operator=(BTInOrderIterator&& toMove) noexcept{ + std::move(curr, toMove.curr); + std::move(stack, toMove.stack); + return *this; +} + +template +bool BTInOrderIterator::operator==(const BTInOrderIterator& itr) const noexcept{ + return (curr == itr.curr && stack == itr.stack ); +} + +template +bool BTInOrderIterator::operator!=(const BTInOrderIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +Data& BTInOrderIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + return curr->Element(); +} + +template +bool BTInOrderIterator::Terminated() const noexcept{ + return (curr == nullptr); +} + +template +void BTInOrderIterator::operator++(){ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + + if(curr->HasRightChild()){ + curr = MostLeftNode(curr->RightChild()); + }else{ + if(stack.Empty()){ + curr = nullptr; + }else{ + curr = stack.TopNPop(); + } + } +} + +/* ----- end of class BTInOrderIterator ----- */ + +/* ----- begin of class BTBreadthIteratorOrderIterator ----- */ + +template +BTBreadthIterator::BTBreadthIterator(const BinaryTree& tree){ + if(tree.Size() > 0) + curr = &(tree.Root()); + else + curr = nullptr; +} + +template +BTBreadthIterator::BTBreadthIterator(const BTBreadthIterator& itr){ + curr = itr.curr; + queue = itr.queue; +} + +template +BTBreadthIterator::BTBreadthIterator(BTBreadthIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(queue, itr.queue); +} + +template +BTBreadthIterator::~BTBreadthIterator(){ + curr = nullptr; + queue.Clear(); +} + +template +BTBreadthIterator& BTBreadthIterator::operator=(const BTBreadthIterator& itr){ + curr = itr.curr; + queue = itr.queue; + return *this; +} + +template +BTBreadthIterator& BTBreadthIterator::operator=(BTBreadthIterator&& itr) noexcept{ + std::swap(curr, itr.curr); + std::swap(queue, itr.queue); + return *this; +} + +template +bool BTBreadthIterator::operator==(const BTBreadthIterator& itr) const noexcept{ + return ( curr==itr.curr && queue==itr.queue ); +} + +template +bool BTBreadthIterator::operator!=(const BTBreadthIterator& itr) const noexcept{ + return !(*this == itr); +} + +template +Data& BTBreadthIterator::operator*() const{ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + return curr->Element(); +} + +template +bool BTBreadthIterator::Terminated() const noexcept{ + return curr == nullptr; +} + +template +void BTBreadthIterator::operator++(){ + if(Terminated()) throw std::out_of_range("Iterator is terminated!"); + + if(curr->HasLeftChild()){ + queue.Enqueue(&(curr->LeftChild())); + } + if(curr->HasRightChild()){ + queue.Enqueue(&(curr->RightChild())); + } + if(!queue.Empty()){ + curr = queue.HeadNDequeue(); + }else{ + curr = nullptr; + } +} /* ************************************************************************** */ diff --git a/librerie/exercise4/binarytree/binarytree.hpp b/librerie/exercise4/binarytree/binarytree.hpp old mode 100644 new mode 100755 index f3333a5..d1be6ad --- a/librerie/exercise4/binarytree/binarytree.hpp +++ b/librerie/exercise4/binarytree/binarytree.hpp @@ -2,32 +2,25 @@ #ifndef BINARYTREE_HPP #define BINARYTREE_HPP -/* ************************************************************************** */ - #include "../container/container.hpp" #include "../iterator/iterator.hpp" -// #include "..." +#include "../queue/queue.hpp" +#include "../queue/vec/queuevec.hpp" +#include "../queue/lst/queuelst.hpp" -/* ************************************************************************** */ +#include "../stack/stack.hpp" +#include "../stack/lst/stacklst.hpp" +#include "../stack/vec/stackvec.hpp" namespace lasd { -/* ************************************************************************** */ - template -class BinaryTree { // Must extend InOrder/BreadthMappableContainer and InOrder/BreadthFoldableContainer - -private: - - // ... - -protected: - - // using InOrder/BreadthMappableContainer::???; - - // ... +class BinaryTree : virtual public InOrderMappableContainer, + virtual public BreadthMappableContainer, + virtual public InOrderFoldableContainer, + virtual public BreadthFoldableContainer{ // Must extend InOrder/BreadthMappableContainer and InOrder/BreadthFoldableContainer public: @@ -35,404 +28,221 @@ public: private: - // ... - protected: + Data data; + // Comparison operators + bool operator==(const Node&) const noexcept; // Comparison of abstract types is possible, but should not be visible. + bool operator!=(const Node&) const noexcept; // Comparison of abstract types is possible, but should not be visible. - // ... + bool EqualNodes(const Node&, const Node&) const; public: - // friend class BinaryTree; - - /* ********************************************************************** */ + friend class BinaryTree; // Destructor - // ~Node() specifiers - - /* ********************************************************************** */ + virtual ~Node() = default; // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Node& operator=(const Node&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. - - /* ********************************************************************** */ - - // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types is possible, but should not be visible. - // type operator!=(argument) specifiers; // Comparison of abstract types is possible, but should not be visible. - - /* ********************************************************************** */ + Node& operator=(Node&&) noexcept = delete; // Move assignment of abstract types should not be possible. // Specific member functions + Data& Element(); // Mutable access to the element (concrete function should not throw exceptions) + const Data& Element() const; // Immutable access to the element (concrete function should not throw exceptions) - // type Element() specifiers; // Mutable access to the element (concrete function should not throw exceptions) - // type Element() specifiers; // Immutable access to the element (concrete function should not throw exceptions) + virtual bool IsLeaf() const noexcept = 0; // (concrete function should not throw exceptions) + virtual bool HasLeftChild() const noexcept = 0; // (concrete function should not throw exceptions) + virtual bool HasRightChild() const noexcept = 0; // (concrete function should not throw exceptions) - // type IsLeaf() specifiers; // (concrete function should not throw exceptions) - // type HasLeftChild() specifiers; // (concrete function should not throw exceptions) - // type HasRightChild() specifiers; // (concrete function should not throw exceptions) - - // type LeftChild() specifiers; // (concrete function must throw std::out_of_range when not existent) - // type RightChild() specifiers; // (concrete function must throw std::out_of_range when not existent) + virtual Node& LeftChild() const = 0; // (concrete function must throw std::out_of_range when not existent) + virtual Node& RightChild() const = 0; // (concrete function must throw std::out_of_range when not existent) }; - /* ************************************************************************ */ + virtual ~BinaryTree() = default; - // Destructor - // ~BinaryTree() specifiers + BinaryTree& operator=(const BinaryTree&) = delete; // Copy assignment of abstract types should not be possible. + BinaryTree& operator=(BinaryTree&&) noexcept = delete; // Move assignment of abstract types should not be possible. - /* ************************************************************************ */ + bool operator==(const BinaryTree&) const noexcept; // Comparison of abstract binary tree is possible. + bool operator!=(const BinaryTree&) const noexcept; // Comparison of abstract binary tree is possible. - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty) - // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + /* ----- Map and fold functions ----- */ - /* ************************************************************************ */ + virtual void MapPreOrder(const typename MappableContainer::MapFunctor, void*) override; // Override MappableContainer member + virtual void MapPostOrder(const typename MappableContainer::MapFunctor, void*) override; // Override MappableContainer member + virtual void MapInOrder(const typename MappableContainer::MapFunctor, void*) override; // Override InOrderMappableContainer member + virtual void MapBreadth(const typename MappableContainer::MapFunctor, void*) override; // Override BreadthMappableContainer member - // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract binary tree is possible. - // type operator!=(argument) specifiers; // Comparison of abstract binary tree is possible. - - /* ************************************************************************ */ - - // Specific member functions - - // type Root() specifiers; // (concrete function must throw std::length_error when empty) - - /* ************************************************************************ */ - - // Specific member functions (inherited from MappableContainer) - - // using typename MappableContainer::MapFunctor; - - // type MapPreOrder(arguments) specifiers; // Override MappableContainer member - // type MapPostOrder(arguments) specifiers; // Override MappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from FoldableContainer) - - // using typename FoldableContainer::FoldFunctor; - - // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member - // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from InOrderMappableContainer) - - // type MapInOrder(arguments) specifiers; // Override InOrderMappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from InOrderFoldableContainer) - - // type FoldInOrder(arguments) specifiers; // Override InOrderFoldableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from BreadthMappableContainer) - - // type MapBreadth(arguments) specifiers; // Override BreadthMappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from BreadthFoldableContainer) - - // type FoldBreadth(arguments) specifiers; // Override BreadthFoldableContainer member + virtual void FoldPreOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member + virtual void FoldPostOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member + virtual void FoldInOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member + virtual void FoldBreadth(const typename FoldableContainer::FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member protected: - // Auxiliary member functions (for MappableContainer) + using BreadthMappableContainer::size; - // type MapPreOrder(arguments) specifiers; // Accessory function executing from one node of the tree - // type MapPostOrder(arguments) specifiers; // Accessory function executing from one node of the tree + /* ----- Auxiliary map and fold functions ----- */ - /* ************************************************************************ */ + void MapPreOrder(const typename MappableContainer::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree + void MapPostOrder(const typename MappableContainer::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree + void MapInOrder(const typename MappableContainer::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree + void MapBreadth(const typename MappableContainer::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree - // Auxiliary member functions (for FoldableContainer) + void FoldPreOrder(const typename FoldableContainer::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree + void FoldPostOrder(const typename FoldableContainer::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree + void FoldInOrder(const typename FoldableContainer::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree + void FoldBreadth(const typename FoldableContainer::FoldFunctor, const void*, void*, Node*) const; // Accessory function executing from one node of the tree +}; - // type FoldPreOrder(arguments) specifiers; // Accessory function executing from one node of the tree - // type FoldPostOrder(arguments) specifiers; // Accessory function executing from one node of the tree - /* ************************************************************************ */ +template +class BTPreOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator - // Auxiliary member functions (for InOrderMappableContainer) +protected: - // type MapInOrder(arguments) specifiers; // Accessory function executing from one node of the tree + struct BinaryTree::Node* curr = nullptr; + StackLst::Node*> stack; - /* ************************************************************************ */ +public: - // Auxiliary member functions (for InOrderFoldableContainer) + virtual ~BTPreOrderIterator(); - // type FoldInOrder(arguments) specifiers; // Accessory function executing from one node of the tree + BTPreOrderIterator(const BinaryTree&); // An iterator over a given binary tree + BTPreOrderIterator(const BTPreOrderIterator&); + BTPreOrderIterator(BTPreOrderIterator&&) noexcept; - /* ************************************************************************ */ + BTPreOrderIterator& operator=(const BTPreOrderIterator&); + BTPreOrderIterator& operator=(BTPreOrderIterator&&) noexcept; - // Auxiliary member functions (for BreadthMappableContainer) + bool operator==(const BTPreOrderIterator&) const noexcept; + bool operator!=(const BTPreOrderIterator&) const noexcept; - // type MapBreadth(arguments) specifiers; // Accessory function executing from one node of the tree + // Specific member functions (inherited from Iterator) - /* ************************************************************************ */ + Data& operator*() const; // (throw std::out_of_range when terminated) + bool Terminated() const noexcept; // (should not throw exceptions) - // Auxiliary member functions (for BreadthFoldableContainer) + // Specific member functions (inherited from ForwardIterator) - // type FoldBreadth(arguments) specifiers; // Accessory function executing from one node of the tree + void operator++(); // (throw std::out_of_range when terminated) }; /* ************************************************************************** */ template -class BTPreOrderIterator { // Must extend ForwardIterator - -private: - - // ... +class BTPostOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator protected: - // ... + struct BinaryTree::Node* curr = nullptr; + StackLst::Node*> stack; + + struct BinaryTree::Node* DeepestLeftLeaf(struct BinaryTree::Node*); public: - // Specific constructors - // BTPreOrderIterator(argument) specifiers; // An iterator over a given binary tree + BTPostOrderIterator(const BinaryTree&); // An iterator over a given binary tree + BTPostOrderIterator(const BTPostOrderIterator&); + BTPostOrderIterator(BTPostOrderIterator&&) noexcept; - /* ************************************************************************ */ + virtual ~BTPostOrderIterator(); - // Copy constructor - // BTPreOrderIterator(argument) specifiers; - // Move constructor - // BTPreOrderIterator(argument) specifiers; + BTPostOrderIterator& operator=(const BTPostOrderIterator&); + BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept; - /* ************************************************************************ */ - - // Destructor - // ~BTPreOrderIterator() specifiers; - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument) specifiers; - - // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; - - /* ************************************************************************ */ + bool operator==(const BTPostOrderIterator&) const noexcept; + bool operator!=(const BTPostOrderIterator&) const noexcept; // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) - - // type Terminated() specifiers; // (should not throw exceptions) - - /* ************************************************************************ */ + Data& operator*() const; // (throw std::out_of_range when terminated) + bool Terminated() const noexcept; // (should not throw exceptions) // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; /* ************************************************************************** */ template -class BTPostOrderIterator { // Must extend ForwardIterator - -private: - - // ... +class BTInOrderIterator : virtual public ForwardIterator { // Must extend ForwardIterator protected: - // ... + struct BinaryTree::Node* curr = nullptr; + StackLst::Node*> stack; + + struct BinaryTree::Node* MostLeftNode(struct BinaryTree::Node&); public: - // Specific constructors - // BTPostOrderIterator(argument) specifiers; // An iterator over a given binary tree + BTInOrderIterator(const BinaryTree&); // An iterator over a given binary tree + BTInOrderIterator(const BTInOrderIterator&); + BTInOrderIterator(BTInOrderIterator&&) noexcept; - /* ************************************************************************ */ + virtual ~BTInOrderIterator(); - // Copy constructor - // BTPostOrderIterator(argument) specifiers; + BTInOrderIterator& operator=(const BTInOrderIterator&); + BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept; - // Move constructor - // BTPostOrderIterator(argument) specifiers; - - /* ************************************************************************ */ - - // Destructor - // ~BTPostOrderIterator() specifiers; - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument) specifiers; - - // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; - - /* ************************************************************************ */ + bool operator==(const BTInOrderIterator&) const noexcept; + bool operator!=(const BTInOrderIterator&) const noexcept; // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) - - // type Terminated() specifiers; // (should not throw exceptions) - - /* ************************************************************************ */ + Data& operator*() const; // (throw std::out_of_range when terminated) + bool Terminated() const noexcept; // (should not throw exceptions) // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; -/* ************************************************************************** */ - template -class BTInOrderIterator { // Must extend ForwardIterator - -private: - - // ... +class BTBreadthIterator : virtual public ForwardIterator{ // Must extend ForwardIterator protected: - // ... + struct BinaryTree::Node* curr = nullptr; + QueueVec::Node*> queue; public: - // Specific constructors - // BTInOrderIterator(argument) specifiers; // An iterator over a given binary tree + BTBreadthIterator(const BinaryTree&); // An iterator over a given binary tree + BTBreadthIterator(const BTBreadthIterator&); + BTBreadthIterator(BTBreadthIterator&&) noexcept; - /* ************************************************************************ */ + virtual ~BTBreadthIterator(); - // Copy constructor - // BTInOrderIterator(argument) specifiers; + BTBreadthIterator& operator=(const BTBreadthIterator&); + BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept; - // Move constructor - // BTInOrderIterator(argument) specifiers; - - /* ************************************************************************ */ - - // Destructor - // ~BTInOrderIterator() specifiers; - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument) specifiers; - - // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; - - /* ************************************************************************ */ + bool operator==(const BTBreadthIterator&) const noexcept; + bool operator!=(const BTBreadthIterator&) const noexcept; // Specific member functions (inherited from Iterator) - // type operator*() specifiers; // (throw std::out_of_range when terminated) - - // type Terminated() specifiers; // (should not throw exceptions) - - /* ************************************************************************ */ + Data& operator*() const; // (throw std::out_of_range when terminated) + bool Terminated() const noexcept; // (should not throw exceptions) // Specific member functions (inherited from ForwardIterator) - // type operator++() specifiers; // (throw std::out_of_range when terminated) + void operator++(); // (throw std::out_of_range when terminated) }; -/* ************************************************************************** */ - -template -class BTBreadthIterator { // Must extend ForwardIterator - -private: - - // ... - -protected: - - // ... - -public: - - // Specific constructors - // BTBreadthIterator(argument) specifiers; // An iterator over a given binary tree - - /* ************************************************************************ */ - - // Copy constructor - // BTBreadthIterator(argument) specifiers; - - // Move constructor - // BTBreadthIterator(argument) specifiers; - - /* ************************************************************************ */ - - // Destructor - // ~BTBreadthIterator() specifiers; - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument) specifiers; - - // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; - - /* ************************************************************************ */ - - // Specific member functions (inherited from Iterator) - - // type operator*() specifiers; // (throw std::out_of_range when terminated) - - // type Terminated() specifiers; // (should not throw exceptions) - - /* ************************************************************************ */ - - // Specific member functions (inherited from ForwardIterator) - - // type operator++() specifiers; // (throw std::out_of_range when terminated) - -}; - -/* ************************************************************************** */ - } #include "binarytree.cpp" diff --git a/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp old mode 100644 new mode 100755 index d374ceb..a14fe11 --- a/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise4/binarytree/lnk/binarytreelnk.cpp @@ -1,10 +1,170 @@ namespace lasd { -/* ************************************************************************** */ +/* ----- begin of struct NodeLnk ----- */ -// ... +template +struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::operator=(const BinaryTreeLnk::NodeLnk& node){ + data = node.data; + left = node.left; + right = node.right; + return *this; +} -/* ************************************************************************** */ +template +struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::operator=(NodeLnk&& node) noexcept{ + std::swap(data, node.data); + std::swap(left, node.left); + std::swap(right, node.right); + return *this; +} + +template +bool BinaryTreeLnk::NodeLnk::IsLeaf() const noexcept{ + return (left==nullptr && right==nullptr); +} + +template +bool BinaryTreeLnk::NodeLnk::HasLeftChild() const noexcept{ + return (left!=nullptr); +} + +template +bool BinaryTreeLnk::NodeLnk::HasRightChild() const noexcept{ + return (right!=nullptr); +} + +template +struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::LeftChild() const{ + return *left; +} + +template +struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::RightChild() const{ + return *right; +} + +/* ----- end of struct NodeLnk ----- */ + +/* ----- begin of class BinaryTreeLnk ----- */ + +// creates a tree from a linear container in breadth +template +BinaryTreeLnk::BinaryTreeLnk(const LinearContainer& lc){ + root = CreateTreeFromLinearContainerInBreadth(lc,0); + size = lc.Size(); +} + +template +struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CreateTreeFromLinearContainerInBreadth(const LinearContainer& lc, ulong position){ + if(position >= lc.Size()) return nullptr; + + struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(lc[position]); + + tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1); + tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2); + + return tmp; + +} + +template +struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CreateNode(const Data& data){ + struct BinaryTreeLnk::NodeLnk* newNode = new struct BinaryTreeLnk::NodeLnk(); + newNode->data = data; + newNode->left = nullptr; + newNode->right = nullptr; + return newNode; +} + +template +BinaryTreeLnk::BinaryTreeLnk(const BinaryTreeLnk& tree){ + if(tree.root == nullptr) return; + size = tree.size; + root = CopyTree(&tree.Root()); +} + +template +struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CopyTree(struct BinaryTreeLnk::Node* nodeToCopy){ + if(nodeToCopy==nullptr) return nullptr; + + struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(nodeToCopy->Element()); + + if(nodeToCopy->HasLeftChild()) + tmp->left = CopyTree(&(nodeToCopy->LeftChild())); + else + tmp->left = nullptr; + + if(nodeToCopy->HasRightChild()) + tmp->right = CopyTree(&(nodeToCopy->RightChild())); + else + tmp->right = nullptr; + + return tmp; +} + +template +BinaryTreeLnk::BinaryTreeLnk(BinaryTreeLnk&& tree) noexcept{ + std::swap(size, tree.size); + std::swap(root, tree.root); +} + +template +BinaryTreeLnk::~BinaryTreeLnk(){ + Clear(); +} + +template +void BinaryTreeLnk::DeleteTree(BinaryTreeLnk::NodeLnk* node){ + if(node == nullptr) return; + DeleteTree(node->left); + DeleteTree(node->right); + delete node; +} + +template +BinaryTreeLnk& BinaryTreeLnk::operator=(const BinaryTreeLnk& tree){ + Clear(); + size = tree.size; + if(tree.root != nullptr) + root = CopyTree(&tree.Root()); + return *this; + +} + +template +BinaryTreeLnk& BinaryTreeLnk::operator=(BinaryTreeLnk&& tree) noexcept{ + Clear(); + std::swap(size, tree.size); + std::swap(root, tree.root); + return *this; +} + +template +bool BinaryTreeLnk::operator==(const BinaryTreeLnk& tree) const noexcept{ + if(size == tree.size){ + return (Root() == tree.Root()); + }else{ + return false; + } +} + +template +bool BinaryTreeLnk::operator!=(const BinaryTreeLnk& tree) const noexcept{ + return !(*this == tree); +} + +template +struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::Root() const{ + if(size==0) throw std::length_error("Empty tree!"); + return *root; +} + +template +void BinaryTreeLnk::Clear(){ + DeleteTree(root); + root = nullptr; + size = 0; +} } diff --git a/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp b/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp old mode 100644 new mode 100755 index 4d18351..85c852e --- a/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp +++ b/librerie/exercise4/binarytree/lnk/binarytreelnk.hpp @@ -1,99 +1,74 @@ - #ifndef BINARYTREELNK_HPP #define BINARYTREELNK_HPP -/* ************************************************************************** */ - #include "../binarytree.hpp" -/* ************************************************************************** */ - namespace lasd { -/* ************************************************************************** */ - template -class BinaryTreeLnk { // Must extend BinaryTree - -private: - - // ... +class BinaryTreeLnk : virtual public BinaryTree{ // Must extend BinaryTree protected: - // using BinaryTree::???; - - // ... - - struct NodeLnk { // Must extend Node - - private: - - // ... + struct NodeLnk : virtual public BinaryTree::Node { // Must extend Node protected: - // ... + using BinaryTree::Node::data; + struct NodeLnk* left = nullptr; + struct NodeLnk* right = nullptr; public: + struct NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible. + struct NodeLnk& operator=(NodeLnk&&) noexcept; // Move assignment of abstract types should not be possible. + bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions) + bool HasLeftChild() const noexcept override; // (concrete function should not throw exceptions) + bool HasRightChild() const noexcept override; // (concrete function should not throw exceptions) - // ... + struct NodeLnk& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent) + struct NodeLnk& RightChild() const override; // (concrete function must throw std::out_of_range when not existent) + + friend class BinaryTreeLnk; }; +protected: + using BinaryTree::size; + + struct BinaryTreeLnk::NodeLnk* root = nullptr; + struct BinaryTreeLnk::NodeLnk* CreateNode(const Data& data); + public: // Default constructor - // BinaryTreeLnk() specifiers; + BinaryTreeLnk() = default; + BinaryTreeLnk(const LinearContainer&); // A binary tree obtained from a LinearContainer + BinaryTreeLnk(const BinaryTreeLnk&); + BinaryTreeLnk(BinaryTreeLnk&&) noexcept; - /* ************************************************************************ */ + virtual ~BinaryTreeLnk(); - // Specific constructors - // BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer + BinaryTreeLnk& operator=(const BinaryTreeLnk&); + BinaryTreeLnk& operator=(BinaryTreeLnk&&) noexcept; - /* ************************************************************************ */ - - // Copy constructor - // BinaryTreeLnk(argument) specifiers; - - // Move constructor - // BinaryTreeLnk(argument) specifiers; - - /* ************************************************************************ */ - - // Destructor - // ~BinaryTreeLnk() specifiers; - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument) specifiers; - - // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; - - /* ************************************************************************ */ + bool operator==(const BinaryTreeLnk&) const noexcept; + bool operator!=(const BinaryTreeLnk&) const noexcept; // Specific member functions (inherited from BinaryTree) - // type Root() specifiers; // Override BinaryTree member (throw std::length_error when empty) - - /* ************************************************************************ */ + NodeLnk& Root() const override; // Override BinaryTree member (throw std::length_error when empty) // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member + // Specific functions + + struct BinaryTreeLnk::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer&,ulong); + struct BinaryTreeLnk::NodeLnk* CopyTree(struct BinaryTreeLnk::Node*); + void DeleteTree(BinaryTreeLnk::NodeLnk* node); }; -/* ************************************************************************** */ - } #include "binarytreelnk.cpp" diff --git a/librerie/exercise4/binarytree/vec/binarytreevec.cpp b/librerie/exercise4/binarytree/vec/binarytreevec.cpp old mode 100644 new mode 100755 index d374ceb..f954771 --- a/librerie/exercise4/binarytree/vec/binarytreevec.cpp +++ b/librerie/exercise4/binarytree/vec/binarytreevec.cpp @@ -1,9 +1,175 @@ namespace lasd { -/* ************************************************************************** */ +/* ----- begin of struct NodeVec ----- */ -// ... +template +BinaryTreeVec::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec* ref){ + data = dat; + index = idx; + ReferenceToTree = ref; +} + +template +struct BinaryTreeVec::NodeVec& BinaryTreeVec::NodeVec::operator=(const BinaryTreeVec::NodeVec& node){ + ReferenceToTree = node.ReferenceToTree; + data = node.data; + index = node.index; + return *this; +} + +template +struct BinaryTreeVec::NodeVec& BinaryTreeVec::NodeVec::operator=(BinaryTreeVec::NodeVec&& node) noexcept{ + std::swap(data, node.data); + std::swap(index, node.index); + std::swap(ReferenceToTree, node.ReferenceToTree); + return *this; +} + +template +bool BinaryTreeVec::NodeVec::IsLeaf() const noexcept{ + return (!HasLeftChild() && !HasRightChild()); +} + +template +bool BinaryTreeVec::NodeVec::HasLeftChild() const noexcept{ + if( (index*2)+1 < ReferenceToTree->size){ + return true; + }else{ + return false; + } +} + +template +bool BinaryTreeVec::NodeVec::HasRightChild() const noexcept{ + if((index*2)+2 < ReferenceToTree->size){ + return true; + }else{ + return false; + } +} + +template +struct BinaryTreeVec::NodeVec& BinaryTreeVec::NodeVec::LeftChild() const{ + if(index*2+1 < ReferenceToTree->size) + return *((ReferenceToTree->tree)[index*2+1]); + else + throw std::out_of_range("Left child does not exist!"); +} + +template +struct BinaryTreeVec::NodeVec& BinaryTreeVec::NodeVec::RightChild() const{ + if(index*2+2 < ReferenceToTree->size) + return *((ReferenceToTree->tree)[index*2+2]); + else + throw std::out_of_range("Right child does not exist!"); +} + +/* ----- end of struct NodeVec ----- */ + +/* ----- begin of class BinaryTreeVec ----- */ + +template +BinaryTreeVec::BinaryTreeVec(const LinearContainer& lc){ + tree.Resize(lc.Size()); + size = lc.Size(); + for(ulong i=0 ; i::NodeVec* tmp = new BinaryTreeVec::NodeVec(lc[i], i, this); + tree[i] = tmp; + } +} + +template +BinaryTreeVec::BinaryTreeVec(const BinaryTreeVec& bt){ + size = bt.size; + tree.Resize(size); + for(ulong i=0 ; i::NodeVec* tmp = new BinaryTreeVec::NodeVec( (bt.tree[i])->data , i, this); + tree[i] = tmp; + } +} + +template +BinaryTreeVec::BinaryTreeVec(BinaryTreeVec&& bt) noexcept{ + std::swap(size,bt.size); + std::swap(tree,bt.tree); + for(ulong i=0 ; iReferenceToTree = this; + } +} + +template +BinaryTreeVec::~BinaryTreeVec(){ + Clear(); +} + +template +BinaryTreeVec& BinaryTreeVec::operator=(const BinaryTreeVec& bt){ + Clear(); + size = bt.size; + tree.Resize(size); + for(ulong i=0 ; i::NodeVec((bt.tree[i])->data,i,this); + tree[i] = tmp; + } + return *this; +} + +template +BinaryTreeVec& BinaryTreeVec::operator=(BinaryTreeVec&& bt) noexcept{ + Clear(); + std::swap(size, bt.size); + std::swap(tree, bt.tree); + for(ulong i=0 ; iReferenceToTree = this; + } + return *this; +} + +template +bool BinaryTreeVec::operator==(const BinaryTreeVec& bt) const noexcept{ + if(size==bt.size){ + for(ulong i=0 ; idata != (bt.tree[i])->data ) return false; + } + return true; + } + return false; +} + +template +bool BinaryTreeVec::operator!=(const BinaryTreeVec& bt) const noexcept{ + return !(*this == bt); +} + +template +struct BinaryTreeVec::NodeVec& BinaryTreeVec::Root() const{ + if(size==0) throw std::length_error("Empty tree!"); + return *(tree.Front()); +} + +template +void BinaryTreeVec::Clear(){ + for(ulong i=0 ; i +void BinaryTreeVec::MapBreadth(const MapFunctor func, void* par){ + for(ulong i=0 ; idata, par); + } +} + +template +void BinaryTreeVec::FoldBreadth(const FoldFunctor func, const void* par, void* acc) const{ + for(ulong i=0 ; idata, par, acc); + } +} /* ************************************************************************** */ diff --git a/librerie/exercise4/binarytree/vec/binarytreevec.hpp b/librerie/exercise4/binarytree/vec/binarytreevec.hpp old mode 100644 new mode 100755 index 83fcfdf..e7e9abc --- a/librerie/exercise4/binarytree/vec/binarytreevec.hpp +++ b/librerie/exercise4/binarytree/vec/binarytreevec.hpp @@ -2,111 +2,73 @@ #ifndef BINARYTREEVEC_HPP #define BINARYTREEVEC_HPP -/* ************************************************************************** */ - #include "../binarytree.hpp" #include "../../vector/vector.hpp" -/* ************************************************************************** */ - namespace lasd { -/* ************************************************************************** */ - template -class BinaryTreeVec { // Must extend BinaryTree - -private: - - // ... +class BinaryTreeVec : virtual public BinaryTree{ // Must extend BinaryTree protected: - // using BinaryTree::???; - - // ... - - struct NodeVec { // Must extend Node - - private: - - // ... + struct NodeVec : virtual public BinaryTree::Node { // Must extend Node protected: - - // ... + using BinaryTree::Node::data; + ulong index; + BinaryTreeVec* ReferenceToTree = nullptr; public: + NodeVec(Data&, ulong, BinaryTreeVec*); + struct NodeVec& operator=(const NodeVec&); // Copy assignment of abstract types should not be possible. + struct NodeVec& operator=(NodeVec&&) noexcept; // Move assignment of abstract types should not be possible. + bool IsLeaf() const noexcept override; // (concrete function should not throw exceptions) + bool HasLeftChild() const noexcept override; // (concrete function should not throw exceptions) + bool HasRightChild() const noexcept override; // (concrete function should not throw exceptions) + struct NodeVec& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent) + struct NodeVec& RightChild() const override; // (concrete function must throw std::out_of_range when not existent) - // ... - + friend class BinaryTreeVec; }; +protected: + + using BinaryTree::size; + Vector::NodeVec*> tree; + public: - // Default constructor - // BinaryTreeVec() specifiers; + BinaryTreeVec() = default; + BinaryTreeVec(const LinearContainer&); // A binary tree obtained from a LinearContainer + BinaryTreeVec(const BinaryTreeVec&); + BinaryTreeVec(BinaryTreeVec&&) noexcept; - /* ************************************************************************ */ + virtual ~BinaryTreeVec(); - // Specific constructors - // BinaryTreeVec(argument) specifiers; // A binary tree obtained from a LinearContainer + BinaryTreeVec& operator=(const BinaryTreeVec&); + BinaryTreeVec& operator=(BinaryTreeVec&&) noexcept; - /* ************************************************************************ */ - - // Copy constructor - // BinaryTreeVec(argument) specifiers; - - // Move constructor - // BinaryTreeVec(argument) specifiers; - - /* ************************************************************************ */ - - // Destructor - // ~BinaryTreeVec() specifiers; - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument) specifiers; - - // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; - - /* ************************************************************************ */ + bool operator==(const BinaryTreeVec&) const noexcept; + bool operator!=(const BinaryTreeVec&) const noexcept; // Specific member functions (inherited from BinaryTree) - // type Root() specifiers; // Override BinaryTree member (throw std::length_error when empty) - - /* ************************************************************************ */ + NodeVec& Root() const override; // Override BinaryTree member (throw std::length_error when empty) // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member - /* ************************************************************************ */ + /* ----- override of map and fold in breadth ----- */ - // Specific member functions (inherited from BreadthMappableContainer) - - // type MapBreadth(arguments) specifiers; // Override BreadthMappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from BreadthFoldableContainer) - - // type FoldBreadth(arguments) specifiers; // Override BreadthFoldableContainer member + using typename BinaryTree::MapFunctor; + using typename BinaryTree::FoldFunctor; + void MapBreadth(const MapFunctor, void*) override; + void FoldBreadth(const FoldFunctor, const void*, void*) const override; }; -/* ************************************************************************** */ - } #include "binarytreevec.cpp" diff --git a/librerie/exercise4/container/container.cpp b/librerie/exercise4/container/container.cpp old mode 100644 new mode 100755 index d374ceb..6bbe4bf --- a/librerie/exercise4/container/container.cpp +++ b/librerie/exercise4/container/container.cpp @@ -1,10 +1,18 @@ namespace lasd { -/* ************************************************************************** */ +template +void AuxFoldExists(const Data& dat, const void* val, void* exists){ + if(dat == *((Data*)val)){ + *((bool*)exists) = true; + } +} -// ... - -/* ************************************************************************** */ +template +bool FoldableContainer::Exists(const Data& dat) const noexcept{ + bool exists = false; + FoldPreOrder(&AuxFoldExists, &dat, &exists); + return exists; +} } diff --git a/librerie/exercise4/container/container.hpp b/librerie/exercise4/container/container.hpp old mode 100644 new mode 100755 index f9d5881..cdeed0e --- a/librerie/exercise4/container/container.hpp +++ b/librerie/exercise4/container/container.hpp @@ -14,382 +14,306 @@ namespace lasd { /* ************************************************************************** */ class Container { - private: - // ... - protected: - // ... + ulong size = 0; public: // Destructor - // ~Container() specifiers - - /* ************************************************************************ */ + virtual ~Container() = default; // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Container& operator=(const Container&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. - - /* ************************************************************************ */ + Container& operator=(Container&&) noexcept = 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 Container&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const Container&&) const noexcept = delete; // Comparison of abstract types might not be possible. + + // Specific member functions + virtual bool Empty() const noexcept { + return (size == 0); + } // (concrete function should not throw exceptions) + + virtual ulong Size() const noexcept { + return size; + } // (concrete function should not throw exceptions) + + virtual void Clear() = 0; +}; + +template +class LinearContainer : virtual public Container{ // Must extend Container + +private: + +protected: + +public: + + // Destructor + virtual ~LinearContainer() = default; + + // Copy assignment + LinearContainer& operator=(const LinearContainer&) = delete; // Copy assignment of abstract types should not be possible. + + // Move assignment + LinearContainer& operator=(LinearContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + + // Comparison operators + bool operator==(const LinearContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const LinearContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions + virtual Data& Front() const = 0; // (concrete function must throw std::length_error when empty) + virtual Data& Back() const = 0; // (concrete function must throw std::length_error when empty) - // type Empty() specifiers; // (concrete function should not throw exceptions) + virtual Data& operator[](const ulong) const = 0; // (concrete function must throw std::out_of_range when out of range) - // type Size() specifiers; // (concrete function should not throw exceptions) +}; - // type Clear() specifiers; +template +class TestableContainer : virtual public Container{ // Must extend Container + +private: + +protected: + +public: + + // Destructor + virtual ~TestableContainer() = default; + + // Copy assignment + TestableContainer& operator=(const TestableContainer&) = delete; // Copy assignment of abstract types should not be possible. + + // Move assignment + TestableContainer& operator=(TestableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + + // Comparison operators + bool operator==(const TestableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const TestableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + + /* ************************************************************************ */ + + // Specific member functions + virtual bool Exists(const Data&) const noexcept = 0; // (concrete function should not throw exceptions) + +}; + +template +class MappableContainer : virtual public Container { // Must extend Container + +private: + +protected: + +public: + + // Destructor + virtual ~MappableContainer() = default; + + // Copy assignment + MappableContainer& operator=(const MappableContainer&) = delete; // Copy assignment of abstract types should not be possible. + + // Move assignment + MappableContainer& operator=(MappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + + // Comparison operators + bool operator==(const MappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const MappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + + // Specific member functions + + typedef std::function MapFunctor; + + virtual void MapPreOrder(const MapFunctor, void*) = 0; + virtual void MapPostOrder(const MapFunctor, void*) = 0; + +}; + +template +class FoldableContainer : virtual public TestableContainer{ // Must extend TestableContainer + +private: + +protected: + +public: + + // Destructor + virtual ~FoldableContainer() = default; + + // Copy assignment + FoldableContainer& operator=(const FoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. + + // Move assignment + FoldableContainer& operator=(FoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + + // Comparison operators + bool operator==(FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + + // Specific member functions + + typedef std::function FoldFunctor; + + virtual void FoldPreOrder(const FoldFunctor, const void*, void*) const = 0; + virtual void FoldPostOrder(const FoldFunctor, const void*, void*) const = 0; + + virtual bool Exists(const Data&) const noexcept override; // Override TestableContainer member }; /* ************************************************************************** */ template -class LinearContainer { // Must extend Container +class InOrderMappableContainer : virtual public MappableContainer { // Must extend MappableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~LinearContainer() specifiers + virtual ~InOrderMappableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + InOrderMappableContainer& operator=(const InOrderMappableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + InOrderMappableContainer& operator=(InOrderMappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types is possible. - // type operator!=(argument) specifiers; // Comparison of abstract types is possible. + bool operator==(const InOrderMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const InOrderMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - - // type Front() specifiers; // (concrete function must throw std::length_error when empty) - // type Back() specifiers; // (concrete function must throw std::length_error when empty) - - // type operator[](argument) specifiers; // (concrete function must throw std::out_of_range when out of range) + virtual void MapInOrder(const typename MappableContainer::MapFunctor, void*) = 0; }; /* ************************************************************************** */ template -class TestableContainer { // Must extend Container +class InOrderFoldableContainer : public virtual FoldableContainer { // Must extend FoldableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~TestableContainer() specifiers + virtual ~InOrderFoldableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + InOrderFoldableContainer& operator=(const InOrderFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + InOrderFoldableContainer& operator=(InOrderFoldableContainer&&) noexcept = 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 InOrderFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const InOrderFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Exists(argument) specifiers; // (concrete function should not throw exceptions) + virtual void FoldInOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const = 0; }; /* ************************************************************************** */ template -class MappableContainer { // Must extend Container +class BreadthMappableContainer : virtual public MappableContainer { // Must extend MappableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~MappableContainer() specifiers + ~BreadthMappableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + BreadthMappableContainer& operator=(const BreadthMappableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + BreadthMappableContainer& operator=(BreadthMappableContainer&&) noexcept = 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 BreadthMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const BreadthMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // typedef std::function MapFunctor; - - // type MapPreOrder(arguments) specifiers; - // type MapPostOrder(arguments) specifiers; + virtual void MapBreadth(const typename MappableContainer::MapFunctor, void*) = 0; }; /* ************************************************************************** */ template -class FoldableContainer { // Must extend TestableContainer +class BreadthFoldableContainer : virtual public FoldableContainer { // Must extend FoldableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~FoldableContainer() specifiers + virtual ~BreadthFoldableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + BreadthFoldableContainer& operator=(const BreadthFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + BreadthFoldableContainer& operator=(BreadthFoldableContainer&&) noexcept = 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 BreadthFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const BreadthFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // typedef std::function FoldFunctor; + using typename FoldableContainer::FoldFunctor; - // type FoldPreOrder(arguments) specifiers; - // type FoldPostOrder(arguments) specifiers; - - // type Exists(argument) specifiers; // Override TestableContainer member - -}; - -/* ************************************************************************** */ - -template -class InOrderMappableContainer { // Must extend MappableContainer - -private: - - // ... - -protected: - - // ... - -public: - - // Destructor - // ~InOrderMappableContainer() specifiers - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. - - // Move assignment - // type operator=(argument); // 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. - - /* ************************************************************************ */ - - // Specific member functions - - // using typename MappableContainer::MapFunctor; - - // type MapInOrder(arguments) specifiers; - -}; - -/* ************************************************************************** */ - -template -class InOrderFoldableContainer { // Must extend FoldableContainer - -private: - - // ... - -protected: - - // ... - -public: - - // Destructor - // ~InOrderFoldableContainer() specifiers - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. - - // Move assignment - // type operator=(argument); // 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. - - /* ************************************************************************ */ - - // Specific member functions - - // using typename MappableContainer::MapFunctor; - - // type FoldInOrder(arguments) specifiers; - -}; - -/* ************************************************************************** */ - -template -class BreadthMappableContainer { // Must extend MappableContainer - -private: - - // ... - -protected: - - // ... - -public: - - // Destructor - // ~BreadthMappableContainer() specifiers - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. - - // Move assignment - // type operator=(argument); // 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. - - /* ************************************************************************ */ - - // Specific member functions - - // using typename MappableContainer::MapFunctor; - - // type MapBreadth(arguments) specifiers; - -}; - -/* ************************************************************************** */ - -template -class BreadthFoldableContainer { // Must extend FoldableContainer - -private: - - // ... - -protected: - - // ... - -public: - - // Destructor - // ~BreadthFoldableContainer() specifiers - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. - - // Move assignment - // type operator=(argument); // 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. - - /* ************************************************************************ */ - - // Specific member functions - - // using typename FoldableContainer::FoldFunctor; - - // type FoldBreadth(arguments) specifiers; + virtual void FoldBreadth(const FoldFunctor, const void*, void*) const = 0; }; diff --git a/librerie/exercise4/iterator/iterator.hpp b/librerie/exercise4/iterator/iterator.hpp old mode 100644 new mode 100755 index 249bf5b..8c0724e --- a/librerie/exercise4/iterator/iterator.hpp +++ b/librerie/exercise4/iterator/iterator.hpp @@ -13,78 +13,70 @@ class Iterator { private: - // ... - protected: - // ... - public: // Destructor - // ~Iterator() specifiers + virtual ~Iterator() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Iterator& operator=(const Iterator&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Iterator& operator=(Iterator&&) noexcept = 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 Iterator&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const Iterator&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type operator*() specifiers; // (concrete function must throw std::out_of_range when terminated) + virtual Data& operator*() const = 0; // (concrete function must throw std::out_of_range when terminated) - // type Terminated() specifiers; // (concrete function should not throw exceptions) + virtual bool Terminated() const noexcept = 0; // (concrete function should not throw exceptions) }; /* ************************************************************************** */ template -class ForwardIterator { // Must extend Iterator +class ForwardIterator : virtual public Iterator { // Must extend Iterator private: - // ... - protected: - // ... - public: // Destructor - // ~ForwardIterator() specifiers + virtual ~ForwardIterator() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + ForwardIterator& operator=(const ForwardIterator&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + ForwardIterator& operator=(ForwardIterator&&) noexcept = 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 ForwardIterator&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const ForwardIterator&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type operator++() specifiers; // (concrete function must throw std::out_of_range when terminated) + virtual void operator++() = 0; // (concrete function must throw std::out_of_range when terminated) }; diff --git a/librerie/exercise4/list/list.cpp b/librerie/exercise4/list/list.cpp old mode 100644 new mode 100755 index d374ceb..3396301 --- a/librerie/exercise4/list/list.cpp +++ b/librerie/exercise4/list/list.cpp @@ -3,8 +3,263 @@ namespace lasd { /* ************************************************************************** */ -// ... - -/* ************************************************************************** */ - +// Specific constructors +template +List::Node::Node(const Data& newValue){ + value = newValue; + next = nullptr; +} + +// Copy constructor +template +List::Node::Node(const Node& copyFrom){ + value = copyFrom.value; + next = nullptr; +} + +// Move constructor +template +List::Node::Node(Node&& moveFrom){ + std::swap(value, moveFrom.value); + std::swap(next, moveFrom.next); +} + +template +List::Node::Node(Data&& moveFrom){ + std::swap(value, moveFrom); +} + +template +bool List::Node::operator==(const Node& node)const noexcept{ + return (node.value == value); +} + +template +bool List::Node::operator!=(const Node& node)const noexcept{ + return !(*this == node); +} + +template +List::List(const LinearContainer& con){ + for(ulong i = 0; i +List::List(const List& copyFrom){ + for(ulong i = 0; i +List::List(List&& moveFrom){ + std::swap(size, moveFrom.size); + std::swap(head, moveFrom.head); + std::swap(tail, moveFrom.tail); +} + +template +List::~List(){ + Clear(); +} + +template + List& List::operator=(const List& copyFrom){ + if(*this != copyFrom){ + Clear(); + for(ulong i = 0 ; i + List& List::operator=(List&& moveFrom)noexcept{ + if(*this != moveFrom){ + std::swap(size, moveFrom.size); + std::swap(head, moveFrom.head); + std::swap(tail, moveFrom.tail); + } + return *this; + } + + template + bool List::operator==(const List& list) const noexcept{ + if(size != list.Size()) return false; + for(ulong i = 0 ; i < size ; ++i){ + if((*this)[i] != list[i]) return false; + } + return true; + } + +template +bool List::operator!=(const List& list) const noexcept{ + return !(*this==list); +} + +template +void List::InsertAtFront(const Data& data){ + struct Node* tmp = new Node(data); + tmp->next = head; + head = tmp; + size++; + if(size == 1){ + tail = head; + } + } + +template +void List::InsertAtFront(Data&& data){ + struct Node* tmp = new Node(std::move(data)); + tmp->next = head; + head = tmp; + size++; + if(size == 1){ + tail = head; + } +} + +template + void List::RemoveFromFront(){ + if(head == nullptr){ + throw std::length_error("List is empty!"); + } + else{ + struct Node* tmp = head; + head = head->next; + tmp->next = nullptr; + delete tmp; + size--; + if(head==nullptr){ + tail=nullptr; + } + } + } + +template +Data List::FrontNRemove(){ + if(head == nullptr){ + throw std::length_error("List is empty!"); + } + else{ + Data value = head->value; + RemoveFromFront(); + return value; + } +} + +template +void List::InsertAtBack(const Data& data){ + if(size == 0){ + InsertAtFront(data); + } + else{ + struct Node* last = new Node(data); + tail->next = last; + tail = last; + size++; + } +} + +template +void List::InsertAtBack(Data&& data){ + if(size == 0){ + InsertAtFront(data); + } + else{ + struct Node* last = new Node(std::move(data)); + tail->next = last; + tail = last; + size++; + } +} + +template +void List::Clear(){ + while(head != nullptr){ + RemoveFromFront(); + } +} + +template +Data& List::Front() const{ + if(size == 0){ + throw std::length_error("List is empty!"); + }else{ + return head->value; + } +} + +template +Data& List::Back() const{ + if(size == 0){ + throw std::length_error("List is empty!"); + }else{ + return tail->value; + } +} + +template +Data& List::operator[](const ulong index) const{ + if(index >= size || index < 0){ + throw std::out_of_range("Out of Range!"); + }else{ + struct Node* tmp = head; + for(ulong i=0; inext; + } + return tmp->value; + } +} + +template +void List::MapPreOrder(MapFunctor function, void* par){ + MapPreOrder(function, par, head); +} + +template +void List::MapPostOrder(MapFunctor function, void* par){ + MapPostOrder(function, par, head); +} + +template +void List::FoldPreOrder(FoldFunctor function, const void* constPar, void* par) const{ + FoldPreOrder(function, constPar, par, head); +} + +template +void List::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{ + FoldPostOrder(function, constPar, par, head); +} + +template +void List::MapPreOrder(MapFunctor function, void* par, struct Node* node){ + if(node == nullptr) return; + function(node->value, par); + MapPreOrder(function, par, node->next); +} + +template +void List::MapPostOrder(MapFunctor function, void* par, struct Node* node){ + if(node == nullptr) return; + MapPostOrder(function, par, node->next); + function(node->value, par); +} + +template +void List::FoldPreOrder(FoldFunctor function, const void* constPar, void* par, struct Node* node) const{ + if(node == nullptr) return; + function(node->value, constPar, par); + FoldPreOrder(function, constPar, par, node->next); +} + +template +void List::FoldPostOrder(FoldFunctor function, const void* constPar, void* par, struct Node* node) const{ + if(node == nullptr) return; + FoldPostOrder(function, constPar, par, node->next); + function(node->value, constPar, par); +} } diff --git a/librerie/exercise4/list/list.hpp b/librerie/exercise4/list/list.hpp old mode 100644 new mode 100755 index c80e4df..926649b --- a/librerie/exercise4/list/list.hpp +++ b/librerie/exercise4/list/list.hpp @@ -13,150 +13,150 @@ namespace lasd { /* ************************************************************************** */ template -class List { // Must extend LinearContainer, MappableContainer, and FoldableContainer +class List : virtual public LinearContainer, + virtual public MappableContainer, + virtual public FoldableContainer{ // Must extend LinearContainer, MappableContainer, and FoldableContainer private: - // ... - protected: - // using LinearContainer::???; + using LinearContainer:: size; struct Node { - - // Data - // ... + Data value; + Node* next = nullptr; /* ********************************************************************** */ // Specific constructors - // ... + Node(const Data&); /* ********************************************************************** */ // Copy constructor - // ... + Node(const Node&); // Move constructor - // ... + Node(Node&&); + Node(Data&&); /* ********************************************************************** */ // Destructor - // ... + ~Node() = default; /* ********************************************************************** */ // Comparison operators - // ... + bool operator==(const Node&) const noexcept; + bool operator!=(const Node&) const noexcept; /* ********************************************************************** */ // Specific member functions - // ... - }; - // ... + struct Node* head = nullptr; + struct Node* tail = nullptr; public: // Default constructor - // List() specifiers; + List() = default; /* ************************************************************************ */ // Specific constructor - // List(argument) specifiers; // A list obtained from a LinearContainer + List(const LinearContainer&); // A list obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // List(argument) specifiers; + List(const List&); // Move constructor - // List(argument) specifiers; + List(List&&); /* ************************************************************************ */ // Destructor - // ~List() specifiers; + ~List(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + List& operator=(const List&); // Move assignment - // type operator=(argument) specifiers; + List& operator=(List&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const List&) const noexcept; + bool operator!=(const List&) const noexcept; /* ************************************************************************ */ // Specific member functions - // type InsertAtFront(argument) specifier; // Copy of the value - // type InsertAtFront(argument) specifier; // Move of the value - // type RemoveFromFront() specifier; // (must throw std::length_error when empty) - // type FrontNRemove() specifier; // (must throw std::length_error when empty) + void InsertAtFront(const Data&); // Copy of the value + void InsertAtFront(Data&&); // Move of the value + void RemoveFromFront(); // (must throw std::length_error when empty) + Data FrontNRemove(); // (must throw std::length_error when empty) - // type InsertAtBack(argument) specifier; // Copy of the value - // type InsertAtBack(argument) specifier; // Move of the value + void InsertAtBack(const Data&); // Copy of the value + void InsertAtBack(Data&&); // Move of the value /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member /* ************************************************************************ */ // Specific member functions (inherited from LinearContainer) - // type Front() specifiers; // Override LinearContainer member (must throw std::length_error when empty) - // type Back() specifiers; // Override LinearContainer member (must throw std::length_error when empty) + Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty) + Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty) - // type operator[](argument) specifiers; // Override LinearContainer member (must throw std::out_of_range when out of range) + Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range) /* ************************************************************************ */ // Specific member functions (inherited from MappableContainer) - // using typename MappableContainer::MapFunctor; + using typename MappableContainer::MapFunctor; - // type MapPreOrder(arguments) specifiers; // Override MappableContainer member - // type MapPostOrder(arguments) specifiers; // Override MappableContainer member + void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member + void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member /* ************************************************************************ */ // Specific member functions (inherited from FoldableContainer) - // using typename FoldableContainer::FoldFunctor; + using typename FoldableContainer::FoldFunctor; - // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member - // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member + void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member + void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member protected: // Auxiliary member functions (for MappableContainer) - // type MapPreOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards - // type MapPostOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards + void MapPreOrder(const MapFunctor, void* , struct Node*); // Accessory function executing from one point of the list onwards + void MapPostOrder(const MapFunctor, void* , struct Node*); // Accessory function executing from one point of the list onwards /* ************************************************************************ */ // Auxiliary member functions (for FoldableContainer) - // type FoldPreOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards - // type FoldPostOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards + void FoldPreOrder(const FoldFunctor, const void*, void*, struct Node*) const; // Accessory function executing from one point of the list onwards + void FoldPostOrder(const FoldFunctor, const void*, void*, struct Node*) const;; // Accessory function executing from one point of the list onwards }; diff --git a/librerie/exercise4/queue/lst/queuelst.cpp b/librerie/exercise4/queue/lst/queuelst.cpp old mode 100644 new mode 100755 index d374ceb..9153b80 --- a/librerie/exercise4/queue/lst/queuelst.cpp +++ b/librerie/exercise4/queue/lst/queuelst.cpp @@ -1,10 +1,73 @@ namespace lasd { -/* ************************************************************************** */ +template +QueueLst::QueueLst(const LinearContainer& linear) + : List(linear) {} -// ... +template +QueueLst::QueueLst(const QueueLst& copyFrom) + : List(copyFrom) {} -/* ************************************************************************** */ +template +QueueLst::QueueLst(QueueLst&& moveFrom) noexcept + : List(std::move(moveFrom)) {} + +template +QueueLst::~QueueLst(){ + Clear(); +} + +template +QueueLst& QueueLst::operator=(const QueueLst& toCopy){ + List::operator=(toCopy); + return *this; +} + +template +QueueLst& QueueLst::operator=(QueueLst&& toMove) noexcept{ + List::operator=(std::move(toMove)); + return *this; +} + +template +bool QueueLst::operator==(const QueueLst& queuelist) const noexcept{ + return List::operator==(queuelist); + } + +template +bool QueueLst::operator!=(const QueueLst& queuelist) const noexcept{ + return List::operator!=(queuelist); + } + +template + void QueueLst::Enqueue(const Data& data){ + List::InsertAtBack(data); + } + +template +void QueueLst::Enqueue(Data&& data){ + List::InsertAtBack(std::move(data)); + } + +template +Data& QueueLst::Head() const{ + return List::Front(); +} + +template +void QueueLst::Dequeue(){ + List::RemoveFromFront(); +} + +template +Data QueueLst::HeadNDequeue(){ + return List::FrontNRemove(); +} + +template +void QueueLst::Clear(){ + List::Clear(); +} } diff --git a/librerie/exercise4/queue/lst/queuelst.hpp b/librerie/exercise4/queue/lst/queuelst.hpp old mode 100644 new mode 100755 index 4f1954c..92ea410 --- a/librerie/exercise4/queue/lst/queuelst.hpp +++ b/librerie/exercise4/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/exercise4/queue/queue.hpp b/librerie/exercise4/queue/queue.hpp old mode 100644 new mode 100755 index 6b612e4..ba810d8 --- a/librerie/exercise4/queue/queue.hpp +++ b/librerie/exercise4/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/exercise4/queue/vec/queuevec.cpp b/librerie/exercise4/queue/vec/queuevec.cpp old mode 100644 new mode 100755 index d374ceb..42f6857 --- a/librerie/exercise4/queue/vec/queuevec.cpp +++ b/librerie/exercise4/queue/vec/queuevec.cpp @@ -1,10 +1,192 @@ namespace lasd { -/* ************************************************************************** */ +template +QueueVec::QueueVec(){ + size = 4; + rear = 0; + front = 0; + Elements = new Data[size]; +} -// ... +template +QueueVec::QueueVec(const LinearContainer& linear){ + size = linear.Size()+1; // 1 free cell + Elements = new Data[size]; //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 = i; +} + +template +QueueVec::QueueVec(QueueVec&& toMove) noexcept{ + Clear(); + std::swap(Elements, toMove.Elements); + std::swap(rear, toMove.rear); + std::swap(front, toMove.front); + std::swap(size, toMove.size); +} + +template +QueueVec::~QueueVec(){ + //vector destructor will be automatically called I hope +} + +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("Queue is empty!"); + } + return Elements[front]; +} + +template +void QueueVec::Dequeue(){ + if(Size() <= 0){ + throw std::length_error("Queue 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 QueueVec::Size() const noexcept{ + //if(size == 0) return 0; // this won't ever get executed, it's here just in case + return ((rear + size) - front) % size; +} + +template +void QueueVec::Clear(){ + if(size!=4){ + delete[] Elements; + Elements = new Data[4]; + size = 4; + } + front = 0; + rear = 0; +} + +template +void QueueVec::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 +void QueueVec::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(current_index != rear){ + tmp[i] = Elements[current_index]; + current_index = (current_index+1)%size; + ++i; + } + delete[] Elements; + Elements = tmp; + front = 0; + rear = i; + size = newsize; +} } diff --git a/librerie/exercise4/queue/vec/queuevec.hpp b/librerie/exercise4/queue/vec/queuevec.hpp old mode 100644 new mode 100755 index 97b7e97..1f9bbe5 --- a/librerie/exercise4/queue/vec/queuevec.hpp +++ b/librerie/exercise4/queue/vec/queuevec.hpp @@ -14,82 +14,83 @@ namespace lasd { /* ************************************************************************** */ template -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; // dimension of the array + 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 + ulong 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/exercise4/stack/lst/stacklst.cpp b/librerie/exercise4/stack/lst/stacklst.cpp old mode 100644 new mode 100755 index d374ceb..f1f24d8 --- a/librerie/exercise4/stack/lst/stacklst.cpp +++ b/librerie/exercise4/stack/lst/stacklst.cpp @@ -3,7 +3,77 @@ namespace lasd { /* ************************************************************************** */ -// ... +// Constructors +template +StackLst::StackLst(const LinearContainer& linear) + : List(linear){} + +template +StackLst::StackLst(const StackLst& stcklist) + : List(stcklist){} + +template +StackLst::StackLst(StackLst&& stcklist) noexcept + : List(std::move(stcklist)){} + +// Destructor +template +StackLst::~StackLst(){ + Clear(); +} + +template +StackLst& StackLst::operator=(const StackLst& copyFrom){ + List::operator=(copyFrom); + return *this; +} + +template +StackLst& StackLst::operator=(StackLst&& moveFrom) noexcept{ + List::operator=(std::move(moveFrom)); + return *this; +} + +template +bool StackLst::operator==(const StackLst& stcklist) const noexcept{ + return List::operator==(stcklist); +} + +template +bool StackLst::operator!=(const StackLst& stcklist) const noexcept{ + return List::operator!=(stcklist); +} + +// Specific member functions (inherited from Stack) +template +void StackLst::Push(const Data& element){ + List::InsertAtFront(element); +} + +template +void StackLst::Push(Data&& element) noexcept{ + List::InsertAtFront(element); +} + +template +Data& StackLst::Top() const{ + return List::Front(); +} + +template +void StackLst::Pop(){ + List::RemoveFromFront(); +} + +template +Data StackLst::TopNPop(){ + return List::FrontNRemove(); +} + +template +void StackLst::Clear(){ + List::Clear(); +} /* ************************************************************************** */ diff --git a/librerie/exercise4/stack/lst/stacklst.hpp b/librerie/exercise4/stack/lst/stacklst.hpp old mode 100644 new mode 100755 index 67fbb32..16ce8fb --- a/librerie/exercise4/stack/lst/stacklst.hpp +++ b/librerie/exercise4/stack/lst/stacklst.hpp @@ -14,70 +14,69 @@ namespace lasd { /* ************************************************************************** */ template -class StackLst { // Must extend Stack and List +class StackLst : virtual public Stack, + virtual protected List { // Must extend Stack and List private: - // ... - protected: - // using List::???; - - // ... + using List::head; + using List::size; + using typename List::Node; public: // Default constructor - // StackLst() specifier; + StackLst() = default; /* ************************************************************************ */ // Specific constructor - // StackLst(argument) specifiers; // A stack obtained from a LinearContainer + StackLst(const LinearContainer&); // A stack obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // StackLst(argument); + StackLst(const StackLst&); // Move constructor - // StackLst(argument); + StackLst(StackLst&&) noexcept; /* ************************************************************************ */ // Destructor - // ~StackLst() specifier; + virtual ~StackLst(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + StackLst& operator=(const StackLst&); // Move assignment - // type operator=(argument); + StackLst& operator=(StackLst&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const StackLst&) const noexcept; + bool operator!=(const StackLst&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Stack) - // type Push(argument) specifiers; // Override Stack member (copy of the value) - // type Push(argument) specifiers; // Override Stack member (move of the value) - // type Top() specifiers; // Override Stack member (must throw std::length_error when empty) - // type Pop() specifiers; // Override Stack member (must throw std::length_error when empty) - // type TopNPop() specifiers; // Override Stack member (must throw std::length_error when empty) + void Push(const Data&) override; // Override Stack member (copy of the value) + void Push(Data&&) noexcept override; // Override Stack member (move of the value) + Data& Top() const override; // Override Stack member (must throw std::length_error when empty) + void Pop() override; // Override Stack member (must throw std::length_error when empty) + Data TopNPop() override; // Override Stack 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/exercise4/stack/stack.hpp b/librerie/exercise4/stack/stack.hpp old mode 100644 new mode 100755 index 73a3a7c..fee854b --- a/librerie/exercise4/stack/stack.hpp +++ b/librerie/exercise4/stack/stack.hpp @@ -13,44 +13,40 @@ namespace lasd { /* ************************************************************************** */ template -class Stack { // Must extend Container +class Stack : virtual public Container { // Must extend Container private: - // ... - protected: - // ... - public: // Destructor - // ~Stack() specifiers + virtual ~Stack() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Stack& operator=(const Stack&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Stack&operator=(Stack&&) = 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 Stack&) = delete; // Comparison of abstract types might not be possible. + bool operator!=(Stack&&) = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Push(argument) specifiers; // Copy of the value - // type Push(argument) specifiers; // Move of the value - // type Top() specifiers; // (concrete function must throw std::length_error when empty) - // type Pop() specifiers; // (concrete function must throw std::length_error when empty) - // type TopNPop() specifiers; // (concrete function must throw std::length_error when empty) + virtual void Push(const Data&) = 0; // Copy of the value + virtual void Push(Data&&) = 0; // Move of the value + virtual Data& Top() const = 0; // (concrete function must throw std::length_error when empty) + virtual void Pop() = 0; // (concrete function must throw std::length_error when empty) + virtual Data TopNPop() = 0; // (concrete function must throw std::length_error when empty) }; diff --git a/librerie/exercise4/stack/vec/stackvec.cpp b/librerie/exercise4/stack/vec/stackvec.cpp old mode 100644 new mode 100755 index d374ceb..eabb6ad --- a/librerie/exercise4/stack/vec/stackvec.cpp +++ b/librerie/exercise4/stack/vec/stackvec.cpp @@ -2,9 +2,142 @@ namespace lasd { /* ************************************************************************** */ - -// ... - -/* ************************************************************************** */ - +// constructors +template +StackVec::StackVec(){ + size = 4; // default vector is instantiated with 4 cells + stackSize = 0; + Elements = new Data[size]; +} + +template +StackVec::StackVec(const LinearContainer& linear) + : Vector(linear){ + stackSize = linear.Size(); // the array is full +} + +template +StackVec::StackVec(const StackVec& stckvec) + : Vector(stckvec){ + stackSize = stckvec.Size(); // the array is full +} + + +template +StackVec::StackVec(StackVec&& toMove) noexcept + : Vector(std::move(toMove)){ + std::swap(stackSize, toMove.stackSize); +} + +template +StackVec::~StackVec(){ + // Vector destructor will be called automatically +} + +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)); + stackSize = moveFrom.Size(); + return *this; +} + +template +bool StackVec::operator==(const StackVec& toCompare) const noexcept{ + if(stackSize == toCompare.Size()){ + for(ulong i=0 ; i +bool StackVec::operator!=(const StackVec& toCompare) const noexcept{ + return !(*this == 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); + ++stackSize; +} + +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); +} + +template +void StackVec::Clear(){ + delete [] Elements; + size = 4; + stackSize = 0; + Elements = new Data[size]; +} } diff --git a/librerie/exercise4/stack/vec/stackvec.hpp b/librerie/exercise4/stack/vec/stackvec.hpp old mode 100644 new mode 100755 index 5e65367..d13a313 --- a/librerie/exercise4/stack/vec/stackvec.hpp +++ b/librerie/exercise4/stack/vec/stackvec.hpp @@ -14,81 +14,81 @@ namespace lasd { /* ************************************************************************** */ template -class StackVec { // Must extend Stack and Vector +class StackVec : virtual public Stack, + virtual protected Vector{ // Must extend Stack and Vector private: - // ... - protected: - // using Vector::???; + ulong stackSize = 0; // first empty cell and # of elements in the vector + using Vector::Elements; + using Vector::size; // dimension of the vector - // ... public: // Default constructor - // StackVec() specifier; + StackVec(); /* ************************************************************************ */ // Specific constructor - // StackVec(argument) specifiers; // A stack obtained from a LinearContainer + StackVec(const LinearContainer&); // A stack obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // StackVec(argument); + StackVec(const StackVec&); // Move constructor - // StackVec(argument); + StackVec(StackVec&&) noexcept; /* ************************************************************************ */ // Destructor - // ~StackVec() specifier; + virtual ~StackVec(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + StackVec& operator=(const StackVec&); // Move assignment - // type operator=(argument); + StackVec& operator=(StackVec&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const StackVec&) const noexcept; + bool operator!=(const StackVec&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Stack) - // type Push(argument) specifiers; // Override Stack member (copy of the value) - // type Push(argument) specifiers; // Override Stack member (move of the value) - // type Top() specifiers; // Override Stack member (must throw std::length_error when empty) - // type Pop() specifiers; // Override Stack member (must throw std::length_error when empty) - // type TopNPop() specifiers; // Override Stack member (must throw std::length_error when empty) + void Push(const Data&) override; // Override Stack member (copy of the value) + void Push(Data&&) override; // Override Stack member (move of the value) + Data& Top() const override; // Override Stack member (must throw std::length_error when empty) + void Pop() override; // Override Stack member (must throw std::length_error when empty) + Data TopNPop() override; // Override Stack 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 + ulong 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; + void Expand(); + void Reduce(); }; diff --git a/librerie/exercise4/vector/vector.cpp b/librerie/exercise4/vector/vector.cpp old mode 100644 new mode 100755 index d374ceb..a7b3d5e --- a/librerie/exercise4/vector/vector.cpp +++ b/librerie/exercise4/vector/vector.cpp @@ -1,10 +1,160 @@ namespace lasd { -/* ************************************************************************** */ - -// ... - -/* ************************************************************************** */ - +template +Vector::Vector(const ulong newsize){ + Elements = new Data[newsize]{}; + size = newsize; +} + +template +Vector::Vector(const LinearContainer& con){ + size = con.Size(); + Elements = new Data[size]{}; + for(ulong i=0; i +Vector::Vector(const Vector& vec){ + size = vec.size; + Elements = new Data[size]{}; + for(ulong i=0; i + Vector::Vector(Vector&& vec)noexcept{ + std::swap(Elements, vec.Elements); + std::swap(size, vec.size); + } + + // Destructor +template +Vector::~Vector(){ + Clear(); +} + +// Copy assignment +template +Vector& Vector::operator=(const Vector& vec){ + Vector* tmpvec = new Vector(vec); + std::swap(*tmpvec, *this); + delete tmpvec; + return *this; +} + +// Move assignment +template +Vector& Vector::operator=(Vector&& vec)noexcept{ + std::swap(Elements,vec.Elements); + std::swap(size, vec.size); + return *this; +} + +template +bool Vector::operator==(const Vector& vec) const noexcept{ + if(size == vec.size){ + for(ulong i=0; i +bool Vector::operator!=(const Vector& vec)const noexcept{ + return !(*this == vec); +} + +template +void Vector::Resize(const ulong newsize){ + if(newsize == 0){ + Clear(); + } + else if(size != newsize){ + ulong limit = (size < newsize) ? size : newsize; + Data* TmpElements = new Data[newsize]{}; + for(ulong i=0; i +void Vector::Clear(){ + delete[] Elements; + Elements = nullptr; + size = 0; +} + +template +Data& Vector::Front() const { + if(size != 0){ + return Elements[0]; + } + else{ + throw std::length_error("Access to an empty vector!"); + } +} + +template +Data& Vector::Back() const { + if(size != 0){ + return Elements[size - 1]; + } + else{ + throw std::length_error("Access to an empty vector!"); + } +} + +template +Data& Vector::operator[](const ulong index) const{ + if(index < size){ + return Elements[index]; + } + else{ + throw std::out_of_range("Tried to access index " + std::to_string(index) + " but the dimension of the vector is " + std::to_string(size)); + } +} + +template +void Vector::MapPreOrder(const MapFunctor fun, void* par){ + for(ulong i=0; i +void Vector::MapPostOrder(const MapFunctor fun, void* par){ + ulong index = size; + while(index > 0){ + fun(Elements[--index], par); + } +} + +template +void Vector::FoldPreOrder(const FoldFunctor fun, const void* par, void* acc) const{ + for(ulong i=0; i +void Vector::FoldPostOrder(const FoldFunctor fun, const void* par, void* acc) const{ + ulong idx = size; + while(idx > 0){ + fun(Elements[--idx], par, acc); + } +} } diff --git a/librerie/exercise4/vector/vector.hpp b/librerie/exercise4/vector/vector.hpp old mode 100644 new mode 100755 index 6f127ff..2ae8fed --- a/librerie/exercise4/vector/vector.hpp +++ b/librerie/exercise4/vector/vector.hpp @@ -13,99 +13,65 @@ namespace lasd { /* ************************************************************************** */ template -class Vector { // Must extend LinearContainer, MappableContainer, and FoldableContainer +class Vector : virtual public LinearContainer, + virtual public MappableContainer, + virtual public FoldableContainer{ // Must extend LinearContainer, MappableContainer, and FoldableContainer private: - // ... protected: - // using LinearContainer::???; - - // ... + using LinearContainer::size; + Data* Elements = nullptr; public: - // Default constructor - // Vector() specifiers; - - /* ************************************************************************ */ + Vector() = default; // Specific constructors - // Vector(argument) specifiers; // A vector with a given initial dimension - // Vector(argument) specifiers; // A vector obtained from a LinearContainer - - /* ************************************************************************ */ + Vector(const ulong); // A vector with a given initial dimension + Vector(const LinearContainer&); // A vector obtained from a LinearContainer // Copy constructor - // Vector(argument) specifiers; + Vector(const Vector&); // Move constructor - // Vector(argument) specifiers; - - /* ************************************************************************ */ + Vector(Vector&&)noexcept; // Destructor - // ~Vector() specifiers; - - /* ************************************************************************ */ + ~Vector(); // Copy assignment - // type operator=(argument) specifiers; + Vector& operator=(const Vector&); // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ + Vector& operator=(Vector&&) noexcept; // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const Vector&) const noexcept; + bool operator!=(const Vector&) const noexcept; - /* ************************************************************************ */ + void Resize(const ulong); // Resize the vector to a given size - // Specific member functions + void Clear() override; // Override Container member - // type Resize(argument) specifiers; // Resize the vector to a given size + Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty) + Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty) - /* ************************************************************************ */ + Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range) - // Specific member functions (inherited from Container) + using typename MappableContainer::MapFunctor; - // type Clear() specifiers; // Override Container member + void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member + void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member - /* ************************************************************************ */ - - // Specific member functions (inherited from LinearContainer) - - // type Front() specifiers; // Override LinearContainer member (must throw std::length_error when empty) - // type Back() specifiers; // Override LinearContainer member (must throw std::length_error when empty) - - // type operator[](argument) specifiers; // Override LinearContainer member (must throw std::out_of_range when out of range) - - /* ************************************************************************ */ - - // Specific member functions (inherited from MappableContainer) - - // using typename MappableContainer::MapFunctor; - - // type MapPreOrder(arguments) specifiers; // Override MappableContainer member - // type MapPostOrder(arguments) specifiers; // Override MappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from FoldableContainer) - - // using typename FoldableContainer::FoldFunctor; - - // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member - // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member + using typename FoldableContainer::FoldFunctor; + void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member + void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member }; -/* ************************************************************************** */ - } #include "vector.cpp"