Esercizio 1

vector.hpp , vector.cpp (non completo)
This commit is contained in:
Alessandro Ferro 2021-03-30 18:59:56 +02:00
parent 02a46f308b
commit 3e73aeb28d
4 changed files with 154 additions and 49 deletions

View File

@ -32,7 +32,7 @@ public:
Container& operator=(const Container&) = delete; // 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 // 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. TestableContainer& operator=(const TestableContainer&) = delete; // Copy assignment of abstract types should not be possible.
// Move assignment // 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. MappableContainer& operator=(const MappableContainer&) = delete; // Copy assignment of abstract types should not be possible.
// Move assignment // 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<void(Data&, void*)> MapFunctor; typedef std::function<void(Data&, void*)> MapFunctor;
virtual voi MapPreOrder(const MapFunctor, void*) = 0; virtual void MapPreOrder(const MapFunctor, void*) = 0;
virtual void MapPostOrder(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. FoldableContainer& operator=(const FoldableContainer&) = delete; // Copy assignment of abstract types should not be possible.
// Move assignment // 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 FoldPreOrder(const FoldFunctor, const void*, void*) const = 0;
virtual void FoldPostOrder(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
}; };

View File

@ -1,8 +1,8 @@
#include "zlasdtest/test.hpp" // #include "zlasdtest/test.hpp"
//
#include "zmytest/test.hpp" // #include "zmytest/test.hpp"
#include "container/container.hpp"
/* ************************************************************************** */ /* ************************************************************************** */
#include <iostream> #include <iostream>
@ -11,6 +11,6 @@
int main() { int main() {
std::cout << "Lasd Libraries 2020" << std::endl; 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; return 0;
} }

View File

@ -1,10 +1,123 @@
namespace lasd { namespace lasd {
// Specific constructor
template typename<Data>
Vector<Data>::Vector(const ulong dimension){
Elements = new Data[dimenion] {};
size = dimenion;
}
/* ************************************************************************** */ // Specific constructor
template typename<Data>
Vector<Data>::Vector(const LinearContainer<Data>& con){
size = con.Size();
Elements = new Data[size] {};
for(ulong i ; i<size; ++i){
Elements[i] = con[i];
}
}
// ... // Copy constructor
template typename<Data>
Vector<Data>::Vector(const Vector<Data>& vec){
size = vec.size;
Elements = new Data[size] {};
for(ulong i=0 ; i<size; ++i){
Elements[i] = vec[i];
}
}
/* ************************************************************************** */ //Move constructor
template typename<Data>
Vector<Data>::Vector(Vector<Data>&& vec) noexcept{
std::swap(Elements, vec.Elements);
std::swap(size, vec.size);
}
// Destructor
template typename<Data>
Vector<Data>::~Vector(){
delete[] Elements;
}
// Copy assignment
template typename<Data>
Vector<Data>& Vector<Data>::operator=(const Vector<Data>& vec){
Vector<Data>* tmpvec = new Vector<Data>(vec);
std::swap(*tmpvec,*this);
delete tmpvec;
return *this;
}
// Move assignment
template typename<Data>
Vector<Data>& Vector<Data>::operator=(Vector<Data>&& vec) noexcept{
std::swap(Elements, vec.Elements);
std::swap(size, vec.size);
return *this;
}
// Comparison operators
template typename<Data>
bool Vector<Data>::operator==(const Vector<Data>& vec) const noexcept{
if(size!=vec.size) return false;
for(ulong i=0 ; i<size ; ++i){
if(Elements[i] != vec.Elements[i]){
return false;
}
}
return true;
}
template typename<Data>
bool Vector<Data>::operator!=(const Vector<Data>& vec) const noexcept{
return !(*this == vec);
}
// Specific member function
// template typename<Data>
// void Vector<Data>::Resize(const ulong newsize){
// if(newsize == 0){
// Clear();
// }
// else{
// size = newsize;
// if(newsize < size){
// Data* tmpvec = new Data[newsize] {};
// for(ulong i=0;i<newsize;++i){
// tmpvec[i] = Elements[i];
// }
// delete[] Elements;
// Elements = tmpvec;
// }else if(newsize > size){
// Data* tmpvec = new Data[newsize] {};
// for(ulong i=0;i<size;++i){
// tmpvec[i] = Elements[i];
// }
// delete[] Elements;
// Elements = tmpvec;
// }
// }
// }
template typename<Data>
void Vector<Data>::Resize(const ulong newsize){
if(newsize == 0){
Clear();
}else if(size != newsize){
ulong limit = (size < newsize) ? size : newsize;
Data* TmpElements = new Data[newsize] {};
for(ulong i = 0 ; i<limit ; ++i){
std::swap(Elements[i], TmpElements[i]);
}
std::swap(Elements, TmpElements);
size = newsize;
delete[] TmpElements;
}
}
template typename<Data>
void Vector<Data>::Clear(){
delete[] Elements;
Elements = nullptr;
size = 0;
} }

View File

@ -2,105 +2,97 @@
#ifndef VECTOR_HPP #ifndef VECTOR_HPP
#define VECTOR_HPP #define VECTOR_HPP
/* ************************************************************************** */
#include "../container/container.hpp" #include "../container/container.hpp"
/* ************************************************************************** */
namespace lasd { namespace lasd {
/* ************************************************************************** */
template <typename Data> template <typename Data>
class Vector { // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data> class Vector : virtual public LinearContainer<Data>,
virtual public MappableContainer<Data>,
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
private: private:
// ...
protected: protected:
// using LinearContainer<Data>::???; using LinearContainer<Data>::size;
Data* Elements = nullptr;
// ...
public: public:
// Default constructor // Default constructor
// Vector() specifiers; Vector() = default;
/* ************************************************************************ */ /* ************************************************************************ */
// Specific constructors // Specific constructors
// Vector(argument) specifiers; // A vector with a given initial dimension Vector(const ulong); // A vector with a given initial dimension
// Vector(argument) specifiers; // A vector obtained from a LinearContainer Vector(const LinearContainer<Data>&); // A vector obtained from a LinearContainer
/* ************************************************************************ */ /* ************************************************************************ */
// Copy constructor // Copy constructor
// Vector(argument) specifiers; Vector(const Vector&);
// Move constructor // Move constructor
// Vector(argument) specifiers; Vector(Vector&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Destructor // Destructor
// ~Vector() specifiers; ~Vector();
/* ************************************************************************ */ /* ************************************************************************ */
// Copy assignment // Copy assignment
// type operator=(argument) specifiers; Vector& operator=(const Vector&);
// Move assignment // Move assignment
// type operator=(argument) specifiers; Vector& operator=(Vector&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Comparison operators // Comparison operators
// type operator==(argument) specifiers; bool operator==(const Vector&&) const noexcept;
// type operator!=(argument) specifiers; bool operator!=(const Vector&&) const noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions // 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) // Specific member functions (inherited from Container)
// type Clear() specifiers; // Override Container member void Clear() override; // Override Container member
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions (inherited from LinearContainer) // Specific member functions (inherited from LinearContainer)
// type Front() 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)
// type Back() specifiers; // 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) // Specific member functions (inherited from MappableContainer)
// using typename MappableContainer<Data>::MapFunctor; using typename MappableContainer<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member void MapPreOrder(const MapFunctor, void*) override; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member void MapPostOrder(const MapFunctor, void*) override; // Override MappableContainer member
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer) // Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::FoldFunctor; using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member void FoldPreOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member void FoldPostOrder(const FoldFunctor, const void*, void*) const override; // Override FoldableContainer member
}; };