/*____________________________________________________________________________
simple stack optimized for use with memory pool
____________________________________________________________________________*/

#pragma once
#ifndef _STACK
#define _STACK

#include "word.h"
#include "node.hpp"
 
/*____________________________________________________________________________
class
____________________________________________________________________________*/
class stack{
    node * _top;    //top of stack;
    int _size;      //number of node in stack
public:
    //constructor
    stack();
    //mutator
    void push(node *, word);
                    //put given cell on top of the stack with desired word
    node * pop();   //return desired cell to put it back on the memory pool
    //getter
    word top()const;
                    //return top of stack
    int size()const;
                    //return size of the stack
    //debug
    word at(int)const;
                    //return the word at given index
};

/*____________________________________________________________________________
method
____________________________________________________________________________*/
stack::stack()
{
    _size = 0;
    _top = 0;
}

void stack::push(node * cell, word data)
{
    cell->data = data;
    cell->next = _top;
    _size++;
    _top = cell;
}

node * stack::pop()
{
    node * cell = _top;
    _top = _top->next;
    _size--;
    return cell;
}

word stack::top()const
{
    if (_size)
        return _top->data;
    return 0;
}

int stack::size()const
{
    return _size;
}

word stack::at(int index)const
{
    node * tmp = _top;
    if (index >= _size)
        return 0;
    while(index--)
        tmp = tmp->next;
    return tmp->data;
}

#endif
