mirror of
https://github.com/xfarrow/lasd.git
synced 2025-06-05 21:49:14 +02:00
Library1
This commit is contained in:
@ -14,4 +14,5 @@ bool FoldableContainer<Data>::Exists(const Data& dat) const noexcept{
|
||||
FoldPreOrder(&AuxFoldExists<Data>, &dat, &exists);
|
||||
return exists;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,17 +2,11 @@
|
||||
#ifndef CONTAINER_HPP
|
||||
#define CONTAINER_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
class Container {
|
||||
|
||||
private:
|
||||
@ -26,40 +20,35 @@ 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.
|
||||
|
||||
/* ************************************************************************ */
|
||||
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.
|
||||
|
||||
/* ************************************************************************ */
|
||||
bool operator!=(const Container&&) const noexcept = delete; // Comparison of abstract types might not be possible.
|
||||
|
||||
// Specific member functions
|
||||
|
||||
virtual bool Empty() const noexcept { // (concrete function should not throw exceptions)
|
||||
return (size==0);
|
||||
};
|
||||
virtual bool Empty() const noexcept {
|
||||
return (size == 0);
|
||||
} // (concrete function should not throw exceptions)
|
||||
|
||||
virtual ulong Size() const noexcept{
|
||||
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
|
||||
class LinearContainer : virtual public Container{ // Must extend Container
|
||||
|
||||
private:
|
||||
|
||||
@ -70,15 +59,11 @@ 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&&) = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
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.
|
||||
@ -95,10 +80,8 @@ public:
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class TestableContainer : public Container { // Must extend Container
|
||||
class TestableContainer : virtual public Container{ // Must extend Container
|
||||
|
||||
private:
|
||||
|
||||
@ -109,16 +92,12 @@ 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.
|
||||
@ -131,8 +110,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class MappableContainer : virtual public Container { // Must extend Container
|
||||
|
||||
@ -145,22 +122,16 @@ 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;
|
||||
@ -170,8 +141,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class FoldableContainer : virtual public TestableContainer<Data>{ // Must extend TestableContainer
|
||||
|
||||
@ -184,21 +153,15 @@ 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==(const FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible.
|
||||
bool operator!=(const 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.
|
||||
bool operator!=(FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible.
|
||||
|
||||
// Specific member functions
|
||||
|
||||
|
@ -1,91 +1,94 @@
|
||||
|
||||
namespace lasd {
|
||||
// Specific constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Data newValue){
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// Specific constructors
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Data newValue){
|
||||
value = newValue;
|
||||
next = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(const Node& copyFrom){
|
||||
// Copy constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(const Node& copyFrom){
|
||||
value = copyFrom.value;
|
||||
}
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(const Data& copyFrom){
|
||||
value = copyFrom;
|
||||
}
|
||||
}
|
||||
|
||||
// Move constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Node&& moveFrom){
|
||||
|
||||
|
||||
// Move constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Node&& moveFrom){
|
||||
std::move(value, moveFrom.value);
|
||||
std::move(next, moveFrom.next);
|
||||
}
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Data&& moveFrom){
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Data&& moveFrom){
|
||||
std::move(value, moveFrom);
|
||||
}
|
||||
}
|
||||
|
||||
// Comparison operator
|
||||
template <typename Data>
|
||||
bool List<Data>::Node::operator==(const Node<Data>& node) const noexcept{
|
||||
if(node.value == value) return true;
|
||||
else return false;
|
||||
// TODO: deve confrontare anche il puntatore?
|
||||
}
|
||||
template <typename Data>
|
||||
bool List<Data>::Node::operator!=(const Node& node) const noexcept{
|
||||
if(node.value != value) return true;
|
||||
else return false;
|
||||
// TODO: deve confrontare anche il puntatore?
|
||||
// return !(this==node)
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
List<Data>::List(const LinearContainer<Data>& con){
|
||||
for(ulong i=0 ; i<con.Size() ; ++i){
|
||||
// Comparison operator
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
// Specific constructor
|
||||
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<con.Size() ; ++i){
|
||||
// Copy constructor
|
||||
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){
|
||||
// Move constructor
|
||||
template <typename Data>
|
||||
List<Data>::List(List<Data>&& moveFrom){
|
||||
std::swap(size, moveFrom.size);
|
||||
std::swap(head, moveFrom.head);
|
||||
std::swap(tail, moveFrom.tail);
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor
|
||||
template <typename Data>
|
||||
List<Data>::~List(){
|
||||
template <typename Data>
|
||||
List<Data>::~List(){
|
||||
Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Copy assignment
|
||||
template <typename Data>
|
||||
List& List<Data>::operator=(const List<Data>& copyFrom){
|
||||
// Copy assignment
|
||||
template <typename Data>
|
||||
List<Data>& List<Data>::operator=(const List<Data>& copyFrom){
|
||||
if(*this != copyFrom){
|
||||
Clear();
|
||||
for(ulong i=0 ; i<copyFrom.Size() ; ++i){
|
||||
for(ulong i = 0; i<copyFrom.Size(); ++i){
|
||||
InsertAtBack(copyFrom[i]);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Move assignment
|
||||
//Move assignment
|
||||
template <typename Data>
|
||||
List& List<Data>::operator=(List<Data>&& moveFrom){
|
||||
List<Data>& List<Data>::operator=(List<Data>&& moveFrom)noexcept{
|
||||
if(*this != moveFrom){
|
||||
Clear();
|
||||
//Clear();
|
||||
std::swap(size, moveFrom.size);
|
||||
std::swap(head, moveFrom.head);
|
||||
std::swap(tail, moveFrom.tail);
|
||||
@ -93,49 +96,54 @@ namespace lasd {
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Comparison operators
|
||||
//Comparison operators
|
||||
template <typename Data>
|
||||
bool List<Data>::operator==(const List<Data>& list) const noexcept{
|
||||
if(this->size != list.Size()) return false;
|
||||
for(ulong i=0 ; i<(this->size) ; ++i){
|
||||
if(*this[i] != list[i]) return false;
|
||||
for(ulong i = 0; i < (this->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>
|
||||
bool List<Data>::operator!=(const List<Data>& list) const noexcept{
|
||||
return !(*this==list);
|
||||
}
|
||||
|
||||
// Specific member functions
|
||||
|
||||
// Specific member functions
|
||||
template <typename Data>
|
||||
void List<Data>::InsertAtFront(const Data& data){
|
||||
struct Node* tmp = new Node(data);
|
||||
tmp->next = head;
|
||||
head = tmp;
|
||||
head =tmp;
|
||||
size++;
|
||||
if(size==1){
|
||||
|
||||
if(size == 1){
|
||||
tail = head;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::InsertAtFront(Data&& data){
|
||||
struct Node* tmp = new Node(data);
|
||||
tmp->next = head;
|
||||
head = tmp;
|
||||
head =tmp;
|
||||
size++;
|
||||
if(size==1){
|
||||
|
||||
if(size == 1){
|
||||
tail = head;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::RemoveFromFront(){
|
||||
if(head==nullptr){
|
||||
if(head == nullptr){
|
||||
throw std::length_error("List is empty!");
|
||||
}else{
|
||||
}
|
||||
else{
|
||||
struct Node* tmp = head;
|
||||
head = head->next;
|
||||
delete tmp;
|
||||
@ -145,9 +153,10 @@ namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
Data List<Data>::FrontNRemove(){
|
||||
if(head==nullptr){
|
||||
if(head == nullptr){
|
||||
throw std::length_error("List is empty!");
|
||||
}else{
|
||||
}
|
||||
else{
|
||||
Data value = head->value;
|
||||
RemoveFromFront();
|
||||
return value;
|
||||
@ -156,9 +165,10 @@ namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::InsertAtBack(const Data& data){
|
||||
if(size==0){
|
||||
if(size == 0){
|
||||
InsertAtFront(data);
|
||||
}else{
|
||||
}
|
||||
else{
|
||||
struct Node* last = new Node(data);
|
||||
tail->next = last;
|
||||
tail = last;
|
||||
@ -168,9 +178,10 @@ namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::InsertAtBack(Data&& data){
|
||||
if(size==0){
|
||||
if(size == 0){
|
||||
InsertAtFront(data);
|
||||
}else{
|
||||
}
|
||||
else{
|
||||
struct Node* last = new Node(data);
|
||||
tail->next = last;
|
||||
tail = last;
|
||||
@ -180,14 +191,14 @@ namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::Clear(){
|
||||
while(head!=nullptr){
|
||||
while(head != nullptr){
|
||||
RemoveFromFront();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& List<Data>::Front() const{
|
||||
if(size==0){
|
||||
if(size == 0){
|
||||
throw std::length_error("List is empty!");
|
||||
}else{
|
||||
return head->value;
|
||||
@ -196,7 +207,7 @@ namespace lasd {
|
||||
|
||||
template <typename Data>
|
||||
Data& List<Data>::Back() const{
|
||||
if(size==0){
|
||||
if(size == 0){
|
||||
throw std::length_error("List is empty!");
|
||||
}else{
|
||||
return tail->value;
|
||||
@ -204,29 +215,23 @@ namespace lasd {
|
||||
}
|
||||
|
||||
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{
|
||||
Data& List<Data>::operator[](const ulong idx) const{
|
||||
if(idx >= size || idx < 0){
|
||||
throw std::out_of_range("Out of Range!");
|
||||
}else
|
||||
{
|
||||
struct Node* tmp = head;
|
||||
for(ulong i=0 ; i<index ; ++i){
|
||||
for(ulong i=0; i<idx; ++i){
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return tmp->value;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::MapPreOrder(const MapFunctor fun, void* par){
|
||||
MapPreOrder(fun, 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);
|
||||
void List<Data>::MapPreOrder(MapFunctor function, void* par){
|
||||
MapPreOrder(function, par, head);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
@ -234,37 +239,53 @@ namespace lasd {
|
||||
MapPostOrder(function, par, head);
|
||||
}
|
||||
|
||||
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) const{
|
||||
FoldPreOrder(function, constPar, par, head);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void List<Data>::FoldPreOrder(FoldFunctor function, const void* constPar, void* par, struct Node* node) const{
|
||||
void List<Data>::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{
|
||||
FoldPostOrder(function, constPar, par, head);
|
||||
}
|
||||
|
||||
//OVERLOAD Accessory Function
|
||||
|
||||
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) const{
|
||||
FoldPostOrder(function, constPar, par, head);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
/* ************************************************************************** */
|
||||
}
|
||||
|
@ -15,13 +15,13 @@ 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>
|
||||
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using LinearContainer<Data>::size;
|
||||
using LinearContainer<Data>:: size;
|
||||
|
||||
struct Node
|
||||
{
|
||||
@ -37,7 +37,6 @@ protected:
|
||||
|
||||
// Copy constructor
|
||||
Node(const Node&);
|
||||
Node(const Data&);
|
||||
|
||||
// Move constructor
|
||||
Node(Node&&);
|
||||
@ -53,6 +52,11 @@ protected:
|
||||
// Comparison operators
|
||||
bool operator==(const Node&) const noexcept;
|
||||
bool operator!=(const Node&) const noexcept;
|
||||
|
||||
/* ********************************************************************** */
|
||||
|
||||
// Specific member functions
|
||||
|
||||
};
|
||||
|
||||
struct Node* head = nullptr;
|
||||
@ -66,14 +70,14 @@ public:
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructor
|
||||
List(const LinearContainer&); // A list obtained from a LinearContainer
|
||||
List(const LinearContainer<Data>&); // A list obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
List(const List&);
|
||||
|
||||
//Move constructor
|
||||
// Move constructor
|
||||
List(List&&);
|
||||
|
||||
/* ************************************************************************ */
|
||||
@ -117,8 +121,8 @@ public:
|
||||
|
||||
// Specific member functions (inherited from LinearContainer)
|
||||
|
||||
Data& Front() override const; // Override LinearContainer member (must throw std::length_error when empty)
|
||||
Data& Back() override const; // Override LinearContainer member (must throw std::length_error when empty)
|
||||
Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty)
|
||||
Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty)
|
||||
|
||||
Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range)
|
||||
|
||||
@ -144,15 +148,15 @@ 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
|
||||
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
|
||||
void FoldPostOrder(const FoldFunctor, const void*, void*, struct Node*) const;; // Accessory function executing from one point of the list onwards
|
||||
|
||||
};
|
||||
|
||||
|
BIN
librerie/exercise1/main
Executable file
BIN
librerie/exercise1/main
Executable file
Binary file not shown.
@ -1,8 +1,10 @@
|
||||
|
||||
// #include "zlasdtest/test.hpp"
|
||||
//
|
||||
// #include "zmytest/test.hpp"
|
||||
#include "container/container.hpp"
|
||||
#include "vector/vector.hpp"
|
||||
#include "list/list.hpp"
|
||||
#include "zlasdtest/test.hpp"
|
||||
|
||||
#include "zmytest/test.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
@ -11,6 +13,7 @@
|
||||
|
||||
int main() {
|
||||
std::cout << "Lasd Libraries 2020" << std::endl;
|
||||
menu();
|
||||
//lasdtest(); // To call in the menu of your library test!
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,180 +1,169 @@
|
||||
namespace lasd {
|
||||
// Specific constructor
|
||||
template typename<Data>
|
||||
Vector<Data>::Vector(const ulong dimension){
|
||||
Elements = new Data[dimenion] {};
|
||||
size = dimenion;
|
||||
}
|
||||
|
||||
// Specific constructor
|
||||
template typename<Data>
|
||||
Vector<Data>::Vector(const LinearContainer<Data>& con){
|
||||
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 ; i<size; ++i){
|
||||
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){
|
||||
// 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 = 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{
|
||||
// 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>
|
||||
template <typename Data>
|
||||
Vector<Data>::~Vector(){
|
||||
delete[] Elements;
|
||||
Clear();
|
||||
}
|
||||
|
||||
// Copy assignment
|
||||
template typename<Data>
|
||||
template <typename Data>
|
||||
Vector<Data>& Vector<Data>::operator=(const Vector<Data>& vec){
|
||||
Vector<Data>* tmpvec = new Vector<Data>(vec);
|
||||
std::swap(*tmpvec,*this);
|
||||
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);
|
||||
template <typename Data>
|
||||
Vector<Data>& Vector<Data>::operator=(Vector<Data>&& vec)noexcept{
|
||||
std::swap(Elements,vec.Elements);
|
||||
std::swap(size, vec.size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Comparison operators
|
||||
template typename<Data>
|
||||
template <typename Data>
|
||||
bool Vector<Data>::operator==(const Vector<Data>& vec) const noexcept{
|
||||
if(size!=vec.size) return false;
|
||||
for(ulong i=0 ; i<size ; ++i){
|
||||
if(Elements[i] != vec.Elements[i]){
|
||||
if(size == vec.size){
|
||||
for(ulong i=0; i<size; ++i){
|
||||
if(Elements[i] != vec.Elements[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template typename<Data>
|
||||
bool Vector<Data>::operator!=(const Vector<Data>& vec) const noexcept{
|
||||
|
||||
template <typename Data>
|
||||
bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
|
||||
return !(*this == vec);
|
||||
}
|
||||
|
||||
// Specific member function
|
||||
// template typename<Data>
|
||||
// void Vector<Data>::Resize(const ulong newsize){
|
||||
// if(newsize == 0){
|
||||
// Clear();
|
||||
// }
|
||||
// else{
|
||||
// size = newsize;
|
||||
// if(newsize < size){
|
||||
// Data* tmpvec = new Data[newsize] {};
|
||||
// for(ulong i=0;i<newsize;++i){
|
||||
// tmpvec[i] = Elements[i];
|
||||
// }
|
||||
// delete[] Elements;
|
||||
// Elements = tmpvec;
|
||||
// }else if(newsize > size){
|
||||
// Data* tmpvec = new Data[newsize] {};
|
||||
// for(ulong i=0;i<size;++i){
|
||||
// tmpvec[i] = Elements[i];
|
||||
// }
|
||||
// delete[] Elements;
|
||||
// Elements = tmpvec;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
template typename<Data>
|
||||
template <typename Data>
|
||||
void Vector<Data>::Resize(const ulong newsize){
|
||||
if(newsize == 0){
|
||||
Clear();
|
||||
}else if(size != newsize){
|
||||
}else if(size != newsize)
|
||||
{
|
||||
ulong limit = (size < newsize) ? size : newsize;
|
||||
Data* TmpElements = new Data[newsize] {};
|
||||
for(ulong i = 0 ; i<limit ; ++i){
|
||||
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(){
|
||||
|
||||
template <typename Data>
|
||||
void Vector<Data>::Clear(){
|
||||
delete[] Elements;
|
||||
Elements = nullptr;
|
||||
size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template typename<Data>
|
||||
Data& Vector<Data>::Front() const{
|
||||
if(size!=0){
|
||||
template <typename Data>
|
||||
Data& Vector<Data>::Front() const {
|
||||
if(size != 0)
|
||||
{
|
||||
return Elements[0];
|
||||
}else{
|
||||
return std::length_error("Access to an empty vector");
|
||||
throw std::length_error("Access to an empty vector!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template typename<Data>
|
||||
Data& Vector<Data>::Back() const{
|
||||
if(size!=0){
|
||||
return Elements[size-1];
|
||||
template <typename Data>
|
||||
Data& Vector<Data>::Back() const {
|
||||
if(size != 0)
|
||||
{
|
||||
return Elements[size - 1];
|
||||
}else{
|
||||
return std::length_error("Access to an empty vector");
|
||||
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("Access at index " + std::to_string(index) + " invalid because the vector size is" + std::to_string(size));
|
||||
}
|
||||
}
|
||||
|
||||
template typename<Data>
|
||||
void Vector<Data>::MapPreOrder(const MapFunctor fun, void* par){
|
||||
for(ulong i = 0 ; i<size ; i++){
|
||||
|
||||
template <typename Data>
|
||||
Data& Vector<Data>::operator[](const ulong idx) const{
|
||||
if(idx < size)
|
||||
{
|
||||
return Elements[idx];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::out_of_range("Tried to access index " + std::to_string(idx) + " 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 i = size;
|
||||
while(i>0){
|
||||
fun(Elements[--i], par);
|
||||
}
|
||||
}
|
||||
|
||||
template typename<Data>
|
||||
void Vector<Data>::FoldPreOrder(const FoldFunctor fun, const void* par, void* acc) const{
|
||||
for(ulong i = 0 ; i<size ; ++i){
|
||||
template <typename Data>
|
||||
void Vector<Data>::MapPostOrder(const MapFunctor fun, void* par){
|
||||
ulong idx = size;
|
||||
while(idx > 0)
|
||||
{
|
||||
fun(Elements[--idx], 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 i = size;
|
||||
while(i>0){
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,16 @@
|
||||
#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>,
|
||||
@ -13,15 +19,21 @@ class Vector : virtual public LinearContainer<Data>,
|
||||
|
||||
private:
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
using LinearContainer<Data>::size;
|
||||
Data* Elements = nullptr;
|
||||
|
||||
// ...
|
||||
|
||||
public:
|
||||
|
||||
// Default constructor
|
||||
// Vector() specifiers;
|
||||
Vector() = default;
|
||||
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructors
|
||||
@ -34,7 +46,7 @@ public:
|
||||
Vector(const Vector&);
|
||||
|
||||
// Move constructor
|
||||
Vector(Vector&&) noexcept;
|
||||
Vector(Vector&&)noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
@ -52,8 +64,8 @@ public:
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const Vector&&) const noexcept;
|
||||
bool operator!=(const Vector&&) const noexcept;
|
||||
bool operator==(const Vector&) const noexcept;
|
||||
bool operator!=(const Vector&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
@ -93,7 +105,6 @@ public:
|
||||
|
||||
void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
|
||||
void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
@ -1,3 +1,7 @@
|
||||
#include"test.hpp"
|
||||
#include<iostream>
|
||||
#include<random>
|
||||
|
||||
void menu(){
|
||||
unsigned short int choice;
|
||||
DataStructure chosenDataStructure;
|
||||
@ -11,7 +15,7 @@ void menu(){
|
||||
|
||||
switch(choice){
|
||||
case 1:
|
||||
lasdtest();
|
||||
//lasd::lasdtest();
|
||||
break;
|
||||
case 2:
|
||||
chosenDataStructure = ChooseDataStructure();
|
||||
@ -23,10 +27,10 @@ void menu(){
|
||||
}
|
||||
UseChosenType(chosenDataStructure,chosenDataType);
|
||||
}
|
||||
|
||||
DataStructure ChooseDataStructure(){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose a data structure:"<<std::endl;
|
||||
std::cout<<"1. Vector"<<std::endl;
|
||||
std::cout<<"2. List"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
@ -37,12 +41,12 @@ DataStructure ChooseDataStructure(){
|
||||
else if(choice == 2)
|
||||
return DataStructure::list;
|
||||
}
|
||||
|
||||
DataType ChooseDataType(){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose a data type:"<<std::endl;
|
||||
std::cout<<"1. Integer"<<std::endl;
|
||||
std::cout<<"2. Double"<<std::endl;
|
||||
std::cout<<"2. Float"<<std::endl;
|
||||
std::cout<<"3. String"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
@ -50,21 +54,553 @@ DataType ChooseDataType(){
|
||||
if(choice==1)
|
||||
return DataType::integer;
|
||||
else if(choice==2)
|
||||
return DataType::double;
|
||||
return DataType::ffloat;
|
||||
else if(choice==3)
|
||||
return DataType::string;
|
||||
return DataType::sstring;
|
||||
}
|
||||
void UseChosenType(DataStructure chosenDataStructure, DataType chosenDataType){
|
||||
std::cout<<"\tTest on ";
|
||||
if(chosenDataStructure == DataStructure::vector)
|
||||
std::cout<<"vector of ";
|
||||
else if(chosenDataStructure == DataStructure::list)
|
||||
std::cout<<"list of ";
|
||||
|
||||
if(chosenDataType == DataType::integer)
|
||||
std::cout<<" integers"<<std::endl;
|
||||
else if(chosenDataType == DataType::double)
|
||||
std::cout<<" doubles"<<std::endl;
|
||||
else if(chosenDataType == DataType::string)
|
||||
std::cout<<" strings"<<std::endl;
|
||||
if(chosenDataStructure == DataStructure::vector){
|
||||
if(chosenDataType == DataType::integer){
|
||||
lasd::Vector<int> myvec;
|
||||
myvec = generateVectorOfIntegers();
|
||||
VectorIntegerFunctions(myvec);
|
||||
}else if(chosenDataType == DataType::ffloat){
|
||||
lasd::Vector<float> myvec;
|
||||
myvec = generateVectorOfFloat();
|
||||
VectorFloatFunctions(myvec);
|
||||
}else if(chosenDataType == DataType::sstring){
|
||||
lasd::Vector<std::string> myvec;
|
||||
myvec = generateVectorOfStrings();
|
||||
VectorStringFunctions(myvec);
|
||||
}
|
||||
}else if(chosenDataStructure == DataStructure::list){
|
||||
if(chosenDataType == DataType::integer){
|
||||
lasd::List<int> mylist;
|
||||
mylist = generateListOfIntegers();
|
||||
ListIntegerFunctions(mylist);
|
||||
}else if(chosenDataType == DataType::ffloat){
|
||||
lasd::List<float> mylist;
|
||||
mylist = generateListOfFloat();
|
||||
ListFloatFunctions(mylist);
|
||||
}else if(chosenDataType == DataType::sstring){
|
||||
lasd::List<std::string> mylist;
|
||||
mylist = generateListOfStrings();
|
||||
ListStringFunctions(mylist);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
********** VECTOR FUNCTIONS **********
|
||||
*/
|
||||
void VectorIntegerFunctions(lasd::Vector<int> myvec){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose one of the following options:"<<std::endl;
|
||||
std::cout<<"1. Visualizza elemento iniziale"<<std::endl;
|
||||
std::cout<<"2. Visualizza elemento finale"<<std::endl;
|
||||
std::cout<<"3. Visualizza elemento con uno specifico indice"<<std::endl;
|
||||
std::cout<<"4. Visualizza tutti gli elementi"<<std::endl;
|
||||
std::cout<<"5. Controlla esistenza di uno specifico valore"<<std::endl;
|
||||
std::cout<<"6. Somma per gli interi minori di uno specifico valore"<<std::endl;
|
||||
std::cout<<"7. Raddoppia gli elementi."<<std::endl;
|
||||
std::cout<<"8. Esci"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
|
||||
switch(choice){
|
||||
case 1:
|
||||
ShowFirstElement(myvec);
|
||||
break;
|
||||
case 2:
|
||||
ShowLastElement(myvec);
|
||||
break;
|
||||
case 3:
|
||||
ShowElementWithASpecificIndex(myvec);
|
||||
break;
|
||||
case 4:
|
||||
ShowAllElements(myvec);
|
||||
break;
|
||||
case 5:
|
||||
CheckElementExists(myvec);
|
||||
break;
|
||||
case 6:
|
||||
SumLessThan(myvec);
|
||||
break;
|
||||
case 7:
|
||||
DoubleIntegers(myvec);
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
}
|
||||
void VectorFloatFunctions(lasd::Vector<float> myvec){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose one of the following options:"<<std::endl;
|
||||
std::cout<<"1. Visualizza elemento iniziale"<<std::endl;
|
||||
std::cout<<"2. Visualizza elemento finale"<<std::endl;
|
||||
std::cout<<"3. Visualizza elemento con uno specifico indice"<<std::endl;
|
||||
std::cout<<"4. Visualizza tutti gli elementi"<<std::endl;
|
||||
std::cout<<"5. Controlla esistenza di uno specifico valore"<<std::endl;
|
||||
std::cout<<"6. Prodotto per i float maggiori di uno specifico valore"<<std::endl;
|
||||
std::cout<<"7. Eleva al quadrato gli elementi."<<std::endl;
|
||||
std::cout<<"8. Esci"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
switch(choice){
|
||||
case 1:
|
||||
ShowFirstElement(myvec);
|
||||
break;
|
||||
case 2:
|
||||
ShowLastElement(myvec);
|
||||
break;
|
||||
case 3:
|
||||
ShowElementWithASpecificIndex(myvec);
|
||||
break;
|
||||
case 4:
|
||||
ShowAllElements(myvec);
|
||||
break;
|
||||
case 5:
|
||||
CheckElementExists(myvec);
|
||||
break;
|
||||
case 6:
|
||||
ProductMoreThan(myvec);
|
||||
break;
|
||||
case 7:
|
||||
SquareFloats(myvec);
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
}
|
||||
void VectorStringFunctions(lasd::Vector<std::string> myvec){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose one of the following options:"<<std::endl;
|
||||
std::cout<<"1. Visualizza elemento iniziale"<<std::endl;
|
||||
std::cout<<"2. Visualizza elemento finale"<<std::endl;
|
||||
std::cout<<"3. Visualizza elemento con uno specifico indice"<<std::endl;
|
||||
std::cout<<"4. Visualizza tutti gli elementi"<<std::endl;
|
||||
std::cout<<"5. Controlla esistenza di uno specifico valore"<<std::endl;
|
||||
std::cout<<"6. Concatenazione delle stringhe lunghe meno di uno specifico n"<<std::endl;
|
||||
std::cout<<"7. Uppercase."<<std::endl;
|
||||
std::cout<<"8. Esci"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
switch(choice){
|
||||
case 1:
|
||||
ShowFirstElement(myvec);
|
||||
break;
|
||||
case 2:
|
||||
ShowLastElement(myvec);
|
||||
break;
|
||||
case 3:
|
||||
ShowElementWithASpecificIndex(myvec);
|
||||
break;
|
||||
case 4:
|
||||
ShowAllElements(myvec);
|
||||
break;
|
||||
case 5:
|
||||
CheckElementExists(myvec);
|
||||
break;
|
||||
case 6:
|
||||
ConcatLessThan(myvec);
|
||||
break;
|
||||
case 7:
|
||||
Uppercase(myvec);
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
}
|
||||
template <typename Data>
|
||||
void ShowFirstElement(lasd::Vector<Data>& vec){
|
||||
std::cout<<std::endl<<"The first element is: "<<vec.Front()<<std::endl<<std::endl;
|
||||
}
|
||||
template <typename Data>
|
||||
void ShowLastElement(lasd::Vector<Data>& vec){
|
||||
std::cout<<std::endl<<"The last element is: "<<vec.Back()<<std::endl<<std::endl;
|
||||
}
|
||||
template <typename Data>
|
||||
void ShowElementWithASpecificIndex(lasd::Vector<Data>& vec){
|
||||
ulong index;
|
||||
std::cout<<"Insert the index: ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>index;
|
||||
try{
|
||||
std::cout<<std::endl<<"The element with the index: "<<index<<" is "<<vec[index]<<std::endl<<std::endl;
|
||||
}catch(std::out_of_range exc){
|
||||
std::cout<<exc.what()<<std::endl;
|
||||
}
|
||||
}
|
||||
template <typename Data>
|
||||
void ShowAllElements(lasd::Vector<Data>& vec){
|
||||
void (*AuxMapPreOrder) (Data&, void*) = PrintSingleElement;
|
||||
std::cout<<"The vector contains:"<<std::endl;
|
||||
vec.MapPreOrder(AuxMapPreOrder, nullptr);
|
||||
std::cout<<std::endl;
|
||||
}
|
||||
template <typename Data>
|
||||
void PrintSingleElement(Data& data, void* _){
|
||||
std::cout << data << " ";
|
||||
}
|
||||
template <typename Data>
|
||||
void CheckElementExists(lasd::Vector<Data>& vec){
|
||||
Data element;
|
||||
std::cout<<"What element do you wanna check its exsistence on? ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>element;
|
||||
if(vec.Exists(element))
|
||||
std::cout<<"The element exists"<<std::endl;
|
||||
else
|
||||
std::cout<<"The element does not exist"<<std::endl;
|
||||
}
|
||||
void SumLessThan(lasd::Vector<int>& vec){
|
||||
int pivot;
|
||||
ulong acc=0;
|
||||
void (*func)(const int&, const void*, void*) = AccumulateSum;
|
||||
std::cout<<"Which element do you choose to performa a sum under it? ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>pivot;
|
||||
vec.FoldPreOrder(func, (void*)&pivot, (void*)&acc);
|
||||
std::cout<<"The result of the sum is "<<acc<<std::endl<<std::endl;
|
||||
}
|
||||
void AccumulateSum(const int& data, const void* par, void* acc){
|
||||
if(data < (*(int*)par)){
|
||||
*(ulong*)acc += data;
|
||||
}
|
||||
}
|
||||
void DoubleIntegers(lasd::Vector<int>& vec){
|
||||
void (*fun)(int&, void*) = DoubleAnInteger;
|
||||
vec.MapPreOrder(fun, nullptr);
|
||||
std::cout<<"The doubled elements of the vector are: ";
|
||||
for (ulong i=0 ; i < vec.Size(); ++i){
|
||||
std::cout<<vec[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl<<std::endl;
|
||||
}
|
||||
void DoubleAnInteger(int& data, void* _){
|
||||
data *= 2;
|
||||
}
|
||||
void ProductMoreThan(lasd::Vector<float>& vec){
|
||||
float pivot, acc=1;
|
||||
void (*func)(const float&, const void*, void*) = AccumulateProduct;
|
||||
std::cout<<"Which element do you choose to performa a product above it? ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>pivot;
|
||||
vec.FoldPreOrder(func, (void*)&pivot, (void*)&acc);
|
||||
std::cout<<"The result of the product is "<<acc<<std::endl<<std::endl;
|
||||
}
|
||||
void AccumulateProduct(const float& data, const void* par, void* acc){
|
||||
if(data > (*(float*)par)){
|
||||
*(float*)acc *= data;
|
||||
}
|
||||
}
|
||||
void SquareFloats(lasd::Vector<float>& vec){
|
||||
void (*fun)(float&, void*) = SquareAFloat;
|
||||
vec.MapPreOrder(fun, nullptr);
|
||||
std::cout<<"The squared elements of the vector are: ";
|
||||
for (ulong i=0 ; i < vec.Size(); ++i){
|
||||
std::cout<<vec[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl<<std::endl;
|
||||
}
|
||||
void SquareAFloat(float& data, void* _){
|
||||
data *= data;
|
||||
}
|
||||
|
||||
void ConcatLessThan(lasd::Vector<std::string>& vec){
|
||||
ulong pivot;
|
||||
std::string acc = "";
|
||||
void (*func)(const std::string&, const void*, void*) = ConcatAux;
|
||||
std::cout<<"Concat elements whose length is less than: ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>pivot;
|
||||
vec.FoldPreOrder(func, (void*)&pivot, (void*)&acc);
|
||||
std::cout<<"The concatenated string is "<<acc<<std::endl<<std::endl;
|
||||
}
|
||||
void ConcatAux(const std::string& data, const void* par, void* acc){
|
||||
if( ((ulong)data.length()) < ((*(ulong*)par)) ){
|
||||
*(std::string*)acc = *(std::string*)acc + data;
|
||||
}
|
||||
}
|
||||
void Uppercase(lasd::Vector<std::string>& vec){
|
||||
void (*fun)(std::string&, void*) = UppercaseAString;
|
||||
vec.MapPreOrder(fun, nullptr);
|
||||
std::cout<<"The uppercased strings are: "<<std::endl;
|
||||
for (ulong i=0 ; i < vec.Size() ; ++i){
|
||||
std::cout<<vec[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl<<std::endl;
|
||||
}
|
||||
void UppercaseAString(std::string& data, void* par){
|
||||
for(ulong i=0 ; i<data.length() ; ++i){
|
||||
data[i] = toupper(data[i]);
|
||||
}
|
||||
}
|
||||
/*
|
||||
********** END VECTOR FUNCTIONS **********
|
||||
*/
|
||||
|
||||
/*
|
||||
********** LIST FUNCTIONS **********
|
||||
*/
|
||||
void ListIntegerFunctions(lasd::List<int> mylist){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose one of the following options:"<<std::endl;
|
||||
std::cout<<"1. Visualizza elemento iniziale"<<std::endl;
|
||||
std::cout<<"2. Visualizza elemento finale"<<std::endl;
|
||||
std::cout<<"3. Visualizza elemento con uno specifico indice"<<std::endl;
|
||||
std::cout<<"4. Visualizza tutti gli elementi"<<std::endl;
|
||||
std::cout<<"5. Controlla esistenza di uno specifico valore"<<std::endl;
|
||||
std::cout<<"6. Somma per gli interi minori di uno specifico valore"<<std::endl;
|
||||
std::cout<<"7. Raddoppia gli elementi."<<std::endl;
|
||||
std::cout<<"8. Esci"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
|
||||
switch(choice){
|
||||
case 1:
|
||||
ShowFirstElement(mylist);
|
||||
break;
|
||||
case 2:
|
||||
ShowLastElement(mylist);
|
||||
break;
|
||||
case 3:
|
||||
ShowElementWithASpecificIndex(mylist);
|
||||
break;
|
||||
case 4:
|
||||
//ShowAllElements(mylist);
|
||||
break;
|
||||
case 5:
|
||||
//CheckElementExists(mylist);
|
||||
break;
|
||||
case 6:
|
||||
//SumLessThan(mylist);
|
||||
break;
|
||||
case 7:
|
||||
//DoubleIntegers(mylist);
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
}
|
||||
void ListFloatFunctions(lasd::List<float> mylist){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose one of the following options:"<<std::endl;
|
||||
std::cout<<"1. Visualizza elemento iniziale"<<std::endl;
|
||||
std::cout<<"2. Visualizza elemento finale"<<std::endl;
|
||||
std::cout<<"3. Visualizza elemento con uno specifico indice"<<std::endl;
|
||||
std::cout<<"4. Visualizza tutti gli elementi"<<std::endl;
|
||||
std::cout<<"5. Controlla esistenza di uno specifico valore"<<std::endl;
|
||||
std::cout<<"6. Prodotto per i float maggiori di uno specifico valore"<<std::endl;
|
||||
std::cout<<"7. Eleva al quadrato gli elementi."<<std::endl;
|
||||
std::cout<<"8. Esci"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
switch(choice){
|
||||
case 1:
|
||||
ShowFirstElement(mylist);
|
||||
break;
|
||||
case 2:
|
||||
ShowLastElement(mylist);
|
||||
break;
|
||||
case 3:
|
||||
ShowElementWithASpecificIndex(mylist);
|
||||
break;
|
||||
case 4:
|
||||
//ShowAllElements(myvec);
|
||||
break;
|
||||
case 5:
|
||||
//CheckElementExists(myvec);
|
||||
break;
|
||||
case 6:
|
||||
//ProductMoreThan(myvec);
|
||||
break;
|
||||
case 7:
|
||||
//SquareFloats(myvec);
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
}
|
||||
void ListStringFunctions(lasd::List<std::string> mylist){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"Choose one of the following options:"<<std::endl;
|
||||
std::cout<<"1. Visualizza elemento iniziale"<<std::endl;
|
||||
std::cout<<"2. Visualizza elemento finale"<<std::endl;
|
||||
std::cout<<"3. Visualizza elemento con uno specifico indice"<<std::endl;
|
||||
std::cout<<"4. Visualizza tutti gli elementi"<<std::endl;
|
||||
std::cout<<"5. Controlla esistenza di uno specifico valore"<<std::endl;
|
||||
std::cout<<"6. Concatenazione delle stringhe lunghe meno di uno specifico n"<<std::endl;
|
||||
std::cout<<"7. Uppercase."<<std::endl;
|
||||
std::cout<<"8. Esci"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
switch(choice){
|
||||
case 1:
|
||||
ShowFirstElement(mylist);
|
||||
break;
|
||||
case 2:
|
||||
ShowLastElement(mylist);
|
||||
break;
|
||||
case 3:
|
||||
ShowElementWithASpecificIndex(mylist);
|
||||
break;
|
||||
case 4:
|
||||
//ShowAllElements(myvec);
|
||||
break;
|
||||
case 5:
|
||||
// CheckElementExists(myvec);
|
||||
break;
|
||||
case 6:
|
||||
//ConcatLessThan(myvec);
|
||||
break;
|
||||
case 7:
|
||||
//Uppercase(myvec);
|
||||
break;
|
||||
}
|
||||
}while(choice!=8);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void ShowFirstElement(lasd::List<Data>& list){
|
||||
std::cout<<std::endl<<"The first element is: "<<list.Front()<<std::endl<<std::endl;
|
||||
}
|
||||
template <typename Data>
|
||||
void ShowLastElement(lasd::List<Data>& list){
|
||||
std::cout<<std::endl<<"The last element is: "<<list.Back()<<std::endl<<std::endl;
|
||||
}
|
||||
template <typename Data>
|
||||
void ShowElementWithASpecificIndex(lasd::List<Data>& list){
|
||||
ulong index;
|
||||
std::cout<<"Insert the index: ";
|
||||
std::cin>>std::ws;
|
||||
std::cin>>index;
|
||||
try{
|
||||
std::cout<<std::endl<<"The element with the index: "<<index<<" is "<<list[index]<<std::endl<<std::endl;
|
||||
}catch(std::out_of_range exc){
|
||||
std::cout<<exc.what()<<std::endl;
|
||||
}
|
||||
}
|
||||
/*
|
||||
********** END LIST FUNCTIONS **********
|
||||
*/
|
||||
|
||||
/*
|
||||
********** GENERATORS **********
|
||||
*/
|
||||
lasd::List<std::string> generateListOfStrings(){
|
||||
ulong dim = getDimension();
|
||||
lasd::List<std::string> mylist = lasd::List<std::string>();
|
||||
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(1,5);
|
||||
|
||||
for(unsigned long i = 0; i < dim; ++i){
|
||||
mylist.InsertAtFront(generateRandomString(dist(gen)));
|
||||
}
|
||||
std::cout<<"Elements of the list:"<<std::endl;
|
||||
for(unsigned long i = 0 ; i<dim ; ++i){
|
||||
std::cout<<mylist[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return mylist;
|
||||
}
|
||||
lasd::List<float> generateListOfFloat(){
|
||||
ulong dim = getDimension();
|
||||
lasd::List<float> mylist = lasd::List<float>();
|
||||
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_real_distribution<double> distr(0,5);
|
||||
for(unsigned long i = 0; i < dim; ++i){
|
||||
mylist.InsertAtFront((round(distr(gen)*10000))/100);
|
||||
}
|
||||
std::cout<<"Elements of the list:"<<std::endl;
|
||||
for(unsigned long i = 0 ; i<dim ; ++i){
|
||||
std::cout<<mylist[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return mylist;
|
||||
}
|
||||
lasd::List<int> generateListOfIntegers(){
|
||||
ulong dim = getDimension();
|
||||
lasd::List<int> mylist = lasd::List<int>();
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(0,1000);
|
||||
for(ulong i=0 ; i<dim ; ++i){
|
||||
mylist.InsertAtFront(dist(gen));
|
||||
}
|
||||
std::cout<<"Elements of the list:"<<std::endl;
|
||||
for(unsigned long i = 0 ; i<dim ; ++i){
|
||||
std::cout<<mylist[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return mylist;
|
||||
}
|
||||
lasd::Vector<int> generateVectorOfIntegers(){
|
||||
ulong dim = getDimension();
|
||||
lasd::Vector<int> myvec = lasd::Vector<int>(dim);
|
||||
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(0,1000);
|
||||
|
||||
for(ulong i = 0 ; i<dim ; ++i){
|
||||
myvec[i] = dist(gen);
|
||||
}
|
||||
std::cout<<"Elements of the vector:"<<std::endl;
|
||||
for(unsigned long i = 0 ; i<dim ; ++i){
|
||||
std::cout<<myvec[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return myvec;
|
||||
}
|
||||
lasd::Vector<float> generateVectorOfFloat(){
|
||||
ulong dim = getDimension();
|
||||
lasd::Vector<float> myvec = lasd::Vector<float>(dim);
|
||||
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_real_distribution<double> distr(0,5);
|
||||
for(unsigned long i = 0; i < dim; ++i){
|
||||
myvec[i] = (round(distr(gen)*10000))/100;
|
||||
}
|
||||
std::cout<<"Elements of the vector:"<<std::endl;
|
||||
for(unsigned long i = 0 ; i<dim ; ++i){
|
||||
std::cout<<myvec[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return myvec;
|
||||
}
|
||||
lasd::Vector<std::string> generateVectorOfStrings(){
|
||||
ulong dim = getDimension();
|
||||
lasd::Vector<std::string> myvec = lasd::Vector<std::string>(dim);
|
||||
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_int_distribution<int> dist(1,5);
|
||||
|
||||
for(unsigned long i = 0; i < dim; ++i){
|
||||
myvec[i] = generateRandomString(dist(gen));
|
||||
}
|
||||
std::cout<<"Elements of the vector:"<<std::endl;
|
||||
for(unsigned long i = 0 ; i<dim ; ++i){
|
||||
std::cout<<myvec[i]<<" ";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
return myvec;
|
||||
}
|
||||
std::string generateRandomString(ulong dim){
|
||||
std::default_random_engine gen(std::random_device{}());
|
||||
std::uniform_int_distribution<char> character('a','z');
|
||||
char newString[dim+1];
|
||||
for(int i=0;i<dim;i++){
|
||||
newString[i]=character(gen);
|
||||
}
|
||||
newString[dim]='\0';
|
||||
return newString;
|
||||
}
|
||||
ulong getDimension(){
|
||||
ulong dimension;
|
||||
std::cout<<"Insert the dimension: ";
|
||||
std::cin>>dimension;
|
||||
return dimension;
|
||||
}
|
||||
|
@ -1,12 +1,85 @@
|
||||
|
||||
#ifndef MYTEST_HPP
|
||||
#define MYTEST_HPP
|
||||
|
||||
#include<iostream>
|
||||
#include"../vector/vector.hpp"
|
||||
#include"../list/list.hpp"
|
||||
enum class DataStructure{vector,list};
|
||||
enum class DataType{integer,double,string};
|
||||
enum class DataType{integer,ffloat,sstring};
|
||||
void menu();
|
||||
DataType ChooseDataType();
|
||||
void UseChosenType(DataStructure, DataType);
|
||||
DataType ChooseDataType(); //choose data type
|
||||
DataStructure ChooseDataStructure();
|
||||
void UseChosenType(DataStructure, DataType);
|
||||
lasd::Vector<int> generateVectorOfIntegers();
|
||||
lasd::Vector<float> generateVectorOfFloat();
|
||||
lasd::Vector<std::string> generateVectorOfStrings();
|
||||
lasd::List<std::string> generateListOfStrings();
|
||||
lasd::List<float> generateListOfFloat();
|
||||
lasd::List<int> generateListOfIntegers();
|
||||
std::string generateRandomString(ulong dim);
|
||||
|
||||
void VectorIntegerFunctions(lasd::Vector<int>);
|
||||
void VectorFloatFunctions(lasd::Vector<float>);
|
||||
void VectorStringFunctions(lasd::Vector<std::string>);
|
||||
template <typename Data>
|
||||
void ShowFirstElement(lasd::Vector<Data>&);
|
||||
template <typename Data>
|
||||
void ShowLastElement(lasd::Vector<Data>&);
|
||||
template <typename Data>
|
||||
void ShowElementWithASpecificIndex(lasd::Vector<Data>&);
|
||||
template <typename Data>
|
||||
void ShowAllElements(lasd::Vector<Data>&);
|
||||
template <typename Data>
|
||||
void PrintSingleElement(Data&, void*);
|
||||
template <typename Data>
|
||||
void CheckElementExists(lasd::Vector<Data>&);
|
||||
void SumLessThan(lasd::Vector<int>&);
|
||||
void AccumulateSum(const int&, const void*, void*);
|
||||
void DoubleIntegers(lasd::Vector<int>&);
|
||||
void DoubleAnInteger(int&, void*);
|
||||
void AccumulateProduct(const float&, const void*, void*);
|
||||
void ProductMoreThan(lasd::Vector<float>&);
|
||||
void SquareAFloat(float&, void*);
|
||||
void SquareFloats(lasd::Vector<float>&);
|
||||
void ConcatLessThan(lasd::Vector<std::string>&);
|
||||
void ConcatAux(const std::string&, const void*, void*);
|
||||
void Uppercase(lasd::Vector<std::string>&);
|
||||
void UppercaseAString(std::string&, void*);
|
||||
void ListIntegerFunctions(lasd::List<int>);
|
||||
void ListFloatFunctions(lasd::List<float>);
|
||||
void ListStringFunctions(lasd::List<std::string>);
|
||||
template <typename Data>
|
||||
void ShowFirstElement(lasd::List<Data>&);
|
||||
template <typename Data>
|
||||
void ShowLastElement(lasd::List<Data>&);
|
||||
template <typename Data>
|
||||
void ShowElementWithASpecificIndex(lasd::List<Data>&);
|
||||
ulong getDimension();
|
||||
/*
|
||||
void ChooseDimension(ulong&);
|
||||
LinearContainer<Data> GenerateRandomStructure(const ulong&);
|
||||
void ViewElement(const LinearContainer<Data>&);
|
||||
void PrintAllElements(const LinearContainer<Data>&);
|
||||
void PrintElement(Data&, void*); // funzione richiamata dalla map
|
||||
void CheckExists(const LinearContainer<Data>&);
|
||||
void ChooseFoldFunction(const LinearContainer<Data>&);
|
||||
void SumLessThan(const LinearContainer<int>&, const ulong&);
|
||||
void SumLessThanFold(int&, const void*, void*);
|
||||
void ProductMoreThan(const LinearContainer<float>&, const ulong&);
|
||||
void ProductMoreThanFold(float&, const void*, void*);
|
||||
void ConcatStringLessThan(const LinearContainer<string>&, const ulong&);
|
||||
void ConcatStringLessThanFold(string&, const void*, void*);
|
||||
|
||||
//7
|
||||
void ChooseMapFunction(const LinearContainer<Data>);
|
||||
void DoubleN(const LinearContainer<int>&, const ulong&);
|
||||
void DoubleNMap(int&, void*);
|
||||
|
||||
void SquareN(const LinearContainer<float>&, const ulong&);
|
||||
void SquareNMap(float&, void*);
|
||||
|
||||
void Uppercase(const LinearContainer<string>&, const ulong&);
|
||||
void UppercaseMap(string&, void*);
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
7
librerie/exercise1old/build.sh
Executable file
7
librerie/exercise1old/build.sh
Executable file
@ -0,0 +1,7 @@
|
||||
|
||||
#! /bin/bash
|
||||
|
||||
g++ -O3 -o main \
|
||||
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
|
||||
zlasdtest/container/container.cpp \
|
||||
zlasdtest/test.cpp zmytest/test.cpp main.cpp
|
17
librerie/exercise1old/container/container.cpp
Normal file
17
librerie/exercise1old/container/container.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
304
librerie/exercise1old/container/container.hpp
Normal file
304
librerie/exercise1old/container/container.hpp
Normal file
@ -0,0 +1,304 @@
|
||||
|
||||
#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 { // (concrete function should not throw exceptions)
|
||||
return (size == 0);
|
||||
}
|
||||
|
||||
virtual ulong Size() const noexcept{
|
||||
return size;
|
||||
}
|
||||
|
||||
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&&) = 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==(const FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible.
|
||||
bool operator!=(const 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 BreadthMappableContainer { // Must extend MappableContainer
|
||||
|
||||
private:
|
||||
|
||||
// ...
|
||||
|
||||
protected:
|
||||
|
||||
// ...
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
// ~BreadthMappableContainer() specifiers
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
// type operator=(argument); // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
// type operator=(argument); // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
// type operator==(argument) specifiers; // Comparison of abstract types might not be possible.
|
||||
// type operator!=(argument) specifiers; // Comparison of abstract types might not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions
|
||||
|
||||
// using typename MappableContainer<Data>::MapFunctor;
|
||||
|
||||
// type MapBreadth(arguments) specifiers;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BreadthFoldableContainer { // Must extend FoldableContainer
|
||||
|
||||
private:
|
||||
|
||||
// ...
|
||||
|
||||
protected:
|
||||
|
||||
// ...
|
||||
|
||||
public:
|
||||
|
||||
// Destructor
|
||||
// ~BreadthFoldableContainer() specifiers
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
// type operator=(argument); // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
// type operator=(argument); // Move assignment of abstract types should not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
// type operator==(argument) specifiers; // Comparison of abstract types might not be possible.
|
||||
// type operator!=(argument) specifiers; // Comparison of abstract types might not be possible.
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions
|
||||
|
||||
// using typename FoldableContainer<Data>::FoldFunctor;
|
||||
|
||||
// type FoldBreadth(arguments) specifiers;
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "container.cpp"
|
||||
|
||||
#endif
|
264
librerie/exercise1old/list/list.cpp
Normal file
264
librerie/exercise1old/list/list.cpp
Normal file
@ -0,0 +1,264 @@
|
||||
namespace lasd {
|
||||
// Specific constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Data newValue){
|
||||
value = newValue;
|
||||
next = nullptr;
|
||||
}
|
||||
|
||||
// Copy constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(const Node& copyFrom){
|
||||
value = copyFrom.value;
|
||||
}
|
||||
|
||||
// Move constructor
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Node&& moveFrom){
|
||||
std::move(value, moveFrom.value);
|
||||
std::move(next, moveFrom.next);
|
||||
}
|
||||
template <typename Data>
|
||||
List<Data>::Node::Node(Data&& moveFrom){
|
||||
std::move(value, moveFrom);
|
||||
}
|
||||
|
||||
// Comparison operator
|
||||
template <typename Data>
|
||||
bool List<Data>::Node::operator==(const Node& node) const noexcept{
|
||||
if(node.value == value) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool List<Data>::Node::operator!=(const Node& node) const noexcept{
|
||||
if(node.value != value) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
template <typename Data>
|
||||
List<Data>::~List(){
|
||||
Clear();
|
||||
}
|
||||
|
||||
// Copy assignment
|
||||
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;
|
||||
}
|
||||
|
||||
// Move assignment
|
||||
template <typename Data>
|
||||
List<Data>& List<Data>::operator=(List<Data>&& moveFrom) noexcept{
|
||||
if(*this != moveFrom){
|
||||
Clear();
|
||||
std::swap(size, moveFrom.size);
|
||||
std::swap(head, moveFrom.head);
|
||||
std::swap(tail, moveFrom.tail);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Comparison operators
|
||||
template <typename Data>
|
||||
bool List<Data>::operator==(const List<Data>& list) const noexcept{
|
||||
if(this->size != list.Size()) return false;
|
||||
for(ulong i=0 ; i<(this->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);
|
||||
}
|
||||
|
||||
// Specific member functions
|
||||
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(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;
|
||||
delete tmp;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
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(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(const MapFunctor fun, void* par){
|
||||
MapPreOrder(fun, 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){
|
||||
MapPostOrder(function, par, head);
|
||||
}
|
||||
|
||||
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) const{
|
||||
FoldPreOrder(function, constPar, par, head);
|
||||
}
|
||||
|
||||
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) const{
|
||||
FoldPostOrder(function, constPar, par, head);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
164
librerie/exercise1old/list/list.hpp
Normal file
164
librerie/exercise1old/list/list.hpp
Normal file
@ -0,0 +1,164 @@
|
||||
|
||||
#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(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;
|
||||
};
|
||||
|
||||
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
|
18
librerie/exercise1old/main.cpp
Normal file
18
librerie/exercise1old/main.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "container/container.hpp"
|
||||
#include "vector/vector.hpp"
|
||||
#include "list/list.hpp"
|
||||
#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;
|
||||
}
|
181
librerie/exercise1old/vector/vector.cpp
Normal file
181
librerie/exercise1old/vector/vector.cpp
Normal file
@ -0,0 +1,181 @@
|
||||
namespace lasd {
|
||||
|
||||
// Specific constructor
|
||||
template <typename Data>
|
||||
Vector<Data>::Vector(const ulong dimension){
|
||||
Elements = new Data[dimension] {};
|
||||
size = dimension;
|
||||
}
|
||||
|
||||
// Specific constructor
|
||||
template <typename Data>
|
||||
Vector<Data>::Vector(const LinearContainer<Data>& con){
|
||||
size = con.Size();
|
||||
Elements = new Data[size] {};
|
||||
for(ulong i ; 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(){
|
||||
delete[] Elements;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Comparison operators
|
||||
template <typename Data>
|
||||
bool Vector<Data>::operator==(const Vector<Data>& vec) const noexcept{
|
||||
if(size!=vec.size) return false;
|
||||
for(ulong i=0 ; i<size ; ++i){
|
||||
if(Elements[i] != vec.Elements[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool Vector<Data>::operator!=(const Vector<Data>& vec) const noexcept{
|
||||
return !(*this == vec);
|
||||
}
|
||||
|
||||
// Specific member function
|
||||
// template typename<Data>
|
||||
// void Vector<Data>::Resize(const ulong newsize){
|
||||
// if(newsize == 0){
|
||||
// Clear();
|
||||
// }
|
||||
// else{
|
||||
// size = newsize;
|
||||
// if(newsize < size){
|
||||
// Data* tmpvec = new Data[newsize] {};
|
||||
// for(ulong i=0;i<newsize;++i){
|
||||
// tmpvec[i] = Elements[i];
|
||||
// }
|
||||
// delete[] Elements;
|
||||
// Elements = tmpvec;
|
||||
// }else if(newsize > size){
|
||||
// Data* tmpvec = new Data[newsize] {};
|
||||
// for(ulong i=0;i<size;++i){
|
||||
// tmpvec[i] = Elements[i];
|
||||
// }
|
||||
// delete[] Elements;
|
||||
// Elements = tmpvec;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
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{
|
||||
return std::length_error("Access to an empty vector");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& Vector<Data>::Back() const{
|
||||
if(size!=0){
|
||||
return Elements[size-1];
|
||||
}else{
|
||||
return 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("Access at index " + std::to_string(index) + " invalid because the vector size 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 i = size;
|
||||
while(i>0){
|
||||
fun(Elements[--i], 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 i = size;
|
||||
while(i>0){
|
||||
fun(Elements[--i], par, acc);
|
||||
}
|
||||
}
|
105
librerie/exercise1old/vector/vector.hpp
Normal file
105
librerie/exercise1old/vector/vector.hpp
Normal file
@ -0,0 +1,105 @@
|
||||
|
||||
#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:
|
||||
|
||||
// Default constructor
|
||||
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;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions
|
||||
|
||||
void Resize(const ulong); // Resize the vector to a given size
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// 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
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "vector.cpp"
|
||||
|
||||
#endif
|
49
librerie/exercise1old/zlasdtest/container/container.cpp
Normal file
49
librerie/exercise1old/zlasdtest/container/container.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
#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 << " 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 << " 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));
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
/* ************************************************************************** */
|
301
librerie/exercise1old/zlasdtest/container/container.hpp
Normal file
301
librerie/exercise1old/zlasdtest/container/container.hpp
Normal file
@ -0,0 +1,301 @@
|
||||
|
||||
#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 << " 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 = true;
|
||||
std::cout << std::endl << "Wrong std::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 << " Setting the front of the linear container to \"" << val << "\": ";
|
||||
con.Front() = val;
|
||||
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 = true;
|
||||
std::cout << std::endl << "Wrong std::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 << " 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 = true;
|
||||
std::cout << std::endl << "Wrong std::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 << " Setting the back of the linear container to \"" << val << "\": ";
|
||||
con.Back() = val;
|
||||
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 = true;
|
||||
std::cout << std::endl << "Wrong std::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 << " Set of the linear container at index \"" << ind << "\" with value \"" << val << "\": ";
|
||||
con[ind] = val;
|
||||
std::cout << ((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 = true;
|
||||
std::cout << std::endl << "Wrong std::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 << " 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 = true;
|
||||
std::cout << std::endl << "Wrong std::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 << " 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 << " 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 << " 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 MapIncrementNPrint(Data& dat, void* _) {
|
||||
std::cout << dat++ << "->" << dat << "; ";
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void MapDouble(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*);
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// 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 << " 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 << " 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*);
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
// 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 << " Executing map in pre order - ";
|
||||
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 << " Executing fold in post order - ";
|
||||
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/exercise1old/zlasdtest/exercise1/fulltest.cpp
Normal file
3
librerie/exercise1old/zlasdtest/exercise1/fulltest.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
void testFullExercise1() {
|
||||
}
|
447
librerie/exercise1old/zlasdtest/exercise1/simpletest.cpp
Normal file
447
librerie/exercise1old/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 = std::move(vec);
|
||||
FoldPreOrder(loctestnum, loctesterr, copvec, true, &FoldStringConcatenate, string(""), string("?"), string("?A !B !"));
|
||||
|
||||
lasd::Vector<string> movvec(std::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 = std::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(std::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 = std::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/exercise1old/zlasdtest/exercise1/test.hpp
Normal file
13
librerie/exercise1old/zlasdtest/exercise1/test.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef EX1TEST_HPP
|
||||
#define EX1TEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void testSimpleExercise1();
|
||||
|
||||
void testFullExercise1();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
92
librerie/exercise1old/zlasdtest/list/list.hpp
Normal file
92
librerie/exercise1old/zlasdtest/list/list.hpp
Normal file
@ -0,0 +1,92 @@
|
||||
|
||||
#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 << " 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 << " 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;
|
||||
}
|
||||
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 << " 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;
|
||||
}
|
||||
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 << " 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 << " 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 << " 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
|
17
librerie/exercise1old/zlasdtest/test.cpp
Normal file
17
librerie/exercise1old/zlasdtest/test.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
#include "./exercise1/test.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void lasdtest() {
|
||||
cout << endl << "~*~#~*~ Welcome to the LASD Test Suite ~*~#~*~ " << endl;
|
||||
testSimpleExercise1();
|
||||
testFullExercise1();
|
||||
cout << endl << "Goodbye!" << endl;
|
||||
}
|
11
librerie/exercise1old/zlasdtest/test.hpp
Normal file
11
librerie/exercise1old/zlasdtest/test.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#ifndef LASDTEST_HPP
|
||||
#define LASDTEST_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
void lasdtest();
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#endif
|
37
librerie/exercise1old/zlasdtest/vector/vector.hpp
Normal file
37
librerie/exercise1old/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 << " 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 << " 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
|
70
librerie/exercise1old/zmytest/test.cpp
Normal file
70
librerie/exercise1old/zmytest/test.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
void menu(){
|
||||
unsigned short int choice;
|
||||
DataStructure chosenDataStructure;
|
||||
DataType chosenDataType;
|
||||
do{
|
||||
std::cout<<"1. Use your tests (to be used by the professor)"<<std::endl;
|
||||
std::cout<<"2. Use the library demo"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
}while(choice!=1 && choice!=2);
|
||||
|
||||
switch(choice){
|
||||
case 1:
|
||||
lasdtest();
|
||||
break;
|
||||
case 2:
|
||||
chosenDataStructure = ChooseDataStructure();
|
||||
chosenDataType = ChooseDataType();
|
||||
break;
|
||||
default:
|
||||
std::cout<<"An error has occurred"<<std::endl;
|
||||
return;
|
||||
}
|
||||
UseChosenType(chosenDataStructure,chosenDataType);
|
||||
}
|
||||
|
||||
DataStructure ChooseDataStructure(){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"1. Vector"<<std::endl;
|
||||
std::cout<<"2. List"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
}while(choice!=1 && choice!=2);
|
||||
if(choice == 1)
|
||||
return DataStructure::vector;
|
||||
else if(choice == 2)
|
||||
return DataStructure::list;
|
||||
}
|
||||
|
||||
DataType ChooseDataType(){
|
||||
unsigned short int choice;
|
||||
do{
|
||||
std::cout<<"1. Integer"<<std::endl;
|
||||
std::cout<<"2. Double"<<std::endl;
|
||||
std::cout<<"3. String"<<std::endl;
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
}while(!(choice>0 && choice<4));
|
||||
if(choice==1)
|
||||
return DataType::integer;
|
||||
else if(choice==2)
|
||||
return DataType::ddouble;
|
||||
else if(choice==3)
|
||||
return DataType::sstring;
|
||||
}
|
||||
void UseChosenType(DataStructure chosenDataStructure, DataType chosenDataType){
|
||||
std::cout<<"\tTest on ";
|
||||
if(chosenDataStructure == DataStructure::vector)
|
||||
std::cout<<"vector of ";
|
||||
else if(chosenDataStructure == DataStructure::list)
|
||||
std::cout<<"list of ";
|
||||
|
||||
if(chosenDataType == DataType::integer)
|
||||
std::cout<<" integers"<<std::endl;
|
||||
else if(chosenDataType == DataType::ddouble)
|
||||
std::cout<<" doubles"<<std::endl;
|
||||
else if(chosenDataType == DataType::sstring)
|
||||
std::cout<<" strings"<<std::endl;
|
||||
}
|
37
librerie/exercise1old/zmytest/test.hpp
Normal file
37
librerie/exercise1old/zmytest/test.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
#ifndef MYTEST_HPP
|
||||
#define MYTEST_HPP
|
||||
|
||||
enum class DataStructure{vector,list};
|
||||
enum class DataType{integer,ffloat,sstring};
|
||||
void menu();
|
||||
DataType ChooseDataType(); //choose data type
|
||||
DataStructure ChooseDataStructure();
|
||||
void UseChosenType(DataStructure, DataType);
|
||||
void ChooseDimension(ulong&);
|
||||
LinearContainer<Data> GenerateRandomStructure(const ulong&);
|
||||
void ViewElement(const LinearContainer<Data>&);
|
||||
void PrintAllElements(const LinearContainer<Data>&);
|
||||
void PrintElement(Data&, void*); // funzione richiamata dalla map
|
||||
void CheckExists(const LinearContainer<Data>&);
|
||||
void ChooseFoldFunction(const LinearContainer<Data>&);
|
||||
void SumLessThan(const LinearContainer<int>&, const ulong&);
|
||||
void SumLessThanFold(int&, const void*, void*);
|
||||
void ProductMoreThan(const LinearContainer<float>&, const ulong&);
|
||||
void ProductMoreThanFold(float&, const void*, void*);
|
||||
void ConcatStringLessThan(const LinearContainer<string>&, const ulong&);
|
||||
void ConcatStringLessThanFold(string&, const void*, void*);
|
||||
|
||||
//7
|
||||
void ChooseMapFunction(const LinearContainer<Data>);
|
||||
void DoubleN(const LinearContainer<int>&, const ulong&);
|
||||
void DoubleNMap(int&, void*);
|
||||
|
||||
void SquareN(const LinearContainer<float>&, const ulong&);
|
||||
void SquareNMap(float&, void*);
|
||||
|
||||
void Uppercase(const LinearContainer<string>&, const ulong&);
|
||||
void UppercaseMap(string&, void*);
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user