mirror of
https://github.com/xfarrow/lasd.git
synced 2025-02-19 13:40:37 +01:00
Library 5
- Added template - Matrix & MatrixVec completed
This commit is contained in:
parent
b7c12b8c8a
commit
b3ca7292c1
BIN
librerie/exercise5/Exercise5.pdf
Normal file
BIN
librerie/exercise5/Exercise5.pdf
Normal file
Binary file not shown.
612
librerie/exercise5/binarytree/binarytree.cpp
Executable file
612
librerie/exercise5/binarytree/binarytree.cpp
Executable file
@ -0,0 +1,612 @@
|
||||
|
||||
#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& tree) const noexcept{
|
||||
if(size == tree.size){
|
||||
if(size == 0) return true;
|
||||
else return (Root() == tree.Root());
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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((stack.Top())->HasLeftChild() && 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;
|
||||
}
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
250
librerie/exercise5/binarytree/binarytree.hpp
Executable file
250
librerie/exercise5/binarytree/binarytree.hpp
Executable file
@ -0,0 +1,250 @@
|
||||
|
||||
#ifndef BINARYTREE_HPP
|
||||
#define BINARYTREE_HPP
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../iterator/iterator.hpp"
|
||||
|
||||
#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 : 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:
|
||||
|
||||
struct Node {
|
||||
|
||||
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>;
|
||||
|
||||
// Destructor
|
||||
virtual ~Node() = default;
|
||||
|
||||
// Copy assignment
|
||||
Node& operator=(const Node&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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;
|
||||
|
||||
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.
|
||||
|
||||
virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty)
|
||||
|
||||
/* ----- 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
|
||||
|
||||
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:
|
||||
|
||||
using BreadthMappableContainer<Data>::size;
|
||||
|
||||
/* ----- 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
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
template <typename Data>
|
||||
class BTPreOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
|
||||
|
||||
protected:
|
||||
|
||||
struct BinaryTree<Data>::Node* curr = nullptr;
|
||||
StackLst<struct BinaryTree<Data>::Node*> stack;
|
||||
|
||||
public:
|
||||
|
||||
virtual ~BTPreOrderIterator();
|
||||
|
||||
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;
|
||||
|
||||
bool operator==(const BTPreOrderIterator&) const noexcept;
|
||||
bool operator!=(const BTPreOrderIterator&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
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:
|
||||
|
||||
BTPostOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
BTPostOrderIterator(const BTPostOrderIterator&);
|
||||
BTPostOrderIterator(BTPostOrderIterator&&) noexcept;
|
||||
|
||||
virtual ~BTPostOrderIterator();
|
||||
|
||||
|
||||
BTPostOrderIterator& operator=(const BTPostOrderIterator&);
|
||||
BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept;
|
||||
|
||||
bool operator==(const BTPostOrderIterator&) const noexcept;
|
||||
bool operator!=(const BTPostOrderIterator&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
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:
|
||||
|
||||
BTInOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
BTInOrderIterator(const BTInOrderIterator&);
|
||||
BTInOrderIterator(BTInOrderIterator&&) noexcept;
|
||||
|
||||
virtual ~BTInOrderIterator();
|
||||
|
||||
BTInOrderIterator& operator=(const BTInOrderIterator&);
|
||||
BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept;
|
||||
|
||||
bool operator==(const BTInOrderIterator&) const noexcept;
|
||||
bool operator!=(const BTInOrderIterator&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
template <typename Data>
|
||||
class BTBreadthIterator : virtual public ForwardIterator<Data>{ // Must extend ForwardIterator<Data>
|
||||
|
||||
protected:
|
||||
|
||||
struct BinaryTree<Data>::Node* curr = nullptr;
|
||||
QueueVec<struct BinaryTree<Data>::Node*> queue;
|
||||
|
||||
public:
|
||||
|
||||
BTBreadthIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
BTBreadthIterator(const BTBreadthIterator&);
|
||||
BTBreadthIterator(BTBreadthIterator&&) noexcept;
|
||||
|
||||
virtual ~BTBreadthIterator();
|
||||
|
||||
BTBreadthIterator& operator=(const BTBreadthIterator&);
|
||||
BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept;
|
||||
|
||||
bool operator==(const BTBreadthIterator&) const noexcept;
|
||||
bool operator!=(const BTBreadthIterator&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#include "binarytree.cpp"
|
||||
|
||||
#endif
|
172
librerie/exercise5/binarytree/lnk/binarytreelnk.cpp
Executable file
172
librerie/exercise5/binarytree/lnk/binarytreelnk.cpp
Executable file
@ -0,0 +1,172 @@
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
** operator== and operator!= can be removed from BinaryTreeLnk since they are
|
||||
** inherited from BinaryTree. They're here just for clarity and because they're
|
||||
** in the template.
|
||||
** Maybe you can make them more optimized here.
|
||||
*/
|
||||
template <typename Data>
|
||||
bool BinaryTreeLnk<Data>::operator==(const BinaryTreeLnk<Data>& tree) const noexcept{
|
||||
return (BinaryTree<Data>::operator==(tree));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
76
librerie/exercise5/binarytree/lnk/binarytreelnk.hpp
Executable file
76
librerie/exercise5/binarytree/lnk/binarytreelnk.hpp
Executable file
@ -0,0 +1,76 @@
|
||||
#ifndef BINARYTREELNK_HPP
|
||||
#define BINARYTREELNK_HPP
|
||||
|
||||
#include "../binarytree.hpp"
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
class BinaryTreeLnk : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
|
||||
|
||||
protected:
|
||||
|
||||
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() = default;
|
||||
BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
|
||||
BinaryTreeLnk(const BinaryTreeLnk<Data>&);
|
||||
BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept;
|
||||
|
||||
virtual ~BinaryTreeLnk();
|
||||
|
||||
BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&);
|
||||
BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) noexcept;
|
||||
|
||||
bool operator==(const BinaryTreeLnk<Data>&) const noexcept;
|
||||
bool operator!=(const BinaryTreeLnk<Data>&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from BinaryTree)
|
||||
|
||||
NodeLnk& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
|
||||
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"
|
||||
|
||||
#endif
|
176
librerie/exercise5/binarytree/vec/binarytreevec.cpp
Executable file
176
librerie/exercise5/binarytree/vec/binarytreevec.cpp
Executable file
@ -0,0 +1,176 @@
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
76
librerie/exercise5/binarytree/vec/binarytreevec.hpp
Executable file
76
librerie/exercise5/binarytree/vec/binarytreevec.hpp
Executable file
@ -0,0 +1,76 @@
|
||||
|
||||
#ifndef BINARYTREEVEC_HPP
|
||||
#define BINARYTREEVEC_HPP
|
||||
|
||||
#include "../binarytree.hpp"
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
|
||||
|
||||
protected:
|
||||
|
||||
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:
|
||||
|
||||
BinaryTreeVec() = default;
|
||||
BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
|
||||
BinaryTreeVec(const BinaryTreeVec<Data>&);
|
||||
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
|
||||
|
||||
virtual ~BinaryTreeVec();
|
||||
|
||||
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
|
||||
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) noexcept;
|
||||
|
||||
bool operator==(const BinaryTreeVec&) const noexcept;
|
||||
bool operator!=(const BinaryTreeVec&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from BinaryTree)
|
||||
|
||||
NodeVec& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
/* ----- override of map and fold in breadth ----- */
|
||||
|
||||
using typename BinaryTree<Data>::MapFunctor;
|
||||
using typename BinaryTree<Data>::FoldFunctor;
|
||||
void MapBreadth(const MapFunctor, void*) override;
|
||||
void FoldBreadth(const FoldFunctor, const void*, void*) const override;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#include "binarytreevec.cpp"
|
||||
|
||||
#endif
|
390
librerie/exercise5/bst/bst.cpp
Executable file
390
librerie/exercise5/bst/bst.cpp
Executable file
@ -0,0 +1,390 @@
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
BST<Data>::BST(const LinearContainer<Data>& lc){
|
||||
for(ulong i=0 ; i<lc.Size() ; ++i){
|
||||
Insert(lc[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
BST<Data>::BST(const BST<Data>& bst)
|
||||
: BinaryTreeLnk<Data>(bst){}
|
||||
|
||||
template <typename Data>
|
||||
BST<Data>::BST(BST<Data>&& bst) noexcept
|
||||
: BinaryTreeLnk<Data>(std::move(bst)){}
|
||||
|
||||
template <typename Data>
|
||||
BST<Data>::~BST(){
|
||||
BinaryTreeLnk<Data>::Clear();
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
BST<Data>& BST<Data>::operator=(const BST<Data>& bst){
|
||||
BinaryTreeLnk<Data>::operator=(bst);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
BST<Data>& BST<Data>::operator=(BST<Data>&& bst) noexcept{
|
||||
BinaryTreeLnk<Data>::operator=(std::move(bst));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool BST<Data>::operator==(const BST<Data>& bst) const noexcept{
|
||||
if(size != bst.Size()) return false;
|
||||
|
||||
BTInOrderIterator<Data> itr1(*this);
|
||||
BTInOrderIterator<Data> itr2(bst);
|
||||
|
||||
for(; !itr1.Terminated() ; ++itr1, ++itr2){
|
||||
if(*itr1 != *itr2) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool BST<Data>::operator!=(const BST<Data>& bst) const noexcept{
|
||||
return !(*this == bst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::Insert(const Data& data) noexcept{
|
||||
NodeLnk*& pointer = FindPointerTo(root, data);
|
||||
if(pointer == nullptr){
|
||||
pointer = BinaryTreeLnk<Data>::CreateNode(data);
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::Insert(Data&& data) noexcept{
|
||||
NodeLnk*& pointer = FindPointerTo(root, data);
|
||||
if(pointer == nullptr){
|
||||
pointer = new NodeLnk();
|
||||
std::swap(pointer->value, data);
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::Remove(const Data& data) noexcept{
|
||||
delete Detach(FindPointerTo(root,data));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& BST<Data>::Min() const{
|
||||
if(root == nullptr) throw std::length_error("Empty tree!");
|
||||
return FindPointerToMin(root)->Element();
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data BST<Data>::MinNRemove(){
|
||||
if(root == nullptr) throw std::length_error("Empty tree!");
|
||||
return DataNDelete(DetachMin(root));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::RemoveMin(){
|
||||
if(root == nullptr) throw std::length_error("Empty tree!");
|
||||
delete DetachMin(root);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& BST<Data>::Max() const{
|
||||
if(root == nullptr) throw std::length_error("Empty tree!");
|
||||
return FindPointerToMax(root)->Element();
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data BST<Data>::MaxNRemove(){
|
||||
if(root == nullptr) throw std::length_error("Empty tree!");
|
||||
return DataNDelete(DetachMax(root));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::RemoveMax(){
|
||||
if(root == nullptr) throw std::length_error("Empty tree!");
|
||||
delete DetachMax(root);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& BST<Data>::Predecessor(const Data& data) const{
|
||||
NodeLnk* const* ptr = FindPointerToPredecessor(root, data);
|
||||
if(ptr!=nullptr){
|
||||
return (*(*ptr)).Element();
|
||||
}else{
|
||||
throw std::length_error("Predecessor not found!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data BST<Data>::PredecessorNRemove(const Data& data){
|
||||
NodeLnk** ptr = FindPointerToPredecessor(root,data);
|
||||
if(ptr!=nullptr){
|
||||
return DataNDelete(Detach(*ptr));
|
||||
}else{
|
||||
throw std::length_error("Predecessor not found!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::RemovePredecessor(const Data& data){
|
||||
NodeLnk** ptr = FindPointerToPredecessor(root,data);
|
||||
if(ptr!=nullptr){
|
||||
delete Detach(*ptr);
|
||||
}else{
|
||||
throw std::length_error("Predecessor not found!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& BST<Data>::Successor(const Data& data) const{
|
||||
NodeLnk* const* ptr = FindPointerToSuccessor(root, data);
|
||||
if(ptr!=nullptr){
|
||||
return (*(*ptr)).Element();
|
||||
}else{
|
||||
throw std::length_error("Successor not found!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data BST<Data>::SuccessorNRemove(const Data& data){
|
||||
NodeLnk** ptr = FindPointerToSuccessor(root,data);
|
||||
if(ptr!=nullptr){
|
||||
return DataNDelete(Detach(*ptr));
|
||||
}else{
|
||||
throw std::length_error("Successor not found!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BST<Data>::RemoveSuccessor(const Data& data){
|
||||
NodeLnk** ptr = FindPointerToSuccessor(root,data);
|
||||
if(ptr!=nullptr){
|
||||
delete Detach(*ptr);
|
||||
}else{
|
||||
throw std::length_error("Successor not found!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool BST<Data>::Exists(const Data& data) const noexcept{
|
||||
return (FindPointerTo(root, data) != nullptr);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data BST<Data>::DataNDelete(struct BST<Data>::NodeLnk* ptr){
|
||||
Data data = ptr->Element();
|
||||
delete ptr;
|
||||
return data;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* BST<Data>::Detach(struct BST<Data>::NodeLnk*& ptrref) noexcept{
|
||||
if(ptrref == nullptr) return nullptr;
|
||||
|
||||
if(ptrref->left == nullptr){
|
||||
return SkipOnRight(ptrref);
|
||||
}
|
||||
else if(ptrref->right == nullptr){
|
||||
return SkipOnLeft(ptrref);
|
||||
}
|
||||
else{
|
||||
NodeLnk* maxNode = DetachMax(ptrref->left);
|
||||
std::swap(ptrref->Element() , maxNode->Element());
|
||||
return maxNode;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* BST<Data>::DetachMin(struct BST<Data>::NodeLnk*& ptrref) noexcept{
|
||||
return SkipOnRight(FindPointerToMin(ptrref));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* BST<Data>::DetachMax(struct BST<Data>::NodeLnk*& ptrref) noexcept{
|
||||
return SkipOnLeft(FindPointerToMax(ptrref));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* BST<Data>::SkipOnLeft(struct BST<Data>::NodeLnk*& ptrref) noexcept{
|
||||
NodeLnk* left = nullptr;
|
||||
if(ptrref != nullptr){
|
||||
std::swap(left, ptrref->left);
|
||||
std::swap(left, ptrref);
|
||||
--size;
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* BST<Data>::SkipOnRight(struct BST<Data>::NodeLnk*& ptrref) noexcept{
|
||||
NodeLnk* right = nullptr;
|
||||
if(ptrref != nullptr){
|
||||
std::swap(right, ptrref->right);
|
||||
std::swap(right, ptrref);
|
||||
--size;
|
||||
}
|
||||
return right;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMin(struct BST<Data>::NodeLnk* const& node) const noexcept{
|
||||
/*
|
||||
In order to return a [reference to a] const pointer, we need to
|
||||
declare a variable (ptr) which points to a const pointer which points to
|
||||
a NodeLnk.
|
||||
This const pointer that points to a NodeLnk is the parameter of the function.
|
||||
|
||||
Hence, *ptr will be a const pointer.
|
||||
*/
|
||||
NodeLnk* const* ptr = &node;
|
||||
NodeLnk* curr = node;
|
||||
if(curr!=nullptr){
|
||||
while(curr->left != nullptr){
|
||||
ptr = &curr->left;
|
||||
curr = curr->left;
|
||||
}
|
||||
}
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMin(struct BST<Data>::NodeLnk*& node) noexcept{
|
||||
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToMin(node));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMax(struct BST<Data>::NodeLnk* const& node) const noexcept{
|
||||
NodeLnk* const* ptr = &node;
|
||||
NodeLnk* curr = node;
|
||||
if(curr!=nullptr){
|
||||
while(curr->right != nullptr){
|
||||
ptr = &curr->right;
|
||||
curr = curr->right;
|
||||
}
|
||||
}
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerToMax(struct BST<Data>::NodeLnk*& node) noexcept{
|
||||
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerToMax(node));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
|
||||
/*
|
||||
In order to return a [reference to a] const pointer, we need to
|
||||
declare a variable (pointer) which points to a const pointer which points to
|
||||
a NodeLnk.
|
||||
This const pointer that points to a NodeLnk is the parameter of the function.
|
||||
Hence, (*pointer) will be a const pointer.
|
||||
|
||||
Note: this function (and others too) could've been written using one single pointer, in this way:
|
||||
while(*pointer != nullptr && (*(*pointer)).Element() != data){
|
||||
if( (*(*pointer)).Element() < data ) pointer = &((*pointer)->right);
|
||||
else if((*(*pointer)).Element() > data ) pointer = &((*pointer)->left);
|
||||
}
|
||||
but I preferred to use a clearer version.
|
||||
*/
|
||||
|
||||
NodeLnk* const* pointer = &ref; //a pointer to a const pointer to a NodeLnk
|
||||
NodeLnk* current = ref;
|
||||
|
||||
while(current != nullptr && current->Element() != data){
|
||||
if(current->Element() < data){
|
||||
pointer = &(current->right);
|
||||
current = current->right;
|
||||
}else if(current->Element() > data){
|
||||
pointer = &(current->left);
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
return *pointer;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
|
||||
|
||||
/*
|
||||
If the element we are looking the predecessor for is the current element,
|
||||
then its predecessor resides in the max node of its left subtree (if it has
|
||||
a left subtree. Return the candidate otherwise).
|
||||
|
||||
If the element we are looking the predecessor for is greater than the current element,
|
||||
then we have to go down right the tree, saving the current "candidate".
|
||||
|
||||
If the element we are looking the predecessor for is less than the current element,
|
||||
then we have to go down left the tree.
|
||||
*/
|
||||
|
||||
NodeLnk* const* pointer = &ref;
|
||||
NodeLnk* current = ref;
|
||||
NodeLnk* const* candidate = nullptr;
|
||||
|
||||
while(current != nullptr){
|
||||
if(data == current->Element()){
|
||||
if(current->HasLeftChild()){
|
||||
return &(FindPointerToMax(current->left));
|
||||
}else{
|
||||
return candidate;
|
||||
}
|
||||
}else if(current->Element() < data){
|
||||
candidate = pointer;
|
||||
pointer = &(current->right);
|
||||
current = current->right;
|
||||
}else if(current->Element() > data){
|
||||
pointer = &(current->left);
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const* BST<Data>::FindPointerToSuccessor(struct BST<Data>::NodeLnk* const& ref, Data data) const noexcept{
|
||||
NodeLnk* const* pointer = &ref;
|
||||
NodeLnk* current = ref;
|
||||
NodeLnk* const* candidate = nullptr;
|
||||
|
||||
while( current != nullptr){
|
||||
if(data == current->Element()){
|
||||
if(current->HasRightChild()){
|
||||
return &(FindPointerToMin(current->right));
|
||||
}
|
||||
else{
|
||||
return candidate;
|
||||
}
|
||||
}else if(current->Element() > data){
|
||||
candidate = pointer;
|
||||
pointer = ¤t->left;
|
||||
current = current->left;
|
||||
}else if(current->Element() < data){
|
||||
pointer = ¤t->right;
|
||||
current = current->right;
|
||||
}
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk*& BST<Data>::FindPointerTo(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
|
||||
return const_cast<NodeLnk*&>(static_cast<const BST<Data> *>(this)->FindPointerTo(node, data));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk** BST<Data>::FindPointerToPredecessor(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
|
||||
return const_cast<NodeLnk**>(static_cast<const BST<Data> *>(this)->FindPointerToPredecessor(node, data));
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
typename BST<Data>::NodeLnk** BST<Data>::FindPointerToSuccessor(struct BST<Data>::NodeLnk*& node, Data data) noexcept{
|
||||
return const_cast<NodeLnk**>(static_cast<const BST<Data> *>(this)->FindPointerToSuccessor(node, data));
|
||||
}
|
||||
|
||||
}
|
86
librerie/exercise5/bst/bst.hpp
Executable file
86
librerie/exercise5/bst/bst.hpp
Executable file
@ -0,0 +1,86 @@
|
||||
|
||||
#ifndef BST_HPP
|
||||
#define BST_HPP
|
||||
|
||||
#include "../binarytree/lnk/binarytreelnk.hpp"
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
class BST : virtual public BinaryTreeLnk<Data> { // Must extend BinaryTreeLnk<Data>
|
||||
|
||||
protected:
|
||||
|
||||
using BinaryTreeLnk<Data>::size;
|
||||
using BinaryTreeLnk<Data>::root;
|
||||
using typename BinaryTreeLnk<Data>::NodeLnk;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
BST() = default;
|
||||
BST(const LinearContainer<Data>&);
|
||||
BST(const BST<Data>&);
|
||||
BST(BST<Data>&&) noexcept;
|
||||
|
||||
virtual ~BST();
|
||||
|
||||
BST<Data>& operator=(const BST<Data>&);
|
||||
BST<Data>& operator=(BST<Data>&&) noexcept;
|
||||
|
||||
bool operator==(const BST<Data>&) const noexcept;
|
||||
bool operator!=(const BST<Data>&) const noexcept;
|
||||
|
||||
void Insert(const Data&) noexcept; // Copy of the value
|
||||
void Insert(Data&&) noexcept; // Move of the value
|
||||
void Remove(const Data&) noexcept;
|
||||
|
||||
const Data& Min() const; // (concrete function must throw std::length_error when empty)
|
||||
Data MinNRemove(); // (concrete function must throw std::length_error when empty)
|
||||
void RemoveMin(); // (concrete function must throw std::length_error when empty)
|
||||
|
||||
const Data& Max() const; // (concrete function must throw std::length_error when empty)
|
||||
Data MaxNRemove(); // (concrete function must throw std::length_error when empty)
|
||||
void RemoveMax(); // (concrete function must throw std::length_error when empty)
|
||||
|
||||
const Data& Predecessor(const Data&) const; // (concrete function must throw std::length_error when empty)
|
||||
Data PredecessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
|
||||
void RemovePredecessor(const Data&); // (concrete function must throw std::length_error when empty)
|
||||
|
||||
const Data& Successor(const Data&) const; // (concrete function must throw std::length_error when empty)
|
||||
Data SuccessorNRemove(const Data&); // (concrete function must throw std::length_error when empty)
|
||||
void RemoveSuccessor(const Data&); // (concrete function must throw std::length_error when empty)
|
||||
|
||||
bool Exists(const Data&) const noexcept override; // Override TestableContainer member
|
||||
|
||||
protected:
|
||||
|
||||
// Auxiliary member functions
|
||||
|
||||
Data DataNDelete(NodeLnk*);
|
||||
|
||||
NodeLnk* Detach(NodeLnk*&) noexcept;
|
||||
|
||||
NodeLnk* DetachMin(NodeLnk*&) noexcept;
|
||||
NodeLnk* DetachMax(NodeLnk*&) noexcept;
|
||||
|
||||
NodeLnk* SkipOnLeft(NodeLnk*&) noexcept;
|
||||
NodeLnk* SkipOnRight(NodeLnk*&) noexcept;
|
||||
|
||||
NodeLnk* const& FindPointerToMin(NodeLnk* const&) const noexcept;
|
||||
NodeLnk*& FindPointerToMin(NodeLnk*&) noexcept;
|
||||
NodeLnk* const& FindPointerToMax(NodeLnk* const&) const noexcept;
|
||||
NodeLnk*& FindPointerToMax(NodeLnk*&) noexcept;
|
||||
|
||||
NodeLnk* const& FindPointerTo(NodeLnk* const&, Data) const noexcept;
|
||||
NodeLnk*& FindPointerTo(NodeLnk*&, Data) noexcept;
|
||||
|
||||
NodeLnk* const* FindPointerToPredecessor(NodeLnk* const&, Data) const noexcept;
|
||||
NodeLnk** FindPointerToPredecessor(NodeLnk*&, Data) noexcept;
|
||||
NodeLnk* const* FindPointerToSuccessor(NodeLnk* const&, Data) const noexcept;
|
||||
NodeLnk** FindPointerToSuccessor(NodeLnk*&, Data) noexcept;
|
||||
};
|
||||
}
|
||||
|
||||
#include "bst.cpp"
|
||||
#endif
|
11
librerie/exercise5/build.sh
Normal file
11
librerie/exercise5/build.sh
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#! /bin/bash
|
||||
|
||||
g++ -O3 -o main \
|
||||
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
|
||||
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
|
||||
zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \
|
||||
zlasdtest/exercise4/simpletest.cpp zlasdtest/exercise4/fulltest.cpp \
|
||||
zlasdtest/exercise5/simpletest.cpp zlasdtest/exercise5/fulltest.cpp \
|
||||
zlasdtest/container/container.cpp \
|
||||
zlasdtest/test.cpp zmytest/test.cpp main.cpp
|
18
librerie/exercise5/container/container.cpp
Executable file
18
librerie/exercise5/container/container.cpp
Executable file
@ -0,0 +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;
|
||||
}
|
||||
|
||||
}
|
326
librerie/exercise5/container/container.hpp
Executable file
326
librerie/exercise5/container/container.hpp
Executable file
@ -0,0 +1,326 @@
|
||||
|
||||
#ifndef CONTAINER_HPP
|
||||
#define CONTAINER_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
class Container {
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
ulong size = 0;
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~Container() = default;
|
||||
|
||||
// Copy assignment
|
||||
Container& operator=(const Container&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
Container& operator=(Container&&) noexcept = delete;; // Move assignment of abstract types should not be possible.
|
||||
|
||||
// Comparison operators
|
||||
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)
|
||||
|
||||
virtual Data& operator[](const ulong) const = 0; // (concrete function must throw std::out_of_range when out of range)
|
||||
|
||||
};
|
||||
|
||||
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 InOrderMappableContainer : virtual public MappableContainer<Data> { // Must extend MappableContainer
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~InOrderMappableContainer() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
InOrderMappableContainer& operator=(const InOrderMappableContainer&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
InOrderMappableContainer& operator=(InOrderMappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
virtual void MapInOrder(const typename MappableContainer<Data>::MapFunctor, void*) = 0;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class InOrderFoldableContainer : public virtual FoldableContainer<Data> { // Must extend FoldableContainer
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~InOrderFoldableContainer() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
InOrderFoldableContainer& operator=(const InOrderFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
InOrderFoldableContainer& operator=(InOrderFoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
virtual void FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const = 0;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BreadthMappableContainer : virtual public MappableContainer<Data> { // Must extend MappableContainer
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
~BreadthMappableContainer() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BreadthMappableContainer& operator=(const BreadthMappableContainer&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
BreadthMappableContainer& operator=(BreadthMappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
virtual void MapBreadth(const typename MappableContainer<Data>::MapFunctor, void*) = 0;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BreadthFoldableContainer : virtual public FoldableContainer<Data> { // Must extend FoldableContainer
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~BreadthFoldableContainer() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BreadthFoldableContainer& operator=(const BreadthFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
BreadthFoldableContainer& operator=(BreadthFoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
using typename FoldableContainer<Data>::FoldFunctor;
|
||||
|
||||
virtual void FoldBreadth(const FoldFunctor, const void*, void*) const = 0;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "container.cpp"
|
||||
|
||||
#endif
|
171
librerie/exercise5/iterator/iterator.hpp
Executable file
171
librerie/exercise5/iterator/iterator.hpp
Executable file
@ -0,0 +1,171 @@
|
||||
|
||||
#ifndef ITERATOR_HPP
|
||||
#define ITERATOR_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class Iterator {
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~Iterator() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
Iterator& operator=(const Iterator&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
Iterator& operator=(Iterator&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
virtual Data& operator*() const = 0; // (concrete function must throw std::out_of_range when terminated)
|
||||
|
||||
virtual bool Terminated() const noexcept = 0; // (concrete function should not throw exceptions)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class ForwardIterator : virtual public Iterator<Data> { // Must extend Iterator
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~ForwardIterator() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
ForwardIterator& operator=(const ForwardIterator&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
ForwardIterator& operator=(ForwardIterator&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
virtual void operator++() = 0; // (concrete function must throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BackwardIterator { // Must extend Iterator
|
||||
|
||||
private:
|
||||
|
||||
// ...
|
||||
|
||||
protected:
|
||||
|
||||
// ...
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
// ~BackwardIterator() 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
|
||||
|
||||
// type operator--() specifiers; // (concrete function must throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BidirectionalIterator { // Must extend ForwardIterator and BackwardIterator
|
||||
|
||||
private:
|
||||
|
||||
// ...
|
||||
|
||||
protected:
|
||||
|
||||
// ...
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
// ~BidirectionalIterator() 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
|
||||
|
||||
// type Terminated() specifiers; // Override Iterator member
|
||||
|
||||
// type ForwardTerminated() specifiers; // (concrete function should not throw exceptions)
|
||||
|
||||
// type BackwardTerminated() specifiers; // (concrete function should not throw exceptions)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#endif
|
265
librerie/exercise5/list/list.cpp
Executable file
265
librerie/exercise5/list/list.cpp
Executable file
@ -0,0 +1,265 @@
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
169
librerie/exercise5/list/list.hpp
Executable file
169
librerie/exercise5/list/list.hpp
Executable file
@ -0,0 +1,169 @@
|
||||
|
||||
#ifndef LIST_HPP
|
||||
#define LIST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename 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>:: size;
|
||||
|
||||
struct Node
|
||||
{
|
||||
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() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructor
|
||||
List(const LinearContainer<Data>&); // A list obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
List(const List&);
|
||||
|
||||
// Move constructor
|
||||
List(List&&);
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
~List();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
List& operator=(const List&);
|
||||
|
||||
// Move assignment
|
||||
List& operator=(List&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const List&) const noexcept;
|
||||
bool operator!=(const List&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions
|
||||
|
||||
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)
|
||||
|
||||
void InsertAtBack(const Data&); // Copy of the value
|
||||
void InsertAtBack(Data&&); // Move of the value
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from LinearContainer)
|
||||
|
||||
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 MappableContainer)
|
||||
|
||||
using typename MappableContainer<Data>::MapFunctor;
|
||||
|
||||
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;
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "list.cpp"
|
||||
|
||||
#endif
|
16
librerie/exercise5/main.cpp
Normal file
16
librerie/exercise5/main.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
#include "zlasdtest/test.hpp"
|
||||
|
||||
#include "zmytest/test.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
int main() {
|
||||
std::cout << "Lasd Libraries 2020" << std::endl;
|
||||
lasdtest(); // To call in the menu of your library test!
|
||||
return 0;
|
||||
}
|
10
librerie/exercise5/matrix/csr/matrixcsr.cpp
Normal file
10
librerie/exercise5/matrix/csr/matrixcsr.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// ...
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
108
librerie/exercise5/matrix/csr/matrixcsr.hpp
Normal file
108
librerie/exercise5/matrix/csr/matrixcsr.hpp
Normal file
@ -0,0 +1,108 @@
|
||||
|
||||
#ifndef MATRIXCSR_HPP
|
||||
#define MATRIXCSR_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../matrix.hpp"
|
||||
|
||||
// #include "../../list/list.hpp"
|
||||
// #include "../../vector/vector.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class MatrixCSR { // Must extend Matrix<Data>
|
||||
|
||||
private:
|
||||
|
||||
// ...
|
||||
|
||||
protected:
|
||||
|
||||
// using Matrix<Data>::???;
|
||||
|
||||
// ...
|
||||
|
||||
public:
|
||||
|
||||
// Default constructor
|
||||
// MatrixCSR() specifiers;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructors
|
||||
// MatrixCSR(argument) specifiers; // A matrix of some specified dimension
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
// MatrixCSR(argument) specifiers;
|
||||
|
||||
// Move constructor
|
||||
// MatrixCSR(argument) specifiers;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
// ~MatrixCSR() 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 Matrix)
|
||||
|
||||
// type RowResize() specifiers; // Override Matrix member
|
||||
// type ColumnResize() specifiers; // Override Matrix member
|
||||
|
||||
// type ExistsCell() specifiers; // Override Matrix member (should not throw exceptions)
|
||||
|
||||
// type operator()() specifiers; // Override Matrix member (mutable access to the element; throw out_of_range when out of range)
|
||||
// type operator()() specifiers; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
|
||||
// type Clear() specifiers; // Override Container member
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from MappableContainer)
|
||||
|
||||
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
|
||||
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from FoldableContainer)
|
||||
|
||||
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
|
||||
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "matrixcsr.cpp"
|
||||
|
||||
#endif
|
14
librerie/exercise5/matrix/matrix.cpp
Normal file
14
librerie/exercise5/matrix/matrix.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
ulong Matrix<Data>::RowNumber() noexcept{
|
||||
return rows;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
ulong Matrix<Data>::ColumnNumber() noexcept{
|
||||
return columns;
|
||||
}
|
||||
|
||||
}
|
69
librerie/exercise5/matrix/matrix.hpp
Normal file
69
librerie/exercise5/matrix/matrix.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
|
||||
#ifndef MATRIX_HPP
|
||||
#define MATRIX_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class Matrix : virtual public MappableContainer<Data>,
|
||||
virtual public FoldableContainer<Data>{ // Must extend MappableContainer<Data> and FoldableContainer<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
//using MappableContainer<Data>::size;
|
||||
ulong rows = 0;
|
||||
ulong columns = 0;
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~Matrix() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
Matrix& operator=(const Matrix&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
Matrix& operator=(Matrix&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const Matrix&) const noexcept = delete; // Comparison of abstract types might not be possible.
|
||||
bool operator!=(const Matrix&) const noexcept = delete; // Comparison of abstract types might not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions
|
||||
|
||||
ulong RowNumber() noexcept; // (concrete function should not throw exceptions)
|
||||
ulong ColumnNumber() noexcept; // (concrete function should not throw exceptions)
|
||||
|
||||
virtual void RowResize(const ulong&) = 0;
|
||||
virtual void ColumnResize(const ulong&) = 0;
|
||||
|
||||
virtual bool ExistsCell(const ulong&, const ulong&) noexcept = 0; // (concrete function should not throw exceptions)
|
||||
|
||||
virtual Data& operator()(const ulong&, const ulong&) = 0; // Mutable access to the element (concrete function should throw exceptions only when out of range)
|
||||
virtual const Data& operator()(const ulong&, const ulong&) const = 0; // Immutable access to the element (concrete function should throw exceptions when not present)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "matrix.cpp"
|
||||
|
||||
#endif
|
133
librerie/exercise5/matrix/vec/matrixvec.cpp
Normal file
133
librerie/exercise5/matrix/vec/matrixvec.cpp
Normal file
@ -0,0 +1,133 @@
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec<Data>::MatrixVec(ulong r, ulong c){
|
||||
rows = r;
|
||||
columns = c;
|
||||
size = r*c;
|
||||
Elements = new Data[size]{};
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec<Data>::MatrixVec(const MatrixVec& toCopy){
|
||||
rows = toCopy.rows;
|
||||
columns = toCopy.columns;
|
||||
size = toCopy.rows * toCopy.columns;
|
||||
Elements = new Data[size]{};
|
||||
for(ulong i=0 ; i<size ; ++i){
|
||||
Elements[i] = toCopy.Elements[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec<Data>::MatrixVec(MatrixVec&& toMove) noexcept{
|
||||
std::swap(rows, toMove.rows);
|
||||
std::swap(columns, toMove.columns);
|
||||
std::swap(size, toMove.size);
|
||||
std::swap(Elements, toMove.Elements);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec<Data>::~MatrixVec(){
|
||||
Clear();
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec& MatrixVec<Data>::operator=(const MatrixVec& toCopy){
|
||||
Clear();
|
||||
rows = toCopy.rows;
|
||||
columns = toCopy.columns;
|
||||
size = toCopy.rows * toCopy.columns;
|
||||
Elements = new Data[size]{};
|
||||
for(ulong i=0 ; i<size ; ++i){
|
||||
Elements[i] = toCopy.Elements[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
MatrixVec& MatrixVec<Data>::operator=(MatrixVec&& toMove) noexcept{
|
||||
Clear();
|
||||
std::swap(rows, toMove.rows);
|
||||
std::swap(columns, toMove.columns);
|
||||
std::swap(size, toMove.size);
|
||||
std::swap(Elements, toMove.Elements);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool MatrixVec<Data>::operator==(const MatrixVec& toCompare) const noexcept{
|
||||
if(rows == toCompare.rows && columns == toCompare.columns){
|
||||
return Vector<Data>::operator==(toCompare);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool MatrixVec<Data>::operator!=(const MatrixVec& toCompare) const noexcept{
|
||||
return !(*this == toCompare);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixVec<Data>::RowResize(const ulong newdim){
|
||||
Vector<Data>::Resize(newdim);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixVec<Data>::ColumnResize(const ulong& new_column_dim){
|
||||
|
||||
if(new_column_dim == 0){
|
||||
Clear();
|
||||
}
|
||||
else if(new_column_dim != columns){
|
||||
|
||||
Data* tmp = new Data[rows * new_column_dim]{};
|
||||
|
||||
if(new_column_dim > columns){
|
||||
for(ulong i=0 ; i<r ; ++i){
|
||||
for(ulong j=0 ; j<new_column_dim ; ++j){
|
||||
if(ExistsCell(i,j)) tmp[(i*new_column_dim)+j] = (*this)(i,j);
|
||||
}
|
||||
}
|
||||
}else if(new_column_dim < columns){
|
||||
for(ulong i=0 ; i<r ; ++i){
|
||||
for(ulong j=0 ; j<new_column_dim ; ++j){
|
||||
tmp[(i*new_column_dim)+j] = (*this)(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size = rows * new_column_dim;
|
||||
columns = new_column_dim;
|
||||
std::swap(Elements, tmp);
|
||||
delete[] tmp;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool MatrixVec<Data>::ExistsCell(const ulong& r, const ulong& c) noexcept override{
|
||||
return (r<rows && c<columns);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
const Data& MatrixVec<Data>::operator()(const ulong& r, const ulong& c) const{
|
||||
if(ExistsCell(r,c)) return Elements[(r*columns)+c];
|
||||
else throw std::out_of_range("Tried to access position ["<<r<<"]["<<c<<"] in a ["<<rows<<"]["<<columns<<"] matrix!");
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& MatrixVec<Data>::operator()(const ulong& r, const ulong& c){
|
||||
if(ExistsCell(r,c)) return Elements[(r*columns)+c];
|
||||
else throw std::out_of_range("Tried to access position ["<<r<<"]["<<c<<"] in a ["<<rows<<"]["<<columns<<"] matrix!");
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MatrixVec<Data>::Clear(){
|
||||
Vector<Data>::Clear();
|
||||
rows = 0;
|
||||
columns = 0;
|
||||
}
|
||||
|
||||
}
|
64
librerie/exercise5/matrix/vec/matrixvec.hpp
Normal file
64
librerie/exercise5/matrix/vec/matrixvec.hpp
Normal file
@ -0,0 +1,64 @@
|
||||
|
||||
#ifndef MATRIXVEC_HPP
|
||||
#define MATRIXVEC_HPP
|
||||
|
||||
#include "../matrix.hpp"
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
class MatrixVec : virtual public Matrix<Data>,
|
||||
virtual public Vector<Data>{ // Must extend Matrix<Data>
|
||||
|
||||
protected:
|
||||
|
||||
using Matrix<Data>::rows;
|
||||
using Matrix<Data>::columns;
|
||||
using Vector<Data>::size;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
MatrixVec() = default;
|
||||
MatrixVec(ulong&, ulong&); // A matrix of some specified dimension
|
||||
MatrixVec(const MatrixVec&);
|
||||
MatrixVec(MatrixVec&&) noexcept;
|
||||
|
||||
virtual ~MatrixVec();
|
||||
|
||||
|
||||
MatrixVec& operator=(const MatrixVec&);
|
||||
MatrixVec& operator=(MatrixVec&&) noexcept;
|
||||
|
||||
bool operator==(const MatrixVec&) const noexcept;
|
||||
bool operator!=(const MatrixVec&) const noexcept;
|
||||
|
||||
// Specific member functions (inherited from Matrix)
|
||||
void RowResize(const ulong&) override; // Override Matrix member
|
||||
void ColumnResize(const ulong&) override; // Override Matrix member
|
||||
bool ExistsCell(const ulong&, const ulong&) noexcept override; // Override Matrix member (should not throw exceptions)
|
||||
Data& operator()(const ulong&, const ulong&) override; // Override Matrix member (mutable access to the element; throw out_of_range when out of range)
|
||||
const Data& operator()(const ulong&, const ulong&) const override; // Override Matrix member (immutable access to the element; throw out_of_range when out of range and length_error when not present)
|
||||
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
using Vector<Data>::MapPreOrder;
|
||||
using Vector<Data>::MapPostOrder;
|
||||
using Vector<Data>::FoldPreOrder;
|
||||
using Vector<Data>::FoldPostOrder;
|
||||
|
||||
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
|
||||
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
|
||||
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
|
||||
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#include "matrixvec.cpp"
|
||||
|
||||
#endif
|
73
librerie/exercise5/queue/lst/queuelst.cpp
Executable file
73
librerie/exercise5/queue/lst/queuelst.cpp
Executable file
@ -0,0 +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();
|
||||
}
|
||||
|
||||
}
|
90
librerie/exercise5/queue/lst/queuelst.hpp
Executable file
90
librerie/exercise5/queue/lst/queuelst.hpp
Executable file
@ -0,0 +1,90 @@
|
||||
|
||||
#ifndef QUEUELST_HPP
|
||||
#define QUEUELST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../queue.hpp"
|
||||
#include "../../list/list.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class QueueLst : virtual public Queue<Data>,
|
||||
virtual protected List<Data>{ // Must extend Queue<Data> and List<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using List<Data>::head;
|
||||
using List<Data>::tail;
|
||||
using List<Data>::size;
|
||||
using typename List<Data>::Node;
|
||||
|
||||
public:
|
||||
|
||||
// Default constructor
|
||||
QueueLst() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructor
|
||||
QueueLst(const LinearContainer<Data>&); // A queue obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
QueueLst(const QueueLst&);
|
||||
|
||||
// Move constructor
|
||||
QueueLst(QueueLst&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
~QueueLst();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
QueueLst& operator=(const QueueLst&);
|
||||
|
||||
// Move assignment
|
||||
QueueLst& operator=(QueueLst&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const QueueLst&) const noexcept;
|
||||
bool operator!=(const QueueLst&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Queue)
|
||||
|
||||
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)
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "queuelst.cpp"
|
||||
|
||||
#endif
|
57
librerie/exercise5/queue/queue.hpp
Executable file
57
librerie/exercise5/queue/queue.hpp
Executable file
@ -0,0 +1,57 @@
|
||||
|
||||
#ifndef QUEUE_HPP
|
||||
#define QUEUE_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class Queue : virtual public Container{ // Must extend Container
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
~Queue() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
Queue& operator=(const Queue&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
Queue& operator=(Queue&&) = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#endif
|
192
librerie/exercise5/queue/vec/queuevec.cpp
Executable file
192
librerie/exercise5/queue/vec/queuevec.cpp
Executable file
@ -0,0 +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;
|
||||
}
|
||||
|
||||
}
|
103
librerie/exercise5/queue/vec/queuevec.hpp
Executable file
103
librerie/exercise5/queue/vec/queuevec.hpp
Executable file
@ -0,0 +1,103 @@
|
||||
|
||||
#ifndef QUEUEVEC_HPP
|
||||
#define QUEUEVEC_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../queue.hpp"
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class QueueVec : virtual public Queue<Data>,
|
||||
virtual protected Vector<Data>{ // Must extend Queue<Data> and Vector<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using Vector<Data>::Elements;
|
||||
using Vector<Data>::size; // dimension of the array
|
||||
ulong front = 0;
|
||||
ulong rear = 0;
|
||||
|
||||
public:
|
||||
|
||||
// Default constructor
|
||||
QueueVec();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructor
|
||||
QueueVec(const LinearContainer<Data>&); // A queue obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
QueueVec(const QueueVec&);
|
||||
|
||||
// Move constructor
|
||||
QueueVec(QueueVec&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~QueueVec();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
QueueVec& operator=(const QueueVec&);
|
||||
|
||||
// Move assignment
|
||||
QueueVec& operator=(QueueVec&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const QueueVec&) const noexcept;
|
||||
bool operator!=(const QueueVec&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Queue)
|
||||
|
||||
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)
|
||||
|
||||
bool Empty() const noexcept override; // Override Container member
|
||||
|
||||
ulong Size() const noexcept override; // Override Container member
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
protected:
|
||||
|
||||
// Auxiliary member functions
|
||||
|
||||
void Expand();
|
||||
void Reduce();
|
||||
|
||||
//void SwapVectors(arguments) specifiers;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "queuevec.cpp"
|
||||
|
||||
#endif
|
80
librerie/exercise5/stack/lst/stacklst.cpp
Executable file
80
librerie/exercise5/stack/lst/stacklst.cpp
Executable file
@ -0,0 +1,80 @@
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
89
librerie/exercise5/stack/lst/stacklst.hpp
Executable file
89
librerie/exercise5/stack/lst/stacklst.hpp
Executable file
@ -0,0 +1,89 @@
|
||||
|
||||
#ifndef STACKLST_HPP
|
||||
#define STACKLST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../stack.hpp"
|
||||
#include "../../list/list.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class StackLst : virtual public Stack<Data>,
|
||||
virtual protected List<Data> { // Must extend Stack<Data> and List<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using List<Data>::head;
|
||||
using List<Data>::size;
|
||||
using typename List<Data>::Node;
|
||||
|
||||
public:
|
||||
|
||||
// Default constructor
|
||||
StackLst() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructor
|
||||
StackLst(const LinearContainer<Data>&); // A stack obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
StackLst(const StackLst&);
|
||||
|
||||
// Move constructor
|
||||
StackLst(StackLst&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~StackLst();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
StackLst& operator=(const StackLst&);
|
||||
|
||||
// Move assignment
|
||||
StackLst& operator=(StackLst&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const StackLst&) const noexcept;
|
||||
bool operator!=(const StackLst&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Stack)
|
||||
|
||||
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)
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "stacklst.cpp"
|
||||
|
||||
#endif
|
57
librerie/exercise5/stack/stack.hpp
Executable file
57
librerie/exercise5/stack/stack.hpp
Executable file
@ -0,0 +1,57 @@
|
||||
|
||||
#ifndef STACK_HPP
|
||||
#define STACK_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class Stack : virtual public Container { // Must extend Container
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
virtual ~Stack() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
Stack& operator=(const Stack&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
Stack&operator=(Stack&&) = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#endif
|
143
librerie/exercise5/stack/vec/stackvec.cpp
Executable file
143
librerie/exercise5/stack/vec/stackvec.cpp
Executable file
@ -0,0 +1,143 @@
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
101
librerie/exercise5/stack/vec/stackvec.hpp
Executable file
101
librerie/exercise5/stack/vec/stackvec.hpp
Executable file
@ -0,0 +1,101 @@
|
||||
|
||||
#ifndef STACKVEC_HPP
|
||||
#define STACKVEC_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../stack.hpp"
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class StackVec : virtual public Stack<Data>,
|
||||
virtual protected Vector<Data>{ // Must extend Stack<Data> and Vector<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
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();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructor
|
||||
StackVec(const LinearContainer<Data>&); // A stack obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
StackVec(const StackVec&);
|
||||
|
||||
// Move constructor
|
||||
StackVec(StackVec&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~StackVec();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
StackVec& operator=(const StackVec&);
|
||||
|
||||
// Move assignment
|
||||
StackVec& operator=(StackVec&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const StackVec&) const noexcept;
|
||||
bool operator!=(const StackVec&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Stack)
|
||||
|
||||
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)
|
||||
|
||||
bool Empty() const noexcept override; // Override Container member
|
||||
|
||||
ulong Size() const noexcept override; // Override Container member
|
||||
|
||||
void Clear() override;// Override Container member
|
||||
|
||||
protected:
|
||||
|
||||
// Auxiliary member functions
|
||||
|
||||
void Expand();
|
||||
void Reduce();
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "stackvec.cpp"
|
||||
|
||||
#endif
|
160
librerie/exercise5/vector/vector.cpp
Executable file
160
librerie/exercise5/vector/vector.cpp
Executable file
@ -0,0 +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);
|
||||
}
|
||||
}
|
||||
}
|
79
librerie/exercise5/vector/vector.hpp
Executable file
79
librerie/exercise5/vector/vector.hpp
Executable file
@ -0,0 +1,79 @@
|
||||
|
||||
#ifndef VECTOR_HPP
|
||||
#define VECTOR_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename 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>::size;
|
||||
Data* Elements = nullptr;
|
||||
|
||||
public:
|
||||
|
||||
Vector() = default;
|
||||
|
||||
// Specific constructors
|
||||
Vector(const ulong); // A vector with a given initial dimension
|
||||
Vector(const LinearContainer<Data>&); // A vector obtained from a LinearContainer
|
||||
|
||||
// Copy constructor
|
||||
Vector(const Vector&);
|
||||
|
||||
// Move constructor
|
||||
Vector(Vector&&)noexcept;
|
||||
|
||||
// Destructor
|
||||
~Vector();
|
||||
|
||||
// Copy assignment
|
||||
Vector& operator=(const Vector&);
|
||||
|
||||
// Move assignment
|
||||
Vector& operator=(Vector&&) noexcept;
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const Vector&) const noexcept;
|
||||
bool operator!=(const Vector&) const noexcept;
|
||||
|
||||
void Resize(const ulong); // Resize the vector to a given size
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
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)
|
||||
|
||||
using typename MappableContainer<Data>::MapFunctor;
|
||||
|
||||
void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member
|
||||
void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer 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"
|
||||
|
||||
#endif
|
130
librerie/exercise5/zlasdtest/binarytree/binarytree.hpp
Normal file
130
librerie/exercise5/zlasdtest/binarytree/binarytree.hpp
Normal file
@ -0,0 +1,130 @@
|
||||
|
||||
#ifndef BINARYTREETEST_HPP
|
||||
#define BINARYTREETEST_HPP
|
||||
|
||||
#include "../../binarytree/binarytree.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void GetElement(uint& testnum, uint& testerr, const typename lasd::BinaryTree<Data>::Node& nod, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The data of the node is \"" << nod.Element() << "\": ";
|
||||
std::cout << ((tst = ((nod.Element() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetElement(uint& testnum, uint& testerr, const typename lasd::BinaryTree<Data>::Node& nod, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting the front of the linear container to \"" << val << "\": ";
|
||||
nod.Element() = val;
|
||||
std::cout << ((tst = ((nod.Element() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void IsLeaf(uint& testnum, uint& testerr, const typename lasd::BinaryTree<Data>::Node& nod, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The node is " << ((tst = nod.IsLeaf()) ? "" : "not ") << "a leaf: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void HasLeftChild(uint& testnum, uint& testerr, const typename lasd::BinaryTree<Data>::Node& nod, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The node does " << ((tst = nod.HasLeftChild()) ? "" : "not ") << "have a left child: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void HasRightChild(uint& testnum, uint& testerr, const typename lasd::BinaryTree<Data>::Node& nod, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The node does " << ((tst = nod.HasRightChild()) ? "" : "not ") << "have a right child: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void EqualBT(uint& testnum, uint& testerr, const lasd::BinaryTree<Data>& bt1, const lasd::BinaryTree<Data>& bt2) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two binary trees are " << ((tst = (bt1 == bt2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << (tst ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void NonEqualBT(uint& testnum, uint& testerr, const lasd::BinaryTree<Data>& bt1, const lasd::BinaryTree<Data>& bt2) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two binary trees are " << ((tst = (bt1 != bt2)) ? "not " : "") << "equal: ";
|
||||
std::cout << (tst ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void Root(uint& testnum, uint& testerr, lasd::BinaryTree<Data>& bt, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Root of the tree with value \"" << bt.Root().Element() << "\": ";
|
||||
std::cout << ((tst = (((bt.Root().Element() == val) && (bt.Root().Element() == val)) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
282
librerie/exercise5/zlasdtest/bst/bst.hpp
Normal file
282
librerie/exercise5/zlasdtest/bst/bst.hpp
Normal file
@ -0,0 +1,282 @@
|
||||
|
||||
#ifndef BSTTEST_HPP
|
||||
#define BSTTEST_HPP
|
||||
|
||||
#include "../../bst/bst.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void EqualBST(uint& testnum, uint& testerr, const lasd::BST<Data>& bst1, const lasd::BST<Data>& bst2) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two bsts are " << ((tst = (bst1 == bst2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << (tst ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void NonEqualBST(uint& testnum, uint& testerr, const lasd::BST<Data>& bst1, const lasd::BST<Data>& bst2) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two bsts are " << ((tst = (bst1 != bst2)) ? "not " : "") << "equal: ";
|
||||
std::cout << (tst ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void InsertC(uint& testnum, uint& testerr, lasd::BST<Data>& bst, const Data& val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Insertion in the bst of the value \"" << val << "\": ";
|
||||
bst.Insert(val);
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void InsertM(uint& testnum, uint& testerr, lasd::BST<Data>& bst, const Data& val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Insertion in the bst of the value \"" << val << "\": ";
|
||||
bst.Insert(std::move(val));
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Remove(uint& testnum, uint& testerr, lasd::BST<Data>& bst, const Data& val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Removal from the bst of the value \"" << val << "\": ";
|
||||
bst.Remove(val);
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Min(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Min of the bst with value \"" << bst.Min() << "\": ";
|
||||
std::cout << ((tst = ((bst.Min() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void RemoveMin(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove min from the the bst: ";
|
||||
bst.RemoveMin();
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MinNRemove(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") MinNRemove from the bst with value \"" << bst.Min() << "\": ";
|
||||
std::cout << ((tst = ((bst.MinNRemove() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Max(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Max of the bst with value \"" << bst.Max() << "\": ";
|
||||
std::cout << ((tst = ((bst.Max() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void RemoveMax(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove max from the the bst: ";
|
||||
bst.RemoveMax();
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MaxNRemove(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") MaxNRemove from the bst with value \"" << bst.Max() << "\": ";
|
||||
std::cout << ((tst = ((bst.MaxNRemove() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Predecessor(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& prd, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Predecessor of " << prd << " with value \"" << bst.Predecessor(prd) << "\": ";
|
||||
std::cout << ((tst = ((bst.Predecessor(prd) == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void RemovePredecessor(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& prd) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove predecessor of " << prd << " from the the bst: \"" << bst.Predecessor(prd) << "\": ";
|
||||
bst.RemovePredecessor(prd);
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void PredecessorNRemove(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& prd, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove predecessor of " << prd << " from the the bst: \"" << bst.Predecessor(prd) << "\": ";
|
||||
std::cout << ((tst = ((bst.PredecessorNRemove(prd) == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Successor(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& prd, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Successor of " << prd << " with value \"" << bst.Successor(prd) << "\": ";
|
||||
std::cout << ((tst = ((bst.Successor(prd) == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void RemoveSuccessor(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& prd) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove successor of " << prd << " from the the bst: \"" << bst.Successor(prd) << "\": ";
|
||||
bst.RemoveSuccessor(prd);
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SuccessorNRemove(uint& testnum, uint& testerr, lasd::BST<Data>& bst, bool chk, const Data& prd, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove successor of " << prd << " from the the bst: \"" << bst.Successor(prd) << "\": ";
|
||||
std::cout << ((tst = ((bst.SuccessorNRemove(prd) == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
53
librerie/exercise5/zlasdtest/container/container.cpp
Normal file
53
librerie/exercise5/zlasdtest/container/container.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// Container member functions!
|
||||
|
||||
void Empty(uint& testnum, uint& testerr, const lasd::Container& con, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
std::cout << " " << testnum << " (" << testerr << ") The container is " << ((tst = con.Empty()) ? "" : "not ") << "empty: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
void Size(uint& testnum, uint& testerr, const lasd::Container& con, bool chk, ulong siz) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
std::cout << " " << testnum << " (" << testerr << ") The container has size " << con.Size() << ": ";
|
||||
std::cout << ((tst = ((con.Size() == siz) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// Auxiliary functions for MappableContainer!
|
||||
|
||||
void MapStringAppend(std::string& dat, void* par) {
|
||||
dat.append(*((std::string*) par));
|
||||
}
|
||||
|
||||
void MapStringNonEmptyAppend(std::string& dat, void* par) {
|
||||
if (!dat.empty()) { dat.append(*((std::string*) par)); }
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// Auxiliary functions for FoldableContainer!
|
||||
|
||||
void FoldParity(const int& dat, const void* _, void* acc) {
|
||||
*((int*) acc) += dat;
|
||||
*((int*) acc) %= 2;
|
||||
}
|
||||
|
||||
void FoldStringConcatenate(const std::string& dat, const void* _, void* acc) {
|
||||
((std::string*) acc)->append(dat);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
353
librerie/exercise5/zlasdtest/container/container.hpp
Normal file
353
librerie/exercise5/zlasdtest/container/container.hpp
Normal file
@ -0,0 +1,353 @@
|
||||
|
||||
#ifndef CONTAINERTEST_HPP
|
||||
#define CONTAINERTEST_HPP
|
||||
|
||||
#include "../../container/container.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// Container member functions!
|
||||
|
||||
void Empty(uint&, uint&, const lasd::Container&, bool);
|
||||
|
||||
void Size(uint&, uint&, const lasd::Container&, bool, ulong);
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// LinearContainer member functions!
|
||||
|
||||
template <typename Data>
|
||||
void GetFront(uint& testnum, uint& testerr, const lasd::LinearContainer<Data>& con, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The front of the linear container is \"" << con.Front() << "\": ";
|
||||
std::cout << ((tst = ((con.Front() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetFront(uint& testnum, uint& testerr, const lasd::LinearContainer<Data>& con, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting the front of the linear container to \"" << val << "\": ";
|
||||
con.Front() = val;
|
||||
std::cout << ((tst = ((con.Front() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void GetBack(uint& testnum, uint& testerr, const lasd::LinearContainer<Data>& con, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The back of the linear container is \"" << con.Back() << "\": ";
|
||||
std::cout << ((tst = ((con.Back() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetBack(uint& testnum, uint& testerr, const lasd::LinearContainer<Data>& con, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting the back of the linear container to \"" << val << "\": ";
|
||||
con.Back() = val;
|
||||
std::cout << ((tst = ((con.Back() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void GetAt(uint& testnum, uint& testerr, lasd::LinearContainer<Data>& con, bool chk, const ulong& ind, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Get of the linear container at index \"" << ind << "\" with value \"" << con[ind] << "\": ";
|
||||
std::cout << ((tst = ((con[ind] == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::out_of_range exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetAt(uint& testnum, uint& testerr, lasd::LinearContainer<Data>& con, bool chk, const ulong& ind, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Set of the linear container at index \"" << ind << "\" with value \"" << val << "\": ";
|
||||
con[ind] = val;
|
||||
std::cout << ((tst = ((con[ind] == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::out_of_range exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// TestableContainer member functions!
|
||||
|
||||
template <typename Data>
|
||||
void Exists(uint& testnum, uint& testerr, const lasd::TestableContainer<Data>& con, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
std::cout << " " << testnum << " (" << testerr << ") Data \"" << val << "\" " << ((tst = con.Exists(val)) ? "does" : "does not") << " exist: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// MappableContainer member functions!
|
||||
|
||||
template <typename Data, typename Parameter>
|
||||
void MapPreOrder(uint& testnum, uint& testerr, lasd::MappableContainer<Data>& con, bool chk, typename lasd::MappableContainer<Data>::MapFunctor fun, const Parameter& inipar) {
|
||||
bool tst = true;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing map in pre order - ";
|
||||
con.MapPreOrder(fun, &par);
|
||||
std::cout << ": " << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data, typename Parameter>
|
||||
void MapPostOrder(uint& testnum, uint& testerr, lasd::MappableContainer<Data>& con, bool chk, typename lasd::MappableContainer<Data>::MapFunctor fun, const Parameter& inipar) {
|
||||
bool tst = true;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing map in post order - ";
|
||||
con.MapPostOrder(fun, &par);
|
||||
std::cout << ": " << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapPrint(const Data& dat, void* _) {
|
||||
std::cout << dat << " ";
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapIncrement(Data& dat, void* _) {
|
||||
dat++;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapDecrement(Data& dat, void* _) {
|
||||
dat--;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapIncrementNPrint(Data& dat, void* _) {
|
||||
std::cout << dat++ << "->" << dat << "; ";
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapDouble(Data& dat, void* _) {
|
||||
dat *= 2;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapHalf(Data& dat, void* _) {
|
||||
dat /= 2;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapDoubleNPrint(Data& dat, void* _) {
|
||||
std::cout << dat << "->" << (dat *= 2) << "; ";
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapInvert(Data& dat, void* _) {
|
||||
dat = -dat;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapInvertNPrint(Data& dat, void* _) {
|
||||
std::cout << dat << "->" << (dat = -dat) << "; ";
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapParityInvert(Data& dat, void* _) {
|
||||
if (dat % 2 != 0) { dat = -dat; }
|
||||
}
|
||||
|
||||
void MapStringAppend(std::string&, void*);
|
||||
|
||||
void MapStringNonEmptyAppend(std::string&, void*);
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// FoldableContainer member functions!
|
||||
|
||||
template <typename Data, typename Parameter, typename Value>
|
||||
void FoldPreOrder(uint& testnum, uint& testerr, const lasd::FoldableContainer<Data>& con, bool chk, typename lasd::FoldableContainer<Data>::FoldFunctor fun, const Parameter& inipar, const Value& inival, const Value& finval) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
Value val = inival;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing fold in pre order - ";
|
||||
con.FoldPreOrder(fun, &par, &val);
|
||||
std::cout << "obtained value is \"" << val << "\": ";
|
||||
std::cout << ((tst = ((val == finval) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data, typename Parameter, typename Value>
|
||||
void FoldPostOrder(uint& testnum, uint& testerr, const lasd::FoldableContainer<Data>& con, bool chk, typename lasd::FoldableContainer<Data>::FoldFunctor fun, const Parameter& inipar, const Value& inival, const Value& finval) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
Value val = inival;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing fold in post order - ";
|
||||
con.FoldPostOrder(fun, &par, &val);
|
||||
std::cout << "obtained value is \"" << val << "\": ";
|
||||
std::cout << ((tst = ((val == finval) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void FoldAdd(const Data& dat, const void* _, void* acc) {
|
||||
*((Data*) acc) += dat;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void FoldMultiply(const Data& dat, const void* _, void* acc) {
|
||||
*((Data*) acc) *= dat;
|
||||
}
|
||||
|
||||
void FoldParity(const int&, const void*, void*);
|
||||
|
||||
void FoldStringConcatenate(const std::string&, const void*, void*);
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// InOrderMappableContainer member functions!
|
||||
|
||||
template <typename Data, typename Parameter>
|
||||
void MapInOrder(uint& testnum, uint& testerr, lasd::InOrderMappableContainer<Data>& con, bool chk, typename lasd::InOrderMappableContainer<Data>::MapFunctor fun, const Parameter& inipar) {
|
||||
bool tst = true;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing map in order - ";
|
||||
con.MapInOrder(fun, &par);
|
||||
std::cout << ": " << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// InOrderFoldableContainer member functions!
|
||||
|
||||
template <typename Data, typename Parameter, typename Value>
|
||||
void FoldInOrder(uint& testnum, uint& testerr, const lasd::InOrderFoldableContainer<Data>& con, bool chk, typename lasd::InOrderFoldableContainer<Data>::FoldFunctor fun, const Parameter& inipar, const Value& inival, const Value& finval) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
Value val = inival;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing fold in order - ";
|
||||
con.FoldInOrder(fun, &par, &val);
|
||||
std::cout << "obtained value is \"" << val << "\": ";
|
||||
std::cout << ((tst = ((val == finval) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// BreadthMappableContainer member functions!
|
||||
|
||||
template <typename Data, typename Parameter>
|
||||
void MapBreadth(uint& testnum, uint& testerr, lasd::BreadthMappableContainer<Data>& con, bool chk, typename lasd::BreadthMappableContainer<Data>::MapFunctor fun, const Parameter& inipar) {
|
||||
bool tst = true;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing map in breadth - ";
|
||||
con.MapBreadth(fun, &par);
|
||||
std::cout << ": " << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// BreadthFoldableContainer member functions!
|
||||
|
||||
template <typename Data, typename Parameter, typename Value>
|
||||
void FoldBreadth(uint& testnum, uint& testerr, const lasd::BreadthFoldableContainer<Data>& con, bool chk, typename lasd::BreadthFoldableContainer<Data>::FoldFunctor fun, const Parameter& inipar, const Value& inival, const Value& finval) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
Parameter par = {inipar};
|
||||
Value val = inival;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Executing fold in breadth - ";
|
||||
con.FoldBreadth(fun, &par, &val);
|
||||
std::cout << "obtained value is \"" << val << "\": ";
|
||||
std::cout << ((tst = ((val == finval) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
3
librerie/exercise5/zlasdtest/exercise1/fulltest.cpp
Normal file
3
librerie/exercise5/zlasdtest/exercise1/fulltest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
void testFullExercise1() {
|
||||
}
|
447
librerie/exercise5/zlasdtest/exercise1/simpletest.cpp
Normal file
447
librerie/exercise5/zlasdtest/exercise1/simpletest.cpp
Normal file
@ -0,0 +1,447 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../vector/vector.hpp"
|
||||
|
||||
#include "../list/list.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void stestVectorInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Vector<int> Test:" << endl;
|
||||
try {
|
||||
{
|
||||
lasd::Vector<int> vec;
|
||||
Empty(loctestnum, loctesterr, vec, true);
|
||||
|
||||
GetFront(loctestnum, loctesterr, vec, false, 0);
|
||||
GetBack(loctestnum, loctesterr, vec, false, 0);
|
||||
SetAt(loctestnum, loctesterr, vec, false, 1, 0);
|
||||
GetAt(loctestnum, loctesterr, vec, false, 2, 0);
|
||||
|
||||
Exists(loctestnum, loctesterr, vec, false, 0);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, vec, true, &MapPrint<int>, 0);
|
||||
MapPostOrder(loctestnum, loctesterr, vec, true, &MapPrint<int>, 0);
|
||||
|
||||
FoldPreOrder(loctestnum, loctesterr, vec, true, &FoldAdd<int>, 0, 0, 0);
|
||||
FoldPostOrder(loctestnum, loctesterr, vec, true, &FoldAdd<int>, 0, 0, 0);
|
||||
}
|
||||
{
|
||||
lasd::Vector<int> vec(3);
|
||||
Empty(loctestnum, loctesterr, vec, false);
|
||||
Size(loctestnum, loctesterr, vec, true, 3);
|
||||
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, 4);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, 3);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, 1);
|
||||
|
||||
GetFront(loctestnum, loctesterr, vec, true, 4);
|
||||
GetBack(loctestnum, loctesterr, vec, true, 1);
|
||||
|
||||
SetFront(loctestnum, loctesterr, vec, true, 5);
|
||||
SetBack(loctestnum, loctesterr, vec, true, 4);
|
||||
|
||||
Exists(loctestnum, loctesterr, vec, true, 4);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, vec, true, &MapPrint<int>, 0);
|
||||
MapPostOrder(loctestnum, loctesterr, vec, true, &MapPrint<int>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, vec, true, &FoldAdd<int>, 0, 0, 12);
|
||||
FoldPostOrder(loctestnum, loctesterr, vec, true, &FoldMultiply<int>, 0, 1, 60);
|
||||
|
||||
vec.Resize(2);
|
||||
FoldPostOrder(loctestnum, loctesterr, vec, true, &FoldMultiply<int>, 0, 1, 15);
|
||||
}
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Vector<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestVectorDouble(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Vector<double> Test:" << endl;
|
||||
try {
|
||||
lasd::Vector<double> vec(3);
|
||||
Empty(loctestnum, loctesterr, vec, false);
|
||||
Size(loctestnum, loctesterr, vec, true, 3);
|
||||
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, 5.5);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, 3.3);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, 1.1);
|
||||
|
||||
GetFront(loctestnum, loctesterr, vec, true, 5.5);
|
||||
GetBack(loctestnum, loctesterr, vec, true, 1.1);
|
||||
|
||||
Exists(loctestnum, loctesterr, vec, true, 3.3);
|
||||
|
||||
FoldPreOrder(loctestnum, loctesterr, vec, true, &FoldAdd<double>, 0.0, 0.0, 9.9);
|
||||
FoldPostOrder(loctestnum, loctesterr, vec, true, &FoldMultiply<double>, 0.0, 1.0, 19.965);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Vector<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestVectorString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Vector<string> Test:" << endl;
|
||||
try {
|
||||
lasd::Vector<string> vec(2);
|
||||
Empty(loctestnum, loctesterr, vec, false);
|
||||
Size(loctestnum, loctesterr, vec, true, 2);
|
||||
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("A"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, string("B"));
|
||||
|
||||
GetFront(loctestnum, loctesterr, vec, true, string("A"));
|
||||
GetBack(loctestnum, loctesterr, vec, true, string("B"));
|
||||
|
||||
Exists(loctestnum, loctesterr, vec, true, string("A"));
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, vec, true, &MapStringAppend, string(" "));
|
||||
MapPreOrder(loctestnum, loctesterr, vec, true, &MapPrint<string>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, vec, true, &FoldStringConcatenate, string(""), string("X"), string("XA B "));
|
||||
FoldPostOrder(loctestnum, loctesterr, vec, true, &FoldStringConcatenate, string(""), string("X"), string("XB A "));
|
||||
|
||||
Exists(loctestnum, loctesterr, vec, false, string("A"));
|
||||
|
||||
lasd::Vector<string> copvec(vec);
|
||||
EqualVector(loctestnum, loctesterr, vec, copvec, true);
|
||||
MapPreOrder(loctestnum, loctesterr, vec, true, &MapStringAppend, string("!"));
|
||||
NonEqualVector(loctestnum, loctesterr, vec, copvec, true);
|
||||
|
||||
copvec = move(vec);
|
||||
FoldPreOrder(loctestnum, loctesterr, copvec, true, &FoldStringConcatenate, string(""), string("?"), string("?A !B !"));
|
||||
|
||||
lasd::Vector<string> movvec(move(vec));
|
||||
FoldPreOrder(loctestnum, loctesterr, movvec, true, &FoldStringConcatenate, string(""), string("?"), string("?A B "));
|
||||
SetAt(loctestnum, loctesterr, vec, false, 1, string(""));
|
||||
vec.Resize(1);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("X"));
|
||||
|
||||
movvec.Clear();
|
||||
Empty(loctestnum, loctesterr, movvec, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Vector<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestVector(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
stestVectorInt(loctestnum, loctesterr);
|
||||
stestVectorDouble(loctestnum, loctesterr);
|
||||
stestVectorString(loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << endl << "Exercise 1 - Vector (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void stestListInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of List<int> Test:" << endl;
|
||||
try {
|
||||
lasd::List<int> lst;
|
||||
Empty(loctestnum, loctesterr, lst, true);
|
||||
Size(loctestnum, loctesterr, lst, true, 0);
|
||||
|
||||
GetFront(loctestnum, loctesterr, lst, false, 0);
|
||||
GetBack(loctestnum, loctesterr, lst, false, 0);
|
||||
|
||||
Exists(loctestnum, loctesterr, lst, false, 0);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapPrint<int>, 0);
|
||||
MapPostOrder(loctestnum, loctesterr, lst, true, &MapPrint<int>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, lst, true, &FoldAdd<int>, 0, 0, 0);
|
||||
FoldPostOrder(loctestnum, loctesterr, lst, true, &FoldAdd<int>, 0, 0, 0);
|
||||
|
||||
RemoveFromFront(loctestnum, loctesterr, lst, false);
|
||||
FrontNRemove(loctestnum, loctesterr, lst, false, 0);
|
||||
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 4);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 5);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 9);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 2);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 1);
|
||||
|
||||
GetFront(loctestnum, loctesterr, lst, true, 1);
|
||||
GetBack(loctestnum, loctesterr, lst, true, 2);
|
||||
SetFront(loctestnum, loctesterr, lst, true, 2);
|
||||
SetBack(loctestnum, loctesterr, lst, true, 6);
|
||||
|
||||
GetAt(loctestnum, loctesterr, lst, true, 3, 4);
|
||||
SetAt(loctestnum, loctesterr, lst, true, 3, 3);
|
||||
|
||||
Exists(loctestnum, loctesterr, lst, false, 4);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapPrint<int>, 0);
|
||||
MapPostOrder(loctestnum, loctesterr, lst, true, &MapPrint<int>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, lst, true, &FoldAdd<int>, 0, 0, 25);
|
||||
FoldPostOrder(loctestnum, loctesterr, lst, true, &FoldMultiply<int>, 0, 1, 1620);
|
||||
|
||||
RemoveFromFront(loctestnum, loctesterr, lst, true);
|
||||
FrontNRemove(loctestnum, loctesterr, lst, true, 9);
|
||||
FoldPostOrder(loctestnum, loctesterr, lst, true, &FoldMultiply<int>, 0, 1, 90);
|
||||
|
||||
lasd::List<int> coplst(lst);
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapIncrement<int>, 0);
|
||||
NonEqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 0);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 0);
|
||||
NonEqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
coplst = lst;
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
|
||||
RemoveFromFront(loctestnum, loctesterr, coplst, true);
|
||||
FrontNRemove(loctestnum, loctesterr, coplst, true, 6);
|
||||
coplst = move(lst);
|
||||
FoldPreOrder(loctestnum, loctesterr, lst, true, &FoldAdd<int>, 0, 0, 11);
|
||||
FoldPreOrder(loctestnum, loctesterr, coplst, true, &FoldAdd<int>, 0, 0, 17);
|
||||
|
||||
lasd::List<int> movlst(move(lst));
|
||||
MapPreOrder(loctestnum, loctesterr, movlst, true, &MapIncrement<int>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, movlst, true, &FoldAdd<int>, 0, 0, 14);
|
||||
|
||||
movlst.Clear();
|
||||
Empty(loctestnum, loctesterr, movlst, true);
|
||||
Size(loctestnum, loctesterr, movlst, true, 0);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of List<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestListDouble(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of List<double> Test:" << endl;
|
||||
try {
|
||||
lasd::List<double> lst;
|
||||
Empty(loctestnum, loctesterr, lst, true);
|
||||
Size(loctestnum, loctesterr, lst, true, 0);
|
||||
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, -2.5);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 2.5);
|
||||
|
||||
lst.Clear();
|
||||
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 0.5);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 3.3);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 5.5);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 1.1);
|
||||
|
||||
GetFront(loctestnum, loctesterr, lst, true, 5.5);
|
||||
GetBack(loctestnum, loctesterr, lst, true, 1.1);
|
||||
|
||||
Exists(loctestnum, loctesterr, lst, false, 0.0);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapPrint<double>, 0);
|
||||
MapPostOrder(loctestnum, loctesterr, lst, true, &MapPrint<double>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, lst, true, &FoldAdd<double>, 0.0, 0.0, 10.4);
|
||||
FoldPostOrder(loctestnum, loctesterr, lst, true, &FoldMultiply<double>, 0.0, 1.0, 9.9825);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of List<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestListString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of List<string> Test:" << endl;
|
||||
try {
|
||||
lasd::List<string> lst;
|
||||
Empty(loctestnum, loctesterr, lst, true);
|
||||
Size(loctestnum, loctesterr, lst, true, 0);
|
||||
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, string("A"));
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, string("B"));
|
||||
|
||||
GetFront(loctestnum, loctesterr, lst, true, string("A"));
|
||||
GetBack(loctestnum, loctesterr, lst, true, string("B"));
|
||||
|
||||
Exists(loctestnum, loctesterr, lst, true, string("B"));
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapStringAppend, string(" "));
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapPrint<string>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, lst, true, &FoldStringConcatenate, string(""), string("X"), string("XA B "));
|
||||
FoldPostOrder(loctestnum, loctesterr, lst, true, &FoldStringConcatenate, string(""), string("X"), string("XB A "));
|
||||
|
||||
Exists(loctestnum, loctesterr, lst, false, string("B"));
|
||||
|
||||
lasd::List<string> coplst(lst);
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
RemoveFromFront(loctestnum, loctesterr, coplst, true);
|
||||
NonEqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
|
||||
lst = coplst;
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, string("A"));
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, string("C"));
|
||||
NonEqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
|
||||
coplst = move(lst);
|
||||
FoldPreOrder(loctestnum, loctesterr, coplst, true, &FoldStringConcatenate, string(""), string("?"), string("?CB A"));
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of List<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestList(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
stestListInt(loctestnum, loctesterr);
|
||||
stestListDouble(loctestnum, loctesterr);
|
||||
stestListString(loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << endl << "Exercise 1 - List (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void stestVectorListInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Vector/List<int> Test:" << endl;
|
||||
try {
|
||||
lasd::Vector<int> vec(3);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, -1);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, 0);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, 1);
|
||||
|
||||
lasd::List<int> lst;
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 1);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 0);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, -1);
|
||||
|
||||
lasd::Vector<int> copvec(lst);
|
||||
EqualVector(loctestnum, loctesterr, vec, copvec, true);
|
||||
lasd::Vector<int> copvecx(vec);
|
||||
EqualVector(loctestnum, loctesterr, copvecx, copvec, true);
|
||||
|
||||
lasd::List<int> coplst(vec);
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
lasd::List<int> coplstx(lst);
|
||||
EqualList(loctestnum, loctesterr, coplstx, coplst, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Vector/List<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestVectorListDouble(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Vector/List<double> Test:" << endl;
|
||||
try {
|
||||
lasd::Vector<double> vec(3);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, -0.5);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, 0.0);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, 0.5);
|
||||
|
||||
lasd::List<double> lst;
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, -0.5);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 0.0);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 0.5);
|
||||
|
||||
lasd::Vector<double> copvec(lst);
|
||||
EqualVector(loctestnum, loctesterr, vec, copvec, true);
|
||||
lasd::Vector<double> copvecx(vec);
|
||||
EqualVector(loctestnum, loctesterr, copvecx, copvec, true);
|
||||
|
||||
lasd::List<double> coplst(vec);
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
lasd::List<double> coplstx(lst);
|
||||
EqualList(loctestnum, loctesterr, coplstx, coplst, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Vector/List<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestVectorListString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Vector/List<string> Test:" << endl;
|
||||
try {
|
||||
lasd::Vector<string> vec(3);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("A"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, string("B"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, string("C"));
|
||||
|
||||
lasd::List<string> lst;
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, string("B"));
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, string("C"));
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, string("A"));
|
||||
|
||||
lasd::Vector<string> copvec(lst);
|
||||
EqualVector(loctestnum, loctesterr, vec, copvec, true);
|
||||
lasd::Vector<string> copvecx(vec);
|
||||
EqualVector(loctestnum, loctesterr, copvecx, copvec, true);
|
||||
|
||||
lasd::List<string> coplst(vec);
|
||||
EqualList(loctestnum, loctesterr, lst, coplst, true);
|
||||
lasd::List<string> coplstx(lst);
|
||||
EqualList(loctestnum, loctesterr, coplstx, coplst, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Vector/List<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestVectorList(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
stestVectorListInt(loctestnum, loctesterr);
|
||||
stestVectorListDouble(loctestnum, loctesterr);
|
||||
stestVectorListString(loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << endl << "Exercise 1 - Vector/List (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise1() {
|
||||
uint testnum = 0, testerr = 0;
|
||||
stestVector(testnum, testerr);
|
||||
stestList(testnum, testerr);
|
||||
stestVectorList(testnum, testerr);
|
||||
cout << endl << "Exercise 1 (Simple Test) (Errors/Tests: " << testerr << "/" << testnum << ")" << endl;
|
||||
}
|
13
librerie/exercise5/zlasdtest/exercise1/test.hpp
Normal file
13
librerie/exercise5/zlasdtest/exercise1/test.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef EX1TEST_HPP
|
||||
#define EX1TEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise1();
|
||||
|
||||
void testFullExercise1();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
3
librerie/exercise5/zlasdtest/exercise2/fulltest.cpp
Normal file
3
librerie/exercise5/zlasdtest/exercise2/fulltest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
void testFullExercise2() {
|
||||
}
|
359
librerie/exercise5/zlasdtest/exercise2/simpletest.cpp
Normal file
359
librerie/exercise5/zlasdtest/exercise2/simpletest.cpp
Normal file
@ -0,0 +1,359 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../stack/stack.hpp"
|
||||
#include "../../stack/vec/stackvec.hpp"
|
||||
#include "../../stack/lst/stacklst.hpp"
|
||||
|
||||
#include "../queue/queue.hpp"
|
||||
#include "../../queue/vec/queuevec.hpp"
|
||||
#include "../../queue/lst/queuelst.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Stk>
|
||||
void stestStackInt(Stk& stk, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
Empty(loctestnum, loctesterr, stk, true);
|
||||
Size(loctestnum, loctesterr, stk, true, 0);
|
||||
|
||||
Top(loctestnum, loctesterr, stk, false, 0);
|
||||
TopNPop(loctestnum, loctesterr, stk, false, 0);
|
||||
|
||||
PushC(loctestnum, loctesterr, stk, 4);
|
||||
PushC(loctestnum, loctesterr, stk, 0);
|
||||
PushC(loctestnum, loctesterr, stk, 3);
|
||||
PushC(loctestnum, loctesterr, stk, 1);
|
||||
PushC(loctestnum, loctesterr, stk, 2);
|
||||
|
||||
Empty(loctestnum, loctesterr, stk, false);
|
||||
Size(loctestnum, loctesterr, stk, true, 5);
|
||||
|
||||
TopNPop(loctestnum, loctesterr, stk, true, 2);
|
||||
Top(loctestnum, loctesterr, stk, true, 1);
|
||||
|
||||
Stk copstk(stk);
|
||||
EqualStack(loctestnum, loctesterr, stk, copstk, true);
|
||||
PushC(loctestnum, loctesterr, stk, 5);
|
||||
NonEqualStack(loctestnum, loctesterr, stk, copstk, true);
|
||||
|
||||
copstk = stk;
|
||||
EqualStack(loctestnum, loctesterr, stk, copstk, true);
|
||||
PushC(loctestnum, loctesterr, copstk, 6);
|
||||
NonEqualStack(loctestnum, loctesterr, stk, copstk, true);
|
||||
|
||||
Top(loctestnum, loctesterr, copstk, true, 6);
|
||||
copstk = move(stk);
|
||||
TopNPop(loctestnum, loctesterr, copstk, true, 5);
|
||||
Pop(loctestnum, loctesterr, copstk, true);
|
||||
Top(loctestnum, loctesterr, copstk, true, 3);
|
||||
|
||||
Stk movstk(move(stk));
|
||||
Top(loctestnum, loctesterr, stk, false, 0);
|
||||
|
||||
movstk.Clear();
|
||||
Pop(loctestnum, loctesterr, movstk, false);
|
||||
Empty(loctestnum, loctesterr, movstk, true);
|
||||
Size(loctestnum, loctesterr, movstk, true, 0);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << "End of Stack<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
void stestStackInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
lasd::StackVec<int> stkvec;
|
||||
cout << endl << "Begin of StackVec<int> Test:" << endl;
|
||||
stestStackInt(stkvec, loctestnum, loctesterr);
|
||||
lasd::StackLst<int> stklst;
|
||||
cout << endl << "Begin of StackLst<int> Test:" << endl;
|
||||
stestStackInt(stklst, loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
template <typename Stk>
|
||||
void stestStackFloat(Stk& stk, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
PushC(loctestnum, loctesterr, stk, 5.9);
|
||||
PushC(loctestnum, loctesterr, stk, 4.4);
|
||||
PushC(loctestnum, loctesterr, stk, 9.5);
|
||||
|
||||
Empty(loctestnum, loctesterr, stk, false);
|
||||
Size(loctestnum, loctesterr, stk, true, 3);
|
||||
|
||||
TopNPop(loctestnum, loctesterr, stk, true, 9.5);
|
||||
Top(loctestnum, loctesterr, stk, true, 4.4);
|
||||
Pop(loctestnum, loctesterr, stk, true);
|
||||
TopNPop(loctestnum, loctesterr, stk, true, 5.9);
|
||||
Pop(loctestnum, loctesterr, stk, false);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << "End of Stack<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
void stestStackFloat(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
lasd::StackVec<double> stkvec;
|
||||
cout << endl << "Begin of StackVec<double> Test:" << endl;
|
||||
stestStackFloat(stkvec, loctestnum, loctesterr);
|
||||
lasd::StackLst<double> stklst;
|
||||
cout << endl << "Begin of StackLst<double> Test:" << endl;
|
||||
stestStackFloat(stklst, loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
template <typename Stk>
|
||||
void stestStackString(Stk& stk, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
PushM(loctestnum, loctesterr, stk, string("A"));
|
||||
PushM(loctestnum, loctesterr, stk, string("B"));
|
||||
|
||||
Empty(loctestnum, loctesterr, stk, false);
|
||||
Size(loctestnum, loctesterr, stk, true, 2);
|
||||
|
||||
TopNPop(loctestnum, loctesterr, stk, true, string("B"));
|
||||
Top(loctestnum, loctesterr, stk, true, string("A"));
|
||||
Pop(loctestnum, loctesterr, stk, true);
|
||||
Pop(loctestnum, loctesterr, stk, false);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << "End of Stack<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
void stestStackString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
lasd::StackVec<string> stkvec;
|
||||
cout << endl << "Begin of StackVec<string> Test:" << endl;
|
||||
stestStackString(stkvec, loctestnum, loctesterr);
|
||||
lasd::StackLst<string> stklst;
|
||||
cout << endl << "Begin of StackLst<string> Test:" << endl;
|
||||
stestStackString(stklst, loctestnum, loctesterr);
|
||||
cout << endl;
|
||||
try {
|
||||
lasd::Vector<string> vec(2);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("A"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, string("B"));
|
||||
|
||||
PushM(loctestnum, loctesterr, stkvec, string("A"));
|
||||
PushM(loctestnum, loctesterr, stkvec, string("B"));
|
||||
lasd::StackVec<string> newstkvec(vec);
|
||||
EqualStack(loctestnum, loctesterr, stkvec, newstkvec, true);
|
||||
|
||||
PushM(loctestnum, loctesterr, stklst, string("B"));
|
||||
PushM(loctestnum, loctesterr, stklst, string("A"));
|
||||
lasd::StackLst<string> newstklst(vec);
|
||||
EqualStack(loctestnum, loctesterr, stklst, newstklst, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestStack(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
stestStackInt(loctestnum, loctesterr);
|
||||
stestStackFloat(loctestnum, loctesterr);
|
||||
stestStackString(loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << endl << "Exercise 2 - Stack (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Que>
|
||||
void stestQueueInt(Que& que, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
Empty(loctestnum, loctesterr, que, true);
|
||||
Size(loctestnum, loctesterr, que, true, 0);
|
||||
|
||||
Head(loctestnum, loctesterr, que, false, 0);
|
||||
HeadNDequeue(loctestnum, loctesterr, que, false, 0);
|
||||
|
||||
EnqueueC(loctestnum, loctesterr, que, 4);
|
||||
EnqueueC(loctestnum, loctesterr, que, 0);
|
||||
EnqueueC(loctestnum, loctesterr, que, 3);
|
||||
EnqueueC(loctestnum, loctesterr, que, 1);
|
||||
EnqueueC(loctestnum, loctesterr, que, 2);
|
||||
|
||||
Empty(loctestnum, loctesterr, que, false);
|
||||
Size(loctestnum, loctesterr, que, true, 5);
|
||||
|
||||
HeadNDequeue(loctestnum, loctesterr, que, true, 4);
|
||||
Head(loctestnum, loctesterr, que, true, 0);
|
||||
|
||||
Que copque(que);
|
||||
EqualQueue(loctestnum, loctesterr, que, copque, true);
|
||||
EnqueueC(loctestnum, loctesterr, que, 5);
|
||||
NonEqualQueue(loctestnum, loctesterr, que, copque, true);
|
||||
|
||||
copque = que;
|
||||
EqualQueue(loctestnum, loctesterr, que, copque, true);
|
||||
EnqueueC(loctestnum, loctesterr, copque, 6);
|
||||
NonEqualQueue(loctestnum, loctesterr, que, copque, true);
|
||||
|
||||
Head(loctestnum, loctesterr, copque, true, 0);
|
||||
copque = move(que);
|
||||
HeadNDequeue(loctestnum, loctesterr, copque, true, 0);
|
||||
Dequeue(loctestnum, loctesterr, copque, true);
|
||||
Head(loctestnum, loctesterr, copque, true, 1);
|
||||
|
||||
Que movque(move(que));
|
||||
Head(loctestnum, loctesterr, que, false, 0);
|
||||
|
||||
movque.Clear();
|
||||
Dequeue(loctestnum, loctesterr, movque, false);
|
||||
Empty(loctestnum, loctesterr, movque, true);
|
||||
Size(loctestnum, loctesterr, movque, true, 0);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << "End of Queue<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
void stestQueueInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
lasd::QueueVec<int> quevec;
|
||||
cout << endl << "Begin of QueueVec<int> Test:" << endl;
|
||||
stestQueueInt(quevec, loctestnum, loctesterr);
|
||||
lasd::QueueLst<int> quelst;
|
||||
cout << endl << "Begin of QueueLst<int> Test:" << endl;
|
||||
stestQueueInt(quelst, loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
template <typename Que>
|
||||
void stestQueueFloat(Que& que, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
EnqueueC(loctestnum, loctesterr, que, 5.9);
|
||||
EnqueueC(loctestnum, loctesterr, que, 4.4);
|
||||
EnqueueC(loctestnum, loctesterr, que, 9.5);
|
||||
|
||||
Empty(loctestnum, loctesterr, que, false);
|
||||
Size(loctestnum, loctesterr, que, true, 3);
|
||||
|
||||
HeadNDequeue(loctestnum, loctesterr, que, true, 5.9);
|
||||
Head(loctestnum, loctesterr, que, true, 4.4);
|
||||
Dequeue(loctestnum, loctesterr, que, true);
|
||||
HeadNDequeue(loctestnum, loctesterr, que, true, 9.5);
|
||||
Dequeue(loctestnum, loctesterr, que, false);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << "End of Queue<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
void stestQueueFloat(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
lasd::QueueVec<double> quevec;
|
||||
cout << endl << "Begin of QueueVec<double> Test:" << endl;
|
||||
stestQueueFloat(quevec, loctestnum, loctesterr);
|
||||
lasd::QueueLst<double> quelst;
|
||||
cout << endl << "Begin of QueueLst<double> Test:" << endl;
|
||||
stestQueueFloat(quelst, loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
template <typename Que>
|
||||
void stestQueueString(Que& que, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
EnqueueM(loctestnum, loctesterr, que, string("A"));
|
||||
EnqueueM(loctestnum, loctesterr, que, string("B"));
|
||||
|
||||
Empty(loctestnum, loctesterr, que, false);
|
||||
Size(loctestnum, loctesterr, que, true, 2);
|
||||
|
||||
HeadNDequeue(loctestnum, loctesterr, que, true, string("A"));
|
||||
Head(loctestnum, loctesterr, que, true, string("B"));
|
||||
Dequeue(loctestnum, loctesterr, que, true);
|
||||
Dequeue(loctestnum, loctesterr, que, false);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << "End of Queue<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
void stestQueueString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
lasd::QueueVec<string> quevec;
|
||||
cout << endl << "Begin of QueueVec<string> Test:" << endl;
|
||||
stestQueueString(quevec, loctestnum, loctesterr);
|
||||
lasd::QueueLst<string> quelst;
|
||||
cout << endl << "Begin of QueueLst<string> Test:" << endl;
|
||||
stestQueueString(quelst, loctestnum, loctesterr);
|
||||
cout << endl;
|
||||
try {
|
||||
lasd::Vector<string> vec(2);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("A"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, string("B"));
|
||||
|
||||
EnqueueM(loctestnum, loctesterr, quevec, string("A"));
|
||||
EnqueueM(loctestnum, loctesterr, quevec, string("B"));
|
||||
lasd::QueueVec<string> newquevec(vec);
|
||||
EqualQueue(loctestnum, loctesterr, quevec, newquevec, true);
|
||||
|
||||
EnqueueM(loctestnum, loctesterr, quelst, string("A"));
|
||||
EnqueueM(loctestnum, loctesterr, quelst, string("B"));
|
||||
lasd::QueueLst<string> newquelst(vec);
|
||||
EqualQueue(loctestnum, loctesterr, quelst, newquelst, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestQueue(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
stestQueueInt(loctestnum, loctesterr);
|
||||
stestQueueFloat(loctestnum, loctesterr);
|
||||
stestQueueString(loctestnum, loctesterr);
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
cout << endl << "Exercise 2 - Queue (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise2() {
|
||||
uint testnum = 0, testerr = 0;
|
||||
stestStack(testnum, testerr);
|
||||
stestQueue(testnum, testerr);
|
||||
cout << endl << "Exercise 2 (Simple Test) (Errors/Tests: " << testerr << "/" << testnum << ")" << endl;
|
||||
}
|
13
librerie/exercise5/zlasdtest/exercise2/test.hpp
Normal file
13
librerie/exercise5/zlasdtest/exercise2/test.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef EX2TEST_HPP
|
||||
#define EX2TEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise2();
|
||||
|
||||
void testFullExercise2();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
3
librerie/exercise5/zlasdtest/exercise3/fulltest.cpp
Normal file
3
librerie/exercise5/zlasdtest/exercise3/fulltest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
void testFullExercise3() {
|
||||
}
|
234
librerie/exercise5/zlasdtest/exercise3/simpletest.cpp
Normal file
234
librerie/exercise5/zlasdtest/exercise3/simpletest.cpp
Normal file
@ -0,0 +1,234 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../iterator/iterator.hpp"
|
||||
|
||||
#include "../vector/vector.hpp"
|
||||
|
||||
#include "../list/list.hpp"
|
||||
|
||||
#include "../binarytree/binarytree.hpp"
|
||||
#include "../../binarytree/lnk/binarytreelnk.hpp"
|
||||
#include "../../binarytree/vec/binarytreevec.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void stestBinaryTreeInt(lasd::BinaryTree<int>& bt, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
MapPreOrder(loctestnum, loctesterr, bt, true, &MapPrint<int>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, bt, true, &FoldAdd<int>, 0, 0, 6);
|
||||
FoldPostOrder(loctestnum, loctesterr, bt, true, &FoldAdd<int>, 0, 0, 6);
|
||||
FoldInOrder(loctestnum, loctesterr, bt, true, &FoldAdd<int>, 0, 0, 6);
|
||||
FoldBreadth(loctestnum, loctesterr, bt, true, &FoldAdd<int>, 0, 0, 6);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BinaryTree<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBinaryTreeInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of BinaryTree<int> Test" << endl;
|
||||
try {
|
||||
lasd::Vector<int> vec(4);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, 0);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, 1);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, 2);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 3, 3);
|
||||
|
||||
lasd::BinaryTreeVec<int> btvec(vec);
|
||||
cout << endl << "Begin of BinaryTreeVec<int> Test:" << endl;
|
||||
stestBinaryTreeInt(btvec, loctestnum, loctesterr);
|
||||
lasd::BinaryTreeLnk<int> btlnk(vec);
|
||||
cout << endl << "Begin of BinaryTreeLnk<int> Test:" << endl;
|
||||
stestBinaryTreeInt(btlnk, loctestnum, loctesterr);
|
||||
cout << "\n";
|
||||
|
||||
lasd::BinaryTreeVec<int> copbtvec(btvec);
|
||||
EqualBT(loctestnum, loctesterr, copbtvec, btvec);
|
||||
btvec.Clear();
|
||||
btvec = move(copbtvec);
|
||||
NonEqualBT(loctestnum, loctesterr, copbtvec, btvec);
|
||||
Empty(loctestnum, loctesterr, copbtvec, true);
|
||||
|
||||
lasd::BinaryTreeLnk<int> copbtlnk(btlnk);
|
||||
EqualBT(loctestnum, loctesterr, copbtlnk, btlnk);
|
||||
btlnk.Clear();
|
||||
btlnk = move(copbtlnk);
|
||||
NonEqualBT(loctestnum, loctesterr, copbtlnk, btlnk);
|
||||
Empty(loctestnum, loctesterr, copbtlnk, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BinaryTree<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBinaryTreeFloat(lasd::BinaryTree<double>& bt, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
lasd::BTPreOrderIterator<double> itr1(bt);
|
||||
GetItrValue(loctestnum, loctesterr, itr1, true, 1.2);
|
||||
++itr1;
|
||||
GetItrValue(loctestnum, loctesterr, itr1, true, 0.3);
|
||||
++itr1;
|
||||
GetItrValue(loctestnum, loctesterr, itr1, true, 2.1);
|
||||
++itr1;
|
||||
GetItrValue(loctestnum, loctesterr, itr1, true, 3.0);
|
||||
++itr1;
|
||||
Terminated(loctestnum, loctesterr, itr1, true);
|
||||
|
||||
lasd::BTBreadthIterator<double> itr2(bt);
|
||||
GetItrValue(loctestnum, loctesterr, itr2, true, 1.2);
|
||||
++itr2;
|
||||
GetItrValue(loctestnum, loctesterr, itr2, true, 0.3);
|
||||
++itr2;
|
||||
GetItrValue(loctestnum, loctesterr, itr2, true, 3.0);
|
||||
++itr2;
|
||||
GetItrValue(loctestnum, loctesterr, itr2, true, 2.1);
|
||||
++itr2;
|
||||
Terminated(loctestnum, loctesterr, itr2, true);
|
||||
|
||||
lasd::BTInOrderIterator<double> itr3(bt);
|
||||
GetItrValue(loctestnum, loctesterr, itr3, true, 2.1);
|
||||
++itr3;
|
||||
GetItrValue(loctestnum, loctesterr, itr3, true, 0.3);
|
||||
++itr3;
|
||||
GetItrValue(loctestnum, loctesterr, itr3, true, 1.2);
|
||||
++itr3;
|
||||
GetItrValue(loctestnum, loctesterr, itr3, true, 3.0);
|
||||
++itr3;
|
||||
Terminated(loctestnum, loctesterr, itr3, true);
|
||||
|
||||
lasd::BTPostOrderIterator<double> itr4(bt);
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, 2.1);
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, 0.3);
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, 3.0);
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, 1.2);
|
||||
++itr4;
|
||||
Terminated(loctestnum, loctesterr, itr4, true);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BinaryTree<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBinaryTreeFloat(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of BinaryTree<double> Test" << endl;
|
||||
try {
|
||||
lasd::List<double> lst;
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 0.3);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 3.0);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 1.2);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 2.1);
|
||||
|
||||
lasd::BinaryTreeVec<double> btvec(lst);
|
||||
cout << endl << "Begin of BinaryTreeVec<double> Test:" << endl;
|
||||
stestBinaryTreeFloat(btvec, loctestnum, loctesterr);
|
||||
lasd::BinaryTreeLnk<double> btlnk(lst);
|
||||
cout << endl << "Begin of BinaryTreeLnk<double> Test:" << endl;
|
||||
stestBinaryTreeFloat(btlnk, loctestnum, loctesterr);
|
||||
cout << "\n";
|
||||
|
||||
lasd::BinaryTreeVec<double> copbtvec(move(btvec));
|
||||
Empty(loctestnum, loctesterr, btvec, true);
|
||||
NonEqualBT(loctestnum, loctesterr, copbtvec, btvec);
|
||||
btvec = copbtvec;
|
||||
EqualBT(loctestnum, loctesterr, copbtvec, btvec);
|
||||
|
||||
lasd::BinaryTreeLnk<double> copbtlnk(move(btlnk));
|
||||
Empty(loctestnum, loctesterr, btlnk, true);
|
||||
NonEqualBT(loctestnum, loctesterr, copbtlnk, btlnk);
|
||||
btlnk = copbtlnk;
|
||||
EqualBT(loctestnum, loctesterr, copbtlnk, btlnk);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BinaryTree<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBinaryTreeString(lasd::BinaryTree<string>& bt, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
MapPreOrder(loctestnum, loctesterr, bt, true, &MapPrint<string>, 0);
|
||||
FoldPreOrder(loctestnum, loctesterr, bt, true, &FoldStringConcatenate, string(""), string("?"), string("?ABDC"));
|
||||
FoldPostOrder(loctestnum, loctesterr, bt, true, &FoldStringConcatenate, string(""), string("?"), string("?DBCA"));
|
||||
FoldInOrder(loctestnum, loctesterr, bt, true, &FoldStringConcatenate, string(""), string("?"), string("?DBAC"));
|
||||
FoldBreadth(loctestnum, loctesterr, bt, true, &FoldStringConcatenate, string(""), string("?"), string("?ABCD"));
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BinaryTree<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBinaryTreeString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of BinaryTree<string> Test" << endl;
|
||||
try {
|
||||
lasd::Vector<string> vec(4);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("A"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, string("B"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, string("C"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 3, string("D"));
|
||||
|
||||
cout << endl << "Begin of BinaryTreeVec<string> Test:" << endl;
|
||||
lasd::BinaryTreeVec<string> btvec(vec);
|
||||
stestBinaryTreeString(btvec, loctestnum, loctesterr);
|
||||
cout << endl << "Begin of BinaryTreeVec<string> Test:" << endl;
|
||||
lasd::BinaryTreeLnk<string> btlnk(vec);
|
||||
stestBinaryTreeString(btlnk, loctestnum, loctesterr);
|
||||
cout << "\n";
|
||||
|
||||
EqualBT(loctestnum, loctesterr, btvec, btlnk);
|
||||
btvec.Root().LeftChild().LeftChild().Element() = string("S");
|
||||
NonEqualBT(loctestnum, loctesterr, btlnk, btvec);
|
||||
btlnk.Root().RightChild().Element() = string("X");
|
||||
NonEqualBT(loctestnum, loctesterr, btvec, btlnk);
|
||||
btvec.Root().RightChild().Element() = string("X");
|
||||
btlnk.Root().LeftChild().LeftChild().Element() = string("S");
|
||||
EqualBT(loctestnum, loctesterr, btlnk, btvec);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BinaryTree<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise3() {
|
||||
uint testnum = 0, testerr = 0;
|
||||
stestBinaryTreeInt(testnum, testerr);
|
||||
stestBinaryTreeFloat(testnum, testerr);
|
||||
stestBinaryTreeString(testnum, testerr);
|
||||
cout << endl << "Exercise 3 (Simple Test) (Errors/Tests: " << testerr << "/" << testnum << ")" << endl;
|
||||
}
|
13
librerie/exercise5/zlasdtest/exercise3/test.hpp
Normal file
13
librerie/exercise5/zlasdtest/exercise3/test.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef EX3TEST_HPP
|
||||
#define EX3TEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise3();
|
||||
|
||||
void testFullExercise3();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
3
librerie/exercise5/zlasdtest/exercise4/fulltest.cpp
Normal file
3
librerie/exercise5/zlasdtest/exercise4/fulltest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
void testFullExercise4() {
|
||||
}
|
323
librerie/exercise5/zlasdtest/exercise4/simpletest.cpp
Normal file
323
librerie/exercise5/zlasdtest/exercise4/simpletest.cpp
Normal file
@ -0,0 +1,323 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../iterator/iterator.hpp"
|
||||
|
||||
#include "../vector/vector.hpp"
|
||||
|
||||
#include "../list/list.hpp"
|
||||
|
||||
#include "../binarytree/binarytree.hpp"
|
||||
|
||||
#include "../bst/bst.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void stestBSTInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of BST<int> Test" << endl;
|
||||
try {
|
||||
lasd::BST<int> bst;
|
||||
|
||||
Empty(loctestnum, loctesterr, bst, true);
|
||||
Size(loctestnum, loctesterr, bst, true, 0);
|
||||
|
||||
Min(loctestnum, loctesterr, bst, false, 0);
|
||||
Max(loctestnum, loctesterr, bst, false, 0);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst, 3);
|
||||
InsertC(loctestnum, loctesterr, bst, 1);
|
||||
InsertC(loctestnum, loctesterr, bst, 4);
|
||||
InsertC(loctestnum, loctesterr, bst, 5);
|
||||
InsertC(loctestnum, loctesterr, bst, 0);
|
||||
InsertC(loctestnum, loctesterr, bst, 2);
|
||||
|
||||
Empty(loctestnum, loctesterr, bst, false);
|
||||
Size(loctestnum, loctesterr, bst, true, 6);
|
||||
|
||||
MapInOrder(loctestnum, loctesterr, bst, true, &MapPrint<int>, 0);
|
||||
|
||||
Min(loctestnum, loctesterr, bst, true, 0);
|
||||
Max(loctestnum, loctesterr, bst, true, 5);
|
||||
|
||||
Root(loctestnum, loctesterr, bst, true, 3);
|
||||
|
||||
RemoveMin(loctestnum, loctesterr, bst, true);
|
||||
MinNRemove(loctestnum, loctesterr, bst, true, 1);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst, -1);
|
||||
InsertC(loctestnum, loctesterr, bst, 1);
|
||||
|
||||
Min(loctestnum, loctesterr, bst, true, -1);
|
||||
MaxNRemove(loctestnum, loctesterr, bst, true, 5);
|
||||
Size(loctestnum, loctesterr, bst, true, 5);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst, 6);
|
||||
|
||||
Size(loctestnum, loctesterr, bst, true, 6);
|
||||
|
||||
Max(loctestnum, loctesterr, bst, true, 6);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst, 7);
|
||||
|
||||
Size(loctestnum, loctesterr, bst, true, 7);
|
||||
|
||||
Exists(loctestnum, loctesterr, bst, true, 6);
|
||||
Exists(loctestnum, loctesterr, bst, false, 8);
|
||||
Exists(loctestnum, loctesterr, bst, false, 0);
|
||||
Exists(loctestnum, loctesterr, bst, true, -1);
|
||||
Exists(loctestnum, loctesterr, bst, true, 2);
|
||||
|
||||
Remove(loctestnum, loctesterr, bst, 5);
|
||||
Remove(loctestnum, loctesterr, bst, 2);
|
||||
|
||||
Exists(loctestnum, loctesterr, bst, false, 5);
|
||||
Exists(loctestnum, loctesterr, bst, false, 2);
|
||||
|
||||
RemoveMax(loctestnum, loctesterr, bst, true);
|
||||
Max(loctestnum, loctesterr, bst, true, 6);
|
||||
|
||||
MapInOrder(loctestnum, loctesterr, bst, true, &MapPrint<int>, 0);
|
||||
|
||||
Predecessor(loctestnum, loctesterr, bst, true, 4, 3);
|
||||
Predecessor(loctestnum, loctesterr, bst, true, 5, 4);
|
||||
|
||||
Successor(loctestnum, loctesterr, bst, true, 2, 3);
|
||||
Successor(loctestnum, loctesterr, bst, true, 4, 6);
|
||||
|
||||
PredecessorNRemove(loctestnum, loctesterr, bst, true, 7, 6);
|
||||
SuccessorNRemove(loctestnum, loctesterr, bst, true, 0, 1);
|
||||
|
||||
FoldPreOrder(loctestnum, loctesterr, bst, true, &FoldAdd<int>, 0, 0, 6);
|
||||
FoldInOrder(loctestnum, loctesterr, bst, true, &FoldAdd<int>, 0, 0, 6);
|
||||
FoldPostOrder(loctestnum, loctesterr, bst, true, &FoldAdd<int>, 0, 0, 6);
|
||||
FoldBreadth(loctestnum, loctesterr, bst, true, &FoldAdd<int>, 0, 0, 6);
|
||||
|
||||
lasd::BST<int> bst1(bst);
|
||||
|
||||
EqualBST(loctestnum, loctesterr, bst, bst1);
|
||||
|
||||
Remove(loctestnum, loctesterr, bst1, 4);
|
||||
|
||||
NonEqualBST(loctestnum, loctesterr, bst, bst1);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst1, 4);
|
||||
|
||||
EqualBST(loctestnum, loctesterr, bst, bst1);
|
||||
|
||||
lasd::BST<int> bst2 = bst1;
|
||||
|
||||
EqualBST(loctestnum, loctesterr, bst1, bst2);
|
||||
|
||||
RemovePredecessor(loctestnum, loctesterr, bst1, true, 9);
|
||||
|
||||
NonEqualBST(loctestnum, loctesterr, bst1, bst2);
|
||||
|
||||
lasd::BST<int> bst3(std::move(bst2));
|
||||
|
||||
Empty(loctestnum, loctesterr, bst2, true);
|
||||
Size(loctestnum, loctesterr, bst2, true, 0);
|
||||
|
||||
Empty(loctestnum, loctesterr, bst3, false);
|
||||
Size(loctestnum, loctesterr, bst3, true, 3);
|
||||
|
||||
bst2 = std::move(bst1);
|
||||
|
||||
Empty(loctestnum, loctesterr, bst1, true);
|
||||
Size(loctestnum, loctesterr, bst1, true, 0);
|
||||
|
||||
Empty(loctestnum, loctesterr, bst2, false);
|
||||
Size(loctestnum, loctesterr, bst2, true, 2);
|
||||
|
||||
NonEqualBST(loctestnum, loctesterr, bst3, bst2);
|
||||
|
||||
MapInOrder(loctestnum, loctesterr, bst2, true, &MapPrint<int>, 0);
|
||||
MapInOrder(loctestnum, loctesterr, bst3, true, &MapPrint<int>, 0);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst2, 4);
|
||||
|
||||
EqualBST(loctestnum, loctesterr, bst3, bst2);
|
||||
|
||||
bst.Clear();
|
||||
|
||||
Empty(loctestnum, loctesterr, bst, true);
|
||||
Size(loctestnum, loctesterr, bst, true, 0);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BST<int> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBSTFloat(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of BST<double> Test" << endl;
|
||||
try {
|
||||
lasd::List<double> lst;
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 4.0);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 0.4);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 1.2);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 2.1);
|
||||
InsertAtFront(loctestnum, loctesterr, lst, true, 3.5);
|
||||
InsertAtBack(loctestnum, loctesterr, lst, true, 5.3);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, lst, true, &MapPrint<double>, 0);
|
||||
|
||||
lasd::BST<double> bst1(lst);
|
||||
|
||||
Empty(loctestnum, loctesterr, bst1, false);
|
||||
Size(loctestnum, loctesterr, bst1, true, 6);
|
||||
|
||||
MapPreOrder(loctestnum, loctesterr, bst1, true, &MapPrint<double>, 0);
|
||||
MapInOrder(loctestnum, loctesterr, bst1, true, &MapPrint<double>, 0);
|
||||
MapPostOrder(loctestnum, loctesterr, bst1, true, &MapPrint<double>, 0);
|
||||
MapBreadth(loctestnum, loctesterr, bst1, true, &MapPrint<double>, 0);
|
||||
|
||||
Root(loctestnum, loctesterr, bst1, true, 3.5);
|
||||
|
||||
lasd::BST<double> bst2;
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst2, 2.1);
|
||||
InsertC(loctestnum, loctesterr, bst2, 0.4);
|
||||
InsertC(loctestnum, loctesterr, bst2, 1.2);
|
||||
InsertC(loctestnum, loctesterr, bst2, 3.5);
|
||||
InsertC(loctestnum, loctesterr, bst2, 5.3);
|
||||
InsertC(loctestnum, loctesterr, bst2, 4.0);
|
||||
|
||||
Root(loctestnum, loctesterr, bst2, true, 2.1);
|
||||
|
||||
EqualBST(loctestnum, loctesterr, bst1, bst2);
|
||||
NonEqualBT(loctestnum, loctesterr, bst1, bst2);
|
||||
|
||||
bst1.Clear();
|
||||
bst2.Clear();
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst1, 0.2);
|
||||
InsertC(loctestnum, loctesterr, bst1, 1.1);
|
||||
InsertC(loctestnum, loctesterr, bst1, 2.1);
|
||||
|
||||
InsertC(loctestnum, loctesterr, bst2, 2.1);
|
||||
InsertC(loctestnum, loctesterr, bst2, 1.1);
|
||||
InsertC(loctestnum, loctesterr, bst2, 0.2);
|
||||
|
||||
EqualBST(loctestnum, loctesterr, bst1, bst2);
|
||||
NonEqualBT(loctestnum, loctesterr, bst1, bst2);
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BST<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestBSTString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of BST<string> Test" << endl;
|
||||
try {
|
||||
lasd::Vector<string> vec(5);
|
||||
SetAt(loctestnum, loctesterr, vec, true, 0, string("B"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 1, string("A"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 2, string("C"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 3, string("D"));
|
||||
SetAt(loctestnum, loctesterr, vec, true, 4, string("E"));
|
||||
|
||||
lasd::BST<string> bst(vec);
|
||||
Size(loctestnum, loctesterr, bst, true, 5);
|
||||
|
||||
// MapPreOrder(loctestnum, loctesterr, bst, true, &MapPrint<string>, 0);
|
||||
//
|
||||
// lasd::BTPreOrderIterator<string> itr1(bst);
|
||||
// GetItrValue(loctestnum, loctesterr, itr1, true, string("B"));
|
||||
// ++itr1;
|
||||
// GetItrValue(loctestnum, loctesterr, itr1, true, string("A"));
|
||||
// ++itr1;
|
||||
// GetItrValue(loctestnum, loctesterr, itr1, true, string("C"));
|
||||
// ++itr1;
|
||||
// GetItrValue(loctestnum, loctesterr, itr1, true, string("D"));
|
||||
// ++itr1;
|
||||
// GetItrValue(loctestnum, loctesterr, itr1, true, string("E"));
|
||||
// Terminated(loctestnum, loctesterr, itr1, false);
|
||||
// ++itr1;
|
||||
// Terminated(loctestnum, loctesterr, itr1, true);
|
||||
//
|
||||
// MapPostOrder(loctestnum, loctesterr, bst, true, &MapPrint<string>, 0);
|
||||
//
|
||||
// lasd::BTPostOrderIterator<string> itr2(bst);
|
||||
// GetItrValue(loctestnum, loctesterr, itr2, true, string("A"));
|
||||
// ++itr2;
|
||||
// GetItrValue(loctestnum, loctesterr, itr2, true, string("E"));
|
||||
// ++itr2;
|
||||
// GetItrValue(loctestnum, loctesterr, itr2, true, string("D"));
|
||||
// ++itr2;
|
||||
// GetItrValue(loctestnum, loctesterr, itr2, true, string("C"));
|
||||
// ++itr2;
|
||||
// GetItrValue(loctestnum, loctesterr, itr2, true, string("B"));
|
||||
// Terminated(loctestnum, loctesterr, itr2, false);
|
||||
// ++itr2;
|
||||
// Terminated(loctestnum, loctesterr, itr2, true);
|
||||
//
|
||||
// MapBreadth(loctestnum, loctesterr, bst, true, &MapPrint<string>, 0);
|
||||
//
|
||||
// lasd::BTBreadthIterator<string> itr3(bst);
|
||||
// GetItrValue(loctestnum, loctesterr, itr3, true, string("B"));
|
||||
// ++itr3;
|
||||
// GetItrValue(loctestnum, loctesterr, itr3, true, string("A"));
|
||||
// ++itr3;
|
||||
// GetItrValue(loctestnum, loctesterr, itr3, true, string("C"));
|
||||
// ++itr3;
|
||||
// GetItrValue(loctestnum, loctesterr, itr3, true, string("D"));
|
||||
// ++itr3;
|
||||
// GetItrValue(loctestnum, loctesterr, itr3, true, string("E"));
|
||||
// Terminated(loctestnum, loctesterr, itr3, false);
|
||||
// ++itr3;
|
||||
// Terminated(loctestnum, loctesterr, itr3, true);
|
||||
|
||||
MapInOrder(loctestnum, loctesterr, bst, true, &MapPrint<string>, 0);
|
||||
|
||||
lasd::BTInOrderIterator<string> itr4(bst);
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, string("A"));
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, string("B"));
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, string("C"));
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, string("D"));
|
||||
++itr4;
|
||||
GetItrValue(loctestnum, loctesterr, itr4, true, string("E"));
|
||||
Terminated(loctestnum, loctesterr, itr4, false);
|
||||
++itr4;
|
||||
Terminated(loctestnum, loctesterr, itr4, true);
|
||||
|
||||
// FoldPreOrder(loctestnum, loctesterr, bst, true, &FoldStringConcatenate, string(""), string("?"), string("?BACDE"));
|
||||
// FoldPostOrder(loctestnum, loctesterr, bst, true, &FoldStringConcatenate, string(""), string("?"), string("?AEDCB"));
|
||||
// FoldBreadth(loctestnum, loctesterr, bst, true, &FoldStringConcatenate, string(""), string("?"), string("?BACDE"));
|
||||
FoldInOrder(loctestnum, loctesterr, bst, true, &FoldStringConcatenate, string(""), string("?"), string("?ABCDE"));
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of BST<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise4() {
|
||||
uint testnum = 0, testerr = 0;
|
||||
stestBSTInt(testnum, testerr);
|
||||
stestBSTFloat(testnum, testerr);
|
||||
stestBSTString(testnum, testerr);
|
||||
cout << endl << "Exercise 4 (Simple Test) (Errors/Tests: " << testerr << "/" << testnum << ")" << endl;
|
||||
}
|
13
librerie/exercise5/zlasdtest/exercise4/test.hpp
Normal file
13
librerie/exercise5/zlasdtest/exercise4/test.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef EX4TEST_HPP
|
||||
#define EX4TEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise4();
|
||||
|
||||
void testFullExercise4();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
3
librerie/exercise5/zlasdtest/exercise5/fulltest.cpp
Normal file
3
librerie/exercise5/zlasdtest/exercise5/fulltest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
void testFullExercise5() {
|
||||
}
|
354
librerie/exercise5/zlasdtest/exercise5/simpletest.cpp
Normal file
354
librerie/exercise5/zlasdtest/exercise5/simpletest.cpp
Normal file
@ -0,0 +1,354 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../iterator/iterator.hpp"
|
||||
|
||||
#include "../matrix/matrix.hpp"
|
||||
#include "../../matrix/vec/matrixvec.hpp"
|
||||
#include "../../matrix/csr/matrixcsr.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <template<typename> typename Mat>
|
||||
void stestMatrixInt(Mat<long>& mat, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
|
||||
Empty(loctestnum, loctesterr, mat, true);
|
||||
Size(loctestnum, loctesterr, mat, true, 0);
|
||||
|
||||
GetRowNumber(loctestnum, loctesterr, mat, true, 0);
|
||||
GetColumnNumber(loctestnum, loctesterr, mat, true, 0);
|
||||
|
||||
ExistsCell(loctestnum, loctesterr, mat, false, 0, 0);
|
||||
|
||||
GetCell<long>(loctestnum, loctesterr, mat, false, 0, 0, 0);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, false, 0, 0, 0);
|
||||
|
||||
SetRowNumber(loctestnum, loctesterr, mat, true, 1);
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 1);
|
||||
|
||||
mat.Clear();
|
||||
|
||||
ExistsCell(loctestnum, loctesterr, mat, false, 0, 0);
|
||||
|
||||
SetRowNumber(loctestnum, loctesterr, mat, true, 5);
|
||||
GetRowNumber(loctestnum, loctesterr, mat, true, 5);
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 4);
|
||||
GetColumnNumber(loctestnum, loctesterr, mat, true, 4);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 0, 1, 1);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 1, 0, 2);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 2, 1, 3);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 0, 3, 4);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 2, 3, 5);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 4, 1, 6);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 4, 3, 7);
|
||||
|
||||
Mat<long> copmat(mat);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, copmat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, copmat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldAdd<long>, 0, 0, 28);
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapIncrement<long>, 0);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 40320);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapDecrement<long>, 0);
|
||||
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 5);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 1, 4, 8);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 3, 4, 9);
|
||||
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 3);
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 4);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldAdd<long>, 0, 0, 12);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapIncrement<long>, 0);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 168);
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapDecrement<long>, 0);
|
||||
|
||||
SetRowNumber(loctestnum, loctesterr, mat, true, 4);
|
||||
SetRowNumber(loctestnum, loctesterr, mat, true, 5);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldAdd<long>, 0, 0, 6);
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapIncrement<long>, 0);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 24);
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapDecrement<long>, 0);
|
||||
|
||||
NonEqualMatrix(loctestnum, loctesterr, mat, copmat);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 4, 1, 6);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 4, 3, 7);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldAdd<long>, 0, 0, 19);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapIncrement<long>, 0);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 1344);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapDecrement<long>, 0);
|
||||
|
||||
NonEqualMatrix(loctestnum, loctesterr, mat, copmat);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 0, 3, 4);
|
||||
SetCell<long>(loctestnum, loctesterr, mat, true, 2, 3, 5);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldAdd<long>, 0, 0, 28);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapIncrement<long>, 0);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldMultiply<long>, 0, 1, 40320);
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapDecrement<long>, 0);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, copmat, true, &MapPrint<long>, 0);
|
||||
|
||||
EqualMatrix(loctestnum, loctesterr, mat, copmat);
|
||||
|
||||
Mat<long> movmat(std::move(mat));
|
||||
|
||||
GetRowNumber(loctestnum, loctesterr, mat, true, 0);
|
||||
GetColumnNumber(loctestnum, loctesterr, mat, true, 0);
|
||||
|
||||
ExistsCell(loctestnum, loctesterr, mat, false, 0, 0);
|
||||
|
||||
Exists<long>(loctestnum, loctesterr, movmat, false, 8);
|
||||
|
||||
SetCell<long>(loctestnum, loctesterr, movmat, true, 4, 2, 8);
|
||||
|
||||
MapPreOrder<long, long>(loctestnum, loctesterr, movmat, true, &MapPrint<long>, 0);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, movmat, true, &MapPrint<long>, 0);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, movmat, true, &FoldAdd<long>, 0, 0, 36);
|
||||
|
||||
Exists<long>(loctestnum, loctesterr, movmat, true, 8);
|
||||
|
||||
SetRowNumber(loctestnum, loctesterr, movmat, true, 4);
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, movmat, true, &MapPrint<long>, 0);
|
||||
|
||||
movmat.Clear();
|
||||
|
||||
GetRowNumber(loctestnum, loctesterr, movmat, true, 0);
|
||||
GetColumnNumber(loctestnum, loctesterr, movmat, true, 0);
|
||||
|
||||
mat = copmat;
|
||||
copmat.Clear();
|
||||
|
||||
MapPostOrder<long, long>(loctestnum, loctesterr, mat, true, &MapPrint<long>, 0);
|
||||
|
||||
SetRowNumber(loctestnum, loctesterr, mat, true, 3);
|
||||
SetColumnNumber(loctestnum, loctesterr, mat, true, 3);
|
||||
|
||||
FoldPreOrder<long, long, long>(loctestnum, loctesterr, mat, true, &FoldAdd<long>, 0, 0, 6);
|
||||
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Matrix<long> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestMatrixInt(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Matrix<long> Test" << endl;
|
||||
try {
|
||||
lasd::MatrixVec<long> matvec;
|
||||
cout << endl << "Begin of MatrixVec<long> Test:" << endl;
|
||||
stestMatrixInt(matvec, loctestnum, loctesterr);
|
||||
lasd::MatrixCSR<long> matcsr;
|
||||
cout << endl << "Begin of MatrixCSR<long> Test:" << endl;
|
||||
stestMatrixInt(matcsr, loctestnum, loctesterr);
|
||||
cout << "\n";
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Matrix<long> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
template <typename Mat>
|
||||
void stestMatrixFloat(Mat& mat, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 0, 0, 3.0);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 1, 1, 2.1);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 2, 2, 1.2);
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 3, 0, 0.3);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 2, 1, 1.2);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 1, 2, 2.1);
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 0, 2, 0.3);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 1, 1, 1.2);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 2, 0, 2.1);
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 3, 2, 3.0);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 2, 1, 2.1);
|
||||
SetCell(loctestnum, loctesterr, mat, true, 1, 0, 1.2);
|
||||
|
||||
Mat newmat(5, 10);
|
||||
newmat = std::move(mat);
|
||||
|
||||
GetRowNumber(loctestnum, loctesterr, mat, true, 5);
|
||||
GetColumnNumber(loctestnum, loctesterr, mat, true, 10);
|
||||
|
||||
GetCell(loctestnum, loctesterr, mat, false, 5, 10, 0.0);
|
||||
|
||||
Exists<double>(loctestnum, loctesterr, mat, false, 3.0);
|
||||
|
||||
GetCell(loctestnum, loctesterr, newmat, true, 1, 1, 1.2);
|
||||
GetCell(loctestnum, loctesterr, newmat, true, 2, 1, 2.1);
|
||||
|
||||
GetRowNumber(loctestnum, loctesterr, newmat, true, 4);
|
||||
GetColumnNumber(loctestnum, loctesterr, newmat, true, 3);
|
||||
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Matrix<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestMatrixFloat(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Matrix<double> Test" << endl;
|
||||
try {
|
||||
lasd::MatrixVec<double> matvec(4, 3);
|
||||
cout << endl << "Begin of MatrixVec<double> Test:" << endl;
|
||||
stestMatrixFloat(matvec, loctestnum, loctesterr);
|
||||
lasd::MatrixCSR<double> matcsr(4, 3);
|
||||
cout << endl << "Begin of MatrixCSR<double> Test:" << endl;
|
||||
stestMatrixFloat(matcsr, loctestnum, loctesterr);
|
||||
cout << "\n";
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Matrix<double> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
template <typename Mat>
|
||||
void stestMatrixString(Mat& mat, uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
try {
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 0, 0, string("| A"));
|
||||
SetCell(loctestnum, loctesterr, mat, true, 0, 2, string("B |"));
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 1, 1, string(" C |"));
|
||||
|
||||
SetCell(loctestnum, loctesterr, mat, true, 2, 0, string(" D"));
|
||||
SetCell(loctestnum, loctesterr, mat, true, 2, 2, string("E |"));
|
||||
|
||||
MapPreOrder<string, int>(loctestnum, loctesterr, mat, true, &MapPrint<string>, 0);
|
||||
MapPostOrder<string, int>(loctestnum, loctesterr, mat, true, &MapPrint<string>, 0);
|
||||
|
||||
FoldPreOrder<string, string, string>(loctestnum, loctesterr, mat, true, &FoldStringConcatenate, string(""), string("?"), string("?| AB | C | DE |"));
|
||||
FoldPostOrder<string, string, string>(loctestnum, loctesterr, mat, true, &FoldStringConcatenate, string(""), string("?"), string("?E | D C |B || A"));
|
||||
|
||||
MapPreOrder<string, string>(loctestnum, loctesterr, mat, true, &MapStringNonEmptyAppend, string(" "));
|
||||
|
||||
FoldPreOrder<string, string, string>(loctestnum, loctesterr, mat, true, &FoldStringConcatenate, string(""), string("?"), string("?| A B | C | D E | "));
|
||||
FoldPostOrder<string, string, string>(loctestnum, loctesterr, mat, true, &FoldStringConcatenate, string(""), string("?"), string("?E | D C | B | | A "));
|
||||
|
||||
MapPreOrder<string, string>(loctestnum, loctesterr, mat, true, &MapStringNonEmptyAppend, string("X"));
|
||||
|
||||
FoldPreOrder<string, string, string>(loctestnum, loctesterr, mat, true, &FoldStringConcatenate, string(""), string("?"), string("?| A XB | X C | X D XE | X"));
|
||||
FoldPostOrder<string, string, string>(loctestnum, loctesterr, mat, true, &FoldStringConcatenate, string(""), string("?"), string("?E | X D X C | XB | X| A X"));
|
||||
|
||||
GetRowNumber(loctestnum, loctesterr, mat, true, 3);
|
||||
GetColumnNumber(loctestnum, loctesterr, mat, true, 3);
|
||||
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Matrix<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
void stestMatrixString(uint& testnum, uint& testerr) {
|
||||
uint loctestnum = 0, loctesterr = 0;
|
||||
cout << endl << "Begin of Matrix<string> Test" << endl;
|
||||
try {
|
||||
lasd::MatrixVec<string> matvec(3, 3);
|
||||
cout << endl << "Begin of MatrixVec<string> Test:" << endl;
|
||||
stestMatrixString(matvec, loctestnum, loctesterr);
|
||||
lasd::MatrixCSR<string> matcsr(3, 3);
|
||||
cout << endl << "Begin of MatrixCSR<string> Test:" << endl;
|
||||
stestMatrixString(matcsr, loctestnum, loctesterr);
|
||||
cout << "\n";
|
||||
} catch(...) {
|
||||
loctestnum++; loctesterr++;
|
||||
cout << endl << "Unmanaged error! " << endl;
|
||||
}
|
||||
cout << "End of Matrix<string> Test! (Errors/Tests: " << loctesterr << "/" << loctestnum << ")" << endl;
|
||||
testnum += loctestnum;
|
||||
testerr += loctesterr;
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise5() {
|
||||
uint testnum = 0, testerr = 0;
|
||||
stestMatrixInt(testnum, testerr);
|
||||
stestMatrixFloat(testnum, testerr);
|
||||
stestMatrixString(testnum, testerr);
|
||||
cout << endl << "Exercise 5 (Simple Test) (Errors/Tests: " << testerr << "/" << testnum << ")" << endl;
|
||||
}
|
13
librerie/exercise5/zlasdtest/exercise5/test.hpp
Normal file
13
librerie/exercise5/zlasdtest/exercise5/test.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef EX5TEST_HPP
|
||||
#define EX5TEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise5();
|
||||
|
||||
void testFullExercise5();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
58
librerie/exercise5/zlasdtest/iterator/iterator.hpp
Normal file
58
librerie/exercise5/zlasdtest/iterator/iterator.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
|
||||
#ifndef ITERATORTEST_HPP
|
||||
#define ITERATORTEST_HPP
|
||||
|
||||
#include "../../iterator/iterator.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void GetItrValue(uint& testnum, uint& testerr, const lasd::Iterator<Data>& itr, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The value pointed by the iterator is \"" << *itr << "\": ";
|
||||
std::cout << ((tst = ((*itr == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::out_of_range exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetItrValue(uint& testnum, uint& testerr, const lasd::Iterator<Data>& itr, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting the value pointed by the iterator to \"" << val << "\": ";
|
||||
*itr = val;
|
||||
std::cout << ((tst = ((*itr == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::out_of_range exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Terminated(uint& testnum, uint& testerr, const lasd::Iterator<Data>& itr, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The iterator is " << ((tst = itr.Terminated()) ? "" : "not ") << "terminated: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
98
librerie/exercise5/zlasdtest/list/list.hpp
Normal file
98
librerie/exercise5/zlasdtest/list/list.hpp
Normal file
@ -0,0 +1,98 @@
|
||||
|
||||
#ifndef LISTTEST_HPP
|
||||
#define LISTTEST_HPP
|
||||
|
||||
#include "../../list/list.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void InsertAtFront(uint& testnum, uint& testerr, lasd::List<Data>& lst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Insert at the front of the list the value \"" << val << "\": ";
|
||||
lst.InsertAtFront(val);
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void RemoveFromFront(uint& testnum, uint& testerr, lasd::List<Data>& lst, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Remove from the list of \"" << lst.Front() << "\": ";
|
||||
lst.RemoveFromFront();
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void FrontNRemove(uint& testnum, uint& testerr, lasd::List<Data>& lst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") FrontNRemove from the list of \"" << lst.Front() << "\": ";
|
||||
std::cout << ((tst = ((lst.FrontNRemove() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void InsertAtBack(uint& testnum, uint& testerr, lasd::List<Data>& lst, bool chk, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Insert at the back of the list the value \"" << val << "\": ";
|
||||
lst.InsertAtBack(val);
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void EqualList(uint& testnum, uint& testerr, const lasd::List<Data>& lst1, const lasd::List<Data>& lst2, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two lists are " << ((tst = (lst1 == lst2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void NonEqualList(uint& testnum, uint& testerr, const lasd::List<Data>& lst1, const lasd::List<Data>& lst2, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two lists are " << ((tst = (lst1 != lst2)) ? "not " : "") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
150
librerie/exercise5/zlasdtest/matrix/matrix.hpp
Normal file
150
librerie/exercise5/zlasdtest/matrix/matrix.hpp
Normal file
@ -0,0 +1,150 @@
|
||||
|
||||
#ifndef MATRIXTEST_HPP
|
||||
#define MATRIXTEST_HPP
|
||||
|
||||
#include "../../matrix/matrix.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Mat>
|
||||
void EqualMatrix(uint& testnum, uint& testerr, const Mat& mat1, const Mat& mat2) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two matrices are " << ((tst = (mat1 == mat2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << (tst ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Mat>
|
||||
void NonEqualMatrix(uint& testnum, uint& testerr, const Mat& mat1, const Mat& mat2) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two matrices are " << ((tst = (mat1 != mat2)) ? "not " : "") << "equal: ";
|
||||
std::cout << (tst ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void GetRowNumber(uint& testnum, uint& testerr, const lasd::Matrix<Data>& mat, bool chk, ulong rows) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The row number is \"" << mat.RowNumber() << "\": ";
|
||||
std::cout << ((tst = ((mat.RowNumber() == rows) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetRowNumber(uint& testnum, uint& testerr, lasd::Matrix<Data>& mat, bool chk, ulong rows) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting the row number to \"" << rows << "\": ";
|
||||
mat.RowResize(rows);
|
||||
std::cout << ((tst = ((mat.RowNumber() == rows) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void GetColumnNumber(uint& testnum, uint& testerr, const lasd::Matrix<Data>& mat, bool chk, ulong cols) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The column number is \"" << mat.ColumnNumber() << "\": ";
|
||||
std::cout << ((tst = ((mat.ColumnNumber() == cols) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetColumnNumber(uint& testnum, uint& testerr, lasd::Matrix<Data>& mat, bool chk, ulong cols) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting the column number to \"" << cols << "\": ";
|
||||
mat.ColumnResize(cols);
|
||||
std::cout << ((tst = ((mat.ColumnNumber() == cols) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void ExistsCell(uint& testnum, uint& testerr, const lasd::Matrix<Data>& mat, bool chk, ulong row, ulong col) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The cell (" << row << ", " << col << ") " << ((tst = mat.ExistsCell(row, col)) ? "does" : "does not") << " exist: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void GetCell(uint& testnum, uint& testerr, const lasd::Matrix<Data>& mat, bool chk, ulong row, ulong col, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Cell (" << row << ", " << col << ") with value \"" << mat(row, col) << "\": ";
|
||||
std::cout << ((tst = ((mat(row, col) == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::out_of_range exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void SetCell(uint& testnum, uint& testerr, lasd::Matrix<Data>& mat, bool chk, ulong row, ulong col, const Data& val) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Setting cell (" << row << ", " << col << ") to value \"" << val << "\": ";
|
||||
mat(row, col) = val;
|
||||
std::cout << ((tst = ((mat(row, col) == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::out_of_range exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
116
librerie/exercise5/zlasdtest/queue/queue.hpp
Normal file
116
librerie/exercise5/zlasdtest/queue/queue.hpp
Normal file
@ -0,0 +1,116 @@
|
||||
|
||||
#ifndef QUEUETEST_HPP
|
||||
#define QUEUETEST_HPP
|
||||
|
||||
#include "../../queue/queue.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void EnqueueC(uint& testnum, uint& testerr, lasd::Queue<Data>& que, const Data& val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Enqueue on the queue of the value \"" << val << "\": ";
|
||||
que.Enqueue(val);
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
tst = false;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void EnqueueM(uint& testnum, uint& testerr, lasd::Queue<Data>& que, Data val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Enqueue on the queue of the value \"" << val << "\": ";
|
||||
que.Enqueue(std::move(val));
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
tst = false;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Head(uint& testnum, uint& testerr, lasd::Queue<Data>& que, bool chk, const Data& val) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Head of the queue with value \"" << que.Head() << "\": ";
|
||||
std::cout << ((tst = ((que.Head() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Dequeue(uint& testnum, uint& testerr, lasd::Queue<Data>& que, bool chk) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Dequeue from the queue: ";
|
||||
que.Dequeue();
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void HeadNDequeue(uint& testnum, uint& testerr, lasd::Queue<Data>& que, bool chk, const Data& val) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") HeadNDequeue from the queue with value \"" << que.Head() << "\": ";
|
||||
std::cout << ((tst = ((que.HeadNDequeue() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Que>
|
||||
void EqualQueue(uint& testnum, uint& testerr, const Que& que1, const Que& que2, bool chk) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two queues are " << ((tst = (que1 == que2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Que>
|
||||
void NonEqualQueue(uint& testnum, uint& testerr, const Que& que1, const Que& que2, bool chk) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two queues are " << ((tst = (que1 != que2)) ? "not " : "") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
116
librerie/exercise5/zlasdtest/stack/stack.hpp
Normal file
116
librerie/exercise5/zlasdtest/stack/stack.hpp
Normal file
@ -0,0 +1,116 @@
|
||||
|
||||
#ifndef STACKTEST_HPP
|
||||
#define STACKTEST_HPP
|
||||
|
||||
#include "../../stack/stack.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void PushC(uint& testnum, uint& testerr, lasd::Stack<Data>& stk, const Data& val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Push on the stack of the value \"" << val << "\": ";
|
||||
stk.Push(val);
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
tst = false;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void PushM(uint& testnum, uint& testerr, lasd::Stack<Data>& stk, Data val) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Push on the stack of the value \"" << val << "\": ";
|
||||
stk.Push(std::move(val));
|
||||
std::cout << "Correct!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << "Error!" << std::endl;
|
||||
tst = false;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Top(uint& testnum, uint& testerr, lasd::Stack<Data>& stk, bool chk, const Data& val) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Top of the stack with value \"" << stk.Top() << "\": ";
|
||||
std::cout << ((tst = ((stk.Top() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void Pop(uint& testnum, uint& testerr, lasd::Stack<Data>& stk, bool chk) {
|
||||
testnum++;
|
||||
bool tst = true;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") Pop from the stack: ";
|
||||
stk.Pop();
|
||||
std::cout << ((tst = chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void TopNPop(uint& testnum, uint& testerr, lasd::Stack<Data>& stk, bool chk, const Data& val) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") TopNPop from the stack with value \"" << stk.Top() << "\": ";
|
||||
std::cout << ((tst = ((stk.TopNPop() == val) == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::length_error exc) {
|
||||
std::cout << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
tst = false;
|
||||
std::cout << std::endl << "Wrong exception: " << exc.what() << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Stk>
|
||||
void EqualStack(uint& testnum, uint& testerr, const Stk& stk1, const Stk& stk2, bool chk) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two stacks are " << ((tst = (stk1 == stk2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Stk>
|
||||
void NonEqualStack(uint& testnum, uint& testerr, const Stk& stk1, const Stk& stk2, bool chk) {
|
||||
testnum++;
|
||||
bool tst;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two stacks are " << ((tst = (stk1 != stk2)) ? "not " : "") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
33
librerie/exercise5/zlasdtest/test.cpp
Normal file
33
librerie/exercise5/zlasdtest/test.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
#include "./exercise1/test.hpp"
|
||||
|
||||
#include "./exercise2/test.hpp"
|
||||
|
||||
#include "./exercise3/test.hpp"
|
||||
|
||||
#include "./exercise4/test.hpp"
|
||||
|
||||
#include "./exercise5/test.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void lasdtest() {
|
||||
cout << endl << "~*~#~*~ Welcome to the LASD Test Suite ~*~#~*~ " << endl;
|
||||
testSimpleExercise1();
|
||||
testFullExercise1();
|
||||
testSimpleExercise2();
|
||||
testFullExercise2();
|
||||
testSimpleExercise3();
|
||||
testFullExercise3();
|
||||
testSimpleExercise4();
|
||||
testFullExercise4();
|
||||
testSimpleExercise5();
|
||||
testFullExercise5();
|
||||
cout << endl << "Goodbye!" << endl;
|
||||
}
|
11
librerie/exercise5/zlasdtest/test.hpp
Normal file
11
librerie/exercise5/zlasdtest/test.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#ifndef LASDTEST_HPP
|
||||
#define LASDTEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void lasdtest();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
37
librerie/exercise5/zlasdtest/vector/vector.hpp
Normal file
37
librerie/exercise5/zlasdtest/vector/vector.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
#ifndef VECTORTEST_HPP
|
||||
#define VECTORTEST_HPP
|
||||
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
void EqualVector(uint& testnum, uint& testerr, const lasd::Vector<Data>& vec1, const lasd::Vector<Data>& vec2, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two vectors are " << ((tst = (vec1 == vec2)) ? "" : "not ") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void NonEqualVector(uint& testnum, uint& testerr, const lasd::Vector<Data>& vec1, const lasd::Vector<Data>& vec2, bool chk) {
|
||||
bool tst;
|
||||
testnum++;
|
||||
try {
|
||||
std::cout << " " << testnum << " (" << testerr << ") The two vectors are " << ((tst = (vec1 != vec2)) ? "not " : "") << "equal: ";
|
||||
std::cout << ((tst = (tst == chk)) ? "Correct" : "Error") << "!" << std::endl;
|
||||
} catch(std::exception exc) {
|
||||
std::cout << "\"" << exc.what() << "\": " << ((tst = !chk) ? "Correct" : "Error") << "!" << std::endl;
|
||||
}
|
||||
testerr += (1 - (uint) tst);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
2
librerie/exercise5/zmytest/test.cpp
Normal file
2
librerie/exercise5/zmytest/test.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
|
||||
// ...
|
11
librerie/exercise5/zmytest/test.hpp
Normal file
11
librerie/exercise5/zmytest/test.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#ifndef MYTEST_HPP
|
||||
#define MYTEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// ...
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user