Library 3

container & iterator.hpp
This commit is contained in:
Alessandro Ferro 2021-04-24 18:39:57 +02:00
parent 31bf18d985
commit 0d599672c4
16 changed files with 1240 additions and 506 deletions

View File

@ -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 <typename Data>
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 <typename Data>
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 <typename Data>
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<void(Data&, void*)> MapFunctor;
virtual void MapPreOrder(const MapFunctor, void*) = 0;
virtual void MapPostOrder(const MapFunctor, void*) = 0;
};
template <typename Data>
class FoldableContainer : virtual public TestableContainer<Data>{ // 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<void(const Data&, const void*, void*) noexcept> 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 <typename Data>
class LinearContainer { // Must extend Container
class InOrderMappableContainer : virtual public MappableContainer<Data> { // 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<Data>::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 <typename Data>
class TestableContainer { // Must extend Container
class InOrderFoldableContainer : public virtual FoldableContainer<Data> { // 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<Data>::MapFunctor;
virtual void FoldInOrder(const MapFunctor, const void*, void*) const = 0;
};
/* ************************************************************************** */
template <typename Data>
class MappableContainer { // Must extend Container
class BreadthMappableContainer : virtual public MappableContainer<Data> { // 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<void(Data&, void*)> MapFunctor;
using typename MappableContainer<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers;
// type MapPostOrder(arguments) specifiers;
void MapBreadth(const MapFunctor, void*) = 0;
};
/* ************************************************************************** */
template <typename Data>
class FoldableContainer { // Must extend TestableContainer
class BreadthFoldableContainer : virtual public FoldableContainer<Data> { // 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<void(const Data&, const void*, void*) noexcept> FoldFunctor;
using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers;
// type FoldPostOrder(arguments) specifiers;
// type Exists(argument) specifiers; // Override TestableContainer member
};
/* ************************************************************************** */
template <typename Data>
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<Data>::MapFunctor;
// type MapInOrder(arguments) specifiers;
};
/* ************************************************************************** */
template <typename Data>
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<Data>::MapFunctor;
// type FoldInOrder(arguments) specifiers;
};
/* ************************************************************************** */
template <typename Data>
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<Data>::MapFunctor;
// type MapBreadth(arguments) specifiers;
};
/* ************************************************************************** */
template <typename Data>
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<Data>::FoldFunctor;
// type FoldBreadth(arguments) specifiers;
virtual void FoldBreadth(const FoldFunctor, const void*, void*) const = 0;
};

View File

@ -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 <typename Data>
class ForwardIterator { // Must extend Iterator
class ForwardIterator : virtual public Iterator<Data> { // 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)
};

View File

@ -3,8 +3,263 @@ namespace lasd {
/* ************************************************************************** */
// ...
/* ************************************************************************** */
// Specific constructors
template <typename Data>
List<Data>::Node::Node(const Data& newValue){
value = newValue;
next = nullptr;
}
// Copy constructor
template <typename Data>
List<Data>::Node::Node(const Node& copyFrom){
value = copyFrom.value;
next = nullptr;
}
// Move constructor
template <typename Data>
List<Data>::Node::Node(Node&& moveFrom){
std::swap(value, moveFrom.value);
std::swap(next, moveFrom.next);
}
template <typename Data>
List<Data>::Node::Node(Data&& moveFrom){
std::swap(value, moveFrom);
}
template <typename Data>
bool List<Data>::Node::operator==(const Node& node)const noexcept{
return (node.value == value);
}
template <typename Data>
bool List<Data>::Node::operator!=(const Node& node)const noexcept{
return !(*this == node);
}
template <typename Data>
List<Data>::List(const LinearContainer<Data>& con){
for(ulong i = 0; i<con.Size(); ++i){
InsertAtBack(con[i]);
}
}
template <typename Data>
List<Data>::List(const List<Data>& copyFrom){
for(ulong i = 0; i<copyFrom.Size(); ++i){
InsertAtBack(copyFrom[i]);
}
}
template <typename Data>
List<Data>::List(List<Data>&& moveFrom){
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
std::swap(tail, moveFrom.tail);
}
template <typename Data>
List<Data>::~List(){
Clear();
}
template <typename Data>
List<Data>& List<Data>::operator=(const List<Data>& copyFrom){
if(*this != copyFrom){
Clear();
for(ulong i = 0 ; i<copyFrom.Size() ; ++i){
InsertAtBack(copyFrom[i]);
}
}
return *this;
}
template <typename Data>
List<Data>& List<Data>::operator=(List<Data>&& moveFrom)noexcept{
if(*this != moveFrom){
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
std::swap(tail, moveFrom.tail);
}
return *this;
}
template <typename Data>
bool List<Data>::operator==(const List<Data>& 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 <typename Data>
bool List<Data>::operator!=(const List<Data>& list) const noexcept{
return !(*this==list);
}
template <typename Data>
void List<Data>::InsertAtFront(const Data& data){
struct Node* tmp = new Node(data);
tmp->next = head;
head = tmp;
size++;
if(size == 1){
tail = head;
}
}
template <typename Data>
void List<Data>::InsertAtFront(Data&& data){
struct Node* tmp = new Node(std::move(data));
tmp->next = head;
head = tmp;
size++;
if(size == 1){
tail = head;
}
}
template <typename Data>
void List<Data>::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 <typename Data>
Data List<Data>::FrontNRemove(){
if(head == nullptr){
throw std::length_error("List is empty!");
}
else{
Data value = head->value;
RemoveFromFront();
return value;
}
}
template <typename Data>
void List<Data>::InsertAtBack(const Data& data){
if(size == 0){
InsertAtFront(data);
}
else{
struct Node* last = new Node(data);
tail->next = last;
tail = last;
size++;
}
}
template <typename Data>
void List<Data>::InsertAtBack(Data&& data){
if(size == 0){
InsertAtFront(data);
}
else{
struct Node* last = new Node(std::move(data));
tail->next = last;
tail = last;
size++;
}
}
template <typename Data>
void List<Data>::Clear(){
while(head != nullptr){
RemoveFromFront();
}
}
template <typename Data>
Data& List<Data>::Front() const{
if(size == 0){
throw std::length_error("List is empty!");
}else{
return head->value;
}
}
template <typename Data>
Data& List<Data>::Back() const{
if(size == 0){
throw std::length_error("List is empty!");
}else{
return tail->value;
}
}
template <typename Data>
Data& List<Data>::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; i<index; ++i){
tmp = tmp->next;
}
return tmp->value;
}
}
template <typename Data>
void List<Data>::MapPreOrder(MapFunctor function, void* par){
MapPreOrder(function, par, head);
}
template <typename Data>
void List<Data>::MapPostOrder(MapFunctor function, void* par){
MapPostOrder(function, par, head);
}
template <typename Data>
void List<Data>::FoldPreOrder(FoldFunctor function, const void* constPar, void* par) const{
FoldPreOrder(function, constPar, par, head);
}
template <typename Data>
void List<Data>::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{
FoldPostOrder(function, constPar, par, head);
}
template <typename Data>
void List<Data>::MapPreOrder(MapFunctor function, void* par, struct Node* node){
if(node == nullptr) return;
function(node->value, par);
MapPreOrder(function, par, node->next);
}
template <typename Data>
void List<Data>::MapPostOrder(MapFunctor function, void* par, struct Node* node){
if(node == nullptr) return;
MapPostOrder(function, par, node->next);
function(node->value, par);
}
template <typename Data>
void List<Data>::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 <typename Data>
void List<Data>::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);
}
}

