mirror of
https://github.com/xfarrow/lasd.git
synced 2025-06-05 21:49:14 +02:00
Library 3
container & iterator.hpp
This commit is contained in:
72
librerie/exercise3/stack/lst/stacklst.cpp
Normal file → Executable file
72
librerie/exercise3/stack/lst/stacklst.cpp
Normal file → Executable 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
41
librerie/exercise3/stack/lst/stacklst.hpp
Normal file → Executable 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
26
librerie/exercise3/stack/stack.hpp
Normal file → Executable 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
143
librerie/exercise3/stack/vec/stackvec.cpp
Normal file → Executable 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
48
librerie/exercise3/stack/vec/stackvec.hpp
Normal file → Executable 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();
|
||||
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user