Library 2

This commit is contained in:
Alessandro Ferro 2021-04-14 08:42:18 +02:00
parent 1100ac3b56
commit 02918497cf
9 changed files with 288 additions and 69 deletions

View File

@ -56,7 +56,7 @@ template <typename Data>
List<Data>::List(const List<Data>& copyFrom){ List<Data>::List(const List<Data>& copyFrom){
for(ulong i = 0; i<copyFrom.Size(); ++i){ for(ulong i = 0; i<copyFrom.Size(); ++i){
InsertAtBack(copyFrom[i]); InsertAtBack(copyFrom[i]);
} }
} }
// Move constructor // Move constructor

View File

@ -2,8 +2,82 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
template <typename Data>
QueueLst(const LinearContainer<Data>& linear){
for(ulong i=0; i<linear.Size() ; ++i){
Enqueue(linear[i]);
}
}
// ... template <typename Data>
QueueLst(const QueueLst& copyFrom){
for(ulong i=0; i<copyFrom.Size() ; ++i){
Enqueue(copyFrom[i]);
}
}
template <typename Data>
QueueLst(QueueLst&& moveFrom) noexcept{
std::swap(head,moveFrom.head);
std::swap(tail,moveFrom.tail);
std::swap(size,moveFrom.size);
}
~QueueLst(){
Clear();
}
template <typename Data>
QueueLst<Data>& operator=(const QueueLst& toCopy){
QueueLst<Data>* tmp = new QueueLst<Data>(toCopy);
std::swap(*this, *tmp);
delete tmp;
return *this;
/*
List<Data>::operator=(copyFrom);
*/
}
QueueLst& operator=(QueueLst&& toMove) noexcept{
if(*this != toMove){
std::swap(head, toMove.head);
std::swap(head, toMove.tail);
std::swap(size, toMove.size);
return *this;
}
bool operator==(const QueueLst& queuelist) const noexcept{
return List<Data>::operator==(queuelist);
}
bool operator==(const QueueLst& queuelist) const noexcept{
return List<Data>::operator!=(queuelist);
}
template <typename Data>
void Enqueue(const Data& data){
List<Data>::InsertAtBack(data);
}
template <typename Data>
void Enqueue(Data&& data){
List<Data>::InsertAtBack(data);
}
template <typename Data>
Data& Head() const{
return List<Data>::Front();
}
template <typename Data>
void Dequeue(){
List<Data>::RemoveFromFront();
}
}
template <typename Data>
Data HeadNDequeue(){
return List<Data>::FrontNRemove();
}
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -14,70 +14,70 @@ namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
template <typename Data> 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: private:
// ...
protected: protected:
// using List<Data>::???; using List<Data>::head;
using List<Data>::tail;
// ... using List<Data>::size;
using typename List<Data>::Node;
public: public:
// Default constructor // Default constructor
// QueueLst() specifier; QueueLst() = default;
/* ************************************************************************ */ /* ************************************************************************ */
// Specific constructor // Specific constructor
// QueueLst(argument) specifiers; // A queue obtained from a LinearContainer QueueLst(const LinearContainer<Data>&); // A queue obtained from a LinearContainer
/* ************************************************************************ */ /* ************************************************************************ */
// Copy constructor // Copy constructor
// QueueLst(argument); QueueLst(const QueueLst&);
// Move constructor // Move constructor
// QueueLst(argument); QueueLst(QueueLst&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Destructor // Destructor
// ~QueueLst() specifier; ~QueueLst();
/* ************************************************************************ */ /* ************************************************************************ */
// Copy assignment // Copy assignment
// type operator=(argument); QueueLst& operator=(const QueueLst&);
// Move assignment // Move assignment
// type operator=(argument); QueueLst& operator=(QueueLst&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Comparison operators // Comparison operators
// type operator==(argument) specifiers; bool operator==(const QueueLst&) const noexcept;
// type operator!=(argument) specifiers; bool operator!=(const QueueLst&) const noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions (inherited from Queue) // Specific member functions (inherited from Queue)
// type Enqueue(argument) specifiers; // Override Queue member (copy of the value) void Enqueue(const Data&) override; // Override Queue member (copy of the value)
// type Enqueue(argument) specifiers; // Override Queue member (move of the value) void Enqueue(Data&&) override; // Override Queue member (move of the value)
// type Head() specifiers; // Override Queue member (must throw std::length_error when empty) Data& Head() const override; // Override Queue member (must throw std::length_error when empty)
// type Dequeue() specifiers; // Override Queue member (must throw std::length_error when empty) void Dequeue() override; // Override Queue member (must throw std::length_error when empty)
// type HeadNDequeue() specifiers; // 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) // Specific member functions (inherited from Container)
// type Clear() specifiers; // Override Container member void Clear() override; // Override Container member
}; };

