| 1 |
The stack module implements pushdown stacks for storing integers, |
| 2 |
characters, or arbitrary pointers (objects). |
| 3 |
|
| 4 |
The module uses a convention of prepending \ccode{I}, \ccode{C}, |
| 5 |
\ccode{P} to \ccode{Create()}, \ccode{Push()}, and \ccode{Pop()} |
| 6 |
function names, to indicate the stack's datatype as integer, |
| 7 |
character, or pointer, respectively. For example, |
| 8 |
\ccode{esl\_stack\_PCreate()} creates a stack for pointer |
| 9 |
storage. (This is also the naming convention in the \ccode{vectorops} |
| 10 |
module.) Stacks can be thought of as typed objects, with the type |
| 11 |
defined by the \ccode{Create()} call. Types may not be mixed for any |
| 12 |
particular created stack. |
| 13 |
|
| 14 |
|
| 15 |
% Table generated by autodoc -t esl_stack.c (so don't edit here, edit esl_stack.c:) |
| 16 |
\begin{table}[hbp] |
| 17 |
\begin{center} |
| 18 |
{\small |
| 19 |
\begin{tabular}{|ll|}\hline |
| 20 |
\apisubhead{The \ccode{ESL\_STACK} object.}\\ |
| 21 |
\hyperlink{func:esl_stack_ICreate()}{\ccode{esl\_stack\_ICreate()}} & Create an integer stack.\\ |
| 22 |
\hyperlink{func:esl_stack_CCreate()}{\ccode{esl\_stack\_CCreate()}} & Create a character stack.\\ |
| 23 |
\hyperlink{func:esl_stack_PCreate()}{\ccode{esl\_stack\_PCreate()}} & Create a pointer stack.\\ |
| 24 |
\hyperlink{func:esl_stack_Reuse()}{\ccode{esl\_stack\_Reuse()}} & Reuse a stack.\\ |
| 25 |
\hyperlink{func:esl_stack_Destroy()}{\ccode{esl\_stack\_Destroy()}} & Free a stack.\\ |
| 26 |
\apisubhead{Other functions in the API.}\\ |
| 27 |
\hyperlink{func:esl_stack_IPush()}{\ccode{esl\_stack\_IPush()}} & Push an integer onto a stack.\\ |
| 28 |
\hyperlink{func:esl_stack_CPush()}{\ccode{esl\_stack\_CPush()}} & Push a char onto a stack.\\ |
| 29 |
\hyperlink{func:esl_stack_PPush()}{\ccode{esl\_stack\_PPush()}} & Push a pointer onto a stack.\\ |
| 30 |
\hyperlink{func:esl_stack_IPop()}{\ccode{esl\_stack\_IPop()}} & Pop an integer off a stack.\\ |
| 31 |
\hyperlink{func:esl_stack_CPop()}{\ccode{esl\_stack\_CPop()}} & Pop a char off a stack.\\ |
| 32 |
\hyperlink{func:esl_stack_PPop()}{\ccode{esl\_stack\_PPop()}} & Pop a pointer off a stack.\\ |
| 33 |
\hyperlink{func:esl_stack_ObjectCount()}{\ccode{esl\_stack\_ObjectCount()}} & Return the number of objects in a stack.\\ |
| 34 |
\hyperlink{func:esl_stack_Convert2String()}{\ccode{esl\_stack\_Convert2String()}} & Convert a char stack to a string.\\ |
| 35 |
\hyperlink{func:esl_stack_DiscardTopN()}{\ccode{esl\_stack\_DiscardTopN()}} & Discard the top elements on a stack.\\ |
| 36 |
\hline |
| 37 |
\end{tabular} |
| 38 |
} |
| 39 |
\end{center} |
| 40 |
\caption{The \eslmod{stack} API.} |
| 41 |
\label{tbl:stack_api} |
| 42 |
\end{table} |
| 43 |
|
| 44 |
\subsection{Example of using the stack API} |
| 45 |
|
| 46 |
Figure~\ref{fig:stack_example} shows an example of using the integer |
| 47 |
stack, pushing 42, 7, and 3 on, then popping them off. |
| 48 |
|
| 49 |
\begin{figure} |
| 50 |
\input{cexcerpts/stack_example} |
| 51 |
\caption{An example of pushing and popping data onto a stack.} |
| 52 |
\label{fig:stack_example} |
| 53 |
\end{figure} |
| 54 |
|
| 55 |
The \ccode{Create()} functions create a stack for a particular |
| 56 |
purpose. \ccode{esl\_stack\_ICreate()} creates a stack for integers, |
| 57 |
\ccode{esl\_stack\_CCreate()} creates a stack for characters, and |
| 58 |
\ccode{esl\_stack\_PCreate()} creates a stack for pointers. They |
| 59 |
throw NULL if an allocation fails. All three stack types are free'd |
| 60 |
by a call to \ccode{esl\_stack\_Destroy()}. All three types can also |
| 61 |
be reused without reallocation or recreation by |
| 62 |
\ccode{esl\_stack\_Reuse()}. A \ccode{Reuse()}'d stack retains its |
| 63 |
original datatype. |
| 64 |
|
| 65 |
The \ccode{Push()} functions push one datum onto the stack, of the |
| 66 |
appropriate type. They throw \ccode{eslEMEM} if the stack needs to |
| 67 |
reallocate internally but fails. |
| 68 |
|
| 69 |
The \ccode{Pop()} functions pop one datum off the stack, returning it |
| 70 |
through a passed pointer. They return \ccode{eslOK} on success, and |
| 71 |
\ccode{eslEOD} if the stack is empty. |
| 72 |
|
| 73 |
\ccode{esl\_stack\_ObjectCount()} returns the number of objects stored |
| 74 |
in the stack. |
| 75 |
|
| 76 |
A special function, \ccode{esl\_stack\_Convert2String()}, operates |
| 77 |
only on character stacks. It converts the stack structure to a |
| 78 |
NUL-terminated string, with characters in the same order they were |
| 79 |
pushed. The stack is destroyed by this operation, leaving a |
| 80 |
\ccode{char *} behind. |
| 81 |
|
| 82 |
\subsection{Allocation strategy} |
| 83 |
|
| 84 |
Stacks are initially allocated for a certain number of objects, |
| 85 |
defined by a compile-time constant \ccode{ESL\_STACK\_INITALLOC} in |
| 86 |
\ccode{esl\_stack.h}. The default is 128. Whenever a stack needs to grow, |
| 87 |
it reallocates by doubling its current allocation. |
| 88 |
|
| 89 |
|