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