home *** CD-ROM | disk | FTP | other *** search
- #ifndef __STACK__
- #define __STACK__ 1
-
- #pragma once
-
- #ifndef __VECTOR__
- #include "Vector.h"
- #endif
-
-
-
-
- /*µ class Stack
- ** An inspectable entity. The stack pointer is 0 based, the vector class
- ** is 0 based. Items from 0 .. SP() can be referenced.
- */
- class Stack : public Vector {
- public:
- Stack();
-
- OSErr IStack();
- OSErr IStack(size_t size, size_t incr);
- OSErr IStack(const Stack *aStack);
- // Initialize the stack.
-
- void SetEmptyItem(void *anItem);
- // Set the item to return when empty
-
- short Depth() const;
- // Return the depth of the stack, the number of items on it
-
- short SP() const;
- // Return the index of the top of stack
-
- Boolean IsEmpty() const;
- // Return true when the stack is empty
-
- void Push(const void *anItem);
- // Push the item onto the stack
-
- void *Pop();
- // Pop the stack and return it
-
- void Top(void *anItem);
- void *Top() const;
- // Return the top of stack. Can be fEmptyItem if stack is empty.
-
- void *Pick(short n) const;
- // Return the n'th item on the stack. The top is item 0
-
- void Drop(short n);
- // Drop "n" items off the stack
-
- private:
- short fSP; // The number of items: one-based
- void *fEmptyItem; // What to return when empty
- };
-
-
- //µ –- inlines -–
-
- //µ Stack::Stack
- #pragma segment Stack
- inline Stack::Stack()
- : Vector(),
- fSP(0),
- fEmptyItem(0)
- {
- }
-
-
- //µ Stack::IStack
- #pragma segment Stack
- inline OSErr Stack::IStack()
- {
- return (IVector());
- }
-
-
- //µ Stack::IStack
- #pragma segment Stack
- inline OSErr Stack::IStack(size_t size, size_t incr)
- {
- return (IVector(size, incr));
- }
-
-
- //µ Stack::SetEmptyItem
- #pragma segment Stack
- inline void Stack::SetEmptyItem(void *anItem)
- {
- fEmptyItem = anItem;
- }
-
-
- //µ Stack::Depth
- #pragma segment Stack
- inline short Stack::Depth() const
- {
- return (fSP);
- }
-
-
- //µ Stack::SP
- #pragma segment Stack
- inline short Stack::SP() const
- {
- return (fSP - 1);
- }
-
-
- //µ Stack::IsEmpty
- #pragma segment Stack
- inline Boolean Stack::IsEmpty() const
- {
- return (fSP <= 0);
- }
-
-
- //µ Stack::Pop
- #pragma segment Stack
- inline void *Stack::Pop()
- {
- return (IsEmpty() ? fEmptyItem : _At(--fSP));
- }
-
-
- //µ Stack::Top
- #pragma segment Stack
- inline void Stack::Top(void *anItem)
- {
- AtPut(SP(), anItem);
- }
-
-
- //µ Stack::Top
- #pragma segment Stack
- inline void *Stack::Top() const
- {
- return (IsEmpty() ? fEmptyItem : _At(SP()));
- }
-
-
- //µ Stack::Pick
- #pragma segment Stack
- inline void *Stack::Pick(short n) const
- {
- return ((fSP > n) ? At(SP() - n) : fEmptyItem);
- }
-
-
- //µ Stack::Drop
- #pragma segment Stack
- inline void Stack::Drop(short n)
- {
- fSP = (fSP >= n) ? (fSP - n) : 0;
- }
-
-
- #endif
-
-
-