View File

@ -13,44 +13,40 @@ namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
template <typename Data> template <typename Data>
class Queue { // Must extend Container class Queue : virtual public Container{ // Must extend Container
private: private:
// ...
protected: protected:
// ...
public: public:
// Destructor // Destructor
// ~Queue() specifiers ~Queue() = default;
/* ************************************************************************ */ /* ************************************************************************ */
// Copy assignment // 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 // 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 // Comparison operators
// 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.
// 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.
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions // Specific member functions
// type Enqueue(argument) specifiers; // Copy of the value virtual void Enqueue(const Data&) = 0; // Copy of the value
// type Enqueue(argument) specifiers; // Move of the value virtual void Enqueue(Data&&) = 0; // Move of the value
// type Head() specifiers; // (concrete function must throw std::length_error when empty) virtual Data& Head() const = 0; // (concrete function must throw std::length_error when empty)
// type Dequeue() specifiers; // (concrete function must throw std::length_error when empty) virtual void Dequeue() = 0; // (concrete function must throw std::length_error when empty)
// type HeadNDequeue() specifiers; // (concrete function must throw std::length_error when empty) virtual Data HeadNDequeue() = 0; // (concrete function must throw std::length_error when empty)
}; };

View File

@ -2,8 +2,20 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
template <typename Data>
QueueVec<Data>::QueueVec(){
size = 4;
Elements = new Data[size];
}
// ... template <typename Data>
QueueVec(const LinearContainer<Data>& linear){
size = linear.Size();
Elements = new Data[size];
for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i];
}
}
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -14,82 +14,82 @@ namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
template <typename Data> 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: private:
// ...
protected: protected:
// using Vector<Data>::???; using Vector<Data>::Elements;
using Vector<Data>::size;
// ... ulong front = 0;
ulong rear = 0;
public: public:
// Default constructor // Default constructor
// QueueVec() specifier; QueueVec();
/* ************************************************************************ */ /* ************************************************************************ */
// Specific constructor // Specific constructor
// QueueVec(argument) specifiers; // A queue obtained from a LinearContainer QueueVec(const LinearContainer<Data>&); // A queue obtained from a LinearContainer
/* ************************************************************************ */ /* ************************************************************************ */
// Copy constructor // Copy constructor
// QueueVec(argument); QueueVec(const QueueVec&);
// Move constructor // Move constructor
// QueueVec(argument); QueueVec(QueueVec&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Destructor // Destructor
// ~QueueVec() specifier; virtual ~QueueVec();
/* ************************************************************************ */ /* ************************************************************************ */
// Copy assignment // Copy assignment
// type operator=(argument); QueueVec& operator=(const QueueVec&);
// Move assignment // Move assignment
// type operator=(argument); QueueVec& operator=(QueueVec&&) noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Comparison operators // Comparison operators
// type operator==(argument) specifiers; bool operator==(const QueueVec&) const noexcept;
// type operator!=(argument) specifiers; bool operator!=(const QueueVec&) const noexcept;
/* ************************************************************************ */ /* ************************************************************************ */
// Specific member functions (inherited from Queue) // Specific member functions (inherited from Queue)
// type Enqueue(argument) specifiers; // Override Queue member (copy of the value) void Enqueue(const Data&) override; // Override Queue member (copy of the value)
// type Enqueue(argument) specifiers; // Override Queue member (move of the value) void Enqueue(Data&&) override; // Override Queue member (move of the value)
// type Head() specifiers; // Override Queue member (must throw std::length_error when empty) Data& Head() const override; // Override Queue member (must throw std::length_error when empty)
// type Dequeue() specifiers; // Override Queue member (must throw std::length_error when empty) void Dequeue() override; // Override Queue member (must throw std::length_error when empty)
// type HeadNDequeue() specifiers; // 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) // 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 unsigned long Size() const noexcept override; // Override Container member
// type Clear() specifiers; // Override Container member void Clear() override; // Override Container member
protected: protected:
// Auxiliary member functions // Auxiliary member functions
// type Expand() specifiers; void Expand();
// type Reduce() specifiers; void Reduce();
// type SwapVectors(arguments) specifiers; //void SwapVectors(arguments) specifiers;
}; };