View File

@ -13,150 +13,150 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class List { // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
class List : virtual public LinearContainer<Data>,
virtual public MappableContainer<Data>,
virtual public FoldableContainer<Data>{ // Must extend LinearContainer<Data>, MappableContainer<Data>, and FoldableContainer<Data>
private:
// ...
protected:
// using LinearContainer<Data>::???;
using LinearContainer<Data>:: 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<Data>&); // 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<Data>::MapFunctor;
using typename MappableContainer<Data>::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<Data>::FoldFunctor;
using typename FoldableContainer<Data>::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
};

69
librerie/exercise3/queue/lst/queuelst.cpp Normal file → Executable file
View File

@ -1,10 +1,73 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear)
: List<Data>(linear) {}
// ...
template <typename Data>
QueueLst<Data>::QueueLst(const QueueLst& copyFrom)
: List<Data>(copyFrom) {}
/* ************************************************************************** */
template <typename Data>
QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept
: List<Data>(std::move(moveFrom)) {}
template <typename Data>
QueueLst<Data>::~QueueLst(){
Clear();
}
template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(const QueueLst& toCopy){
List<Data>::operator=(toCopy);
return *this;
}
template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(QueueLst&& toMove) noexcept{
List<Data>::operator=(std::move(toMove));
return *this;
}
template <typename Data>
bool QueueLst<Data>::operator==(const QueueLst& queuelist) const noexcept{
return List<Data>::operator==(queuelist);
}
template <typename Data>
bool QueueLst<Data>::operator!=(const QueueLst& queuelist) const noexcept{
return List<Data>::operator!=(queuelist);
}
template <typename Data>
void QueueLst<Data>::Enqueue(const Data& data){
List<Data>::InsertAtBack(data);
}
template <typename Data>
void QueueLst<Data>::Enqueue(Data&& data){
List<Data>::InsertAtBack(std::move(data));
}
template <typename Data>
Data& QueueLst<Data>::Head() const{
return List<Data>::Front();
}
template <typename Data>
void QueueLst<Data>::Dequeue(){
List<Data>::RemoveFromFront();
}
template <typename Data>
Data QueueLst<Data>::HeadNDequeue(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void QueueLst<Data>::Clear(){
List<Data>::Clear();
}
}

