From 3e73aeb28d209fa8a940709d6030386d2fbd086c Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Tue, 30 Mar 2021 18:59:56 +0200 Subject: [PATCH] Esercizio 1 vector.hpp , vector.cpp (non completo) --- librerie/exercise1/container/container.hpp | 12 +- librerie/exercise1/main.cpp | 10 +- librerie/exercise1/vector/vector.cpp | 121 ++++++++++++++++++++- librerie/exercise1/vector/vector.hpp | 60 +++++----- 4 files changed, 154 insertions(+), 49 deletions(-) diff --git a/librerie/exercise1/container/container.hpp b/librerie/exercise1/container/container.hpp index 2eda291..bd42995 100644 --- a/librerie/exercise1/container/container.hpp +++ b/librerie/exercise1/container/container.hpp @@ -32,7 +32,7 @@ public: Container& operator=(const Container&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - Container& operator=(Container&&) = delete; // Move assignment of abstract types should not be possible. + Container& operator=(Container&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ @@ -115,7 +115,7 @@ public: TestableContainer& operator=(const TestableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - TestableContainer& operator=(TestableContainer&&) = delete; // Move assignment of abstract types should not be possible. + TestableContainer& operator=(TestableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ @@ -151,7 +151,7 @@ public: MappableContainer& operator=(const MappableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - MappableContainer& operator=(MappableContainer&&) = delete; // Move assignment of abstract types should not be possible. + MappableContainer& operator=(MappableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ @@ -165,7 +165,7 @@ public: typedef std::function MapFunctor; - virtual voi MapPreOrder(const MapFunctor, void*) = 0; + virtual void MapPreOrder(const MapFunctor, void*) = 0; virtual void MapPostOrder(const MapFunctor, void*) = 0; }; @@ -190,7 +190,7 @@ public: FoldableContainer& operator=(const FoldableContainer&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - FoldableContainer& operator=(FoldableContainer&&) = delete; // Move assignment of abstract types should not be possible. + FoldableContainer& operator=(FoldableContainer&&) noexcept = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ @@ -207,7 +207,7 @@ public: 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 + virtual bool Exists(const Data&) const noexcept override; // Override TestableContainer member }; diff --git a/librerie/exercise1/main.cpp b/librerie/exercise1/main.cpp index 34faa12..16c4875 100644 --- a/librerie/exercise1/main.cpp +++ b/librerie/exercise1/main.cpp @@ -1,8 +1,8 @@ -#include "zlasdtest/test.hpp" - -#include "zmytest/test.hpp" - +// #include "zlasdtest/test.hpp" +// +// #include "zmytest/test.hpp" +#include "container/container.hpp" /* ************************************************************************** */ #include @@ -11,6 +11,6 @@ int main() { std::cout << "Lasd Libraries 2020" << std::endl; - lasdtest(); // To call in the menu of your library test! + //lasdtest(); // To call in the menu of your library test! return 0; } diff --git a/librerie/exercise1/vector/vector.cpp b/librerie/exercise1/vector/vector.cpp index d374ceb..316d1dd 100644 --- a/librerie/exercise1/vector/vector.cpp +++ b/librerie/exercise1/vector/vector.cpp @@ -1,10 +1,123 @@ - namespace lasd { + // Specific constructor + template typename + Vector::Vector(const ulong dimension){ + Elements = new Data[dimenion] {}; + size = dimenion; + } -/* ************************************************************************** */ + // Specific constructor + template typename + Vector::Vector(const LinearContainer& con){ + size = con.Size(); + Elements = new Data[size] {}; + for(ulong i ; 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 typename + Vector::~Vector(){ + delete[] Elements; + } + + // Copy assignment + template typename + Vector& Vector::operator=(const Vector& vec){ + Vector* tmpvec = new Vector(vec); + std::swap(*tmpvec,*this); + delete tmpvec; + return *this; + } + + // Move assignment + template typename + Vector& Vector::operator=(Vector&& vec) noexcept{ + std::swap(Elements, vec.Elements); + std::swap(size, vec.size); + return *this; + } + + // Comparison operators + template typename + bool Vector::operator==(const Vector& vec) const noexcept{ + if(size!=vec.size) return false; + for(ulong i=0 ; i + bool Vector::operator!=(const Vector& vec) const noexcept{ + return !(*this == vec); + } + + // Specific member function + // template typename + // void Vector::Resize(const ulong newsize){ + // if(newsize == 0){ + // Clear(); + // } + // else{ + // size = newsize; + // if(newsize < size){ + // Data* tmpvec = new Data[newsize] {}; + // for(ulong i=0;i size){ + // Data* tmpvec = new Data[newsize] {}; + // for(ulong i=0;i + 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; } diff --git a/librerie/exercise1/vector/vector.hpp b/librerie/exercise1/vector/vector.hpp index 6f127ff..8fedf96 100644 --- a/librerie/exercise1/vector/vector.hpp +++ b/librerie/exercise1/vector/vector.hpp @@ -2,105 +2,97 @@ #ifndef VECTOR_HPP #define VECTOR_HPP -/* ************************************************************************** */ - #include "../container/container.hpp" -/* ************************************************************************** */ - 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; /* ************************************************************************ */ // Specific member functions - // type Resize(argument) specifiers; // Resize the vector to a given size + void Resize(const ulong); // Resize the vector to a given size /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member /* ************************************************************************ */ // Specific member functions (inherited from LinearContainer) - // type Front() specifiers; // Override LinearContainer member (must throw std::length_error when empty) - // type Back() specifiers; // Override LinearContainer member (must throw std::length_error when empty) + Data& Front() const override; // Override LinearContainer member (must throw std::length_error when empty) + Data& Back() const override; // Override LinearContainer member (must throw std::length_error when empty) - // type operator[](argument) specifiers; // Override LinearContainer member (must throw std::out_of_range when out of range) + Data& operator[](const ulong) const override; // Override LinearContainer member (must throw std::out_of_range when out of range) /* ************************************************************************ */ // Specific member functions (inherited from MappableContainer) - // using typename MappableContainer::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 };