From 10c5148efc8abb72e701659af95020dbf9210ed1 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Sat, 1 May 2021 23:58:12 +0200 Subject: [PATCH] Library 3 binarytreevec --- librerie/exercise3/Exercise3.pdf | Bin librerie/exercise3/binarytree/binarytree.cpp | 0 librerie/exercise3/binarytree/binarytree.hpp | 0 .../binarytree/lnk/binarytreelnk.cpp | 15 +- .../binarytree/lnk/binarytreelnk.hpp | 14 +- .../binarytree/vec/binarytreevec.cpp | 157 +++++++++++++++++- .../binarytree/vec/binarytreevec.hpp | 57 ++++--- librerie/exercise3/build.sh | 0 librerie/exercise3/container/container.cpp | 0 librerie/exercise3/container/container.hpp | 0 librerie/exercise3/iterator/iterator.hpp | 0 librerie/exercise3/list/list.cpp | 0 librerie/exercise3/list/list.hpp | 0 librerie/exercise3/main.cpp | 0 librerie/exercise3/vector/vector.cpp | 0 librerie/exercise3/vector/vector.hpp | 0 .../zlasdtest/binarytree/binarytree.hpp | 0 .../zlasdtest/container/container.cpp | 0 .../zlasdtest/container/container.hpp | 0 .../zlasdtest/exercise1/fulltest.cpp | 0 .../zlasdtest/exercise1/simpletest.cpp | 0 .../exercise3/zlasdtest/exercise1/test.hpp | 0 .../zlasdtest/exercise2/fulltest.cpp | 0 .../zlasdtest/exercise2/simpletest.cpp | 0 .../exercise3/zlasdtest/exercise2/test.hpp | 0 .../zlasdtest/exercise3/fulltest.cpp | 0 .../zlasdtest/exercise3/simpletest.cpp | 0 .../exercise3/zlasdtest/exercise3/test.hpp | 0 .../exercise3/zlasdtest/iterator/iterator.hpp | 0 librerie/exercise3/zlasdtest/list/list.hpp | 0 librerie/exercise3/zlasdtest/queue/queue.hpp | 0 librerie/exercise3/zlasdtest/stack/stack.hpp | 0 librerie/exercise3/zlasdtest/test.cpp | 0 librerie/exercise3/zlasdtest/test.hpp | 0 .../exercise3/zlasdtest/vector/vector.hpp | 0 librerie/exercise3/zmytest/test.cpp | 0 librerie/exercise3/zmytest/test.hpp | 0 37 files changed, 200 insertions(+), 43 deletions(-) mode change 100644 => 100755 librerie/exercise3/Exercise3.pdf mode change 100644 => 100755 librerie/exercise3/binarytree/binarytree.cpp mode change 100644 => 100755 librerie/exercise3/binarytree/binarytree.hpp mode change 100644 => 100755 librerie/exercise3/binarytree/lnk/binarytreelnk.cpp mode change 100644 => 100755 librerie/exercise3/binarytree/lnk/binarytreelnk.hpp mode change 100644 => 100755 librerie/exercise3/binarytree/vec/binarytreevec.cpp mode change 100644 => 100755 librerie/exercise3/binarytree/vec/binarytreevec.hpp mode change 100644 => 100755 librerie/exercise3/build.sh mode change 100644 => 100755 librerie/exercise3/container/container.cpp mode change 100644 => 100755 librerie/exercise3/container/container.hpp mode change 100644 => 100755 librerie/exercise3/iterator/iterator.hpp mode change 100644 => 100755 librerie/exercise3/list/list.cpp mode change 100644 => 100755 librerie/exercise3/list/list.hpp mode change 100644 => 100755 librerie/exercise3/main.cpp mode change 100644 => 100755 librerie/exercise3/vector/vector.cpp mode change 100644 => 100755 librerie/exercise3/vector/vector.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/binarytree/binarytree.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/container/container.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/container/container.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise1/fulltest.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise1/simpletest.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise1/test.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise2/fulltest.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise2/simpletest.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise2/test.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise3/fulltest.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise3/simpletest.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/exercise3/test.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/iterator/iterator.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/list/list.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/queue/queue.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/stack/stack.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/test.cpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/test.hpp mode change 100644 => 100755 librerie/exercise3/zlasdtest/vector/vector.hpp mode change 100644 => 100755 librerie/exercise3/zmytest/test.cpp mode change 100644 => 100755 librerie/exercise3/zmytest/test.hpp diff --git a/librerie/exercise3/Exercise3.pdf b/librerie/exercise3/Exercise3.pdf old mode 100644 new mode 100755 diff --git a/librerie/exercise3/binarytree/binarytree.cpp b/librerie/exercise3/binarytree/binarytree.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/binarytree/binarytree.hpp b/librerie/exercise3/binarytree/binarytree.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp old mode 100644 new mode 100755 index 1321605..43ac635 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.cpp @@ -34,20 +34,15 @@ bool BinaryTreeLnk::NodeLnk::HasRightChild() const noexcept{ } template -NodeLnk& BinaryTreeLnk::NodeLnk::LeftChild() const{ +Node& BinaryTreeLnk::NodeLnk::LeftChild() const{ return *left; } template -NodeLnk& BinaryTreeLnk::NodeLnk::RightChild() const{ +Node& BinaryTreeLnk::NodeLnk::RightChild() const{ return *right; } -template -BinaryTreeLnk::BinaryTreeLnk(){ - size = 0; -} - // creates a tree from a linear container in breadth template BinaryTreeLnk::BinaryTreeLnk(const LinearContainer& lc){ @@ -85,15 +80,17 @@ BinaryTreeLnk::BinaryTreeLnk(const BinaryTreeLnk& tree){ } BinaryTreeLnk::NodeLnk* BinaryTreeLnk::CopyTree(struct BinaryTreeLnk::NodeLnk* nodeToCopy){ + if(nodeToCopy==nullptr) return nullptr; + struct BinaryTreeLnk::NodeLnk* tmp = CreateNode(nodeToCopy.Element()); if(nodeToCopy.HasLeftChild()) - tmp->left = copyTrees(&(nodeToCopy.LeftChild())); + tmp->left = copyTree(&(nodeToCopy.LeftChild())); else tmp->left = nullptr; if(nodeToCopy.HasRightChild()) - tmp->right = copyTrees(&(nodeToCopy.RightChild())); + tmp->right = copyTree(&(nodeToCopy.RightChild())); else tmp->right = nullptr; diff --git a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp old mode 100644 new mode 100755 index 3f15ed8..aaf8350 --- a/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp +++ b/librerie/exercise3/binarytree/lnk/binarytreelnk.hpp @@ -31,13 +31,11 @@ protected: using BinaryTree::Node::data; struct NodeLnk* left; - struct* NodeLnk* right; + struct NodeLnk* right; public: - NodeLnk& operator=(const NodeLnk&); // Copy assignment of abstract types should not be possible. - - // Move assignment - Node& operator=(Node&&) noexcept override; // Move assignment of abstract types should not be possible. + 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. 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) @@ -52,7 +50,7 @@ protected: public: // Default constructor - BinaryTreeLnk(); + BinaryTreeLnk() = default; /* ************************************************************************ */ @@ -75,10 +73,10 @@ public: /* ************************************************************************ */ // Copy assignment - BinaryTreeLnk& operator=(const BinaryTreeLnk&); + BinaryTreeLnk& operator=(const BinaryTreeLnk&); // Move assignment - BinaryTreeLnk operator=(BinaryTreeLnk&&) noexcept; + BinaryTreeLnk& operator=(BinaryTreeLnk&&) noexcept; /* ************************************************************************ */ diff --git a/librerie/exercise3/binarytree/vec/binarytreevec.cpp b/librerie/exercise3/binarytree/vec/binarytreevec.cpp old mode 100644 new mode 100755 index d374ceb..ee328d7 --- a/librerie/exercise3/binarytree/vec/binarytreevec.cpp +++ b/librerie/exercise3/binarytree/vec/binarytreevec.cpp @@ -3,7 +3,162 @@ namespace lasd { /* ************************************************************************** */ -// ... +template +struct BinaryTree:Node& BinaryTreeVec::NodeVec::operator=(const BinaryTreeVec::NodeVec& node){ + ReferenceToTree = node.ReferenceToTree; + data = node.data; + index = node.index; + return *this; +} + +template +struct BinaryTree:Node& 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 BinaryTree::Node& 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 BinaryTree::Node& 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!"); +} + +template +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; + tree[i] = tmp; + } +} + +template +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; + tree[i] = tmp; + } +} + +template +BinaryTreeVec::BinaryTreeVec(BinaryTreeVec&& bt) noexcept{ + std::swap(size,bt.size); + std::swap(tree,bt.tree); +} + +template +BinaryTreeVec::~BinaryTreeVec(){ + Clear(); +} + +template +bool 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; + tree[i] = tmp; + } + return *this; +} + +template +bool BinaryTreeVec::operator=(BinaryTreeVec&& bt) noexcept{ + std::swap(size, bt.size); + std::swap(tree, bt.tree); + 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::Node& BinaryTreeVec::Root(){ + return *(tree.Front()); +} + +template +void BinaryTreeVec::Clear(){ + for(ulong i=0 ; i +void BinaryTreeVec::MapBreadth(const MapFunctor func, void* par) override{ + for(ulong i=0 ; i +void BinaryTreeVec::FoldBreadth(const MapFunctor func, const void* par, void* acc) override{ + for(ulong i=0 ; i -class BinaryTreeVec { // Must extend BinaryTree +class BinaryTreeVec : virtual public BinaryTree{ // Must extend BinaryTree private: - // ... - protected: - // using BinaryTree::???; + using BinaryTree::size; + // using BinaryTree::Node; + Vector tree; - // ... - - struct NodeVec { // Must extend Node + struct NodeVec : virtual protected BinaryTree::Node { // Must extend Node private: - // ... - protected: - - // ... + using BinaryTree::Node::data; + ulong index; + 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. + 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) }; public: // Default constructor - // BinaryTreeVec() specifiers; + BinaryTreeVec() = default; /* ************************************************************************ */ // Specific constructors - // BinaryTreeVec(argument) specifiers; // A binary tree obtained from a LinearContainer + BinaryTreeVec(const LinearContainer&); // A binary tree obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // BinaryTreeVec(argument) specifiers; + BinaryTreeVec(const BinaryTreeVec&); // Move constructor - // BinaryTreeVec(argument) specifiers; + BinaryTreeVec(BinaryTreeVec&&) noexcept; /* ************************************************************************ */ // Destructor - // ~BinaryTreeVec() specifiers; + virtual ~BinaryTreeVec(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument) specifiers; + BinaryTreeVec& operator=(const BinaryTreeVec&); // Move assignment - // type operator=(argument) specifiers; + BinaryTreeVec& operator=(BinaryTreeVec&&) noexcept; /* ************************************************************************ */ // 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) + struct BinaryTreeVec::NodeVec& Root() 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 ----- */ + using typename BinaryTree::MapFunctor; + using typename BinaryTree::FoldFunctor; + void MapBreadth(const MapFunctor, void*) override; + void FoldBreadth(const FoldFunctor, const void*, void*) const override; }; diff --git a/librerie/exercise3/build.sh b/librerie/exercise3/build.sh old mode 100644 new mode 100755 diff --git a/librerie/exercise3/container/container.cpp b/librerie/exercise3/container/container.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/container/container.hpp b/librerie/exercise3/container/container.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/iterator/iterator.hpp b/librerie/exercise3/iterator/iterator.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/list/list.cpp b/librerie/exercise3/list/list.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/list/list.hpp b/librerie/exercise3/list/list.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/main.cpp b/librerie/exercise3/main.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/vector/vector.cpp b/librerie/exercise3/vector/vector.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/vector/vector.hpp b/librerie/exercise3/vector/vector.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/binarytree/binarytree.hpp b/librerie/exercise3/zlasdtest/binarytree/binarytree.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/container/container.cpp b/librerie/exercise3/zlasdtest/container/container.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/container/container.hpp b/librerie/exercise3/zlasdtest/container/container.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise1/fulltest.cpp b/librerie/exercise3/zlasdtest/exercise1/fulltest.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise1/simpletest.cpp b/librerie/exercise3/zlasdtest/exercise1/simpletest.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise1/test.hpp b/librerie/exercise3/zlasdtest/exercise1/test.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise2/fulltest.cpp b/librerie/exercise3/zlasdtest/exercise2/fulltest.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise2/simpletest.cpp b/librerie/exercise3/zlasdtest/exercise2/simpletest.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise2/test.hpp b/librerie/exercise3/zlasdtest/exercise2/test.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise3/fulltest.cpp b/librerie/exercise3/zlasdtest/exercise3/fulltest.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise3/simpletest.cpp b/librerie/exercise3/zlasdtest/exercise3/simpletest.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/exercise3/test.hpp b/librerie/exercise3/zlasdtest/exercise3/test.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/iterator/iterator.hpp b/librerie/exercise3/zlasdtest/iterator/iterator.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/list/list.hpp b/librerie/exercise3/zlasdtest/list/list.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/queue/queue.hpp b/librerie/exercise3/zlasdtest/queue/queue.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/stack/stack.hpp b/librerie/exercise3/zlasdtest/stack/stack.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/test.cpp b/librerie/exercise3/zlasdtest/test.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/test.hpp b/librerie/exercise3/zlasdtest/test.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zlasdtest/vector/vector.hpp b/librerie/exercise3/zlasdtest/vector/vector.hpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zmytest/test.cpp b/librerie/exercise3/zmytest/test.cpp old mode 100644 new mode 100755 diff --git a/librerie/exercise3/zmytest/test.hpp b/librerie/exercise3/zmytest/test.hpp old mode 100644 new mode 100755