42
librerie/exercise3/queue/lst/queuelst.hpp Normal file → Executable file
View File

@ -14,70 +14,70 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class QueueLst { // Must extend Queue<Data> and List<Data>
class QueueLst : virtual public Queue<Data>,
virtual protected List<Data>{ // Must extend Queue<Data> and List<Data>
private:
// ...
protected:
// using List<Data>::???;
// ...
using List<Data>::head;
using List<Data>::tail;
using List<Data>::size;
using typename List<Data>::Node;
public:
// Default constructor
// QueueLst() specifier;
QueueLst() = default;
/* ************************************************************************ */
// Specific constructor
// QueueLst(argument) specifiers; // A queue obtained from a LinearContainer
QueueLst(const LinearContainer<Data>&); // 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
};

26
librerie/exercise3/queue/queue.hpp Normal file → Executable file
View File

@ -13,44 +13,40 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
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)
};

188
librerie/exercise3/queue/vec/queuevec.cpp Normal file → Executable file
View File

@ -1,10 +1,192 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueVec<Data>::QueueVec(){
size = 4;
rear = 0;
front = 0;
Elements = new Data[size];
}
// ...
template <typename Data>
QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){
size = linear.Size()+1; // 1 free cell
Elements = new Data[size]; //forse da espandere
for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i];
}
front = 0;
rear = size-1; // the vector will be full
}
/* ************************************************************************** */
template <typename Data>
QueueVec<Data>::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 <typename Data>
QueueVec<Data>::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 <typename Data>
QueueVec<Data>::~QueueVec(){
//vector destructor will be automatically called I hope
}
template <typename Data>
QueueVec<Data>& QueueVec<Data>::operator=(const QueueVec& toCopy){
QueueVec<Data>* tmpQueue = new QueueVec<Data>(toCopy);
std::swap(*tmpQueue, *this);
delete tmpQueue;
return *this;
}
template <typename Data>
QueueVec<Data>& QueueVec<Data>::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 <typename Data>
bool QueueVec<Data>::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 <typename Data>
bool QueueVec<Data>::operator!=(const QueueVec& toCompare) const noexcept{
return !(*this == toCompare);
}
template <typename Data>
void QueueVec<Data>::Enqueue(const Data& data){
if((rear+1)%size == front){
Expand();
}
Elements[rear] = data;
rear = (rear + 1) % size;
}
template <typename Data>
void QueueVec<Data>::Enqueue(Data&& data){
if((rear+1)%size == front){
Expand();
}
std::swap(Elements[rear],data);
rear = (rear + 1) % size;
}
template <typename Data>
Data& QueueVec<Data>::Head() const{
if(Size()<=0){
throw std::length_error("Queue is empty!");
}
return Elements[front];
}
template <typename Data>
void QueueVec<Data>::Dequeue(){
if(Size() <= 0){
throw std::length_error("Queue is empty!");
}
front = (front + 1) % size;
if(Size() < size/4){
Reduce();
}
}
template <typename Data>
Data QueueVec<Data>::HeadNDequeue(){
Data tmp = Head();
Dequeue();
return tmp;
}
template <typename Data>
bool QueueVec<Data>::Empty() const noexcept{
return (front == rear);
}
template <typename Data>
ulong QueueVec<Data>::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 <typename Data>
void QueueVec<Data>::Clear(){
if(size!=4){
delete[] Elements;
Elements = new Data[4];
size = 4;
}
front = 0;
rear = 0;
}
template <typename Data>
void QueueVec<Data>::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 <typename Data>
void QueueVec<Data>::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;
}
}

