diff --git a/librerie/exercise1/list/list.cpp b/librerie/exercise1/list/list.cpp index d374ceb..87c2860 100644 --- a/librerie/exercise1/list/list.cpp +++ b/librerie/exercise1/list/list.cpp @@ -1,10 +1,19 @@ - namespace lasd { + // Specific constructor + template + List::Node::Node(Data newValue){ + value = newValue; + } -/* ************************************************************************** */ - -// ... - -/* ************************************************************************** */ + // Copy constructor + template + List::Node::Node(const Data& copyFrom){ + value = copyFrom.value; + } + // Move constructor + template + List::Node::Node(Data&& moveFrom){ + + } } diff --git a/librerie/exercise1/list/list.hpp b/librerie/exercise1/list/list.hpp index c80e4df..7d91aa8 100644 --- a/librerie/exercise1/list/list.hpp +++ b/librerie/exercise1/list/list.hpp @@ -13,150 +13,144 @@ 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(Data); /* ********************************************************************** */ // Copy constructor - // ... + Node(const Node&); // Move constructor - // ... + Node(Node&&); /* ********************************************************************** */ // Destructor - // ... + ~Node() = default; /* ********************************************************************** */ // Comparison operators - // ... - - /* ********************************************************************** */ - - // Specific member functions - - // ... - + bool operator==(const Node&) const noexcept; + bool operator!=(const Node&) const noexcept; }; - // ... + 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; + //Move constructor + 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() override const; // Override LinearContainer member (must throw std::length_error when empty) + Data& Back() override const; // Override LinearContainer member (must throw std::length_error when empty) - // 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/exercise1/vector/vector.cpp b/librerie/exercise1/vector/vector.cpp index 316d1dd..1c72e0a 100644 --- a/librerie/exercise1/vector/vector.cpp +++ b/librerie/exercise1/vector/vector.cpp @@ -121,3 +121,60 @@ void Vector::Clear(){ Elements = nullptr; size = 0; } + +template typename +Data& Vector::Front() const{ + if(size!=0){ + return Elements[0]; + }else{ + return std::length_error("Access to an empty vector"); + } +} + +template typename +Data& Vector::Back() const{ + if(size!=0){ + return Elements[size-1]; + }else{ + return std::length_error("Access to an empty vector"); + } +} + +template typename +Data& Vector::operator[](const ulong index) const{ + if(index < size){ + return Elements[index]; + }else{ + throw std::out_of_range("Access at index " + std::to_string(index) + " invalid because the vector size is" + std::to_string(size)); + } +} + +template typename +void Vector::MapPreOrder(const MapFunctor fun, void* par){ + for(ulong i = 0 ; i +void Vector::MapPostOrder(const MapFunctor fun, void* par){ + ulong i = size; + while(i>0){ + fun(Elements[--i], par); + } +} + +template typename +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 i = size; + while(i>0){ + fun(Elements[--i], par, acc); + } +}