2021-07-06 00:00:36 +02:00
|
|
|
#define MINSIZE 4
|
2021-04-10 13:34:50 +02:00
|
|
|
namespace lasd {
|
|
|
|
|
|
|
|
/* ************************************************************************** */
|
2021-04-14 08:42:18 +02:00
|
|
|
// constructors
|
|
|
|
template <typename Data>
|
|
|
|
StackVec<Data>::StackVec(){
|
2021-07-06 00:00:36 +02:00
|
|
|
size = MINSIZE; // default vector is instantiated with 4 cells
|
2021-04-19 12:32:33 +02:00
|
|
|
stackSize = 0;
|
2021-04-14 08:42:18 +02:00
|
|
|
Elements = new Data[size];
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:16:51 +02:00
|
|
|
template <typename Data>
|
|
|
|
StackVec<Data>::StackVec(const LinearContainer<Data>& linear)
|
|
|
|
: Vector<Data>(linear){
|
|
|
|
stackSize = linear.Size(); // the array is full
|
|
|
|
}
|
2021-04-14 08:42:18 +02:00
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-21 00:14:10 +02:00
|
|
|
StackVec<Data>::StackVec(const StackVec& stckvec)
|
|
|
|
: Vector<Data>(stckvec){
|
|
|
|
stackSize = stckvec.Size(); // the array is full
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
2021-04-21 00:14:10 +02:00
|
|
|
|
2021-04-14 08:42:18 +02:00
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-21 00:14:10 +02:00
|
|
|
StackVec<Data>::StackVec(StackVec&& toMove) noexcept
|
|
|
|
: Vector<Data>(std::move(toMove)){
|
2021-04-14 08:42:18 +02:00
|
|
|
std::swap(stackSize, toMove.stackSize);
|
|
|
|
}
|
|
|
|
|
2021-04-19 12:32:33 +02:00
|
|
|
template <typename Data>
|
2021-04-14 08:42:18 +02:00
|
|
|
StackVec<Data>::~StackVec(){
|
|
|
|
// Vector destructor will be called automatically
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
StackVec<Data>& StackVec<Data>::operator=(const StackVec<Data>& copyFrom){
|
2021-04-14 08:42:18 +02:00
|
|
|
Vector<Data>::operator=(copyFrom); // espandere di un po' forse
|
|
|
|
stackSize = copyFrom.Size();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
StackVec<Data>& StackVec<Data>::operator=(StackVec<Data>&& moveFrom) noexcept{
|
2021-04-21 00:14:10 +02:00
|
|
|
Vector<Data>::operator=(std::move(moveFrom));
|
2021-06-09 09:31:27 +02:00
|
|
|
std::swap(stackSize, moveFrom.stackSize);
|
2021-04-21 00:14:10 +02:00
|
|
|
return *this;
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
bool StackVec<Data>::operator!=(const StackVec<Data>& toCompare) const noexcept{
|
|
|
|
return !(*this == toCompare);
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
2021-04-10 13:34:50 +02:00
|
|
|
|
2021-04-14 08:42:18 +02:00
|
|
|
// 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);
|
2021-04-19 12:32:33 +02:00
|
|
|
++stackSize;
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
Data& StackVec<Data>::Top() const{
|
|
|
|
if(stackSize == 0){
|
2021-04-21 00:14:10 +02:00
|
|
|
throw std::length_error("Empty Stack!");
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
2021-04-19 12:32:33 +02:00
|
|
|
return Elements[stackSize-1];
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
void StackVec<Data>::Pop(){
|
2021-04-19 12:32:33 +02:00
|
|
|
if(stackSize==0){
|
2021-04-21 00:14:10 +02:00
|
|
|
throw std::length_error("Empty Stack!");
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
|
|
|
--stackSize;
|
|
|
|
if(stackSize < (int)(size/4)){
|
|
|
|
Reduce();
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 19:49:34 +02:00
|
|
|
|
2021-04-14 08:42:18 +02:00
|
|
|
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(){
|
2021-07-06 00:00:36 +02:00
|
|
|
if(size < (MINSIZE*2)) return; // we're not going to have vectors with less than 4 cells
|
2021-04-14 08:42:18 +02:00
|
|
|
Vector<Data>::Resize((ulong)size/2);
|
|
|
|
}
|
2021-04-10 13:34:50 +02:00
|
|
|
|
2021-04-19 12:32:33 +02:00
|
|
|
template <typename Data>
|
|
|
|
void StackVec<Data>::Clear(){
|
|
|
|
delete [] Elements;
|
2021-07-06 00:00:36 +02:00
|
|
|
size = MINSIZE;
|
2021-04-19 12:32:33 +02:00
|
|
|
stackSize = 0;
|
2021-07-06 00:00:36 +02:00
|
|
|
Elements = new Data[MINSIZE];
|
2021-04-19 12:32:33 +02:00
|
|
|
}
|
2021-04-10 13:34:50 +02:00
|
|
|
}
|