From dc0739a4f884ad3d47649d89fc8eff2c41f63e6f Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Fri, 23 Apr 2021 21:44:06 +0200 Subject: [PATCH] Library 1 fixed issue #2 and cleaned code --- .gitignore | 2 + librerie/exercise1/container/container.hpp | 7 - librerie/exercise1/list/list.cpp | 316 ++++++++++----------- librerie/exercise1/vector/vector.cpp | 249 ++++++++-------- librerie/exercise1/vector/vector.hpp | 53 +--- 5 files changed, 273 insertions(+), 354 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da21387 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +**/main diff --git a/librerie/exercise1/container/container.hpp b/librerie/exercise1/container/container.hpp index c28cc43..7b7e4e5 100644 --- a/librerie/exercise1/container/container.hpp +++ b/librerie/exercise1/container/container.hpp @@ -31,7 +31,6 @@ public: bool operator!=(const Container&&) const noexcept = delete; // Comparison of abstract types might not be possible. // Specific member functions - virtual bool Empty() const noexcept { return (size == 0); } // (concrete function should not throw exceptions) @@ -43,10 +42,6 @@ public: virtual void Clear() = 0; }; -/* ************************************************************************** */ - - - template class LinearContainer : virtual public Container{ // Must extend Container @@ -72,7 +67,6 @@ public: /* ************************************************************************ */ // 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) @@ -105,7 +99,6 @@ public: /* ************************************************************************ */ // Specific member functions - virtual bool Exists(const Data&) const noexcept = 0; // (concrete function should not throw exceptions) }; diff --git a/librerie/exercise1/list/list.cpp b/librerie/exercise1/list/list.cpp index 371af2d..3396301 100644 --- a/librerie/exercise1/list/list.cpp +++ b/librerie/exercise1/list/list.cpp @@ -29,21 +29,16 @@ List::Node::Node(Data&& moveFrom){ std::swap(value, moveFrom); } - -// Comparison operator template bool List::Node::operator==(const Node& node)const noexcept{ return (node.value == value); } - template bool List::Node::operator!=(const Node& node)const noexcept{ return !(*this == node); } - -// Specific constructor template List::List(const LinearContainer& con){ for(ulong i = 0; i::List(const LinearContainer& con){ } } -// Copy constructor template List::List(const List& copyFrom){ for(ulong i = 0; i::List(const List& copyFrom){ } } -// Move constructor template List::List(List&& moveFrom){ std::swap(size, moveFrom.size); @@ -72,19 +65,17 @@ List::~List(){ Clear(); } -// Copy assignment template List& List::operator=(const List& copyFrom){ if(*this != copyFrom){ Clear(); - for(ulong i = 0; i List& List::operator=(List&& moveFrom)noexcept{ if(*this != moveFrom){ @@ -95,11 +86,10 @@ template return *this; } - //Comparison operators template bool List::operator==(const List& list) const noexcept{ - if(this->size != list.Size()) return false; - for(ulong i = 0; i < (this->size); ++i){ + if(size != list.Size()) return false; + for(ulong i = 0 ; i < size ; ++i){ if((*this)[i] != list[i]) return false; } return true; @@ -110,34 +100,29 @@ bool List::operator!=(const List& list) const noexcept{ return !(*this==list); } -// Specific member functions - - template - void List::InsertAtFront(const Data& data){ - struct Node* tmp = new Node(data); - tmp->next = head; - head =tmp; - size++; - - if(size == 1){ - tail = head; - } +template +void List::InsertAtFront(const Data& data){ + struct Node* tmp = new Node(data); + tmp->next = head; + head = tmp; + size++; + if(size == 1){ + tail = head; + } } - - template - void List::InsertAtFront(Data&& data){ - struct Node* tmp = new Node(data); - tmp->next = head; - head =tmp; - size++; - - if(size == 1){ - tail = head; - } +template +void List::InsertAtFront(Data&& data){ + struct Node* tmp = new Node(std::move(data)); + tmp->next = head; + head = tmp; + size++; + if(size == 1){ + tail = head; } +} - template +template void List::RemoveFromFront(){ if(head == nullptr){ throw std::length_error("List is empty!"); @@ -148,148 +133,133 @@ bool List::operator!=(const List& list) const noexcept{ tmp->next = nullptr; delete tmp; size--; - if(head==nullptr){ tail=nullptr; } } } - template - Data List::FrontNRemove(){ - if(head == nullptr){ - throw std::length_error("List is empty!"); - } - else{ - Data value = head->value; - RemoveFromFront(); - return value; - } +template +Data List::FrontNRemove(){ + if(head == nullptr){ + throw std::length_error("List is empty!"); } - - template - void List::InsertAtBack(const Data& data){ - if(size == 0){ - InsertAtFront(data); - } - else{ - struct Node* last = new Node(data); - tail->next = last; - tail = last; - size++; - } + else{ + Data value = head->value; + RemoveFromFront(); + return value; } - - template - void List::InsertAtBack(Data&& data){ - if(size == 0){ - InsertAtFront(data); - } - else{ - struct Node* last = new Node(data); - tail->next = last; - tail = last; - size++; - } - } - - template - void List::Clear(){ - while(head != nullptr){ - RemoveFromFront(); - } - } - - template - Data& List::Front() const{ - if(size == 0){ - throw std::length_error("List is empty!"); - }else{ - return head->value; - } - } - - template - Data& List::Back() const{ - if(size == 0){ - throw std::length_error("List is empty!"); - }else{ - return tail->value; - } - } - - template - Data& List::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; inext; - } - return tmp->value; - } - } - - - template - void List::MapPreOrder(MapFunctor function, void* par){ - MapPreOrder(function, par, head); - } - - template - void List::MapPostOrder(MapFunctor function, void* par){ - MapPostOrder(function, par, head); - } - - template - void List::FoldPreOrder(FoldFunctor function, const void* constPar, void* par) const{ - FoldPreOrder(function, constPar, par, head); - } - - template - void List::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{ - FoldPostOrder(function, constPar, par, head); - } - - //OVERLOAD Accessory Function - - template - void List::MapPreOrder(MapFunctor function, void* par, struct Node* node){ - if(node == nullptr) - return; - - function(node->value, par); - MapPreOrder(function, par, node->next); - } - - template - void List::MapPostOrder(MapFunctor function, void* par, struct Node* node){ - if(node == nullptr) - return; - - MapPostOrder(function, par, node->next); - function(node->value, par); - } - - template - void List::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 - void List::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); - } -/* ************************************************************************** */ +} + +template +void List::InsertAtBack(const Data& data){ + if(size == 0){ + InsertAtFront(data); + } + else{ + struct Node* last = new Node(data); + tail->next = last; + tail = last; + size++; + } +} + +template +void List::InsertAtBack(Data&& data){ + if(size == 0){ + InsertAtFront(data); + } + else{ + struct Node* last = new Node(std::move(data)); + tail->next = last; + tail = last; + size++; + } +} + +template +void List::Clear(){ + while(head != nullptr){ + RemoveFromFront(); + } +} + +template +Data& List::Front() const{ + if(size == 0){ + throw std::length_error("List is empty!"); + }else{ + return head->value; + } +} + +template +Data& List::Back() const{ + if(size == 0){ + throw std::length_error("List is empty!"); + }else{ + return tail->value; + } +} + +template +Data& List::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; inext; + } + return tmp->value; + } +} + +template +void List::MapPreOrder(MapFunctor function, void* par){ + MapPreOrder(function, par, head); +} + +template +void List::MapPostOrder(MapFunctor function, void* par){ + MapPostOrder(function, par, head); +} + +template +void List::FoldPreOrder(FoldFunctor function, const void* constPar, void* par) const{ + FoldPreOrder(function, constPar, par, head); +} + +template +void List::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{ + FoldPostOrder(function, constPar, par, head); +} + +template +void List::MapPreOrder(MapFunctor function, void* par, struct Node* node){ + if(node == nullptr) return; + function(node->value, par); + MapPreOrder(function, par, node->next); +} + +template +void List::MapPostOrder(MapFunctor function, void* par, struct Node* node){ + if(node == nullptr) return; + MapPostOrder(function, par, node->next); + function(node->value, par); +} + +template +void List::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 +void List::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); +} } diff --git a/librerie/exercise1/vector/vector.cpp b/librerie/exercise1/vector/vector.cpp index 2ebef36..a7b3d5e 100644 --- a/librerie/exercise1/vector/vector.cpp +++ b/librerie/exercise1/vector/vector.cpp @@ -34,136 +34,127 @@ template } // Destructor - template - Vector::~Vector(){ +template +Vector::~Vector(){ + Clear(); +} + +// Copy assignment +template +Vector& Vector::operator=(const Vector& vec){ + Vector* tmpvec = new Vector(vec); + std::swap(*tmpvec, *this); + delete tmpvec; + return *this; +} + +// Move assignment +template +Vector& Vector::operator=(Vector&& vec)noexcept{ + std::swap(Elements,vec.Elements); + std::swap(size, vec.size); + return *this; +} + +template +bool Vector::operator==(const Vector& vec) const noexcept{ + if(size == vec.size){ + for(ulong i=0; i +bool Vector::operator!=(const Vector& vec)const noexcept{ + return !(*this == vec); +} + +template +void Vector::Resize(const ulong newsize){ + if(newsize == 0){ Clear(); } - - // Copy assignment - template - Vector& Vector::operator=(const Vector& vec){ - Vector* tmpvec = new Vector(vec); - std::swap(*tmpvec, *this); - delete tmpvec; - return *this; - } - - // Move assignment - template - Vector& Vector::operator=(Vector&& vec)noexcept{ - std::swap(Elements,vec.Elements); - std::swap(size, vec.size); - return *this; - } - - // Comparison operators - template - bool Vector::operator==(const Vector& vec) const noexcept{ - if(size == vec.size){ - for(ulong i=0; i - bool Vector::operator!=(const Vector& vec)const noexcept{ - return !(*this == vec); - } - - template - void Vector::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 - void Vector::Clear(){ - delete[] Elements; - Elements = nullptr; - size = 0; - } - - template - Data& Vector::Front() const { - if(size != 0) - { - return Elements[0]; - }else{ - throw std::length_error("Access to an empty vector!"); - } - } - - template - Data& Vector::Back() const { - if(size != 0) - { - return Elements[size - 1]; - }else{ - throw std::length_error("Access to an empty vector!"); - } - } - - - template - Data& Vector::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 - void Vector::MapPreOrder(const MapFunctor fun, void* par){ - for(ulong i=0; i - void Vector::MapPostOrder(const MapFunctor fun, void* par){ - ulong idx = size; - while(idx > 0) - { - fun(Elements[--idx], par); - } - } - - - template - void Vector::FoldPreOrder(const FoldFunctor fun, const void* par, void* acc) const{ - for(ulong i=0; i - void Vector::FoldPostOrder(const FoldFunctor fun, const void* par, void* acc) const{ - ulong idx = size; - while(idx > 0){ - fun(Elements[--idx], par, acc); - } + else if(size != newsize){ + ulong limit = (size < newsize) ? size : newsize; + Data* TmpElements = new Data[newsize]{}; + for(ulong i=0; i +void Vector::Clear(){ + delete[] Elements; + Elements = nullptr; + size = 0; +} + +template +Data& Vector::Front() const { + if(size != 0){ + return Elements[0]; + } + else{ + throw std::length_error("Access to an empty vector!"); + } +} + +template +Data& Vector::Back() const { + if(size != 0){ + return Elements[size - 1]; + } + else{ + throw std::length_error("Access to an empty vector!"); + } +} + +template +Data& Vector::operator[](const ulong index) const{ + if(index < size){ + return Elements[index]; + } + else{ + throw std::out_of_range("Tried to access index " + std::to_string(index) + " but the dimension of the vector is " + std::to_string(size)); + } +} + +template +void Vector::MapPreOrder(const MapFunctor fun, void* par){ + for(ulong i=0; i +void Vector::MapPostOrder(const MapFunctor fun, void* par){ + ulong index = size; + while(index > 0){ + fun(Elements[--index], par); + } +} + +template +void Vector::FoldPreOrder(const FoldFunctor fun, const void* par, void* acc) const{ + for(ulong i=0; i +void Vector::FoldPostOrder(const FoldFunctor fun, const void* par, void* acc) const{ + ulong idx = size; + while(idx > 0){ + fun(Elements[--idx], par, acc); + } +} } diff --git a/librerie/exercise1/vector/vector.hpp b/librerie/exercise1/vector/vector.hpp index b5fcbe5..2ae8fed 100644 --- a/librerie/exercise1/vector/vector.hpp +++ b/librerie/exercise1/vector/vector.hpp @@ -25,35 +25,22 @@ protected: using LinearContainer::size; Data* Elements = nullptr; - // ... - public: - // Default constructor - // Vector() specifiers; - Vector() = default; - - - /* ************************************************************************ */ + Vector() = default; // Specific constructors Vector(const ulong); // A vector with a given initial dimension Vector(const LinearContainer&); // A vector obtained from a LinearContainer - /* ************************************************************************ */ - // Copy constructor - Vector(const Vector&); + Vector(const Vector&); // Move constructor - Vector(Vector&&)noexcept; - - /* ************************************************************************ */ + Vector(Vector&&)noexcept; // Destructor - ~Vector(); - - /* ************************************************************************ */ + ~Vector(); // Copy assignment Vector& operator=(const Vector&); @@ -61,54 +48,30 @@ public: // 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) - /* ************************************************************************ */ + using typename MappableContainer::MapFunctor; - // Specific member functions (inherited from MappableContainer) + void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member + void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member - using typename MappableContainer::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::FoldFunctor; + using typename FoldableContainer::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"