Library 4

added previous libreries
This commit is contained in:
Alessandro Ferro 2021-05-11 06:52:44 +02:00
parent 25dadb7de6
commit 97407c2a31
23 changed files with 2366 additions and 962 deletions

602
librerie/exercise4/binarytree/binarytree.cpp Normal file → Executable file
View File

@ -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<iostream>
namespace lasd {
/* ************************************************************************** */
/* ----- begin of class BinaryTree ----- */
// ...
/* ----- begin of struct Node ----- */
template <typename Data>
bool BinaryTree<Data>::Node::operator==(const Node& toEvaluate) const noexcept{
return EqualNodes(*this, toEvaluate);
}
template <typename Data>
bool BinaryTree<Data>::Node::operator!=(const Node& toEvaluate) const noexcept{
return !(*this == toEvaluate);
}
/* given two nodes, checks if the subtree is the same */
template <typename Data>
bool BinaryTree<Data>::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 <typename Data>
Data& BinaryTree<Data>::Node::Element(){
return this->data;
}
template <typename Data>
const Data& BinaryTree<Data>::Node::Element() const{
return this->data;
}
/* ----- end of struct Node ----- */
template <typename Data>
bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{
if(size!=toCompare.size) return false;
return(Root() == toCompare.Root());
}
template <typename Data>
bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{
return !(*this == toCompare);
}
/* ----- Map and fold functions ----- */
template <typename Data>
void BinaryTree<Data>::MapPreOrder(const typename MappableContainer<Data>::MapFunctor function, void* par){
if(size == 0) return;
MapPreOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapPostOrder(const typename MappableContainer<Data>::MapFunctor function, void* par){
if(size == 0) return;
MapPostOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapInOrder(const typename MappableContainer<Data>::MapFunctor function, void* par){
if(size == 0) return;
MapInOrder(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::MapBreadth(const typename MappableContainer<Data>::MapFunctor function, void* par){
if(size == 0) return;
MapBreadth(function, par, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc) const{
if(size == 0) return;
FoldPreOrder(function, par, acc, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc) const{
if(size == 0) return;
FoldPostOrder(function, par, acc, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc) const{
if(size == 0) return;
FoldInOrder(function, par, acc, &Root());
}
template <typename Data>
void BinaryTree<Data>::FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc) const{
if(size == 0) return;
FoldBreadth(function, par, acc, &Root());
}
/* ----- Auxiliary map and fold functions ----- */
template <typename Data>
void BinaryTree<Data>::MapPreOrder(const typename MappableContainer<Data>::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 <typename Data>
void BinaryTree<Data>::MapPostOrder(const typename MappableContainer<Data>::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 <typename Data>
void BinaryTree<Data>::MapInOrder(const typename MappableContainer<Data>::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 <typename Data>
void BinaryTree<Data>::MapBreadth(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
QueueLst<struct Node*> 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 <typename Data>
void BinaryTree<Data>::FoldPreOrder(const typename FoldableContainer<Data>::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 <typename Data>
void BinaryTree<Data>::FoldPostOrder(const typename FoldableContainer<Data>::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 <typename Data>
void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::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 <typename Data>
void BinaryTree<Data>::FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, Node* node) const{
QueueLst<BinaryTree<Data>::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 <typename Data>
BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = &tree.Root();
else
curr = nullptr;
}
template <typename Data>
BTPreOrderIterator<Data>::BTPreOrderIterator(const BTPreOrderIterator& itr){
curr = itr.curr;
stack = itr.stack;
}
template <typename Data>
BTPreOrderIterator<Data>::BTPreOrderIterator(BTPreOrderIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(stack, itr.stack);
}
template <typename Data>
BTPreOrderIterator<Data>::~BTPreOrderIterator(){
stack.Clear();
curr = nullptr;
}
template <typename Data>
BTPreOrderIterator<Data>& BTPreOrderIterator<Data>::operator=(const BTPreOrderIterator& itr){
curr = itr.curr;
stack = itr.stack;
return *this;
}
template <typename Data>
BTPreOrderIterator<Data>& BTPreOrderIterator<Data>::operator=(BTPreOrderIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(stack, itr.stack);
return *this;
}
template <typename Data>
bool BTPreOrderIterator<Data>::operator==(const BTPreOrderIterator& itr) const noexcept{
return ( curr==itr.curr && stack==itr.stack );
}
template <typename Data>
bool BTPreOrderIterator<Data>::operator!=(const BTPreOrderIterator& itr) const noexcept{
return !(*this == itr);
}
template <typename Data>
Data& BTPreOrderIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element();
}
template <typename Data>
bool BTPreOrderIterator<Data>::Terminated() const noexcept{
return (curr==nullptr);
}
template <typename Data>
void BTPreOrderIterator<Data>::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 <typename Data>
struct BinaryTree<Data>::Node* BTPostOrderIterator<Data>::DeepestLeftLeaf(struct BinaryTree<Data>::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 <typename Data>
BTPostOrderIterator<Data>::BTPostOrderIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = DeepestLeftLeaf(&tree.Root());
else
curr = nullptr;
}
template <typename Data>
BTPostOrderIterator<Data>::BTPostOrderIterator(const BTPostOrderIterator& itr){
curr = itr.curr;
stack = itr.stack;
}
template <typename Data>
BTPostOrderIterator<Data>::BTPostOrderIterator(BTPostOrderIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(stack, itr.stack);
}
template <typename Data>
BTPostOrderIterator<Data>::~BTPostOrderIterator(){
curr = nullptr;
stack.Clear();
}
template <typename Data>
BTPostOrderIterator<Data>& BTPostOrderIterator<Data>::operator=(const BTPostOrderIterator& itr){
curr = itr.curr;
stack = itr.stack;
return *this;
}
template <typename Data>
BTPostOrderIterator<Data>& BTPostOrderIterator<Data>::operator=(BTPostOrderIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(stack, itr.stack);
return *this;
}
template <typename Data>
bool BTPostOrderIterator<Data>::operator==(const BTPostOrderIterator& itr) const noexcept{
return (curr == itr.curr && stack == itr.stack );
}
template <typename Data>
bool BTPostOrderIterator<Data>::operator!=(const BTPostOrderIterator& itr) const noexcept{
return !(*this == itr);
}
template <typename Data>
Data& BTPostOrderIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element();
}
template <typename Data>
bool BTPostOrderIterator<Data>::Terminated() const noexcept{
return (curr == nullptr);
}
template <typename Data>
void BTPostOrderIterator<Data>::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 <typename Data>
struct BinaryTree<Data>::Node* BTInOrderIterator<Data>::MostLeftNode(struct BinaryTree<Data>::Node& root){
if(root.HasLeftChild()){
stack.Push(&root);
return MostLeftNode(root.LeftChild());
}else{
return &root;
}
}
template <typename Data>
BTInOrderIterator<Data>::BTInOrderIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = MostLeftNode(tree.Root());
else
curr = nullptr;
}
template <typename Data>
BTInOrderIterator<Data>::BTInOrderIterator(const BTInOrderIterator& itr){
curr = itr.curr;
stack = itr.stack;
}
template <typename Data>
BTInOrderIterator<Data>::BTInOrderIterator(BTInOrderIterator&& toMove) noexcept{
std::move(curr, toMove.curr);
std::move(stack, toMove.stack);
}
template <typename Data>
BTInOrderIterator<Data>::~BTInOrderIterator(){
stack.Clear();
curr = nullptr;
}
template <typename Data>
BTInOrderIterator<Data>& BTInOrderIterator<Data>::operator=(const BTInOrderIterator& itr){
curr = itr.curr;
stack = itr.stack;
return *this;
}
template <typename Data>
BTInOrderIterator<Data>& BTInOrderIterator<Data>::operator=(BTInOrderIterator&& toMove) noexcept{
std::move(curr, toMove.curr);
std::move(stack, toMove.stack);
return *this;
}
template <typename Data>
bool BTInOrderIterator<Data>::operator==(const BTInOrderIterator& itr) const noexcept{
return (curr == itr.curr && stack == itr.stack );
}
template <typename Data>
bool BTInOrderIterator<Data>::operator!=(const BTInOrderIterator& itr) const noexcept{
return !(*this == itr);
}
template <typename Data>
Data& BTInOrderIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element();
}
template <typename Data>
bool BTInOrderIterator<Data>::Terminated() const noexcept{
return (curr == nullptr);
}
template <typename Data>
void BTInOrderIterator<Data>::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 <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){
if(tree.Size() > 0)
curr = &(tree.Root());
else
curr = nullptr;
}
template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(const BTBreadthIterator& itr){
curr = itr.curr;
queue = itr.queue;
}
template <typename Data>
BTBreadthIterator<Data>::BTBreadthIterator(BTBreadthIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(queue, itr.queue);
}
template <typename Data>
BTBreadthIterator<Data>::~BTBreadthIterator(){
curr = nullptr;
queue.Clear();
}
template <typename Data>
BTBreadthIterator<Data>& BTBreadthIterator<Data>::operator=(const BTBreadthIterator& itr){
curr = itr.curr;
queue = itr.queue;
return *this;
}
template <typename Data>
BTBreadthIterator<Data>& BTBreadthIterator<Data>::operator=(BTBreadthIterator&& itr) noexcept{
std::swap(curr, itr.curr);
std::swap(queue, itr.queue);
return *this;
}
template <typename Data>
bool BTBreadthIterator<Data>::operator==(const BTBreadthIterator& itr) const noexcept{
return ( curr==itr.curr && queue==itr.queue );
}
template <typename Data>
bool BTBreadthIterator<Data>::operator!=(const BTBreadthIterator& itr) const noexcept{
return !(*this == itr);
}
template <typename Data>
Data& BTBreadthIterator<Data>::operator*() const{
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
return curr->Element();
}
template <typename Data>
bool BTBreadthIterator<Data>::Terminated() const noexcept{
return curr == nullptr;
}
template <typename Data>
void BTBreadthIterator<Data>::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;
}
}
/* ************************************************************************** */

424
librerie/exercise4/binarytree/binarytree.hpp Normal file → Executable file
View File

@ -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 <typename Data>
class BinaryTree { // Must extend InOrder/BreadthMappableContainer<Data> and InOrder/BreadthFoldableContainer<Data>
private:
// ...
protected:
// using InOrder/BreadthMappableContainer<Data>::???;
// ...
class BinaryTree : virtual public InOrderMappableContainer<Data>,
virtual public BreadthMappableContainer<Data>,
virtual public InOrderFoldableContainer<Data>,
virtual public BreadthFoldableContainer<Data>{ // Must extend InOrder/BreadthMappableContainer<Data> and InOrder/BreadthFoldableContainer<Data>
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<Data>;
/* ********************************************************************** */
friend class BinaryTree<Data>;
// 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<Data>::MapFunctor, void*) override; // Override MappableContainer member
virtual void MapPostOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override MappableContainer member
virtual void MapInOrder(const typename MappableContainer<Data>::MapFunctor, void*) override; // Override InOrderMappableContainer member
virtual void MapBreadth(const typename MappableContainer<Data>::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<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::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<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
virtual void FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
virtual void FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member
virtual void FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member
protected:
// Auxiliary member functions (for MappableContainer)
using BreadthMappableContainer<Data>::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<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
void MapPostOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
void MapInOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
void MapBreadth(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
// Auxiliary member functions (for FoldableContainer)
void FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree
void FoldPostOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree
void FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, const Node*) const; // Accessory function executing from one node of the tree
void FoldBreadth(const typename FoldableContainer<Data>::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 <typename Data>
class BTPreOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
// Auxiliary member functions (for InOrderMappableContainer)
protected:
// type MapInOrder(arguments) specifiers; // Accessory function executing from one node of the tree
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::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<Data>&); // 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 <typename Data>
class BTPreOrderIterator { // Must extend ForwardIterator<Data>
private:
// ...
class BTPostOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
protected:
// ...
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
struct BinaryTree<Data>::Node* DeepestLeftLeaf(struct BinaryTree<Data>::Node*);
public:
// Specific constructors
// BTPreOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTPostOrderIterator(const BinaryTree<Data>&); // 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 <typename Data>
class BTPostOrderIterator { // Must extend ForwardIterator<Data>
private:
// ...
class BTInOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
protected:
// ...
struct BinaryTree<Data>::Node* curr = nullptr;
StackLst<struct BinaryTree<Data>::Node*> stack;
struct BinaryTree<Data>::Node* MostLeftNode(struct BinaryTree<Data>::Node&);
public:
// Specific constructors
// BTPostOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTInOrderIterator(const BinaryTree<Data>&); // 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 <typename Data>
class BTInOrderIterator { // Must extend ForwardIterator<Data>
private:
// ...
class BTBreadthIterator : virtual public ForwardIterator<Data>{ // Must extend ForwardIterator<Data>
protected:
// ...
struct BinaryTree<Data>::Node* curr = nullptr;
QueueVec<struct BinaryTree<Data>::Node*> queue;
public:
// Specific constructors
// BTInOrderIterator(argument) specifiers; // An iterator over a given binary tree
BTBreadthIterator(const BinaryTree<Data>&); // 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 <typename Data>
class BTBreadthIterator { // Must extend ForwardIterator<Data>
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"

166
librerie/exercise4/binarytree/lnk/binarytreelnk.cpp Normal file → Executable file
View File

@ -1,10 +1,170 @@
namespace lasd {
/* ************************************************************************** */
/* ----- begin of struct NodeLnk ----- */
// ...
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(const BinaryTreeLnk<Data>::NodeLnk& node){
data = node.data;
left = node.left;
right = node.right;
return *this;
}
/* ************************************************************************** */
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(NodeLnk&& node) noexcept{
std::swap(data, node.data);
std::swap(left, node.left);
std::swap(right, node.right);
return *this;
}
template <typename Data>
bool BinaryTreeLnk<Data>::NodeLnk::IsLeaf() const noexcept{
return (left==nullptr && right==nullptr);
}
template <typename Data>
bool BinaryTreeLnk<Data>::NodeLnk::HasLeftChild() const noexcept{
return (left!=nullptr);
}
template <typename Data>
bool BinaryTreeLnk<Data>::NodeLnk::HasRightChild() const noexcept{
return (right!=nullptr);
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::LeftChild() const{
return *left;
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::RightChild() const{
return *right;
}
/* ----- end of struct NodeLnk ----- */
/* ----- begin of class BinaryTreeLnk ----- */
// creates a tree from a linear container in breadth
template <typename Data>
BinaryTreeLnk<Data>::BinaryTreeLnk(const LinearContainer<Data>& lc){
root = CreateTreeFromLinearContainerInBreadth(lc,0);
size = lc.Size();
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>& lc, ulong position){
if(position >= lc.Size()) return nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(lc[position]);
tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1);
tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2);
return tmp;
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CreateNode(const Data& data){
struct BinaryTreeLnk<Data>::NodeLnk* newNode = new struct BinaryTreeLnk<Data>::NodeLnk();
newNode->data = data;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}
template <typename Data>
BinaryTreeLnk<Data>::BinaryTreeLnk(const BinaryTreeLnk<Data>& tree){
if(tree.root == nullptr) return;
size = tree.size;
root = CopyTree(&tree.Root());
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CopyTree(struct BinaryTreeLnk<Data>::Node* nodeToCopy){
if(nodeToCopy==nullptr) return nullptr;
struct BinaryTreeLnk<Data>::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 <typename Data>
BinaryTreeLnk<Data>::BinaryTreeLnk(BinaryTreeLnk<Data>&& tree) noexcept{
std::swap(size, tree.size);
std::swap(root, tree.root);
}
template <typename Data>
BinaryTreeLnk<Data>::~BinaryTreeLnk(){
Clear();
}
template <typename Data>
void BinaryTreeLnk<Data>::DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node){
if(node == nullptr) return;
DeleteTree(node->left);
DeleteTree(node->right);
delete node;
}
template <typename Data>
BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(const BinaryTreeLnk<Data>& tree){
Clear();
size = tree.size;
if(tree.root != nullptr)
root = CopyTree(&tree.Root());
return *this;
}
template <typename Data>
BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(BinaryTreeLnk<Data>&& tree) noexcept{
Clear();
std::swap(size, tree.size);
std::swap(root, tree.root);
return *this;
}
template <typename Data>
bool BinaryTreeLnk<Data>::operator==(const BinaryTreeLnk<Data>& tree) const noexcept{
if(size == tree.size){
return (Root() == tree.Root());
}else{
return false;
}
}
template <typename Data>
bool BinaryTreeLnk<Data>::operator!=(const BinaryTreeLnk<Data>& tree) const noexcept{
return !(*this == tree);
}
template <typename Data>
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::Root() const{
if(size==0) throw std::length_error("Empty tree!");
return *root;
}
template <typename Data>
void BinaryTreeLnk<Data>::Clear(){
DeleteTree(root);
root = nullptr;
size = 0;
}
}

97
librerie/exercise4/binarytree/lnk/binarytreelnk.hpp Normal file → Executable file
View File

@ -1,99 +1,74 @@
#ifndef BINARYTREELNK_HPP
#define BINARYTREELNK_HPP
/* ************************************************************************** */
#include "../binarytree.hpp"
/* ************************************************************************** */
namespace lasd {
/* ************************************************************************** */
template <typename Data>
class BinaryTreeLnk { // Must extend BinaryTree<Data>
private:
// ...
class BinaryTreeLnk : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
protected:
// using BinaryTree<Data>::???;
// ...
struct NodeLnk { // Must extend Node
private:
// ...
struct NodeLnk : virtual public BinaryTree<Data>::Node { // Must extend Node
protected:
// ...
using BinaryTree<Data>::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<Data>;
};
protected:
using BinaryTree<Data>::size;
struct BinaryTreeLnk<Data>::NodeLnk* root = nullptr;
struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data);
public:
// Default constructor
// BinaryTreeLnk() specifiers;
BinaryTreeLnk() = default;
BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
BinaryTreeLnk(const BinaryTreeLnk<Data>&);
BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept;
/* ************************************************************************ */
virtual ~BinaryTreeLnk();
// Specific constructors
// BinaryTreeLnk(argument) specifiers; // A binary tree obtained from a LinearContainer
BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&);
BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) 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<Data>&) const noexcept;
bool operator!=(const BinaryTreeLnk<Data>&) 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<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>&,ulong);
struct BinaryTreeLnk<Data>::NodeLnk* CopyTree(struct BinaryTreeLnk<Data>::Node*);
void DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node);
};
/* ************************************************************************** */
}
#include "binarytreelnk.cpp"

170
librerie/exercise4/binarytree/vec/binarytreevec.cpp Normal file → Executable file
View File

@ -1,9 +1,175 @@
namespace lasd {
/* ************************************************************************** */
/* ----- begin of struct NodeVec ----- */
// ...
template <typename Data>
BinaryTreeVec<Data>::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec<Data>* ref){
data = dat;
index = idx;
ReferenceToTree = ref;
}
template <typename Data>
struct BinaryTreeVec<Data>::NodeVec& BinaryTreeVec<Data>::NodeVec::operator=(const BinaryTreeVec<Data>::NodeVec& node){
ReferenceToTree = node.ReferenceToTree;
data = node.data;
index = node.index;
return *this;
}
template <typename Data>
struct BinaryTreeVec<Data>::NodeVec& 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 BinaryTreeVec<Data>::NodeVec& 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 BinaryTreeVec<Data>::NodeVec& 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!");
}
/* ----- end of struct NodeVec ----- */
/* ----- begin of class BinaryTreeVec ----- */
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 BinaryTreeVec<Data>::NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec(lc[i], i, 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 BinaryTreeVec<Data>::NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec( (bt.tree[i])->data , i, this);
tree[i] = tmp;
}
}
template <typename Data>
BinaryTreeVec<Data>::BinaryTreeVec(BinaryTreeVec<Data>&& bt) noexcept{
std::swap(size,bt.size);
std::swap(tree,bt.tree);
for(ulong i=0 ; i<size ; ++i){
tree[i]->ReferenceToTree = this;
}
}
template <typename Data>
BinaryTreeVec<Data>::~BinaryTreeVec(){
Clear();
}
template <typename Data>
BinaryTreeVec<Data>& BinaryTreeVec<Data>::operator=(const BinaryTreeVec<Data>& bt){
Clear();
size = bt.size;
tree.Resize(size);
for(ulong i=0 ; i<size ; ++i){
struct NodeVec* tmp = new BinaryTreeVec<Data>::NodeVec((bt.tree[i])->data,i,this);
tree[i] = tmp;
}
return *this;
}
template <typename Data>
BinaryTreeVec<Data>& BinaryTreeVec<Data>::operator=(BinaryTreeVec<Data>&& bt) noexcept{
Clear();
std::swap(size, bt.size);
std::swap(tree, bt.tree);
for(ulong i=0 ; i<size ; ++i){
tree[i]->ReferenceToTree = this;
}
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>::NodeVec& BinaryTreeVec<Data>::Root() const{
if(size==0) throw std::length_error("Empty tree!");
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){
for(ulong i=0 ; i<size ; ++i){
func( (tree[i])->data, par);
}
}
template <typename Data>
void BinaryTreeVec<Data>::FoldBreadth(const FoldFunctor func, const void* par, void* acc) const{
for(ulong i=0 ; i<size ; ++i){
func( (tree[i])->data, par, acc);
}
}
/* ************************************************************************** */

108
librerie/exercise4/binarytree/vec/binarytreevec.hpp Normal file → Executable file
View File

@ -2,111 +2,73 @@
#ifndef BINARYTREEVEC_HPP
#define BINARYTREEVEC_HPP
/* ************************************************************************** */
#include "../binarytree.hpp"
#include "../../vector/vector.hpp"
/* ************************************************************************** */
namespace lasd {
/* ************************************************************************** */
template <typename Data>
class BinaryTreeVec { // Must extend BinaryTree<Data>
private:
// ...
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
protected:
// using BinaryTree<Data>::???;
// ...
struct NodeVec { // Must extend Node
private:
// ...
struct NodeVec : virtual public BinaryTree<Data>::Node { // Must extend Node
protected:
// ...
using BinaryTree<Data>::Node::data;
ulong index;
BinaryTreeVec<Data>* ReferenceToTree = nullptr;
public:
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.
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<Data>;
};
protected:
using BinaryTree<Data>::size;
Vector<struct BinaryTreeVec<Data>::NodeVec*> tree;
public:
// Default constructor
// BinaryTreeVec() specifiers;
BinaryTreeVec() = default;
BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
BinaryTreeVec(const BinaryTreeVec<Data>&);
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
/* ************************************************************************ */
virtual ~BinaryTreeVec();
// Specific constructors
// BinaryTreeVec(argument) specifiers; // A binary tree obtained from a LinearContainer
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) 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<Data>::MapFunctor;
using typename BinaryTree<Data>::FoldFunctor;
void MapBreadth(const MapFunctor, void*) override;
void FoldBreadth(const FoldFunctor, const void*, void*) const override;
};
/* ************************************************************************** */
}
#include "binarytreevec.cpp"

16
librerie/exercise4/container/container.cpp Normal file → Executable file
View File

@ -1,10 +1,18 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
void AuxFoldExists(const Data& dat, const void* val, void* exists){
if(dat == *((Data*)val)){
*((bool*)exists) = true;
}
}
// ...
/* ************************************************************************** */
template <typename Data>
bool FoldableContainer<Data>::Exists(const Data& dat) const noexcept{
bool exists = false;
FoldPreOrder(&AuxFoldExists<Data>, &dat, &exists);
return exists;
}
}

402
librerie/exercise4/container/container.hpp Normal file → Executable file
View File

@ -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 <typename Data>
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 <typename Data>
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 <typename Data>
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<void(Data&, void*)> MapFunctor;
virtual void MapPreOrder(const MapFunctor, void*) = 0;
virtual void MapPostOrder(const MapFunctor, void*) = 0;
};
template <typename Data>
class FoldableContainer : virtual public TestableContainer<Data>{ // 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<void(const Data&, const void*, void*) noexcept> 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 <typename Data>
class LinearContainer { // Must extend Container
class InOrderMappableContainer : virtual public MappableContainer<Data> { // 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<Data>::MapFunctor, void*) = 0;
};
/* ************************************************************************** */
template <typename Data>
class TestableContainer { // Must extend Container
class InOrderFoldableContainer : public virtual FoldableContainer<Data> { // 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<Data>::FoldFunctor, const void*, void*) const = 0;
};
/* ************************************************************************** */
template <typename Data>
class MappableContainer { // Must extend Container
class BreadthMappableContainer : virtual public MappableContainer<Data> { // 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<void(Data&, void*)> MapFunctor;
// type MapPreOrder(arguments) specifiers;
// type MapPostOrder(arguments) specifiers;
virtual void MapBreadth(const typename MappableContainer<Data>::MapFunctor, void*) = 0;
};
/* ************************************************************************** */
template <typename Data>
class FoldableContainer { // Must extend TestableContainer
class BreadthFoldableContainer : virtual public FoldableContainer<Data> { // 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<void(const Data&, const void*, void*) noexcept> FoldFunctor;
using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers;
// type FoldPostOrder(arguments) specifiers;
// type Exists(argument) specifiers; // Override TestableContainer member
};
/* ************************************************************************** */
template <typename Data>
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<Data>::MapFunctor;
// type MapInOrder(arguments) specifiers;
};
/* ************************************************************************** */
template <typename Data>
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<Data>::MapFunctor;
// type FoldInOrder(arguments) specifiers;
};
/* ************************************************************************** */
template <typename Data>
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<Data>::MapFunctor;
// type MapBreadth(arguments) specifiers;
};
/* ************************************************************************** */
template <typename Data>
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<Data>::FoldFunctor;
// type FoldBreadth(arguments) specifiers;
virtual void FoldBreadth(const FoldFunctor, const void*, void*) const = 0;
};

36
librerie/exercise4/iterator/iterator.hpp Normal file → Executable file
View File

@ -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 <typename Data>
class ForwardIterator { // Must extend Iterator
class ForwardIterator : virtual public Iterator<Data> { // 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)
};

263
librerie/exercise4/list/list.cpp Normal file → Executable file
View File

@ -3,8 +3,263 @@ namespace lasd {
/* ************************************************************************** */
// ...
/* ************************************************************************** */
// Specific constructors
template <typename Data>
List<Data>::Node::Node(const Data& newValue){
value = newValue;
next = nullptr;
}
// Copy constructor
template <typename Data>
List<Data>::Node::Node(const Node& copyFrom){
value = copyFrom.value;
next = nullptr;
}
// Move constructor
template <typename Data>
List<Data>::Node::Node(Node&& moveFrom){
std::swap(value, moveFrom.value);
std::swap(next, moveFrom.next);
}
template <typename Data>
List<Data>::Node::Node(Data&& moveFrom){
std::swap(value, moveFrom);
}
template <typename Data>
bool List<Data>::Node::operator==(const Node& node)const noexcept{
return (node.value == value);
}
template <typename Data>
bool List<Data>::Node::operator!=(const Node& node)const noexcept{
return !(*this == node);
}
template <typename Data>
List<Data>::List(const LinearContainer<Data>& con){
for(ulong i = 0; i<con.Size(); ++i){
InsertAtBack(con[i]);
}
}
template <typename Data>
List<Data>::List(const List<Data>& copyFrom){
for(ulong i = 0; i<copyFrom.Size(); ++i){
InsertAtBack(copyFrom[i]);
}
}
template <typename Data>
List<Data>::List(List<Data>&& moveFrom){
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
std::swap(tail, moveFrom.tail);
}
template <typename Data>
List<Data>::~List(){
Clear();
}
template <typename Data>
List<Data>& List<Data>::operator=(const List<Data>& copyFrom){
if(*this != copyFrom){
Clear();
for(ulong i = 0 ; i<copyFrom.Size() ; ++i){
InsertAtBack(copyFrom[i]);
}
}
return *this;
}
template <typename Data>
List<Data>& List<Data>::operator=(List<Data>&& moveFrom)noexcept{
if(*this != moveFrom){
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
std::swap(tail, moveFrom.tail);
}
return *this;
}
template <typename Data>
bool List<Data>::operator==(const List<Data>& 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 <typename Data>
bool List<Data>::operator!=(const List<Data>& list) const noexcept{
return !(*this==list);
}
template <typename Data>
void List<Data>::InsertAtFront(const Data& data){
struct Node* tmp = new Node(data);
tmp->next = head;
head = tmp;
size++;
if(size == 1){
tail = head;
}
}
template <typename Data>
void List<Data>::InsertAtFront(Data&& data){
struct Node* tmp = new Node(std::move(data));
tmp->next = head;
head = tmp;
size++;
if(size == 1){
tail = head;
}
}
template <typename Data>
void List<Data>::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 <typename Data>
Data List<Data>::FrontNRemove(){
if(head == nullptr){
throw std::length_error("List is empty!");
}
else{
Data value = head->value;
RemoveFromFront();
return value;
}
}
template <typename Data>
void List<Data>::InsertAtBack(const Data& data){
if(size == 0){
InsertAtFront(data);
}
else{
struct Node* last = new Node(data);
tail->next = last;
tail = last;
size++;
}
}
template <typename Data>
void List<Data>::InsertAtBack(Data&& data){
if(size == 0){
InsertAtFront(data);
}
else{
struct Node* last = new Node(std::move(data));
tail->next = last;
tail = last;
size++;
}
}
template <typename Data>
void List<Data>::Clear(){
while(head != nullptr){
RemoveFromFront();
}
}
template <typename Data>
Data& List<Data>::Front() const{
if(size == 0){
throw std::length_error("List is empty!");
}else{
return head->value;
}
}
template <typename Data>
Data& List<Data>::Back() const{
if(size == 0){
throw std::length_error("List is empty!");
}else{
return tail->value;
}
}
template <typename Data>
Data& List<Data>::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; i<index; ++i){
tmp = tmp->next;
}
return tmp->value;
}
}
template <typename Data>
void List<Data>::MapPreOrder(MapFunctor function, void* par){
MapPreOrder(function, par, head);
}
template <typename Data>
void List<Data>::MapPostOrder(MapFunctor function, void* par){
MapPostOrder(function, par, head);
}
template <typename Data>
void List<Data>::FoldPreOrder(FoldFunctor function, const void* constPar, void* par) const{
FoldPreOrder(function, constPar, par, head);
}
template <typename Data>
void List<Data>::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{
FoldPostOrder(function, constPar, par, head);
}
template <typename Data>
void List<Data>::MapPreOrder(MapFunctor function, void* par, struct Node* node){
if(node == nullptr) return;
function(node->value, par);
MapPreOrder(function, par, node->next);
}
template <typename Data>
void List<Data>::MapPostOrder(MapFunctor function, void* par, struct Node* node){
if(node == nullptr) return;
MapPostOrder(function, par, node->next);
function(node->value, par);
}
template <typename Data>
void List<Data>::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 <typename Data>
void List<Data>::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);
}
}

88
librerie/exercise4/list/list.hpp Normal file → Executable file
View File

@ -13,150 +13,150 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class List { // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
class List : virtual public LinearContainer<Data>,
virtual public MappableContainer<Data>,
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
private:
// ...
protected:
// using LinearContainer<Data>::???;
using LinearContainer<Data>:: 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<Data>&); // 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<Data>::MapFunctor;
using typename MappableContainer<Data>::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<Data>::FoldFunctor;
using typename FoldableContainer<Data>::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
};

69
librerie/exercise4/queue/lst/queuelst.cpp Normal file → Executable file
View File

@ -1,10 +1,73 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear)
: List<Data>(linear) {}
// ...
template <typename Data>
QueueLst<Data>::QueueLst(const QueueLst& copyFrom)
: List<Data>(copyFrom) {}
/* ************************************************************************** */
template <typename Data>
QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept
: List<Data>(std::move(moveFrom)) {}
template <typename Data>
QueueLst<Data>::~QueueLst(){
Clear();
}
template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(const QueueLst& toCopy){
List<Data>::operator=(toCopy);
return *this;
}
template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(QueueLst&& toMove) noexcept{
List<Data>::operator=(std::move(toMove));
return *this;
}
template <typename Data>
bool QueueLst<Data>::operator==(const QueueLst& queuelist) const noexcept{
return List<Data>::operator==(queuelist);
}
template <typename Data>
bool QueueLst<Data>::operator!=(const QueueLst& queuelist) const noexcept{
return List<Data>::operator!=(queuelist);
}
template <typename Data>
void QueueLst<Data>::Enqueue(const Data& data){
List<Data>::InsertAtBack(data);
}
template <typename Data>
void QueueLst<Data>::Enqueue(Data&& data){
List<Data>::InsertAtBack(std::move(data));
}
template <typename Data>
Data& QueueLst<Data>::Head() const{
return List<Data>::Front();
}
template <typename Data>
void QueueLst<Data>::Dequeue(){
List<Data>::RemoveFromFront();
}
template <typename Data>
Data QueueLst<Data>::HeadNDequeue(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void QueueLst<Data>::Clear(){
List<Data>::Clear();
}
}

42
librerie/exercise4/queue/lst/queuelst.hpp Normal file → Executable file
View File

@ -14,70 +14,70 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class QueueLst { // Must extend Queue<Data> and List<Data>
class QueueLst : virtual public Queue<Data>,
virtual protected List<Data>{ // Must extend Queue<Data> and List<Data>
private:
// ...
protected:
// using List<Data>::???;
// ...
using List<Data>::head;
using List<Data>::tail;
using List<Data>::size;
using typename List<Data>::Node;
public:
// Default constructor
// QueueLst() specifier;
QueueLst() = default;
/* ************************************************************************ */
// Specific constructor
// QueueLst(argument) specifiers; // A queue obtained from a LinearContainer
QueueLst(const LinearContainer<Data>&); // 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
};

26
librerie/exercise4/queue/queue.hpp Normal file → Executable file
View File

@ -13,44 +13,40 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
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)
};

188
librerie/exercise4/queue/vec/queuevec.cpp Normal file → Executable file
View File

@ -1,10 +1,192 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueVec<Data>::QueueVec(){
size = 4;
rear = 0;
front = 0;
Elements = new Data[size];
}
// ...
template <typename Data>
QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){
size = linear.Size()+1; // 1 free cell
Elements = new Data[size]; //forse da espandere
for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i];
}
front = 0;
rear = size-1; // the vector will be full
}
/* ************************************************************************** */
template <typename Data>
QueueVec<Data>::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 <typename Data>
QueueVec<Data>::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 <typename Data>
QueueVec<Data>::~QueueVec(){
//vector destructor will be automatically called I hope
}
template <typename Data>
QueueVec<Data>& QueueVec<Data>::operator=(const QueueVec& toCopy){
QueueVec<Data>* tmpQueue = new QueueVec<Data>(toCopy);
std::swap(*tmpQueue, *this);
delete tmpQueue;
return *this;
}
template <typename Data>
QueueVec<Data>& QueueVec<Data>::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 <typename Data>
bool QueueVec<Data>::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 <typename Data>
bool QueueVec<Data>::operator!=(const QueueVec& toCompare) const noexcept{
return !(*this == toCompare);
}
template <typename Data>
void QueueVec<Data>::Enqueue(const Data& data){
if((rear+1)%size == front){
Expand();
}
Elements[rear] = data;
rear = (rear + 1) % size;
}
template <typename Data>
void QueueVec<Data>::Enqueue(Data&& data){
if((rear+1)%size == front){
Expand();
}
std::swap(Elements[rear],data);
rear = (rear + 1) % size;
}
template <typename Data>
Data& QueueVec<Data>::Head() const{
if(Size()<=0){
throw std::length_error("Queue is empty!");
}
return Elements[front];
}
template <typename Data>
void QueueVec<Data>::Dequeue(){
if(Size() <= 0){
throw std::length_error("Queue is empty!");
}
front = (front + 1) % size;
if(Size() < size/4){
Reduce();
}
}
template <typename Data>
Data QueueVec<Data>::HeadNDequeue(){
Data tmp = Head();
Dequeue();
return tmp;
}
template <typename Data>
bool QueueVec<Data>::Empty() const noexcept{
return (front == rear);
}
template <typename Data>
ulong QueueVec<Data>::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 <typename Data>
void QueueVec<Data>::Clear(){
if(size!=4){
delete[] Elements;
Elements = new Data[4];
size = 4;
}
front = 0;
rear = 0;
}
template <typename Data>
void QueueVec<Data>::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 <typename Data>
void QueueVec<Data>::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;
}
}

