﻿/*
Name:     circstack.js
Creation: 24-05-2006
LastEdit: 24-05-2006
Author:   Egbert van der Haring
Remarks:  implements a circular stack
*/

function circStack(nMax)
{
    this.nMax = nMax;
    this.nEntries = 0;
    this.nLast = 0;
    this.oItems = new Array(nMax);
    
    this.clear = function ()
    {
        this.nEntries = 0;
        this.nLast = 0;
    }
    
    this.push = function (oItem)
    {
        var bPushed = false;
        if (oItem != null)
        {
            bPushed = true;
            this.oItems[this.nLast] = oItem;
            this.nLast += 1;
            if (this.nEntries < this.nMax)
            {
                this.nEntries += 1;
            }
            if (this.nLast == this.nMax)
            {
                this.nLast = 0;
            }
        }
        return bPushed;
    }

    this.pop = function ()
    {
        var oItem = null;
        if (this.nEntries > 0)
        {
            bValid = true;
            this.nEntries -= 1;
            this.nLast -= 1;
            if (this.nLast < 0)
            {
                this.nLast = this.nMax-1;
            }
            oItem = this.oItems[this.nLast];
        }
        return oItem;
    }

    this.isEmpty = function ()
    {
        return (this.nEntries == 0);
    }

}

