lasd/librerie/exercise3/binarytree/vec/binarytreevec.hpp

116 lines
3.6 KiB
C++
Raw Normal View History

2021-04-24 16:58:05 +02:00
#ifndef BINARYTREEVEC_HPP
#define BINARYTREEVEC_HPP
/* ************************************************************************** */
#include "../binarytree.hpp"
#include "../../vector/vector.hpp"
/* ************************************************************************** */
namespace lasd {
/* ************************************************************************** */
template <typename Data>
2021-05-01 23:58:12 +02:00
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
2021-04-24 16:58:05 +02:00
private:
protected:
2021-05-01 23:58:12 +02:00
using BinaryTree<Data>::size;
// using BinaryTree<Data>::Node;
2021-04-24 16:58:05 +02:00
2021-05-01 23:58:12 +02:00
struct NodeVec : virtual protected BinaryTree<Data>::Node { // Must extend Node
2021-04-24 16:58:05 +02:00
private:
protected:
2021-05-01 23:58:12 +02:00
using BinaryTree<Data>::Node::data;
ulong index;
BinaryTreeVec<Data>* ReferenceToTree = nullptr;
2021-04-24 16:58:05 +02:00
public:
2021-05-04 16:05:48 +02:00
NodeVec(Data&, ulong, BinaryTreeVec<Data>*);
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.
2021-05-01 23:58:12 +02:00
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)
2021-05-04 16:05:48 +02:00
friend class BinaryTreeVec<Data>;
2021-04-24 16:58:05 +02:00
};
2021-05-04 16:05:48 +02:00
protected:
Vector<struct BinaryTreeVec<Data>::NodeVec*> tree;
2021-04-24 16:58:05 +02:00
public:
2021-05-04 16:05:48 +02:00
2021-04-24 16:58:05 +02:00
// Default constructor
2021-05-01 23:58:12 +02:00
BinaryTreeVec() = default;
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Specific constructors
2021-05-01 23:58:12 +02:00
BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Copy constructor
2021-05-01 23:58:12 +02:00
BinaryTreeVec(const BinaryTreeVec<Data>&);
2021-04-24 16:58:05 +02:00
// Move constructor
2021-05-01 23:58:12 +02:00
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Destructor
2021-05-01 23:58:12 +02:00
virtual ~BinaryTreeVec();
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Copy assignment
2021-05-01 23:58:12 +02:00
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
2021-04-24 16:58:05 +02:00
// Move assignment
2021-05-01 23:58:12 +02:00
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) noexcept;
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Comparison operators
2021-05-01 23:58:12 +02:00
bool operator==(const BinaryTreeVec&) const noexcept;
bool operator!=(const BinaryTreeVec&) const noexcept;
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Specific member functions (inherited from BinaryTree)
2021-05-04 16:05:48 +02:00
struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
2021-04-24 16:58:05 +02:00
/* ************************************************************************ */
// Specific member functions (inherited from Container)
2021-05-01 23:58:12 +02:00
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;
2021-04-24 16:58:05 +02:00
};
/* ************************************************************************** */
}
#include "binarytreevec.cpp"
#endif