home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / misc / stack.ada < prev    next >
Encoding:
Text File  |  1988-05-03  |  6.7 KB  |  195 lines

  1.  
  2. -------- SIMTEL20 Ada Software Repository Prologue ------------
  3. --                                                           -*
  4. -- Unit name    : stack_package
  5. -- Version      : 1.0
  6. -- Author       : Tom Duke
  7. --              : TI Ada Technology Branch
  8. --              : PO Box 801, MS 8007
  9. --              : McKinney, TX  75069
  10. -- DDN Address  : DUKE%TI-EG at CSNET-RELAY
  11. -- Copyright    : (c) N/A 
  12. -- Date created : 16 Apr 85 
  13. -- Release date : 16 Apr 85 
  14. -- Last update  : 16 Apr 85 
  15. -- Machine/System Compiled/Run on :DG MV 10000, ROLM ADE
  16. --                                                           -*
  17. ---------------------------------------------------------------
  18. --                                                           -*
  19. -- Keywords     : stack, generic stack 
  20. ----------------:
  21. --
  22. -- Abstract     : This is a generic package that provides the types,
  23. ----------------: procedures, and exceptions to define an abstract stack
  24. ----------------: and its corresponding operations.  Using an
  25. ----------------: instantiation of this generic package, one can declare
  26. ----------------: multiple versions of a stack of type GENERIC_STACK.
  27. ----------------: The stack operations provided include:
  28. ----------------: 1. clear the stack,
  29. ----------------: 2. pop the stack,
  30. ----------------: 3. push an element onto the stack, and
  31. ----------------: 4. access the top element on the stack.
  32. ----------------:  
  33. --                                                           -*
  34. ------------------ Revision history ---------------------------
  35. --                                                           -*
  36. -- DATE         VERSION    AUTHOR                  HISTORY
  37. -- 4/16/85    1.0    Tom Duke        Initial Release
  38. --                                                           -*
  39. ------------------ Distribution and Copyright -----------------
  40. --                                                           -*
  41. -- This prologue must be included in all copies of this software.
  42. --
  43. -- This software is released to the Ada community.
  44. -- This software is released to the Public Domain (note:
  45. --   software released to the Public Domain is not subject
  46. --   to copyright protection).
  47. -- Restrictions on use or distribution:  NONE
  48. --                                                           -*
  49. ------------------ Disclaimer ---------------------------------
  50. --                                                           -*
  51. -- This software and its documentation are provided "AS IS" and
  52. -- without any expressed or implied warranties whatsoever.
  53. -- No warranties as to performance, merchantability, or fitness
  54. -- for a particular purpose exist.
  55. --
  56. -- Because of the diversity of conditions and hardware under
  57. -- which this software may be used, no warranty of fitness for
  58. -- a particular purpose is offered.  The user is advised to
  59. -- test the software thoroughly before relying on it.  The user
  60. -- must assume the entire risk and liability of using this
  61. -- software.
  62. --
  63. -- In no event shall any person or organization of people be
  64. -- held responsible for any direct, indirect, consequential
  65. -- or inconsequential damages or lost profits.
  66. --                                                           -*
  67. -------------------END-PROLOGUE--------------------------------
  68.  
  69. generic
  70.  
  71.   type ELEMENTS is private;
  72.   SIZE : POSITIVE;
  73.  
  74. package STACK_PACKAGE is
  75.  
  76.   type GENERIC_STACK is  private;
  77.  
  78.  
  79.   function TOP_ELEMENT( STACK  : in  GENERIC_STACK )
  80.     return ELEMENTS;
  81.  
  82.   function STACK_IS_EMPTY( STACK : in GENERIC_STACK )
  83.     return BOOLEAN;
  84.  
  85.   procedure CLEAR_STACK( STACK : in out GENERIC_STACK );
  86.  
  87.  
  88.   procedure PUSH       ( FRAME : in ELEMENTS;
  89.                          STACK : in out GENERIC_STACK );
  90.  
  91.   procedure POP        ( FRAME : out ELEMENTS;
  92.                          STACK : in out GENERIC_STACK );
  93.  
  94.   NULL_STACK      : exception;
  95.   STACK_OVERFLOW  : exception;
  96.   STACK_UNDERFLOW : exception;
  97.  
  98.  
  99. private
  100.  
  101.   type STACK_LIST is array ( 1 .. SIZE ) of ELEMENTS;
  102.  
  103.   type GENERIC_STACK  is
  104.      record
  105.       CONTENTS       :  STACK_LIST;
  106.       TOP            :  NATURAL range NATURAL'FIRST .. SIZE := NATURAL'FIRST;
  107.      end record;
  108.  
  109. end STACK_PACKAGE;
  110.  
  111.  
  112. -------------------------------------------------------------------------
  113.  
  114.  
  115. package body STACK_PACKAGE is
  116.  
  117. ---------------
  118. --  function TOP_ELEMENT  --  This function returns the value of the top
  119. --                            element on the stack.  It does not return a
  120. --  pointer to the top element.  If the stack is empty, a constraint error
  121. --  occurs.  The exception handler will then raise the NULL_STACK
  122. --  exception and pass it to the calling procedure.
  123. ---------------
  124.   function TOP_ELEMENT( STACK : in  GENERIC_STACK ) return ELEMENTS is
  125.   begin
  126.    return STACK.CONTENTS(STACK.TOP);
  127.    exception
  128.       when CONSTRAINT_ERROR =>
  129.          raise NULL_STACK;
  130.       when others =>
  131.          raise;
  132.   end TOP_ELEMENT;
  133.  
  134.   ----------
  135.   --  Is stack empty?
  136.   ----------
  137.   function STACK_IS_EMPTY( STACK : in GENERIC_STACK )
  138.     return BOOLEAN is
  139.   begin
  140.     return (STACK.TOP = NATURAL'FIRST);
  141.   exception
  142.     when OTHERS =>
  143.          raise;
  144.   end STACK_IS_EMPTY;
  145.  
  146.  
  147. ---------------
  148. --  procedure CLEAR_STACK  --  This procedure resets the stack pointer, TOP,
  149. --                             to a value representing an empty stack.
  150. ---------------
  151.   procedure CLEAR_STACK( STACK : in out GENERIC_STACK ) is
  152.   begin
  153.    STACK.TOP := NATURAL'FIRST;
  154.   end CLEAR_STACK;
  155.  
  156.  
  157. ---------------
  158. --  procedure PUSH  --  This procedure attempts to push another element onto
  159. --                      the stack.  If the stack is full, a constraint error
  160. --  occurs.  The exception handler will then raise the STACK_OVERFLOW
  161. --  exception and pass it to the calling procedure.
  162. ---------------
  163.   procedure PUSH       ( FRAME : in ELEMENTS;
  164.                          STACK : in out GENERIC_STACK ) is
  165.   begin
  166.    STACK.TOP := STACK.TOP + 1;
  167.    STACK.CONTENTS(STACK.TOP) := FRAME;
  168.    exception
  169.       when CONSTRAINT_ERROR =>
  170.          raise STACK_OVERFLOW;
  171.       when others =>
  172.          raise;
  173.   end PUSH;
  174.  
  175.  
  176. ---------------
  177. --  procedure POP  --  This procedure attempts to pop an element from
  178. --                     the stack.  If the stack is empty, a constraint error
  179. --  occurs.  The exception handler will then raise the STACK_UNDERFLOW
  180. --  exception and pass it to the calling procedure.
  181. ---------------
  182.   procedure POP        ( FRAME : out ELEMENTS;
  183.                          STACK : in out GENERIC_STACK ) is
  184.   begin
  185.    FRAME := STACK.CONTENTS(STACK.TOP);
  186.    STACK.TOP := STACK.TOP - 1;
  187.    exception
  188.       when CONSTRAINT_ERROR =>
  189.          raise STACK_UNDERFLOW;
  190.       when others =>
  191.          raise;
  192.   end POP;
  193.  
  194. end STACK_PACKAGE;
  195.