View File

@ -10,12 +10,23 @@ StackLst(const LinearContainer<Data>& linear){
Push(linear[i]); Push(linear[i]);
} }
} }
/*
PROVARE
template <typename Data>
StackLst(const LinearContainer<Data>& linear) : List<Data>(linear){
return;
}
*/
StackLst(const StackLst& stcklist){ StackLst(const StackLst& stcklist){
for(ulong i=stcklist.Size()-1 ; i>=0 ; --i){ for(ulong i=stcklist.Size()-1 ; i>=0 ; --i){
Push(stcklist[i]); Push(stcklist[i]);
} }
} }
/*
PROVARE
StackLst(const StackLst& stcklist) : List<Data>(stcklist){}
*/
template <typename Data> template <typename Data>
StackLst<Data>::StackLst(StackLst&& stcklist) noexcept{ StackLst<Data>::StackLst(StackLst&& stcklist) noexcept{

View File

@ -2,8 +2,134 @@
namespace lasd { namespace lasd {
/* ************************************************************************** */ /* ************************************************************************** */
// constructors
template <typename Data>
StackVec<Data>::StackVec(){
size = 4; // default vector is instantiated with 4 cells
Elements = new Data[size];
}
// ... template <typename Data>
StackVec<Data>::StackVec(const LinearContainer<Data>& linear){ // si può richiamare il costruttore della classe Vector
Elements = new Data[linear.Size()]; // espandere di un po' forse
for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i];
}
size = linear.Size();
stackSize = linear.Size();
}
template <typename Data>
StackVec<Data>::StackVec(const StackVec& stckvec){// si può richiamare il costruttore della classe Vector
Elements = new Data[stckvec.Size()]; // espandere di un po' forse
for(ulong i=0 ; i<stckvec.Size() ; ++i){
Elements[i] = stckvec[i];
}
size = stckvec.Size();
stackSize = stckvec.stackSize;
}
/*
StackVec(const StackVec& stckvec) : Vector<Data>(copyFrom)
*/
template <typename Data>
StackVec<Data>::StackVec(StackVec&& toMove) noexcept{
std::swap(Elements, toMove.Elements);
std::swap(size, toMove.size);
std::swap(stackSize, toMove.stackSize);
}
StackVec<Data>::~StackVec(){
// Vector destructor will be called automatically
// (TEST IT)
}
template <typename Data>
StackVec& StackVec<Data>::operator=(const StackVec& copyFrom){
Vector<Data>::operator=(copyFrom); // espandere di un po' forse
stackSize = copyFrom.Size();
return *this;
}
template <typename Data>
StackVec& StackVec<Data>::operator=(StackVec&& moveFrom) noexcept{
Vector<Data>::operator=(std::move(moveFrom)); // espandere di un po' forse
stackSize = copyFrom.Size();
}
template <typename Data>
bool StackVec<Data>::operator==(const StackVec& toCompare) const noexcept{
Vector<Data>::operator==(toCompare);
}
template <typename Data>
bool StackVec<Data>::operator!=(const StackVec& toCompare) const noexcept{
Vector<Data>::operator!=(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);
}
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);
}
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -21,7 +21,7 @@ private:
protected: protected:
ulong stackSize = 0; // Actual stack dimension ulong stackSize = 0; // first empty cell
using Vector<Data>::Elements; using Vector<Data>::Elements;
using Vector<Data>::size; using Vector<Data>::size;
@ -52,7 +52,7 @@ public:
/* ************************************************************************ */ /* ************************************************************************ */
// Copy assignment // Copy assignment
StackVec& operator=(const StackVec&) StackVec& operator=(const StackVec&);
// Move assignment // Move assignment
StackVec& operator=(StackVec&&) noexcept; StackVec& operator=(StackVec&&) noexcept;
@ -79,7 +79,7 @@ public:
bool Empty() const noexcept override; // Override Container member bool Empty() const noexcept override; // Override Container member
ulong Size() const noexcept override; specifiers; // Override Container member ulong Size() const noexcept override; // Override Container member
void Clear() override;// Override Container member void Clear() override;// Override Container member