Library 3

binarytreevec
This commit is contained in:
Alessandro Ferro
2021-05-01 23:58:12 +02:00
parent 27726938dd
commit 10c5148efc
37 changed files with 200 additions and 43 deletions

157
librerie/exercise3/binarytree/vec/binarytreevec.cpp Normal file → Executable file
View File

@@ -3,7 +3,162 @@ namespace lasd {
/* ************************************************************************** */
// ...
template <typename Data>
struct BinaryTree<Data>:Node& BinaryTreeVec<Data>::NodeVec::operator=(const BinaryTreeVec<Data>::NodeVec& node){
ReferenceToTree = node.ReferenceToTree;
data = node.data;
index = node.index;
return *this;
}
template <typename Data>
struct BinaryTree<Data>:Node& BinaryTreeVec<Data>::NodeVec::operator=(BinaryTreeVec<Data>::NodeVec&& node) noexcept{
std::swap(data, node.data);
std::swap(index, node.index);
std::swap(ReferenceToTree, node.ReferenceToTree);
return *this;
}
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::IsLeaf() const noexcept{
return (!HasLeftChild() && !HasRightChild());
}
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::HasLeftChild() const noexcept{
if(index*2+1 < ReferenceToTree->size){
return true;
}else{
return false;
}
}
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::HasRightChild() const noexcept{
if(index*2+2 < ReferenceToTree->size){
return true;
}else{
return false;
}
}
template <typename Data>
struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::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 <typename Data>
struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::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 <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(const LinearContainer<Data>& lc){
tree.Resize(lc.Size());
size = lc.Size();
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec;
tmp->data = lc[i];
tmp->index = i;
tmp->ReferenceToTree = this;
tree[i] = tmp;
}
}
template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(const BinaryTreeVec<Data>& bt){
size = bt.size;
tree.Resize(size);
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec;
tmp->data = bt[i];
tmp->index = i;
tmp->ReferenceToTree = this;
tree[i] = tmp;
}
}
template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(BinaryTreeVec<Data>&& bt) noexcept{
std::swap(size,bt.size);
std::swap(tree,bt.tree);
}
template <typename Data>
BinaryTreeVec<Data>::~BinaryTreeVec(){
Clear();
}
template <typename Data>
bool BinaryTreeVec<Data>::operator=(const BinaryTreeVec<Data>& bt){
size = bt.size;
tree.Resize(size);
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec;
tmp->data = bt[i];
tmp->index = i;
tmp->ReferenceToTree = this;
tree[i] = tmp;
}
return *this;
}
template <typename Data>
bool BinaryTreeVec<Data>::operator=(BinaryTreeVec<Data>&& bt) noexcept{
std::swap(size, bt.size);
std::swap(tree, bt.tree);
return *this;
}
template <typename Data>
bool BinaryTreeVec<Data>::operator==(const BinaryTreeVec& bt) const noexcept{
if(size==bt.size){
for(ulong i=0 ; i<size ; ++i){
if((tree[i])->data != ((bt.tree)[i])->data ) return false;
}
return true;
}
return false;
}
template <typename Data>
bool BinaryTreeVec<Data>::operator!=(const BinaryTreeVec& bt) const noexcept{
return !(*this == bt);
}
template <typename Data>
struct BinaryTreeVec<Data>::Node& BinaryTreeVec<Data>::Root(){
return *(tree.Front());
}
template <typename Data>
void BinaryTreeVec<Data>::Clear(){
for(ulong i=0 ; i<size ; ++i){
delete tree[i];
tree[i] = nullptr
}
size = 0;
}
template <typename Data>
void BinaryTreeVec<Data>::MapBreadth(const MapFunctor func, void* par) override{
for(ulong i=0 ; i<size ; ++i){
func( *(tree[i]), par);
}
}
template <typename Data>
void BinaryTreeVec<Data>::FoldBreadth(const MapFunctor func, const void* par, void* acc) override{
for(ulong i=0 ; i<size ; ++i){
func( *(tree[i]), par, acc);
}
}
/* ************************************************************************** */

57
librerie/exercise3/binarytree/vec/binarytreevec.hpp Normal file → Executable file
View File

@@ -14,82 +14,89 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class BinaryTreeVec { // Must extend BinaryTree<Data>
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
private:
// ...
protected:
// using BinaryTree<Data>::???;
using BinaryTree<Data>::size;
// using BinaryTree<Data>::Node;
Vector<struct NodeVec*> tree;
// ...
struct NodeVec { // Must extend Node
struct NodeVec : virtual protected BinaryTree<Data>::Node { // Must extend Node
private:
// ...
protected:
// ...
using BinaryTree<Data>::Node::data;
ulong index;
BinaryTreeVec<Data>* ReferenceToTree = nullptr;
public:
// ...
struct BinaryTree<Data>::Node& operator=(const NodeVec&) override; // Copy assignment of abstract types should not be possible.
struct BinaryTree<Data>::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<Data>::Node& LeftChild() const override; // (concrete function must throw std::out_of_range when not existent)
struct BinaryTree<Data>::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<Data>&); // A binary tree obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
// BinaryTreeVec(argument) specifiers;
BinaryTreeVec(const BinaryTreeVec<Data>&);
// Move constructor
// BinaryTreeVec(argument) specifiers;
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
/* ************************************************************************ */
// Destructor
// ~BinaryTreeVec() specifiers;
virtual ~BinaryTreeVec();
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
// Move assignment
// type operator=(argument) specifiers;
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) 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<Data>::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<Data>::MapFunctor;
using typename BinaryTree<Data>::FoldFunctor;
void MapBreadth(const MapFunctor, void*) override;
void FoldBreadth(const FoldFunctor, const void*, void*) const override;
};