53
librerie/exercise3/queue/vec/queuevec.hpp Normal file → Executable file
View File

@ -14,82 +14,83 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class QueueVec { // Must extend Queue<Data> and Vector<Data>
class QueueVec : virtual public Queue<Data>,
virtual protected Vector<Data>{ // Must extend Queue<Data> and Vector<Data>
private:
// ...
protected:
// using Vector<Data>::???;
// ...
using Vector<Data>::Elements;
using Vector<Data>::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<Data>&); // 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;
};

72
librerie/exercise3/stack/lst/stacklst.cpp Normal file → Executable file
View File

@ -3,7 +3,77 @@ namespace lasd {
/* ************************************************************************** */
// ...
// Constructors
template <typename Data>
StackLst<Data>::StackLst(const LinearContainer<Data>& linear)
: List<Data>(linear){}
template <typename Data>
StackLst<Data>::StackLst(const StackLst& stcklist)
: List<Data>(stcklist){}
template <typename Data>
StackLst<Data>::StackLst(StackLst&& stcklist) noexcept
: List<Data>(std::move(stcklist)){}
// Destructor
template <typename Data>
StackLst<Data>::~StackLst(){
Clear();
}
template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(const StackLst& copyFrom){
List<Data>::operator=(copyFrom);
return *this;
}
template <typename Data>
StackLst<Data>& StackLst<Data>::operator=(StackLst&& moveFrom) noexcept{
List<Data>::operator=(std::move(moveFrom));
return *this;
}
template <typename Data>
bool StackLst<Data>::operator==(const StackLst& stcklist) const noexcept{
return List<Data>::operator==(stcklist);
}
template <typename Data>
bool StackLst<Data>::operator!=(const StackLst& stcklist) const noexcept{
return List<Data>::operator!=(stcklist);
}
// Specific member functions (inherited from Stack)
template <typename Data>
void StackLst<Data>::Push(const Data& element){
List<Data>::InsertAtFront(element);
}
template <typename Data>
void StackLst<Data>::Push(Data&& element) noexcept{
List<Data>::InsertAtFront(element);
}
template <typename Data>
Data& StackLst<Data>::Top() const{
return List<Data>::Front();
}
template <typename Data>
void StackLst<Data>::Pop(){
List<Data>::RemoveFromFront();
}
template <typename Data>
Data StackLst<Data>::TopNPop(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void StackLst<Data>::Clear(){
List<Data>::Clear();
}
/* ************************************************************************** */

41
librerie/exercise3/stack/lst/stacklst.hpp Normal file → Executable file
View File

@ -14,70 +14,69 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class StackLst { // Must extend Stack<Data> and List<Data>
class StackLst : virtual public Stack<Data>,
virtual protected List<Data> { // Must extend Stack<Data> and List<Data>
private:
// ...
protected:
// using List<Data>::???;
// ...
using List<Data>::head;
using List<Data>::size;
using typename List<Data>::Node;
public:
// Default constructor
// StackLst() specifier;
StackLst() = default;
/* ************************************************************************ */
// Specific constructor
// StackLst(argument) specifiers; // A stack obtained from a LinearContainer
StackLst(const LinearContainer<Data>&); // 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
};

26
librerie/exercise3/stack/stack.hpp Normal file → Executable file
View File

@ -13,44 +13,40 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
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)
};

143
librerie/exercise3/stack/vec/stackvec.cpp Normal file → Executable file
View File

@ -2,9 +2,142 @@
namespace lasd {
/* ************************************************************************** */
// ...
/* ************************************************************************** */
// constructors
template <typename Data>
StackVec<Data>::StackVec(){
size = 4; // default vector is instantiated with 4 cells
stackSize = 0;
Elements = new Data[size];
}
template <typename Data>
StackVec<Data>::StackVec(const LinearContainer<Data>& linear)
: Vector<Data>(linear){
stackSize = linear.Size(); // the array is full
}
template <typename Data>
StackVec<Data>::StackVec(const StackVec& stckvec)
: Vector<Data>(stckvec){
stackSize = stckvec.Size(); // the array is full
}
template <typename Data>
StackVec<Data>::StackVec(StackVec&& toMove) noexcept
: Vector<Data>(std::move(toMove)){
std::swap(stackSize, toMove.stackSize);
}
template <typename Data>
StackVec<Data>::~StackVec(){
// Vector destructor will be called automatically
}
template <typename Data>
StackVec<Data>& StackVec<Data>::operator=(const StackVec<Data>& copyFrom){
Vector<Data>::operator=(copyFrom); // espandere di un po' forse
stackSize = copyFrom.Size();
return *this;
}
template <typename Data>
StackVec<Data>& StackVec<Data>::operator=(StackVec<Data>&& moveFrom) noexcept{
Vector<Data>::operator=(std::move(moveFrom));
stackSize = moveFrom.Size();
return *this;
}
template <typename Data>
bool StackVec<Data>::operator==(const StackVec<Data>& toCompare) const noexcept{
if(stackSize == toCompare.Size()){
for(ulong i=0 ; i<stackSize ; ++i){
if(Elements[i] != toCompare[i]){
return false;
}
}
return true;
}else{
return false;
}
}
template <typename Data>
bool StackVec<Data>::operator!=(const StackVec<Data>& toCompare) const noexcept{
return !(*this == toCompare);
}
// Specific member functions (inherited from Stack)
template <typename Data>
void StackVec<Data>::Push(const Data& data){
if(size == stackSize){
Expand();
}
Elements[stackSize] = data;
++stackSize;
}
template <typename Data>
void StackVec<Data>::Push(Data&& data){
if(size == stackSize){
Expand();
}
std::swap(Elements[stackSize], data);
++stackSize;
}
template <typename Data>
Data& StackVec<Data>::Top() const{
if(stackSize == 0){
throw std::length_error("Empty Stack!");
}
return Elements[stackSize-1];
}
template <typename Data>
void StackVec<Data>::Pop(){
if(stackSize==0){
throw std::length_error("Empty Stack!");
}
--stackSize;
if(stackSize < (int)(size/4)){
Reduce();
}
}
template <typename Data>
Data StackVec<Data>::TopNPop(){
Data data = Top();
Pop();
return data;
}
template <typename Data>
bool StackVec<Data>::Empty() const noexcept{
return (stackSize == 0);
}
template <typename Data>
ulong StackVec<Data>::Size() const noexcept{
return stackSize;
}
template <typename Data>
void StackVec<Data>::Expand(){
Vector<Data>::Resize(size * 2);
}
template <typename Data>
void StackVec<Data>::Reduce(){
if(size <= 4) return; // we're not going to have vectors with less than 4 cells
Vector<Data>::Resize((ulong)size/2);
}
template <typename Data>
void StackVec<Data>::Clear(){
delete [] Elements;
size = 4;
stackSize = 0;
Elements = new Data[size];
}
}

48
librerie/exercise3/stack/vec/stackvec.hpp Normal file → Executable file
View File

@ -14,81 +14,81 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class StackVec { // Must extend Stack<Data> and Vector<Data>
class StackVec : virtual public Stack<Data>,
virtual protected Vector<Data>{ // Must extend Stack<Data> and Vector<Data>
private:
// ...
protected:
// using Vector<Data>::???;
ulong stackSize = 0; // first empty cell and # of elements in the vector
using Vector<Data>::Elements;
using Vector<Data>::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<Data>&); // 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();
};

View File

@ -1,10 +1,160 @@
namespace lasd {
/* ************************************************************************** */
// ...
/* ************************************************************************** */
template <typename Data>
Vector<Data>::Vector(const ulong newsize){
Elements = new Data[newsize]{};
size = newsize;
}
template <typename Data>
Vector<Data>::Vector(const LinearContainer<Data>& con){
size = con.Size();
Elements = new Data[size]{};
for(ulong i=0; 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(){
Clear();
}
// 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;
}
template <typename Data>
bool Vector<Data>::operator==(const Vector<Data>& vec) const noexcept{
if(size == vec.size){
for(ulong i=0; i<size; ++i){
if(Elements[i] != vec.Elements[i])
return false;
}
return true;
}else{
return false;
}
}
template <typename Data>
bool Vector<Data>::operator!=(const Vector<Data>& vec)const noexcept{
return !(*this == vec);
}
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;
}
template <typename Data>
Data& Vector<Data>::Front() const {
if(size != 0){
return Elements[0];
}
else{
throw std::length_error("Access to an empty vector!");
}
}
template <typename Data>
Data& Vector<Data>::Back() const {
if(size != 0){
return Elements[size - 1];
}
else{
throw std::length_error("Access to an empty vector!");
}
}
template <typename Data>
Data& Vector<Data>::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 <typename Data>
void Vector<Data>::MapPreOrder(const MapFunctor fun, void* par){
for(ulong i=0; i<size; ++i){
fun(Elements[i], par);
}
}
template <typename Data>
void Vector<Data>::MapPostOrder(const MapFunctor fun, void* par){
ulong index = size;
while(index > 0){
fun(Elements[--index], par);
}
}
template <typename Data>
void Vector<Data>::FoldPreOrder(const FoldFunctor fun, const void* par, void* acc) const{
for(ulong i=0; i<size; ++i){
fun(Elements[i], par, acc);
}
}
template <typename Data>
void Vector<Data>::FoldPostOrder(const FoldFunctor fun, const void* par, void* acc) const{
ulong idx = size;
while(idx > 0){
fun(Elements[--idx], par, acc);
}
}
}

View File

@ -13,99 +13,65 @@ namespace lasd {
/* ************************************************************************** */
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:
// ...
protected:
// using LinearContainer<Data>::???;
// ...
using LinearContainer<Data>::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<Data>&); // 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<Data>::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<Data>::MapFunctor;
// type MapPreOrder(arguments) specifiers; // Override MappableContainer member
// type MapPostOrder(arguments) specifiers; // Override MappableContainer member
/* ************************************************************************ */
// Specific member functions (inherited from FoldableContainer)
// using typename FoldableContainer<Data>::FoldFunctor;
// type FoldPreOrder(arguments) specifiers; // Override FoldableContainer member
// type FoldPostOrder(arguments) specifiers; // Override FoldableContainer member
using typename FoldableContainer<Data>::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"