The "Stack" class implementation
#define Stack_Class_Source
#include <stack.ih>
SOM_Scope boolean SOMLINK full(Stack somSelf, Environment *ev)
{
StackData *somThis = StackGetData(somSelf);
StackMethodDebug("Stack","full");
/* Return TRUE if stack is full. */
return (_stackTop == stackSize);
}
SOM_Scope boolean SOMLINK empty(Stack somSelf, Environment *ev)
{
StackData *somThis = StackGetData(somSelf);
StackMethodDebug("Stack","empty");
/* Return TRUE if stack is empty.*/
return (_stackTop == 0);
}
SOM_Scope long SOMLINK top(Stack somSelf, Environment *ev)
{
StackData *somThis = StackGetData(somSelf);
StackMethodDebug("Stack","top");
if (_stackTop > 0)
{
/* Return top element in stack without removing it from
* the stack.
*/
return (_stackValues[_stackTop-1]);
}
else
{
somSetException(ev, USER_EXCEPTION,
ex_STACK_UNDERFLOW, NULL);
return (-1L);
}
}
SOM_Scope long SOMLINK pop(Stack somSelf, Environment *ev)
{
StackData *somThis = StackGetData(somSelf);
StackMethodDebug("Stack","pop");
if (_stackTop > 0)
{
/* Return top element in stack and remove it from the
* stack.
*/
_stackTop--;
return (_stackValues[_stackTop]);
}
else
{
somSetException(ev, USER_EXCEPTION,
ex_STACK_UNDERFLOW, NULL);
return (-1L);
}
}
SOM_Scope void SOMLINK push(Stack somSelf,
Environment *ev, long el)
{
StackData *somThis = StackGetData(somSelf);
StackMethodDebug("Stack","push");
if (_stackTop < stackSize)
{
/* Add element to top of the stack. */
_stackValues[_stackTop] = el;
_stackTop++;
}
else
{
somSetException(ev, USER_EXCEPTION,
ex_STACK_OVERFLOW, NULL);
}
}
SOM_Scope void SOMLINK somDefaultInit(Stack somSelf,
somInitCtrl* ctrl)
{
StackData *somThis;
somInitCtrl globalCtrl;
somBooleanVector myMask;
StackMethodDebug("Stack","somDefaultInit");
Stack_BeginInitializer_somDefaultInit;
Stack_Init_SOMObject_somDefaultInit(somSelf, ctrl);
/* stackTop is index into stackValues for next pushed
* stack element.
* stackValues[0..(stackSize-1)] holds stack elements.
*/
_stackTop = 0;
}
[Back: The "Stack" interface]
[Next: Client program using a local stack]