RefStack:
CEDAR
DEFINITIONS =
BEGIN
--Intro: This is a stack (of REF ANY's) abstraction. The numbering of elements of the stack
-- are from 1 to stack's size (bottom to top) with the oldest element at the bottom and
-- the most recent at the top.
--Impl: Implemented by Sequence that doubles in size when size is exceeded. Size of sequence
-- does not reduce.
Ref: TYPE = REF Rep;
Rep: TYPE; --Opaque
Index: TYPE = [1..LAST[INTEGER]]; --NB: Index starts at 1
SizeHint: NAT = 8; --%Default initial size = 8
EachItemAction: TYPE = PROC[item: REF] RETURNS [quit: BOOL ← FALSE];
RStackUnderflow: ERROR;
RStackRangeFault: ERROR;
Create:
PROC [hint: Index ← SizeHint]
RETURNS [Ref];
-- hint is starting size of Rep
Push:
PROC[rStack: Ref, item:
REF];
-- push item onto top of rStack
Pop1:
PROC[rStack: Ref]
RETURNS [
REF];
-- Returns and removes item at top of stack
-- ! ERROR RStackUnderflow if rStack is empty
Pop:
PROC[rStack: Ref, n:
NAT, p: EachItemAction];
-- = {THROUGH [0..n) DO IF p[Pop1[rStack]] THEN EXIT ENDLOOP};
--! ERROR RStackUnderflow--
Copy:
PROC[rStack: Ref, after:
NAT ← 0]
RETURNS [
LIST
OF
REF];
--Returns elements of rStack in the order bottom to top of rStack
-- ie. items in list are ordered oldest to newest
-- Return list consist of item numbered after+1 to size of rStack
-- eg. after = 0 <=> copy from 1 to stack's size
Items:
PROC[rStack: Ref, p: EachItemAction, topDown:
BOOL ←
TRUE];
--Enumerates items.
ItemAt:
PROC[rStack: Ref, index: Index]
RETURNS [
REF];
-- Direct access
--! ERROR RStackRangeFault if index is not in [1..rStackSize].
Size:
PROC[rStack: Ref]
RETURNS [
NAT];
--Returns number of elements in rStack
Reset:
PROC[rStack: Ref];
-- Empties rStack by resetting top-of-stack pointer
Empty:
PROC[rStack: Ref]
RETURNS [
BOOL];
-- Returns true <=> rStack is empty
END.