diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp index 7032298..dabdb14 100755 --- a/librerie/exercise3/binarytree/binarytree.cpp +++ b/librerie/exercise3/binarytree/binarytree.cpp @@ -13,26 +13,27 @@ namespace lasd { // checks if two trees which have the same root node are identical template -bool BinaryTree::Node::operator==(const Node& toEvaluate){ - return EqualNodes(this, &toEvaluate); +bool BinaryTree::Node::operator==(const Node& toEvaluate) const noexcept{ + return EqualNodes(*this, toEvaluate); } /* given two nodes, checks if the subtree is the same */ -bool BinaryTree::Node::EqualNodes(Node* n1, Node* n2){ - if(n1->data == n2->data){ - if(n1->IsLeaf() && n2->IsLeaf()) return true; - if( (n1->HasLeftChild() && !n2->HasLeftChild()) || (n1->HasRightChild() && !n2->HasRightChild()) ) return false; - return( EqualNodes(n1->LeftChild(),n2->LeftChild()) && - EqualNodes(n1->RightChild(),n2->RightChild()) - ) +template +bool BinaryTree::Node::EqualNodes(const Node& n1, const Node& n2) const{ + if(n1.data == n2.data){ + if(n1.IsLeaf() && n2.IsLeaf()) return true; + if( (n1.HasLeftChild() && !n2.HasLeftChild()) || (n1.HasRightChild() && !n2.HasRightChild()) ) return false; + return( EqualNodes(n1.LeftChild(),n2.LeftChild()) && + EqualNodes(n1.RightChild(),n2.RightChild()) + ); }else{ return false; } } template -bool BinaryTree::Node::operator!=(const Node& toEvaluate){ - return !(*this == toEvaluate) +bool BinaryTree::Node::operator!=(const Node& toEvaluate) const noexcept{ + return !(*this == toEvaluate); } template @@ -41,7 +42,7 @@ Data& BinaryTree::Node::Element(){ } template -const Data& BinaryTree::Node::Element(){ +const Data& BinaryTree::Node::Element() const{ return this->data; } @@ -58,132 +59,132 @@ bool BinaryTree::operator!=(const BinaryTree& toCompare) const noexcept{ /* ----- Map and fold functions ----- */ template -void BinaryTree::MapPreOrder(const MapFunctor function, void* par){ +void BinaryTree::MapPreOrder(const typename MappableContainer::MapFunctor function, void* par){ MapPreOrder(function, par, &Root()); } template -void BinaryTree::MapPostOrder(const MapFunctor function, void* par){ +void BinaryTree::MapPostOrder(const typename MappableContainer::MapFunctor function, void* par){ MapPostOrder(function, par, &Root()); } template -void BinaryTree::MapInOrder(const MapFunctor function, void* par){ +void BinaryTree::MapInOrder(const typename MappableContainer::MapFunctor function, void* par){ MapInOrder(function, par, &Root()); } template -void BinaryTree::MapBreadth(const MapFunctor function, void* par){ +void BinaryTree::MapBreadth(const typename MappableContainer::MapFunctor function, void* par){ MapBreadth(function, par, &Root()); } template -void BinaryTree::FoldPreOrder(const FoldFunctor function, const void* par, void* acc){ +void BinaryTree::FoldPreOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ FoldPreOrder(function, par, acc, &Root()); } template -void BinaryTree::FoldPostOrder(const FoldFunctor function, const void* par, void* acc) const{ +void BinaryTree::FoldPostOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ FoldPostOrder(function, par, acc, &Root()); } template -void BinaryTree::FoldInOrder(const MapFunctor function, const void* par, void* acc) const{ +void BinaryTree::FoldInOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ FoldInOrder(function, par, acc, &Root()); } template -void BinaryTree::FoldBreadth(const FoldFunctor function, const void* par, void* acc) const{ +void BinaryTree::FoldBreadth(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc) const{ FoldBreadth(function, par, acc, &Root()); } /* ----- Auxiliary map and fold functions ----- */ template -void BinaryTree::MapPreOrder(const MapFunctor function, void* par, Node* node){ +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()); + MapPreOrder(function, par, &(node->LeftChild())); } if(node->HasRightChild()){ - MapPreOrder(function, par, node->RightChild()); + MapPreOrder(function, par, &(node->RightChild())); } } } template -void BinaryTree::MapPostOrder(const MapFunctor function, void* par, Node* node){ +void BinaryTree::MapPostOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ if(node != nullptr){ if(node->HasLeftChild()){ - MapPreOrder(function, par, node->LeftChild()); + MapPreOrder(function, par, &(node->LeftChild())); } if(node->HasRightChild()){ - MapPreOrder(function, par, node->RightChild()); + MapPreOrder(function, par, &(node->RightChild())); } function(node->Element(), par); } } template -void BinaryTree::FoldPreOrder(const FoldFunctor function, const void* par, void* acc, const Node* node){ +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()); + FoldPreOrder(function, par, acc, &(node->LeftChild())); } if(node->HasRightChild()){ - FoldPreOrder(function, par, acc, node->RightChild()); + FoldPreOrder(function, par, acc, &(node->RightChild())); } } } template -void BinaryTree::FoldPostOrder(const FoldFunctor function, const void* par, void* acc, const Node* node){ +void BinaryTree::FoldPostOrder(const typename FoldableContainer::FoldFunctor function, const void* par, void* acc, const Node* node) const{ if(node != nullptr){ if(node->HasLeftChild()){ - FoldPreOrder(function, par, acc, node->LeftChild()); + FoldPreOrder(function, par, acc, &(node->LeftChild())); } if(node->HasRightChild()){ - MapPreOrder(function, par, acc, node->RightChild()); + FoldPreOrder(function, par, acc, &(node->RightChild())); } function(node->Element(), par, acc); } } template -void BinaryTree::MapInOrder(const MapFunctor function, void* par, Node* node){ +void BinaryTree::MapInOrder(const typename MappableContainer::MapFunctor function, void* par, Node* node){ if(node != nullptr){ if(node->HasLeftChild()){ - MapInOrder(function, par, node->LeftChild()); + MapInOrder(function, par, &(node->LeftChild())); } function(node->Element(), par); if(node->HasRightChild()){ - MapInOrder(function, par, node->RightChild()); + MapInOrder(function, par, &(node->RightChild())); } } } template -void FoldInOrder(const FoldFunctor function, const void* par, void* acc, const Node* node) const{ +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()); + FoldInOrder(function, par, acc, &(node->LeftChild())); } function(node->Element(), par, acc); if(node->HasRightChild()){ - FoldPreOrder(function, par, acc, node->RightChild()); + FoldPreOrder(function, par, acc, &(node->RightChild())); } } } template -void BinaryTree::MapBreadth(const MapFunctor function, void* par, Node* node){ +void BinaryTree::MapBreadth(const typename MappableContainer::MapFunctor function, void* par, Node* node){ QueueLst toVisit; if(node != nullptr){ toVisit.Enqueue(node); while(!toVisit.Empty()){ - func(toVisit.Head()->Element(), par); + function(toVisit.Head()->Element(), par); if(toVisit.Head()->HasLeftChild()){ toVisit.Enqueue(&(toVisit.Head()->LeftChild())); @@ -197,12 +198,12 @@ void BinaryTree::MapBreadth(const MapFunctor function, void* par, Node* no } template -void BinaryTree::FoldBreadth(const MapFunctor function, const void* par, void* acc, const Node* node){ - QueueLst toVisit; +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()){ - func(toVisit.Head()->Element(), par, acc); + function(toVisit.Head()->Element(), par, acc); if(toVisit.Head()->HasLeftChild()){ toVisit.Enqueue(&(toVisit.Head()->LeftChild())); @@ -240,7 +241,7 @@ BTPreOrderIterator::~BTPreOrderIterator(){ } template -BTPreOrderIterator& BTPreOrderIterator::operator=(const BTPreOrderIterator& itr){ +BTPreOrderIterator& BTPreOrderIterator::operator=(const BTPreOrderIterator& itr){ //curr = &(*itr); curr = itr.curr; stack = itr.stack; @@ -248,7 +249,7 @@ BTPreOrderIterator& BTPreOrderIterator::operator=(const BTPreOrderIterator } template -BTPreOrderIterator& BTPreOrderIterator::operator=(BTPreOrderIterator&& itr) noexcept{ +BTPreOrderIterator& BTPreOrderIterator::operator=(BTPreOrderIterator&& itr) noexcept{ std::swap(curr, itr.curr); std::swap(stack, itr.stack); return *this; @@ -265,31 +266,31 @@ bool BTPreOrderIterator::operator!=(const BTPreOrderIterator& itr) const n } template -struct BinaryTree::Node BTPreOrderIterator::operator*() const{ - return *curr; +Data& BTPreOrderIterator::operator*() const{ + return curr->Element(); } template -bool BTPreOrderIterator::Terminated() noexcept{ - return curr==nullptr; +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->HasLeftChild()){ curr = &(curr->LeftChild()); - if( (curr->HasRightChild() ) + if( curr->HasRightChild() ) stack.Push(&curr->RightChild()); - }else if( (curr->HasRightChild() ){ + }else if(curr->HasRightChild()){ curr = &curr->RightChild(); } else{ try{ - curr = &stack.TopNPop(); + curr = stack.TopNPop(); }catch(std:: length_error exc){ curr = nullptr; } @@ -300,11 +301,11 @@ template struct BinaryTree::Node* BTPostOrderIterator::DeepestLeftLeaf(struct BinaryTree::Node* node){ if(node->HasLeftChild()){ stack.Push(node); - MostLeftLeaf(node->LeftChild()); + DeepestLeftLeaf(&(node->LeftChild())); } else if(node->HasRightChild()){ stack.Push(node); - MostLeftLeaf(node->RightChild()); + DeepestLeftLeaf(&(node->RightChild())); } else return node; @@ -312,7 +313,7 @@ struct BinaryTree::Node* BTPostOrderIterator::DeepestLeftLeaf(struct template BTPostOrderIterator::BTPostOrderIterator(const BinaryTree& tree){ - curr = DeepestLeftLeaf(&tree.Root()) + curr = DeepestLeftLeaf(&tree.Root()); } template @@ -334,14 +335,14 @@ BTPostOrderIterator::~BTPostOrderIterator(){ } template -BTPostOrderIterator& BTPostOrderIterator::operator=(const BTPostOrderIterator& itr){ +BTPostOrderIterator& BTPostOrderIterator::operator=(const BTPostOrderIterator& itr){ curr = itr.curr; stack = itr.stack; return *this; } template -BTPostOrderIterator& BTPostOrderIterator::operator=(BTPostOrderIterator&& itr){ +BTPostOrderIterator& BTPostOrderIterator::operator=(BTPostOrderIterator&& itr) noexcept{ std::swap(curr, itr.curr); std::swap(stack, itr.stack); return *this; @@ -358,27 +359,27 @@ bool BTPostOrderIterator::operator!=(const BTPostOrderIterator& itr) const } template -struct BinaryTree::Node BTPostOrderIterator::operator*() const{ - return *curr; +Data& BTPostOrderIterator::operator*() const{ + return curr->Element(); } template -bool BTPostOrderIterator::Terminated() noexcept{ - return (curr == nullptr) +bool BTPostOrderIterator::Terminated() const noexcept{ + return (curr == nullptr); } template void BTPostOrderIterator::operator++(){ if(Terminated()) throw std::out_of_range("Iterator is terminated!"); try{ - if(curr == &stack.Top().LeftChild()){ - if(stack.Top().HasRightChild()){ - curr = DeepestLeftLeaf(&stack.Top().RightChild()); + if( curr == &((stack.Top())->LeftChild()) ){ + if( (stack.Top())->HasRightChild() ){ + curr = DeepestLeftLeaf(&((stack.Top())->RightChild())); }else{ - curr = &stack.TopNPop(); + curr = stack.TopNPop(); } }else{ - curr = &stack.TopNPop(); + curr = stack.TopNPop(); } }catch(std::length_error exc){ curr = nullptr; @@ -386,81 +387,81 @@ void BTPostOrderIterator::operator++(){ } template -struct BinaryTree::Node* BTInOrdertOrderIterator::MostLeftNode(struct BinaryTree::Node* root){ - if(root->HasLeftChild()){ - stack.Push(root); - MostLeftNode(root->LeftChild()); +struct BinaryTree::Node* BTInOrderIterator::MostLeftNode(struct BinaryTree::Node& root){ + if(root.HasLeftChild()){ + stack.Push(&root); + MostLeftNode(root.LeftChild()); }else{ - return root; + return &root; } } template -BTInOrdertOrderIterator::BTInOrderIterator(const BinaryTree& tree){ +BTInOrderIterator::BTInOrderIterator(const BinaryTree& tree){ curr = MostLeftNode(tree.Root()); } template -BTInOrdertOrderIterator::BTInOrderIterator(const BTInOrderIterator& itr){ +BTInOrderIterator::BTInOrderIterator(const BTInOrderIterator& itr){ curr = itr.curr; stack = itr.stack; } template -BTInOrdertOrderIterator::BTInOrderIterator(BTInOrderIterator&& toMove) noexcept{ +BTInOrderIterator::BTInOrderIterator(BTInOrderIterator&& toMove) noexcept{ std::move(curr, toMove.curr); std::move(stack, toMove.stack); } template - BTInOrdertOrderIterator::~BTInOrderIterator(){ +BTInOrderIterator::~BTInOrderIterator(){ stack.Clear(); curr = nullptr; } template -BTInOrdertOrderIterator::BTInOrderIterator& operator=(const BTInOrderIterator& itr){ +BTInOrderIterator& BTInOrderIterator::operator=(const BTInOrderIterator& itr){ curr = itr.curr; stack = itr.stack; return *this; } template -BTInOrdertOrderIterator::BTInOrderIterator& operator=(BTInOrderIterator&& itr){ +BTInOrderIterator& BTInOrderIterator::operator=(BTInOrderIterator&& toMove) noexcept{ std::move(curr, toMove.curr); std::move(stack, toMove.stack); return *this; } template -bool BTInOrdertOrderIterator::operator==(const BTInOrderIterator& itr) const noexcept{ +bool BTInOrderIterator::operator==(const BTInOrderIterator& itr) const noexcept{ return (curr == itr.curr && stack == itr.stack ); } template -bool BTInOrdertOrderIterator::operator!=(const BTInOrderIterator& itr) const noexcept{ +bool BTInOrderIterator::operator!=(const BTInOrderIterator& itr) const noexcept{ return !(*this == itr); } template -struct BinaryTree::Node BTInOrdertOrderIterator::operator*() const{ - return *curr; +Data& BTInOrderIterator::operator*() const{ + return curr->Element(); } template -bool BTInOrdertOrderIterator::Terminated() noexcept{ +bool BTInOrderIterator::Terminated() const noexcept{ return (curr == nullptr); } template -void BTInOrdertOrderIterator::operator++(){ +void BTInOrderIterator::operator++(){ if(Terminated()) throw std::out_of_range("Iterator is terminated!"); if(curr->HasRightChild()){ curr = MostLeftNode(curr->RightChild()); }else{ try{ - curr = &(stack.TopNPop()); + curr = stack.TopNPop(); }catch(std::length_error exc){ curr = nullptr; } @@ -491,14 +492,14 @@ BTBreadthIterator::~BTBreadthIterator(){ } template -BTBreadthIterator& BTBreadthIterator::operator=(const BTBreadthIterator& itr){ +BTBreadthIterator& BTBreadthIterator::operator=(const BTBreadthIterator& itr){ curr = itr.curr; queue = itr.queue; return *this; } template -BTBreadthIterator& BTBreadthIterator::operator=(BTBreadthIterator&& itr) noexcept{ +BTBreadthIterator& BTBreadthIterator::operator=(BTBreadthIterator&& itr) noexcept{ std::swap(curr, itr.curr); std::swap(queue, itr.queue); return *this; @@ -515,12 +516,12 @@ bool BTBreadthIterator::operator!=(const BTBreadthIterator& itr) const noe } template -struct BinaryTree::Node BTBreadthIterator::operator*() const{ - return *curr; +Data& BTBreadthIterator::operator*() const{ + return curr->Element(); } template -bool BTBreadthIterator::Terminated() noexcept{ +bool BTBreadthIterator::Terminated() const noexcept{ return curr == nullptr; } diff --git a/librerie/exercise3/binarytree/binarytree.hpp b/librerie/exercise3/binarytree/binarytree.hpp index f6a82e6..e71be10 100755 --- a/librerie/exercise3/binarytree/binarytree.hpp +++ b/librerie/exercise3/binarytree/binarytree.hpp @@ -47,7 +47,7 @@ public: 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(Node*, Node*); + bool EqualNodes(const Node&, const Node&) const; public: @@ -95,33 +95,30 @@ public: /* ----- Map and fold functions ----- */ - using typename MappableContainer::MapFunctor; - using typename FoldableContainer::FoldFunctor; + 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 - void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member - void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member - void MapInOrder(const MapFunctor, void*) override; // Override InOrderMappableContainer member - void MapBreadth(const MapFunctor, void*) override; // Override BreadthMappableContainer 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 - void FoldInOrder(const FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member - void FoldBreadth(const FoldFunctor, const void*, void*) const override; // 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 map and fold functions ----- */ - void MapPreOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree - void MapPostOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree - void MapInOrder(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree - void MapBreadth(const MapFunctor, void*, Node*) override; // Accessory function executing from one node of the tree + 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 - void FoldPreOrder(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree - void FoldPostOrder(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree - void FoldInOrder(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree - void FoldBreadth(const FoldFunctor, const void*, void*, const Node*) const override; // Accessory function executing from one node of the tree + 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 }; /* ************************************************************************** */ @@ -133,7 +130,7 @@ private: protected: struct BinaryTree::Node* curr; - StackLst::Node*> stack(); // default constructor for stacklst + StackLst::Node*> stack; public: @@ -171,9 +168,9 @@ public: // Specific member functions (inherited from Iterator) - struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) + Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() noexcept; // (should not throw exceptions) + bool Terminated() const noexcept; // (should not throw exceptions) /* ************************************************************************ */ @@ -192,7 +189,7 @@ private: protected: struct BinaryTree::Node* curr; - StackLst::Node*> stack; + StackLst::Node*> stack; struct BinaryTree::Node* DeepestLeftLeaf(struct BinaryTree::Node*); public: @@ -230,9 +227,9 @@ public: // Specific member functions (inherited from Iterator) - struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) + Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() noexcept; // (should not throw exceptions) + bool Terminated() const noexcept; // (should not throw exceptions) /* ************************************************************************ */ @@ -251,8 +248,8 @@ private: protected: struct BinaryTree::Node* curr; - StackLst::Node*> stack; - struct BinaryTree::Node* MostLeftNode(struct BinaryTree::Node*); + StackLst::Node*> stack; + struct BinaryTree::Node* MostLeftNode(struct BinaryTree::Node&); public: // Specific constructors @@ -289,9 +286,9 @@ public: // Specific member functions (inherited from Iterator) - struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) + Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() noexcept; // (should not throw exceptions) + bool Terminated() const noexcept; // (should not throw exceptions) /* ************************************************************************ */ @@ -311,7 +308,7 @@ private: protected: struct BinaryTree::Node* curr; - QueueVec::Node*> queue; + QueueVec::Node*> queue; public: @@ -349,9 +346,9 @@ public: // Specific member functions (inherited from Iterator) - struct BinaryTree::Node operator*() const; // (throw std::out_of_range when terminated) + Data& operator*() const; // (throw std::out_of_range when terminated) - bool Terminated() noexcept; // (should not throw exceptions) + bool Terminated() const noexcept; // (should not throw exceptions) /* ************************************************************************ */ diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp index 43ac635..a84a4e0 100755 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp @@ -3,7 +3,7 @@ namespace lasd { /* ************************************************************************** */ template -struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::operator=(const NodeLnk& node){ +struct BinaryTreeLnk::NodeLnk& BinaryTreeLnk::NodeLnk::operator=(const BinaryTreeLnk::NodeLnk& node){ data = node.data; left = node.left; right = node.right; @@ -34,12 +34,12 @@ bool BinaryTreeLnk::NodeLnk::HasRightChild() const noexcept{ } template -Node& BinaryTreeLnk::NodeLnk::LeftChild() const{ +struct BinaryTree::Node& BinaryTreeLnk::NodeLnk::LeftChild() const{ return *left; } template -Node& BinaryTreeLnk::NodeLnk::RightChild() const{ +struct BinaryTree::Node& BinaryTreeLnk::NodeLnk::RightChild() const{ return *right; } @@ -50,7 +50,8 @@ BinaryTreeLnk::BinaryTreeLnk(const LinearContainer& lc){ size = lc.Size(); } -struct BinaryTreeLnk::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer& lc, ulong position){ +template +struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CreateTreeFromLinearContainerInBreadth(const LinearContainer& lc, ulong position){ if(position >= lc.Size()) return nullptr; struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(lc[position]); @@ -64,7 +65,7 @@ struct BinaryTreeLnk::NodeLnk* CreateTreeFromLinearContainerInBreadth(cons } template -struct BinaryTreeLnk::NodeLnk* CreateNode(const Data& data){ +struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CreateNode(const Data& data){ struct BinaryTreeLnk::NodeLnk* newNode = new struct BinaryTreeLnk::NodeLnk(); newNode->data = data; newNode->left = nullptr; @@ -79,18 +80,19 @@ BinaryTreeLnk::BinaryTreeLnk(const BinaryTreeLnk& tree){ root = CopyTree(&tree.Root()); } -BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CopyTree(struct BinaryTreeLnk::NodeLnk* nodeToCopy){ +template +struct BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CopyTree(struct BinaryTreeLnk::Node* nodeToCopy){ if(nodeToCopy==nullptr) return nullptr; - struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(nodeToCopy.Element()); + struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(nodeToCopy->Element()); - if(nodeToCopy.HasLeftChild()) - tmp->left = copyTree(&(nodeToCopy.LeftChild())); + if(nodeToCopy->HasLeftChild()) + tmp->left = CopyTree(&(nodeToCopy->LeftChild())); else tmp->left = nullptr; - if(nodeToCopy.HasRightChild()) - tmp->right = copyTree(&(nodeToCopy.RightChild())); + if(nodeToCopy->HasRightChild()) + tmp->right = CopyTree(&(nodeToCopy->RightChild())); else tmp->right = nullptr; @@ -144,12 +146,12 @@ bool BinaryTreeLnk::operator!=(const BinaryTreeLnk& tree) const noex } template -struct NodeLnk& Root() override{ +struct BinaryTree::Node& BinaryTreeLnk::Root() const{ return *root; } template -void Clear() override{ +void BinaryTreeLnk::Clear(){ DeleteTree(root); } diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp index aaf8350..f37aa8a 100755 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp @@ -13,16 +13,14 @@ namespace lasd { /* ************************************************************************** */ template -class BinaryTreeLnk : virtual protected BinaryTree{ // Must extend BinaryTree +class BinaryTreeLnk : virtual public BinaryTree{ // Must extend BinaryTree private: protected: using BinaryTree::size; - struct NodeLnk* root = nullptr; - // Node struct NodeLnk : virtual protected BinaryTree::Node { // Must extend Node private: @@ -34,19 +32,22 @@ protected: struct NodeLnk* right; public: - Node& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible. - Node& operator=(NodeLnk&&) noexcept override; // Move assignment of abstract types should not be possible. + 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) - Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent) - Node& RightChild() const override; // (concrete function must throw std::out_of_range when not existent) + struct BinaryTree::Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent) + struct BinaryTree::Node& RightChild() const override; // (concrete function must throw std::out_of_range when not existent) - struct BinaryTreeLnk::NodeLnk* CreateNode(const Data& data); + friend class BinaryTreeLnk; }; +protected: + struct BinaryTreeLnk::NodeLnk* root = nullptr; + struct BinaryTreeLnk::NodeLnk* CreateNode(const Data& data); public: // Default constructor @@ -88,7 +89,7 @@ public: // Specific member functions (inherited from BinaryTree) - struct NodeLnk& Root() override; // Override BinaryTree member (throw std::length_error when empty) + struct BinaryTree::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty) /* ************************************************************************ */ @@ -97,8 +98,8 @@ public: void Clear() override; // Override Container member struct BinaryTreeLnk::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer&,ulong); - struct BinaryTreeLnk::NodeLnk* CopyTree(struct BinaryTreeLnk::NodeLnk*); - void DeleteTree(BinaryTreeLnk::NodeLnk* node) + struct BinaryTreeLnk::NodeLnk* CopyTree(struct BinaryTreeLnk::Node*); + void DeleteTree(BinaryTreeLnk::NodeLnk* node); }; /* ************************************************************************** */ diff --git a/librerie/exercise3/binarytree/vec/binarytreevec.cpp b/librerie/exercise3/binarytree/vec/binarytreevec.cpp index ee328d7..53a6ff2 100755 --- a/librerie/exercise3/binarytree/vec/binarytreevec.cpp +++ b/librerie/exercise3/binarytree/vec/binarytreevec.cpp @@ -2,9 +2,15 @@ namespace lasd { /* ************************************************************************** */ +template +BinaryTreeVec::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec* ref){ + data = dat; + index = idx; + ReferenceToTree = ref; +} template -struct BinaryTree:Node& BinaryTreeVec::NodeVec::operator=(const BinaryTreeVec::NodeVec& node){ +struct BinaryTreeVec::NodeVec& BinaryTreeVec::NodeVec::operator=(const BinaryTreeVec::NodeVec& node){ ReferenceToTree = node.ReferenceToTree; data = node.data; index = node.index; @@ -12,7 +18,7 @@ struct BinaryTree:Node& BinaryTreeVec::NodeVec::operator=(const Bina } template -struct BinaryTree:Node& BinaryTreeVec::NodeVec::operator=(BinaryTreeVec::NodeVec&& node) noexcept{ +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); @@ -63,10 +69,7 @@ BinaryTreeVec::BinaryTreeVec(const LinearContainer& lc){ tree.Resize(lc.Size()); size = lc.Size(); for(ulong i=0 ; i::NodeVec; - tmp->data = lc[i]; - tmp->index = i; - tmp->ReferenceToTree = this; + struct BinaryTreeVec::NodeVec* tmp = new BinaryTreeVec::NodeVec(lc[i], i, this); tree[i] = tmp; } } @@ -76,10 +79,7 @@ BinaryTreeVec::BinaryTreeVec(const BinaryTreeVec& bt){ size = bt.size; tree.Resize(size); for(ulong i=0 ; i::NodeVec; - tmp->data = bt[i]; - tmp->index = i; - tmp->ReferenceToTree = this; + struct BinaryTreeVec::NodeVec* tmp = new BinaryTreeVec::NodeVec( (bt.tree[i])->data , i, this); tree[i] = tmp; } } @@ -88,6 +88,9 @@ template BinaryTreeVec::BinaryTreeVec(BinaryTreeVec&& bt) noexcept{ std::swap(size,bt.size); std::swap(tree,bt.tree); + for(ulong i=0 ; iReferenceToTree = this; + } } template @@ -96,23 +99,23 @@ BinaryTreeVec::~BinaryTreeVec(){ } template -bool BinaryTreeVec::operator=(const BinaryTreeVec& bt){ +BinaryTreeVec& BinaryTreeVec::operator=(const BinaryTreeVec& bt){ size = bt.size; tree.Resize(size); for(ulong i=0 ; i::NodeVec; - tmp->data = bt[i]; - tmp->index = i; - tmp->ReferenceToTree = this; + struct NodeVec* tmp = new BinaryTreeVec::NodeVec((bt.tree[i])->data,i,this); tree[i] = tmp; } return *this; } template -bool BinaryTreeVec::operator=(BinaryTreeVec&& bt) noexcept{ +BinaryTreeVec& BinaryTreeVec::operator=(BinaryTreeVec&& bt) noexcept{ std::swap(size, bt.size); std::swap(tree, bt.tree); + for(ulong i=0 ; iReferenceToTree = this; + } return *this; } @@ -120,7 +123,7 @@ template bool BinaryTreeVec::operator==(const BinaryTreeVec& bt) const noexcept{ if(size==bt.size){ for(ulong i=0 ; idata != ((bt.tree)[i])->data ) return false; + if( tree[i]->data != (bt.tree[i])->data ) return false; } return true; } @@ -133,7 +136,7 @@ bool BinaryTreeVec::operator!=(const BinaryTreeVec& bt) const noexcept{ } template -struct BinaryTreeVec::Node& BinaryTreeVec::Root(){ +struct BinaryTree::Node& BinaryTreeVec::Root() const{ return *(tree.Front()); } @@ -141,22 +144,22 @@ template void BinaryTreeVec::Clear(){ for(ulong i=0 ; i -void BinaryTreeVec::MapBreadth(const MapFunctor func, void* par) override{ +void BinaryTreeVec::MapBreadth(const MapFunctor func, void* par){ for(ulong i=0 ; idata, par); } } template -void BinaryTreeVec::FoldBreadth(const MapFunctor func, const void* par, void* acc) override{ +void BinaryTreeVec::FoldBreadth(const FoldFunctor func, const void* par, void* acc) const{ for(ulong i=0 ; idata, par, acc); } } diff --git a/librerie/exercise3/binarytree/vec/binarytreevec.hpp b/librerie/exercise3/binarytree/vec/binarytreevec.hpp index 29a3764..ec4b0fd 100755 --- a/librerie/exercise3/binarytree/vec/binarytreevec.hpp +++ b/librerie/exercise3/binarytree/vec/binarytreevec.hpp @@ -22,7 +22,6 @@ protected: using BinaryTree::size; // using BinaryTree::Node; - Vector tree; struct NodeVec : virtual protected BinaryTree::Node { // Must extend Node @@ -34,17 +33,24 @@ protected: BinaryTreeVec* ReferenceToTree = nullptr; public: - struct BinaryTree::Node& operator=(const NodeVec&) override; // Copy assignment of abstract types should not be possible. - struct BinaryTree::Node& operator=(NodeVec&&) noexcept override; // Move assignment of abstract types should not be possible. + 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 BinaryTree::Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent) struct BinaryTree::Node& RightChild() const override; // (concrete function must throw std::out_of_range when not existent) + + friend class BinaryTreeVec; }; +protected: + Vector::NodeVec*> tree; + public: + // Default constructor BinaryTreeVec() = default; @@ -84,7 +90,7 @@ public: // Specific member functions (inherited from BinaryTree) - struct BinaryTreeVec::NodeVec& Root() override; // Override BinaryTree member (throw std::length_error when empty) + struct BinaryTree::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty) /* ************************************************************************ */ diff --git a/librerie/exercise3/container/container.hpp b/librerie/exercise3/container/container.hpp index 633b438..cdeed0e 100755 --- a/librerie/exercise3/container/container.hpp +++ b/librerie/exercise3/container/container.hpp @@ -203,10 +203,7 @@ public: /* ************************************************************************ */ // Specific member functions - - using typename MappableContainer::MapFunctor; - - virtual void MapInOrder(const MapFunctor, void*) = 0; + virtual void MapInOrder(const typename MappableContainer::MapFunctor, void*) = 0; }; @@ -230,7 +227,7 @@ public: InOrderFoldableContainer& operator=(const InOrderFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - InOrderFoldableContainer operator=(InOrderFoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + InOrderFoldableContainer& operator=(InOrderFoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ @@ -242,9 +239,7 @@ public: // Specific member functions - using typename MappableContainer::MapFunctor; - - virtual void FoldInOrder(const MapFunctor, const void*, void*) const = 0; + virtual void FoldInOrder(const typename FoldableContainer::FoldFunctor, const void*, void*) const = 0; }; @@ -280,9 +275,7 @@ public: // Specific member functions - using typename MappableContainer::MapFunctor; - - void MapBreadth(const MapFunctor, void*) = 0; + virtual void MapBreadth(const typename MappableContainer::MapFunctor, void*) = 0; }; diff --git a/librerie/exercise3/iterator/iterator.hpp b/librerie/exercise3/iterator/iterator.hpp index 6b682e4..8c0724e 100755 --- a/librerie/exercise3/iterator/iterator.hpp +++ b/librerie/exercise3/iterator/iterator.hpp @@ -38,9 +38,9 @@ public: // Specific member functions - virtual Data& operator*() = 0; // (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) - virtual bool Terminated() noexcept = 0; // (concrete function should not throw exceptions) + virtual bool Terminated() const noexcept = 0; // (concrete function should not throw exceptions) }; @@ -76,7 +76,7 @@ public: // Specific member functions - virtual ForwardIterator& operator++() = 0; // (concrete function must throw std::out_of_range when terminated) + virtual void operator++() = 0; // (concrete function must throw std::out_of_range when terminated) };