From 0d599672c459761b785d2a4039d6fdf7384733a0 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Sat, 24 Apr 2021 18:39:57 +0200 Subject: [PATCH] Library 3 container & iterator.hpp --- librerie/exercise3/container/container.hpp | 403 +++++++++------------ librerie/exercise3/iterator/iterator.hpp | 36 +- librerie/exercise3/list/list.cpp | 263 +++++++++++++- librerie/exercise3/list/list.hpp | 88 ++--- librerie/exercise3/queue/lst/queuelst.cpp | 69 +++- librerie/exercise3/queue/lst/queuelst.hpp | 42 +-- librerie/exercise3/queue/queue.hpp | 26 +- librerie/exercise3/queue/vec/queuevec.cpp | 188 +++++++++- librerie/exercise3/queue/vec/queuevec.hpp | 53 +-- librerie/exercise3/stack/lst/stacklst.cpp | 72 +++- librerie/exercise3/stack/lst/stacklst.hpp | 41 +-- librerie/exercise3/stack/stack.hpp | 26 +- librerie/exercise3/stack/vec/stackvec.cpp | 143 +++++++- librerie/exercise3/stack/vec/stackvec.hpp | 48 +-- librerie/exercise3/vector/vector.cpp | 162 ++++++++- librerie/exercise3/vector/vector.hpp | 86 ++--- 16 files changed, 1240 insertions(+), 506 deletions(-) mode change 100644 => 100755 librerie/exercise3/queue/lst/queuelst.cpp mode change 100644 => 100755 librerie/exercise3/queue/lst/queuelst.hpp mode change 100644 => 100755 librerie/exercise3/queue/queue.hpp mode change 100644 => 100755 librerie/exercise3/queue/vec/queuevec.cpp mode change 100644 => 100755 librerie/exercise3/queue/vec/queuevec.hpp mode change 100644 => 100755 librerie/exercise3/stack/lst/stacklst.cpp mode change 100644 => 100755 librerie/exercise3/stack/lst/stacklst.hpp mode change 100644 => 100755 librerie/exercise3/stack/stack.hpp mode change 100644 => 100755 librerie/exercise3/stack/vec/stackvec.cpp mode change 100644 => 100755 librerie/exercise3/stack/vec/stackvec.hpp diff --git a/librerie/exercise3/container/container.hpp b/librerie/exercise3/container/container.hpp index f9d5881..633b438 100644 --- a/librerie/exercise3/container/container.hpp +++ b/librerie/exercise3/container/container.hpp @@ -14,382 +14,313 @@ namespace lasd { /* ************************************************************************** */ class Container { - private: - // ... - protected: - // ... + ulong size = 0; public: // Destructor - // ~Container() specifiers - - /* ************************************************************************ */ + virtual ~Container() = default; // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Container& operator=(const Container&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. - - /* ************************************************************************ */ + Container& operator=(Container&&) noexcept = delete;; // Move assignment of abstract types should not be possible. // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const Container&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const Container&&) const noexcept = delete; // Comparison of abstract types might not be possible. + + // Specific member functions + virtual bool Empty() const noexcept { + return (size == 0); + } // (concrete function should not throw exceptions) + + virtual ulong Size() const noexcept { + return size; + } // (concrete function should not throw exceptions) + + virtual void Clear() = 0; +}; + +template +class LinearContainer : virtual public Container{ // Must extend Container + +private: + +protected: + +public: + + // Destructor + virtual ~LinearContainer() = default; + + // Copy assignment + LinearContainer& operator=(const LinearContainer&) = delete; // Copy assignment of abstract types should not be possible. + + // Move assignment + LinearContainer& operator=(LinearContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + + // Comparison operators + bool operator==(const LinearContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const LinearContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions + virtual Data& Front() const = 0; // (concrete function must throw std::length_error when empty) + virtual Data& Back() const = 0; // (concrete function must throw std::length_error when empty) - // type Empty() specifiers; // (concrete function should not throw exceptions) + virtual Data& operator[](const ulong) const = 0; // (concrete function must throw std::out_of_range when out of range) - // type Size() specifiers; // (concrete function should not throw exceptions) +}; - // type Clear() specifiers; +template +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 +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 MapFunctor; + + virtual void MapPreOrder(const MapFunctor, void*) = 0; + virtual void MapPostOrder(const MapFunctor, void*) = 0; + +}; + +template +class FoldableContainer : virtual public TestableContainer{ // Must extend TestableContainer + +private: + +protected: + +public: + + // Destructor + virtual ~FoldableContainer() = default; + + // Copy assignment + FoldableContainer& operator=(const FoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. + + // Move assignment + FoldableContainer& operator=(FoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. + + // Comparison operators + bool operator==(FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(FoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + + // Specific member functions + + typedef std::function 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 -class LinearContainer { // Must extend Container +class InOrderMappableContainer : virtual public MappableContainer { // Must extend MappableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~LinearContainer() specifiers + virtual ~InOrderMappableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + InOrderMappableContainer& operator=(const InOrderMappableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + InOrderMappableContainer& operator=(InOrderMappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types is possible. - // type operator!=(argument) specifiers; // Comparison of abstract types is possible. + bool operator==(const InOrderMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const InOrderMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Front() specifiers; // (concrete function must throw std::length_error when empty) - // type Back() specifiers; // (concrete function must throw std::length_error when empty) + using typename MappableContainer::MapFunctor; - // type operator[](argument) specifiers; // (concrete function must throw std::out_of_range when out of range) + virtual void MapInOrder(const MapFunctor, void*) = 0; }; /* ************************************************************************** */ template -class TestableContainer { // Must extend Container +class InOrderFoldableContainer : public virtual FoldableContainer { // Must extend FoldableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~TestableContainer() specifiers + virtual ~InOrderFoldableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + InOrderFoldableContainer& operator=(const InOrderFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + InOrderFoldableContainer operator=(InOrderFoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const InOrderFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const InOrderFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Exists(argument) specifiers; // (concrete function should not throw exceptions) + using typename MappableContainer::MapFunctor; + + virtual void FoldInOrder(const MapFunctor, const void*, void*) const = 0; }; /* ************************************************************************** */ template -class MappableContainer { // Must extend Container +class BreadthMappableContainer : virtual public MappableContainer { // Must extend MappableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~MappableContainer() specifiers + ~BreadthMappableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + BreadthMappableContainer& operator=(const BreadthMappableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + BreadthMappableContainer& operator=(BreadthMappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const BreadthMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const BreadthMappableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // typedef std::function MapFunctor; + using typename MappableContainer::MapFunctor; - // type MapPreOrder(arguments) specifiers; - // type MapPostOrder(arguments) specifiers; + void MapBreadth(const MapFunctor, void*) = 0; }; /* ************************************************************************** */ template -class FoldableContainer { // Must extend TestableContainer +class BreadthFoldableContainer : virtual public FoldableContainer { // Must extend FoldableContainer private: - // ... - protected: - // ... - public: // Destructor - // ~FoldableContainer() specifiers + virtual ~BreadthFoldableContainer() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + BreadthFoldableContainer& operator=(const BreadthFoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + BreadthFoldableContainer& operator=(BreadthFoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const BreadthFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const BreadthFoldableContainer&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // typedef std::function FoldFunctor; + using typename FoldableContainer::FoldFunctor; - // type FoldPreOrder(arguments) specifiers; - // type FoldPostOrder(arguments) specifiers; - - // type Exists(argument) specifiers; // Override TestableContainer member - -}; - -/* ************************************************************************** */ - -template -class InOrderMappableContainer { // Must extend MappableContainer - -private: - - // ... - -protected: - - // ... - -public: - - // Destructor - // ~InOrderMappableContainer() specifiers - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. - - // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. - - /* ************************************************************************ */ - - // Specific member functions - - // using typename MappableContainer::MapFunctor; - - // type MapInOrder(arguments) specifiers; - -}; - -/* ************************************************************************** */ - -template -class InOrderFoldableContainer { // Must extend FoldableContainer - -private: - - // ... - -protected: - - // ... - -public: - - // Destructor - // ~InOrderFoldableContainer() specifiers - - /* ************************************************************************ */ - - // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. - - // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. - - /* ************************************************************************ */ - - // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. - - /* ************************************************************************ */ - - // Specific member functions - - // using typename MappableContainer::MapFunctor; - - // type FoldInOrder(arguments) specifiers; - -}; - -/* ************************************************************************** */ - -template -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::MapFunctor; - - // type MapBreadth(arguments) specifiers; - -}; - -/* ************************************************************************** */ - -template -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::FoldFunctor; - - // type FoldBreadth(arguments) specifiers; + virtual void FoldBreadth(const FoldFunctor, const void*, void*) const = 0; }; diff --git a/librerie/exercise3/iterator/iterator.hpp b/librerie/exercise3/iterator/iterator.hpp index 249bf5b..6b682e4 100644 --- a/librerie/exercise3/iterator/iterator.hpp +++ b/librerie/exercise3/iterator/iterator.hpp @@ -13,78 +13,70 @@ class Iterator { private: - // ... - protected: - // ... - public: // Destructor - // ~Iterator() specifiers + virtual ~Iterator() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Iterator& operator=(const Iterator&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Iterator& operator=(Iterator&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const Iterator&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const Iterator&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type operator*() specifiers; // (concrete function must throw std::out_of_range when terminated) + virtual Data& operator*() = 0; // (concrete function must throw std::out_of_range when terminated) - // type Terminated() specifiers; // (concrete function should not throw exceptions) + virtual bool Terminated() noexcept = 0; // (concrete function should not throw exceptions) }; /* ************************************************************************** */ template -class ForwardIterator { // Must extend Iterator +class ForwardIterator : virtual public Iterator { // Must extend Iterator private: - // ... - protected: - // ... - public: // Destructor - // ~ForwardIterator() specifiers + virtual ~ForwardIterator() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + ForwardIterator& operator=(const ForwardIterator&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + ForwardIterator& operator=(ForwardIterator&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const ForwardIterator&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const ForwardIterator&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type operator++() specifiers; // (concrete function must throw std::out_of_range when terminated) + virtual ForwardIterator& operator++() = 0; // (concrete function must throw std::out_of_range when terminated) }; diff --git a/librerie/exercise3/list/list.cpp b/librerie/exercise3/list/list.cpp index d374ceb..3396301 100644 --- a/librerie/exercise3/list/list.cpp +++ b/librerie/exercise3/list/list.cpp @@ -3,8 +3,263 @@ namespace lasd { /* ************************************************************************** */ -// ... - -/* ************************************************************************** */ - +// Specific constructors +template +List::Node::Node(const Data& newValue){ + value = newValue; + next = nullptr; +} + +// Copy constructor +template +List::Node::Node(const Node& copyFrom){ + value = copyFrom.value; + next = nullptr; +} + +// Move constructor +template +List::Node::Node(Node&& moveFrom){ + std::swap(value, moveFrom.value); + std::swap(next, moveFrom.next); +} + +template +List::Node::Node(Data&& moveFrom){ + std::swap(value, moveFrom); +} + +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); +} + +template +List::List(const LinearContainer& con){ + for(ulong i = 0; i +List::List(const List& copyFrom){ + for(ulong i = 0; i +List::List(List&& moveFrom){ + std::swap(size, moveFrom.size); + std::swap(head, moveFrom.head); + std::swap(tail, moveFrom.tail); +} + +template +List::~List(){ + Clear(); +} + +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){ + std::swap(size, moveFrom.size); + std::swap(head, moveFrom.head); + std::swap(tail, moveFrom.tail); + } + return *this; + } + + template + bool List::operator==(const List& list) const noexcept{ + if(size != list.Size()) return false; + for(ulong i = 0 ; i < size ; ++i){ + if((*this)[i] != list[i]) return false; + } + return true; + } + +template +bool List::operator!=(const List& list) const noexcept{ + return !(*this==list); +} + +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(std::move(data)); + tmp->next = head; + head = tmp; + size++; + if(size == 1){ + tail = head; + } +} + +template + void List::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 +Data List::FrontNRemove(){ + if(head == nullptr){ + throw std::length_error("List is empty!"); + } + else{ + Data value = head->value; + RemoveFromFront(); + return value; + } +} + +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/exercise3/list/list.hpp b/librerie/exercise3/list/list.hpp index c80e4df..926649b 100644 --- a/librerie/exercise3/list/list.hpp +++ b/librerie/exercise3/list/list.hpp @@ -13,150 +13,150 @@ namespace lasd { /* ************************************************************************** */ template -class List { // Must extend LinearContainer, MappableContainer, and FoldableContainer +class List : virtual public LinearContainer, + virtual public MappableContainer, + virtual public FoldableContainer{ // Must extend LinearContainer, MappableContainer, and FoldableContainer private: - // ... - protected: - // using LinearContainer::???; + using LinearContainer:: 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&); // 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::MapFunctor; + using typename MappableContainer::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::FoldFunctor; + using typename FoldableContainer::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 }; diff --git a/librerie/exercise3/queue/lst/queuelst.cpp b/librerie/exercise3/queue/lst/queuelst.cpp old mode 100644 new mode 100755 index d374ceb..9153b80 --- a/librerie/exercise3/queue/lst/queuelst.cpp +++ b/librerie/exercise3/queue/lst/queuelst.cpp @@ -1,10 +1,73 @@ namespace lasd { -/* ************************************************************************** */ +template +QueueLst::QueueLst(const LinearContainer& linear) + : List(linear) {} -// ... +template +QueueLst::QueueLst(const QueueLst& copyFrom) + : List(copyFrom) {} -/* ************************************************************************** */ +template +QueueLst::QueueLst(QueueLst&& moveFrom) noexcept + : List(std::move(moveFrom)) {} + +template +QueueLst::~QueueLst(){ + Clear(); +} + +template +QueueLst& QueueLst::operator=(const QueueLst& toCopy){ + List::operator=(toCopy); + return *this; +} + +template +QueueLst& QueueLst::operator=(QueueLst&& toMove) noexcept{ + List::operator=(std::move(toMove)); + return *this; +} + +template +bool QueueLst::operator==(const QueueLst& queuelist) const noexcept{ + return List::operator==(queuelist); + } + +template +bool QueueLst::operator!=(const QueueLst& queuelist) const noexcept{ + return List::operator!=(queuelist); + } + +template + void QueueLst::Enqueue(const Data& data){ + List::InsertAtBack(data); + } + +template +void QueueLst::Enqueue(Data&& data){ + List::InsertAtBack(std::move(data)); + } + +template +Data& QueueLst::Head() const{ + return List::Front(); +} + +template +void QueueLst::Dequeue(){ + List::RemoveFromFront(); +} + +template +Data QueueLst::HeadNDequeue(){ + return List::FrontNRemove(); +} + +template +void QueueLst::Clear(){ + List::Clear(); +} } diff --git a/librerie/exercise3/queue/lst/queuelst.hpp b/librerie/exercise3/queue/lst/queuelst.hpp old mode 100644 new mode 100755 index 4f1954c..92ea410 --- a/librerie/exercise3/queue/lst/queuelst.hpp +++ b/librerie/exercise3/queue/lst/queuelst.hpp @@ -14,70 +14,70 @@ namespace lasd { /* ************************************************************************** */ template -class QueueLst { // Must extend Queue and List +class QueueLst : virtual public Queue, + virtual protected List{ // Must extend Queue and List private: - // ... - protected: - // using List::???; - - // ... + using List::head; + using List::tail; + using List::size; + using typename List::Node; public: // Default constructor - // QueueLst() specifier; + QueueLst() = default; /* ************************************************************************ */ // Specific constructor - // QueueLst(argument) specifiers; // A queue obtained from a LinearContainer + QueueLst(const LinearContainer&); // A queue obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // QueueLst(argument); + QueueLst(const QueueLst&); // Move constructor - // QueueLst(argument); + QueueLst(QueueLst&&) noexcept; /* ************************************************************************ */ // Destructor - // ~QueueLst() specifier; + ~QueueLst(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + QueueLst& operator=(const QueueLst&); // Move assignment - // type operator=(argument); + QueueLst& operator=(QueueLst&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const QueueLst&) const noexcept; + bool operator!=(const QueueLst&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Queue) - // type Enqueue(argument) specifiers; // Override Queue member (copy of the value) - // type Enqueue(argument) specifiers; // Override Queue member (move of the value) - // type Head() specifiers; // Override Queue member (must throw std::length_error when empty) - // type Dequeue() specifiers; // Override Queue member (must throw std::length_error when empty) - // type HeadNDequeue() specifiers; // Override Queue member (must throw std::length_error when empty) + void Enqueue(const Data&) override; // Override Queue member (copy of the value) + void Enqueue(Data&&) override; // Override Queue member (move of the value) + Data& Head() const override; // Override Queue member (must throw std::length_error when empty) + void Dequeue() override; // Override Queue member (must throw std::length_error when empty) + Data HeadNDequeue() override; // Override Queue member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member }; diff --git a/librerie/exercise3/queue/queue.hpp b/librerie/exercise3/queue/queue.hpp old mode 100644 new mode 100755 index 6b612e4..ba810d8 --- a/librerie/exercise3/queue/queue.hpp +++ b/librerie/exercise3/queue/queue.hpp @@ -13,44 +13,40 @@ namespace lasd { /* ************************************************************************** */ template -class Queue { // Must extend Container +class Queue : virtual public Container{ // Must extend Container private: - // ... - protected: - // ... - public: // Destructor - // ~Queue() specifiers + ~Queue() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Queue& operator=(const Queue&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Queue& operator=(Queue&&) = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const Queue&) const noexcept = delete; // Comparison of abstract types might not be possible. + bool operator!=(const Queue&) const noexcept = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Enqueue(argument) specifiers; // Copy of the value - // type Enqueue(argument) specifiers; // Move of the value - // type Head() specifiers; // (concrete function must throw std::length_error when empty) - // type Dequeue() specifiers; // (concrete function must throw std::length_error when empty) - // type HeadNDequeue() specifiers; // (concrete function must throw std::length_error when empty) + virtual void Enqueue(const Data&) = 0; // Copy of the value + virtual void Enqueue(Data&&) = 0; // Move of the value + virtual Data& Head() const = 0; // (concrete function must throw std::length_error when empty) + virtual void Dequeue() = 0; // (concrete function must throw std::length_error when empty) + virtual Data HeadNDequeue() = 0; // (concrete function must throw std::length_error when empty) }; diff --git a/librerie/exercise3/queue/vec/queuevec.cpp b/librerie/exercise3/queue/vec/queuevec.cpp old mode 100644 new mode 100755 index d374ceb..42f6857 --- a/librerie/exercise3/queue/vec/queuevec.cpp +++ b/librerie/exercise3/queue/vec/queuevec.cpp @@ -1,10 +1,192 @@ namespace lasd { -/* ************************************************************************** */ +template +QueueVec::QueueVec(){ + size = 4; + rear = 0; + front = 0; + Elements = new Data[size]; +} -// ... +template +QueueVec::QueueVec(const LinearContainer& linear){ + size = linear.Size()+1; // 1 free cell + Elements = new Data[size]; //forse da espandere + for(ulong i=0 ; i +QueueVec::QueueVec(const QueueVec& toCopy){ + size = toCopy.size; + ulong index_of_the_element = toCopy.front , i=0; + Elements = new Data[size]; + + while(index_of_the_element != toCopy.rear){ + Elements[i] = toCopy[index_of_the_element]; + ++i; + index_of_the_element = (index_of_the_element+1)%size; + } + front = 0; + rear = i; +} + +template +QueueVec::QueueVec(QueueVec&& toMove) noexcept{ + Clear(); + std::swap(Elements, toMove.Elements); + std::swap(rear, toMove.rear); + std::swap(front, toMove.front); + std::swap(size, toMove.size); +} + +template +QueueVec::~QueueVec(){ + //vector destructor will be automatically called I hope +} + +template +QueueVec& QueueVec::operator=(const QueueVec& toCopy){ + QueueVec* tmpQueue = new QueueVec(toCopy); + std::swap(*tmpQueue, *this); + delete tmpQueue; + return *this; +} + +template +QueueVec& QueueVec::operator=(QueueVec&& toMove) noexcept{ + std::swap(Elements, toMove.Elements); + std::swap(size, toMove.size); + std::swap(rear, toMove.rear); + std::swap(front, toMove.front); + return *this; +} + +template +bool QueueVec::operator==(const QueueVec& toCompare) const noexcept{ + if(Size() == toCompare.Size()){ + ulong indexToCompare = toCompare.front; + ulong index = front; + while(indexToCompare != toCompare.rear){ + if(Elements[index]!=toCompare[indexToCompare]){ + return false; + } + index = (index+1)%size; + indexToCompare = (indexToCompare+1)%size; + } + return true; + }else{ + return false; + } +} + +template +bool QueueVec::operator!=(const QueueVec& toCompare) const noexcept{ + return !(*this == toCompare); +} + +template +void QueueVec::Enqueue(const Data& data){ + if((rear+1)%size == front){ + Expand(); + } + Elements[rear] = data; + rear = (rear + 1) % size; +} + +template +void QueueVec::Enqueue(Data&& data){ + if((rear+1)%size == front){ + Expand(); + } + std::swap(Elements[rear],data); + rear = (rear + 1) % size; +} + +template +Data& QueueVec::Head() const{ + if(Size()<=0){ + throw std::length_error("Queue is empty!"); + } + return Elements[front]; +} + +template +void QueueVec::Dequeue(){ + if(Size() <= 0){ + throw std::length_error("Queue is empty!"); + } + front = (front + 1) % size; + if(Size() < size/4){ + Reduce(); + } +} + +template +Data QueueVec::HeadNDequeue(){ + Data tmp = Head(); + Dequeue(); + return tmp; +} + +template +bool QueueVec::Empty() const noexcept{ + return (front == rear); +} + +template +ulong QueueVec::Size() const noexcept{ + //if(size == 0) return 0; // this won't ever get executed, it's here just in case + return ((rear + size) - front) % size; +} + +template +void QueueVec::Clear(){ + if(size!=4){ + delete[] Elements; + Elements = new Data[4]; + size = 4; + } + front = 0; + rear = 0; +} + +template +void QueueVec::Expand(){ + Data* tmp = new Data[size * 2]; + ulong current_index = front , i=0; + while(current_index != rear){ + tmp[i] = Elements[current_index]; + current_index = (current_index+1)%size; + ++i; + } + delete[] Elements; + Elements = tmp; + front = 0; + rear = i; + size *= 2; +} + +template +void QueueVec::Reduce(){ + if(size<=4) return; // we are not going to have vectors with less than 4 Elements + ulong newsize = (ulong)size/2; + Data* tmp = new Data[newsize]; + ulong current_index = front , i=0; + while(current_index != rear){ + tmp[i] = Elements[current_index]; + current_index = (current_index+1)%size; + ++i; + } + delete[] Elements; + Elements = tmp; + front = 0; + rear = i; + size = newsize; +} } diff --git a/librerie/exercise3/queue/vec/queuevec.hpp b/librerie/exercise3/queue/vec/queuevec.hpp old mode 100644 new mode 100755 index 97b7e97..1f9bbe5 --- a/librerie/exercise3/queue/vec/queuevec.hpp +++ b/librerie/exercise3/queue/vec/queuevec.hpp @@ -14,82 +14,83 @@ namespace lasd { /* ************************************************************************** */ template -class QueueVec { // Must extend Queue and Vector +class QueueVec : virtual public Queue, + virtual protected Vector{ // Must extend Queue and Vector private: - // ... - protected: - // using Vector::???; - - // ... + using Vector::Elements; + using Vector::size; // dimension of the array + ulong front = 0; + ulong rear = 0; public: // Default constructor - // QueueVec() specifier; + QueueVec(); /* ************************************************************************ */ // Specific constructor - // QueueVec(argument) specifiers; // A queue obtained from a LinearContainer + QueueVec(const LinearContainer&); // A queue obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // QueueVec(argument); + QueueVec(const QueueVec&); // Move constructor - // QueueVec(argument); + QueueVec(QueueVec&&) noexcept; /* ************************************************************************ */ // Destructor - // ~QueueVec() specifier; + virtual ~QueueVec(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + QueueVec& operator=(const QueueVec&); // Move assignment - // type operator=(argument); + QueueVec& operator=(QueueVec&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const QueueVec&) const noexcept; + bool operator!=(const QueueVec&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Queue) - // type Enqueue(argument) specifiers; // Override Queue member (copy of the value) - // type Enqueue(argument) specifiers; // Override Queue member (move of the value) - // type Head() specifiers; // Override Queue member (must throw std::length_error when empty) - // type Dequeue() specifiers; // Override Queue member (must throw std::length_error when empty) - // type HeadNDequeue() specifiers; // Override Queue member (must throw std::length_error when empty) + void Enqueue(const Data&) override; // Override Queue member (copy of the value) + void Enqueue(Data&&) override; // Override Queue member (move of the value) + Data& Head() const override; // Override Queue member (must throw std::length_error when empty) + void Dequeue() override; // Override Queue member (must throw std::length_error when empty) + Data HeadNDequeue() override; // Override Queue member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Empty() specifiers; // Override Container member + bool Empty() const noexcept override; // Override Container member - // type Size() specifiers; // Override Container member + ulong Size() const noexcept override; // Override Container member - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member protected: // Auxiliary member functions - // type Expand() specifiers; - // type Reduce() specifiers; - // type SwapVectors(arguments) specifiers; + void Expand(); + void Reduce(); + + //void SwapVectors(arguments) specifiers; }; diff --git a/librerie/exercise3/stack/lst/stacklst.cpp b/librerie/exercise3/stack/lst/stacklst.cpp old mode 100644 new mode 100755 index d374ceb..f1f24d8 --- a/librerie/exercise3/stack/lst/stacklst.cpp +++ b/librerie/exercise3/stack/lst/stacklst.cpp @@ -3,7 +3,77 @@ namespace lasd { /* ************************************************************************** */ -// ... +// Constructors +template +StackLst::StackLst(const LinearContainer& linear) + : List(linear){} + +template +StackLst::StackLst(const StackLst& stcklist) + : List(stcklist){} + +template +StackLst::StackLst(StackLst&& stcklist) noexcept + : List(std::move(stcklist)){} + +// Destructor +template +StackLst::~StackLst(){ + Clear(); +} + +template +StackLst& StackLst::operator=(const StackLst& copyFrom){ + List::operator=(copyFrom); + return *this; +} + +template +StackLst& StackLst::operator=(StackLst&& moveFrom) noexcept{ + List::operator=(std::move(moveFrom)); + return *this; +} + +template +bool StackLst::operator==(const StackLst& stcklist) const noexcept{ + return List::operator==(stcklist); +} + +template +bool StackLst::operator!=(const StackLst& stcklist) const noexcept{ + return List::operator!=(stcklist); +} + +// Specific member functions (inherited from Stack) +template +void StackLst::Push(const Data& element){ + List::InsertAtFront(element); +} + +template +void StackLst::Push(Data&& element) noexcept{ + List::InsertAtFront(element); +} + +template +Data& StackLst::Top() const{ + return List::Front(); +} + +template +void StackLst::Pop(){ + List::RemoveFromFront(); +} + +template +Data StackLst::TopNPop(){ + return List::FrontNRemove(); +} + +template +void StackLst::Clear(){ + List::Clear(); +} /* ************************************************************************** */ diff --git a/librerie/exercise3/stack/lst/stacklst.hpp b/librerie/exercise3/stack/lst/stacklst.hpp old mode 100644 new mode 100755 index 67fbb32..16ce8fb --- a/librerie/exercise3/stack/lst/stacklst.hpp +++ b/librerie/exercise3/stack/lst/stacklst.hpp @@ -14,70 +14,69 @@ namespace lasd { /* ************************************************************************** */ template -class StackLst { // Must extend Stack and List +class StackLst : virtual public Stack, + virtual protected List { // Must extend Stack and List private: - // ... - protected: - // using List::???; - - // ... + using List::head; + using List::size; + using typename List::Node; public: // Default constructor - // StackLst() specifier; + StackLst() = default; /* ************************************************************************ */ // Specific constructor - // StackLst(argument) specifiers; // A stack obtained from a LinearContainer + StackLst(const LinearContainer&); // A stack obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // StackLst(argument); + StackLst(const StackLst&); // Move constructor - // StackLst(argument); + StackLst(StackLst&&) noexcept; /* ************************************************************************ */ // Destructor - // ~StackLst() specifier; + virtual ~StackLst(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + StackLst& operator=(const StackLst&); // Move assignment - // type operator=(argument); + StackLst& operator=(StackLst&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const StackLst&) const noexcept; + bool operator!=(const StackLst&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Stack) - // type Push(argument) specifiers; // Override Stack member (copy of the value) - // type Push(argument) specifiers; // Override Stack member (move of the value) - // type Top() specifiers; // Override Stack member (must throw std::length_error when empty) - // type Pop() specifiers; // Override Stack member (must throw std::length_error when empty) - // type TopNPop() specifiers; // Override Stack member (must throw std::length_error when empty) + void Push(const Data&) override; // Override Stack member (copy of the value) + void Push(Data&&) noexcept override; // Override Stack member (move of the value) + Data& Top() const override; // Override Stack member (must throw std::length_error when empty) + void Pop() override; // Override Stack member (must throw std::length_error when empty) + Data TopNPop() override; // Override Stack member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member }; diff --git a/librerie/exercise3/stack/stack.hpp b/librerie/exercise3/stack/stack.hpp old mode 100644 new mode 100755 index 73a3a7c..fee854b --- a/librerie/exercise3/stack/stack.hpp +++ b/librerie/exercise3/stack/stack.hpp @@ -13,44 +13,40 @@ namespace lasd { /* ************************************************************************** */ template -class Stack { // Must extend Container +class Stack : virtual public Container { // Must extend Container private: - // ... - protected: - // ... - public: // Destructor - // ~Stack() specifiers + virtual ~Stack() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Stack& operator=(const Stack&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Stack&operator=(Stack&&) = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const Stack&) = delete; // Comparison of abstract types might not be possible. + bool operator!=(Stack&&) = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Push(argument) specifiers; // Copy of the value - // type Push(argument) specifiers; // Move of the value - // type Top() specifiers; // (concrete function must throw std::length_error when empty) - // type Pop() specifiers; // (concrete function must throw std::length_error when empty) - // type TopNPop() specifiers; // (concrete function must throw std::length_error when empty) + virtual void Push(const Data&) = 0; // Copy of the value + virtual void Push(Data&&) = 0; // Move of the value + virtual Data& Top() const = 0; // (concrete function must throw std::length_error when empty) + virtual void Pop() = 0; // (concrete function must throw std::length_error when empty) + virtual Data TopNPop() = 0; // (concrete function must throw std::length_error when empty) }; diff --git a/librerie/exercise3/stack/vec/stackvec.cpp b/librerie/exercise3/stack/vec/stackvec.cpp old mode 100644 new mode 100755 index d374ceb..eabb6ad --- a/librerie/exercise3/stack/vec/stackvec.cpp +++ b/librerie/exercise3/stack/vec/stackvec.cpp @@ -2,9 +2,142 @@ namespace lasd { /* ************************************************************************** */ - -// ... - -/* ************************************************************************** */ - +// constructors +template +StackVec::StackVec(){ + size = 4; // default vector is instantiated with 4 cells + stackSize = 0; + Elements = new Data[size]; +} + +template +StackVec::StackVec(const LinearContainer& linear) + : Vector(linear){ + stackSize = linear.Size(); // the array is full +} + +template +StackVec::StackVec(const StackVec& stckvec) + : Vector(stckvec){ + stackSize = stckvec.Size(); // the array is full +} + + +template +StackVec::StackVec(StackVec&& toMove) noexcept + : Vector(std::move(toMove)){ + std::swap(stackSize, toMove.stackSize); +} + +template +StackVec::~StackVec(){ + // Vector destructor will be called automatically +} + +template +StackVec& StackVec::operator=(const StackVec& copyFrom){ + Vector::operator=(copyFrom); // espandere di un po' forse + stackSize = copyFrom.Size(); + return *this; +} + +template +StackVec& StackVec::operator=(StackVec&& moveFrom) noexcept{ + Vector::operator=(std::move(moveFrom)); + stackSize = moveFrom.Size(); + return *this; +} + +template +bool StackVec::operator==(const StackVec& toCompare) const noexcept{ + if(stackSize == toCompare.Size()){ + for(ulong i=0 ; i +bool StackVec::operator!=(const StackVec& toCompare) const noexcept{ + return !(*this == toCompare); +} + +// Specific member functions (inherited from Stack) +template +void StackVec::Push(const Data& data){ + if(size == stackSize){ + Expand(); + } + Elements[stackSize] = data; + ++stackSize; +} + +template +void StackVec::Push(Data&& data){ + if(size == stackSize){ + Expand(); + } + std::swap(Elements[stackSize], data); + ++stackSize; +} + +template +Data& StackVec::Top() const{ + if(stackSize == 0){ + throw std::length_error("Empty Stack!"); + } + return Elements[stackSize-1]; +} + +template +void StackVec::Pop(){ + if(stackSize==0){ + throw std::length_error("Empty Stack!"); + } + --stackSize; + if(stackSize < (int)(size/4)){ + Reduce(); + } +} + +template +Data StackVec::TopNPop(){ + Data data = Top(); + Pop(); + return data; +} + +template +bool StackVec::Empty() const noexcept{ + return (stackSize == 0); +} + +template +ulong StackVec::Size() const noexcept{ + return stackSize; +} + +template +void StackVec::Expand(){ + Vector::Resize(size * 2); +} + +template +void StackVec::Reduce(){ + if(size <= 4) return; // we're not going to have vectors with less than 4 cells + Vector::Resize((ulong)size/2); +} + +template +void StackVec::Clear(){ + delete [] Elements; + size = 4; + stackSize = 0; + Elements = new Data[size]; +} } diff --git a/librerie/exercise3/stack/vec/stackvec.hpp b/librerie/exercise3/stack/vec/stackvec.hpp old mode 100644 new mode 100755 index 5e65367..d13a313 --- a/librerie/exercise3/stack/vec/stackvec.hpp +++ b/librerie/exercise3/stack/vec/stackvec.hpp @@ -14,81 +14,81 @@ namespace lasd { /* ************************************************************************** */ template -class StackVec { // Must extend Stack and Vector +class StackVec : virtual public Stack, + virtual protected Vector{ // Must extend Stack and Vector private: - // ... - protected: - // using Vector::???; + ulong stackSize = 0; // first empty cell and # of elements in the vector + using Vector::Elements; + using Vector::size; // dimension of the vector - // ... public: // Default constructor - // StackVec() specifier; + StackVec(); /* ************************************************************************ */ // Specific constructor - // StackVec(argument) specifiers; // A stack obtained from a LinearContainer + StackVec(const LinearContainer&); // A stack obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // StackVec(argument); + StackVec(const StackVec&); // Move constructor - // StackVec(argument); + StackVec(StackVec&&) noexcept; /* ************************************************************************ */ // Destructor - // ~StackVec() specifier; + virtual ~StackVec(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + StackVec& operator=(const StackVec&); // Move assignment - // type operator=(argument); + StackVec& operator=(StackVec&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const StackVec&) const noexcept; + bool operator!=(const StackVec&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Stack) - // type Push(argument) specifiers; // Override Stack member (copy of the value) - // type Push(argument) specifiers; // Override Stack member (move of the value) - // type Top() specifiers; // Override Stack member (must throw std::length_error when empty) - // type Pop() specifiers; // Override Stack member (must throw std::length_error when empty) - // type TopNPop() specifiers; // Override Stack member (must throw std::length_error when empty) + void Push(const Data&) override; // Override Stack member (copy of the value) + void Push(Data&&) override; // Override Stack member (move of the value) + Data& Top() const override; // Override Stack member (must throw std::length_error when empty) + void Pop() override; // Override Stack member (must throw std::length_error when empty) + Data TopNPop() override; // Override Stack member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Empty() specifiers; // Override Container member + bool Empty() const noexcept override; // Override Container member - // type Size() specifiers; // Override Container member + ulong Size() const noexcept override; // Override Container member - // type Clear() specifiers; // Override Container member + void Clear() override;// Override Container member protected: // Auxiliary member functions - // type Expand() specifiers; - // type Reduce() specifiers; + void Expand(); + void Reduce(); }; diff --git a/librerie/exercise3/vector/vector.cpp b/librerie/exercise3/vector/vector.cpp index d374ceb..a7b3d5e 100644 --- a/librerie/exercise3/vector/vector.cpp +++ b/librerie/exercise3/vector/vector.cpp @@ -1,10 +1,160 @@ namespace lasd { -/* ************************************************************************** */ - -// ... - -/* ************************************************************************** */ - +template +Vector::Vector(const ulong newsize){ + Elements = new Data[newsize]{}; + size = newsize; +} + +template +Vector::Vector(const LinearContainer& con){ + size = con.Size(); + Elements = new Data[size]{}; + for(ulong i=0; i +Vector::Vector(const Vector& vec){ + size = vec.size; + Elements = new Data[size]{}; + for(ulong i=0; i + Vector::Vector(Vector&& vec)noexcept{ + std::swap(Elements, vec.Elements); + std::swap(size, vec.size); + } + + // Destructor +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(); + } + 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/exercise3/vector/vector.hpp b/librerie/exercise3/vector/vector.hpp index 6f127ff..2ae8fed 100644 --- a/librerie/exercise3/vector/vector.hpp +++ b/librerie/exercise3/vector/vector.hpp @@ -13,99 +13,65 @@ namespace lasd { /* ************************************************************************** */ template -class Vector { // Must extend LinearContainer, MappableContainer, and FoldableContainer +class Vector : virtual public LinearContainer, + virtual public MappableContainer, + virtual public FoldableContainer{ // Must extend LinearContainer, MappableContainer, and FoldableContainer private: - // ... protected: - // using LinearContainer::???; - - // ... + using LinearContainer::size; + Data* Elements = nullptr; public: - // Default constructor - // Vector() specifiers; - - /* ************************************************************************ */ + Vector() = default; // Specific constructors - // Vector(argument) specifiers; // A vector with a given initial dimension - // Vector(argument) specifiers; // A vector obtained from a LinearContainer - - /* ************************************************************************ */ + Vector(const ulong); // A vector with a given initial dimension + Vector(const LinearContainer&); // A vector obtained from a LinearContainer // Copy constructor - // Vector(argument) specifiers; + Vector(const Vector&); // Move constructor - // Vector(argument) specifiers; - - /* ************************************************************************ */ + Vector(Vector&&)noexcept; // Destructor - // ~Vector() specifiers; - - /* ************************************************************************ */ + ~Vector(); // Copy assignment - // type operator=(argument) specifiers; + Vector& operator=(const Vector&); // Move assignment - // type operator=(argument) specifiers; - - /* ************************************************************************ */ + Vector& operator=(Vector&&) noexcept; // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const Vector&) const noexcept; + bool operator!=(const Vector&) const noexcept; - /* ************************************************************************ */ + void Resize(const ulong); // Resize the vector to a given size - // Specific member functions + void Clear() override; // Override Container member - // type Resize(argument) specifiers; // Resize the vector to a given size + Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty) + Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty) - /* ************************************************************************ */ + Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range) - // Specific member functions (inherited from Container) + using typename MappableContainer::MapFunctor; - // type Clear() specifiers; // Override Container member + void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member + void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member - /* ************************************************************************ */ - - // Specific member functions (inherited from LinearContainer) - - // type Front() specifiers; // Override LinearContainer member (must throw std::length_error when empty) - // type Back() specifiers; // Override LinearContainer member (must throw std::length_error when empty) - - // type operator[](argument) specifiers; // Override LinearContainer member (must throw std::out_of_range when out of range) - - /* ************************************************************************ */ - - // Specific member functions (inherited from MappableContainer) - - // using typename MappableContainer::MapFunctor; - - // type MapPreOrder(arguments) specifiers; // Override MappableContainer member - // type MapPostOrder(arguments) specifiers; // Override MappableContainer member - - /* ************************************************************************ */ - - // Specific member functions (inherited from FoldableContainer) - - // using typename FoldableContainer::FoldFunctor; - - // type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member - // type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member + 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"