| 1 | (* A generic module that needs no library: a stack of at most eight |
| 2 | values of T. *) |
| 3 | module Pile(T); |
| 4 | |
| 5 | const Max = 8; |
| 6 | |
| 7 | type Pile* = record |
| 8 | items: [Max]T; |
| 9 | len*: INT; |
| 10 | end; |
| 11 | |
| 12 | (* x on top; false if the pile is full. *) |
| 13 | proc push*(p: ^Pile; x: T): BOOL; |
| 14 | begin |
| 15 | if p^.len = Max then return false; end; |
| 16 | p^.items[p^.len] := x; |
| 17 | INC(p^.len); |
| 18 | return true; |
| 19 | end; |
| 20 | |
| 21 | (* The top, taken off; false if the pile is empty. *) |
| 22 | proc pop*(p: ^Pile): (T, BOOL); |
| 23 | var zero: T; |
| 24 | begin |
| 25 | if p^.len = 0 then return zero, false; end; |
| 26 | DEC(p^.len); |
| 27 | return p^.items[p^.len], true; |
| 28 | end; |
| 29 | |
| 30 | (* The top, left where it is; the pile must not be empty. *) |
| 31 | proc top*(p: ^Pile): T; |
| 32 | begin |
| 33 | return p^.items[p^.len - 1]; |
| 34 | end; |
| 35 | |
| 36 | end Pile. |