Library 2

Added the necessary code from the previous library
This commit is contained in:
Alessandro Ferro 2021-04-11 18:06:59 +02:00
parent 04acfaf125
commit c30aaf96c9
5 changed files with 536 additions and 89 deletions

View File

@ -609,11 +609,11 @@ lasd::List<std::string> generateListOfStrings(){
for(unsigned long i = 0; i < dim; ++i){
mylist.InsertAtFront(generateRandomString(dist(gen)));
}
std::cout<<"Elements of the list:"<<std::endl;
std::cout<<std::endl<<"Elements of the list:"<<std::endl;
for(unsigned long i = 0 ; i<dim ; ++i){
std::cout<<mylist[i]<<" ";
}
std::cout<<std::endl;
std::cout<<std::endl<<std::endl;
return mylist;
}
lasd::List<float> generateListOfFloat(){
@ -625,11 +625,11 @@ lasd::List<float> generateListOfFloat(){
for(unsigned long i = 0; i < dim; ++i){
mylist.InsertAtFront((round(distr(gen)*10000))/100);
}
std::cout<<"Elements of the list:"<<std::endl;
std::cout<<std::endl<<"Elements of the list:"<<std::endl;
for(unsigned long i = 0 ; i<dim ; ++i){
std::cout<<mylist[i]<<" ";
}
std::cout<<std::endl;
std::cout<<std::endl<<std::endl;
return mylist;
}
lasd::List<int> generateListOfIntegers(){
@ -640,11 +640,11 @@ lasd::List<int> generateListOfIntegers(){
for(ulong i=0 ; i<dim ; ++i){
mylist.InsertAtFront(dist(gen));
}
std::cout<<"Elements of the list:"<<std::endl;
std::cout<<std::endl<<"Elements of the list:"<<std::endl;
for(unsigned long i = 0 ; i<dim ; ++i){
std::cout<<mylist[i]<<" ";
}
std::cout<<std::endl;
std::cout<<std::endl<<std::endl;
return mylist;
}
lasd::Vector<int> generateVectorOfIntegers(){
@ -657,11 +657,11 @@ lasd::Vector<int> generateVectorOfIntegers(){
for(ulong i = 0 ; i<dim ; ++i){
myvec[i] = dist(gen);
}
std::cout<<std::endl<<"Elements of the vector:"<<std::endl;
std::cout<<std::endl<<std::endl<<"Elements of the vector:"<<std::endl;
for(unsigned long i = 0 ; i<dim ; ++i){
std::cout<<myvec[i]<<" ";
}
std::cout<<std::endl;
std::cout<<std::endl<<std::endl;
return myvec;
}
lasd::Vector<float> generateVectorOfFloat(){
@ -673,11 +673,11 @@ lasd::Vector<float> generateVectorOfFloat(){
for(unsigned long i = 0; i < dim; ++i){
myvec[i] = (round(distr(gen)*10000))/100;
}
std::cout<<std::endl<<"Elements of the vector:"<<std::endl;
std::cout<<std::endl<<std::endl<<"Elements of the vector:"<<std::endl;
for(unsigned long i = 0 ; i<dim ; ++i){
std::cout<<myvec[i]<<" ";
}
std::cout<<std::endl;
std::cout<<std::endl<<std::endl;
return myvec;
}
lasd::Vector<std::string> generateVectorOfStrings(){
@ -690,7 +690,7 @@ lasd::Vector<std::string> generateVectorOfStrings(){
for(unsigned long i = 0; i < dim; ++i){
myvec[i] = generateRandomString(dist(gen));
}
std::cout<<std::endl<<"Elements of the vector:"<<std::endl;
std::cout<<std::endl<<std::endl<<"Elements of the vector:"<<std::endl;
for(unsigned long i = 0 ; i<dim ; ++i){
std::cout<<myvec[i]<<" ";
}

293
librerie/exercise2/list/list.cpp Executable file → Normal file
View File

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

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

@ -13,150 +13,150 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class List { // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
class List : virtual public LinearContainer<Data>,
virtual public MappableContainer<Data>,
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
private:
// ...
protected:
// using LinearContainer<Data>::???;
using LinearContainer<Data>:: size;
struct Node
{
// Data
// ...
Data value;
Node* next = nullptr;
/* ********************************************************************** */
// Specific constructors
// ...
Node(const Data&);
/* ********************************************************************** */
// Copy constructor
// ...
Node(const Node&);
// Move constructor
// ...
Node(Node&&);
Node(Data&&);
/* ********************************************************************** */
// Destructor
// ...
~Node() = default;
/* ********************************************************************** */
// Comparison operators
// ...
bool operator==(const Node&) const noexcept;
bool operator!=(const Node&) const noexcept;
/* ********************************************************************** */
// Specific member functions
// ...
};
// ...
struct Node* head = nullptr;
struct Node* tail = nullptr;
public:
// Default constructor
// List() specifiers;
List() = default;
/* ************************************************************************ */
// Specific constructor
// List(argument) specifiers; // A list obtained from a LinearContainer
List(const LinearContainer<Data>&); // A list obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
// List(argument) specifiers;
List(const List&);
// Move constructor
// List(argument) specifiers;
List(List&&);
/* ************************************************************************ */
// Destructor
// ~List() specifiers;
~List();
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
List& operator=(const List&);
// Move assignment
// type operator=(argument) specifiers;
List& operator=(List&&) noexcept;
/* ************************************************************************ */
// Comparison operators
// type operator==(argument) specifiers;
// type operator!=(argument) specifiers;
bool operator==(const List&) const noexcept;
bool operator!=(const List&) const noexcept;
/* ************************************************************************ */
// Specific member functions
// type InsertAtFront(argument) specifier; // Copy of the value
// type InsertAtFront(argument) specifier; // Move of the value
// type RemoveFromFront() specifier; // (must throw std::length_error when empty)
// type FrontNRemove() specifier; // (must throw std::length_error when empty)
void InsertAtFront(const Data&); // Copy of the value
void InsertAtFront(Data&&); // Move of the value
void RemoveFromFront(); // (must throw std::length_error when empty)
Data FrontNRemove(); // (must throw std::length_error when empty)
// type InsertAtBack(argument) specifier; // Copy of the value
// type InsertAtBack(argument) specifier; // Move of the value
void InsertAtBack(const Data&); // Copy of the value
void InsertAtBack(Data&&); // Move of the value
/* ************************************************************************ */
// Specific member functions (inherited from Container)
// type Clear() specifiers; // Override Container member
void Clear() override; // Override Container member
/* ************************************************************************ */
// Specific member functions (inherited from LinearContainer)
// type Front() specifiers; // Override LinearContainer member (must throw std::length_error when empty)
// type Back() specifiers; // Override LinearContainer member (must throw std::length_error when empty)
Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty)
Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty)
// type operator[](argument) specifiers; // Override LinearContainer member (must throw std::out_of_range when out of range)
Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range)
/* ************************************************************************ */
// Specific member functions (inherited from MappableContainer)
// using typename MappableContainer<Data>::MapFunctor;
using typename MappableContainer<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member
void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::FoldFunctor;
using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
protected:
// Auxiliary member functions (for MappableContainer)
// type MapPreOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards
// type MapPostOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards
void MapPreOrder(const MapFunctor, void* , struct Node*); // Accessory function executing from one point of the list onwards
void MapPostOrder(const MapFunctor, void* , struct Node*); // Accessory function executing from one point of the list onwards
/* ************************************************************************ */
// Auxiliary member functions (for FoldableContainer)
// type FoldPreOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards
// type FoldPostOrder(arguments) specifiers; // Accessory function executing from one point of the list onwards
void FoldPreOrder(const FoldFunctor, const void*, void*, struct Node*) const; // Accessory function executing from one point of the list onwards
void FoldPostOrder(const FoldFunctor, const void*, void*, struct Node*) const;; // Accessory function executing from one point of the list onwards
};

171
librerie/exercise2/vector/vector.cpp Executable file → Normal file
View File

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

51
librerie/exercise2/vector/vector.hpp Executable file → Normal file
View File

@ -13,15 +13,17 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class Vector { // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
class Vector : virtual public LinearContainer<Data>,
virtual public MappableContainer<Data>,
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
private:
// ...
protected:
// using LinearContainer<Data>::???;
using LinearContainer<Data>::size;
Data* Elements = nullptr;
// ...
@ -29,79 +31,80 @@ public:
// Default constructor
// Vector() specifiers;
Vector() = default;
/* ************************************************************************ */
// Specific constructors
// Vector(argument) specifiers; // A vector with a given initial dimension
// Vector(argument) specifiers; // A vector obtained from a LinearContainer
Vector(const ulong); // A vector with a given initial dimension
Vector(const LinearContainer<Data>&); // A vector obtained from a LinearContainer
/* ************************************************************************ */
// Copy constructor
// Vector(argument) specifiers;
Vector(const Vector&);
// Move constructor
// Vector(argument) specifiers;
Vector(Vector&&)noexcept;
/* ************************************************************************ */
// Destructor
// ~Vector() specifiers;
~Vector();
/* ************************************************************************ */
// Copy assignment
// type operator=(argument) specifiers;
Vector& operator=(const Vector&);
// Move assignment
// type operator=(argument) specifiers;
Vector& operator=(Vector&&) noexcept;
/* ************************************************************************ */
// Comparison operators
// type operator==(argument) specifiers;
// type operator!=(argument) specifiers;
bool operator==(const Vector&) const noexcept;
bool operator!=(const Vector&) const noexcept;
/* ************************************************************************ */
// Specific member functions
// type Resize(argument) specifiers; // Resize the vector to a given size
void Resize(const ulong); // Resize the vector to a given size
/* ************************************************************************ */
// Specific member functions (inherited from Container)
// type Clear() specifiers; // Override Container member
void Clear() override; // Override Container member
/* ************************************************************************ */
// Specific member functions (inherited from LinearContainer)
// type Front() specifiers; // Override LinearContainer member (must throw std::length_error when empty)
// type Back() specifiers; // Override LinearContainer member (must throw std::length_error when empty)
Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty)
Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty)
// type operator[](argument) specifiers; // Override LinearContainer member (must throw std::out_of_range when out of range)
Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range)
/* ************************************************************************ */
// Specific member functions (inherited from MappableContainer)
// using typename MappableContainer<Data>::MapFunctor;
using typename MappableContainer<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member
void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
using typename FoldableContainer<Data>::FoldFunctor;
void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
};
/* ************************************************************************** */