53
librerie/exercise4/queue/vec/queuevec.hpp Normal file → Executable file
View File

@ -14,82 +14,83 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class QueueVec { // Must extend Queue<Data> and Vector<Data>
class QueueVec : virtual public Queue<Data>,
virtual protected Vector<Data>{ // Must extend Queue<Data> and Vector<Data>
private:
// ...
protected:
// using Vector<Data>::???;
// ...
using Vector<Data>::Elements;
using Vector<Data>::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<Data>&); // 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;
};

72
librerie/exercise4/stack/lst/stacklst.cpp Normal file → Executable file
View File

@ -3,7 +3,77 @@ namespace lasd {
/* ************************************************************************** */
// ...
// Constructors
template <typename Data>
StackLst<Data>::StackLst(const LinearContainer<Data>& linear)
: List<Data>(linear){}
template <typename Data>
StackLst<Data>::StackLst(const StackLst& stcklist)
: List<Data>(stcklist){}
template <typename Data>
StackLst<Data>::StackLst(StackLst&& stcklist) noexcept
: List<Data>(std::move(stcklist)){}
// Destructor
template <typename Data>
StackLst<Data>::~StackLst(){
Clear();
}
template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(const StackLst& copyFrom){
List<Data>::operator=(copyFrom);
return *this;
}
template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(StackLst&& moveFrom) noexcept{
List<Data>::operator=(std::move(moveFrom));
return *this;
}
template <typename Data>
bool StackLst<Data>::operator==(const StackLst& stcklist) const noexcept{
return List<Data>::operator==(stcklist);
}
template <typename Data>
bool StackLst<Data>::operator!=(const StackLst& stcklist) const noexcept{
return List<Data>::operator!=(stcklist);
}
// Specific member functions (inherited from Stack)
template <typename Data>
void StackLst<Data>::Push(const Data& element){
List<Data>::InsertAtFront(element);
}
template <typename Data>
void StackLst<Data>::Push(Data&& element) noexcept{
List<Data>::InsertAtFront(element);
}
template <typename Data>
Data& StackLst<Data>::Top() const{
return List<Data>::Front();
}
template <typename Data>
void StackLst<Data>::Pop(){
List<Data>::RemoveFromFront();
}
template <typename Data>
Data StackLst<Data>::TopNPop(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void StackLst<Data>::Clear(){
List<Data>::Clear();
}
/* ************************************************************************** */

41
librerie/exercise4/stack/lst/stacklst.hpp Normal file → Executable file
View File

@ -14,70 +14,69 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class StackLst { // Must extend Stack<Data> and List<Data>
class StackLst : virtual public Stack<Data>,
virtual protected List<Data> { // Must extend Stack<Data> and List<Data>
private:
// ...
protected:
// using List<Data>::???;
// ...
using List<Data>::head;
using List<Data>::size;
using typename List<Data>::Node;
public:
// Default constructor
// StackLst() specifier;
StackLst() = default;
/* ************************************************************************ */
// Specific constructor
// StackLst(argument) specifiers; // A stack obtained from a LinearContainer
StackLst(const LinearContainer<Data>&); // 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
};

26
librerie/exercise4/stack/stack.hpp Normal file → Executable file
View File

@ -13,44 +13,40 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
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)
};

143
librerie/exercise4/stack/vec/stackvec.cpp Normal file → Executable file
View File

@ -2,9 +2,142 @@
namespace lasd {
/* ************************************************************************** */
// ...
/* ************************************************************************** */
// constructors
template <typename Data>
StackVec<Data>::StackVec(){
size = 4; // default vector is instantiated with 4 cells
stackSize = 0;
Elements = new Data[size];
}
template <typename Data>
StackVec<Data>::StackVec(const LinearContainer<Data>& linear)
: Vector<Data>(linear){
stackSize = linear.Size(); // the array is full
}
template <typename Data>
StackVec<Data>::StackVec(const StackVec& stckvec)
: Vector<Data>(stckvec){
stackSize = stckvec.Size(); // the array is full
}
template <typename Data>
StackVec<Data>::StackVec(StackVec&& toMove) noexcept
: Vector<Data>(std::move(toMove)){
std::swap(stackSize, toMove.stackSize);
}
template <typename Data>
StackVec<Data>::~StackVec(){
// Vector destructor will be called automatically
}
template <typename Data>
StackVec<Data>& StackVec<Data>::operator=(const StackVec<Data>& copyFrom){
Vector<Data>::operator=(copyFrom); // espandere di un po' forse
stackSize = copyFrom.Size();
return *this;
}
template <typename Data>
StackVec<Data>& StackVec<Data>::operator=(StackVec<Data>&& moveFrom) noexcept{
Vector<Data>::operator=(std::move(moveFrom));
stackSize = moveFrom.Size();
return *this;
}
template <typename Data>
bool StackVec<Data>::operator==(const StackVec<Data>& toCompare) const noexcept{
if(stackSize == toCompare.Size()){
for(ulong i=0 ; i<stackSize ; ++i){
if(Elements[i] != toCompare[i]){
return false;
}
}
return true;
}else{
return false;
}
}
template <typename Data>
bool StackVec<Data>::operator!=(const StackVec<Data>& toCompare) const noexcept{
return !(*this == toCompare);
}
// Specific member functions (inherited from Stack)
template <typename Data>
void StackVec<Data>::Push(const Data& data){
if(size == stackSize){
Expand();
}
Elements[stackSize] = data;
++stackSize;
}
template <typename Data>
void StackVec<Data>::Push(Data&& data){
if(size == stackSize){
Expand();
}
std::swap(Elements[stackSize], data);
++stackSize;
}
template <typename Data>
Data& StackVec<Data>::Top() const{
if(stackSize == 0){
throw std::length_error("Empty Stack!");
}
return Elements[stackSize-1];
}
template <typename Data>
void StackVec<Data>::Pop(){
if(stackSize==0){
throw std::length_error("Empty Stack!");
}
--stackSize;
if(stackSize < (int)(size/4)){
Reduce();
}
}
template <typename Data>
Data StackVec<Data>::TopNPop(){
Data data = Top();
Pop();
return data;
}
template <typename Data>
bool StackVec<Data>::Empty() const noexcept{
return (stackSize == 0);
}
template <typename Data>
ulong StackVec<Data>::Size() const noexcept{
return stackSize;
}
template <typename Data>
void StackVec<Data>::Expand(){
Vector<Data>::Resize(size * 2);
}
template <typename Data>
void StackVec<Data>::Reduce(){
if(size <= 4) return; // we're not going to have vectors with less than 4 cells
Vector<Data>::Resize((ulong)size/2);
}
template <typename Data>
void StackVec<Data>::Clear(){
delete [] Elements;
size = 4;
stackSize = 0;
Elements = new Data[size];
}
}

48
librerie/exercise4/stack/vec/stackvec.hpp Normal file → Executable file
View File

@ -14,81 +14,81 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class StackVec { // Must extend Stack<Data> and Vector<Data>
class StackVec : virtual public Stack<Data>,
virtual protected Vector<Data>{ // Must extend Stack<Data> and Vector<Data>
private:
// ...
protected:
// using Vector<Data>::???;
ulong stackSize = 0; // first empty cell and # of elements in the vector
using Vector<Data>::Elements;
using Vector<Data>::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<Data>&); // 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();
};

162
librerie/exercise4/vector/vector.cpp Normal file → Executable file
View File

@ -1,10 +1,160 @@
namespace lasd {
/* ************************************************************************** */
// ...
/* ************************************************************************** */
template <typename Data>
Vector<Data>::Vector(const ulong newsize){
Elements = new Data[newsize]{};
size = newsize;
}
template <typename Data>
Vector<Data>::Vector(const LinearContainer<Data>& con){
size = con.Size();
Elements = new Data[size]{};
for(ulong i=0; i<size; ++i){
Elements[i] = con[i];
}
}
// Copy constructor
template <typename Data>
Vector<Data>::Vector(const Vector<Data>& vec){
size = vec.size;
Elements = new Data[size]{};
for(ulong i=0; i<size; ++i){
Elements[i] = vec[i];
}
}
// Move constructor
template <typename Data>
Vector<Data>::Vector(Vector<Data>&& vec)noexcept{
std::swap(Elements, vec.Elements);
std::swap(size, vec.size);
}
// Destructor
template <typename Data>
Vector<Data>::~Vector(){
Clear();
}
// Copy assignment
template <typename Data>
Vector<Data>& Vector<Data>::operator=(const Vector<Data>& vec){
Vector<Data>* tmpvec = new Vector<Data>(vec);
std::swap(*tmpvec, *this);
delete tmpvec;
return *this;
}
// Move assignment
template <typename Data>
Vector<Data>& Vector<Data>::operator=(Vector<Data>&& vec)noexcept{
std::swap(Elements,vec.Elements);
std::swap(size, vec.size);
return *this;
}
template <typename Data>
bool Vector<Data>::operator==(const Vector<Data>& vec) const noexcept{
if(size == vec.size){
for(ulong i=0; i<size; ++i){
if(Elements[i] != vec.Elements[i])
return false;
}
return true;
}else{
return false;
}
}
template <typename Data>
bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
return !(*this == vec);
}
template <typename Data>
void Vector<Data>::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<limit; ++i){
std::swap(Elements[i], TmpElements[i]);
}
std::swap(Elements, TmpElements);
size = newsize;
delete[] TmpElements;
}
}
template <typename Data>
void Vector<Data>::Clear(){
delete[] Elements;
Elements = nullptr;
size = 0;
}
template <typename Data>
Data& Vector<Data>::Front() const {
if(size != 0){
return Elements[0];
}
else{
throw std::length_error("Access to an empty vector!");
}
}
template <typename Data>
Data& Vector<Data>::Back() const {
if(size != 0){
return Elements[size - 1];
}
else{
throw std::length_error("Access to an empty vector!");
}
}
template <typename Data>
Data& Vector<Data>::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 <typename Data>
void Vector<Data>::MapPreOrder(const MapFunctor fun, void* par){
for(ulong i=0; i<size; ++i){
fun(Elements[i], par);
}
}
template <typename Data>
void Vector<Data>::MapPostOrder(const MapFunctor fun, void* par){
ulong index = size;
while(index > 0){
fun(Elements[--index], par);
}
}
template <typename Data>
void Vector<Data>::FoldPreOrder(const FoldFunctor fun, const void* par, void* acc) const{
for(ulong i=0; i<size; ++i){
fun(Elements[i], par, acc);
}
}
template <typename Data>
void Vector<Data>::FoldPostOrder(const FoldFunctor fun, const void* par, void* acc) const{
ulong idx = size;
while(idx > 0){
fun(Elements[--idx], par, acc);
}
}
}

86
librerie/exercise4/vector/vector.hpp Normal file → Executable file
View File

@ -13,99 +13,65 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class Vector { // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
class Vector : virtual public LinearContainer<Data>,
virtual public MappableContainer<Data>,
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
private:
// ...
protected:
// using LinearContainer<Data>::???;
// ...
using LinearContainer<Data>::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<Data>&); // 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<Data>::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<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
using typename FoldableContainer<Data>::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"