home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / ada_1 / Examples_ada_stack_ < prev    next >
Encoding:
Text File  |  1994-08-14  |  1.0 KB  |  39 lines

  1. -- ++
  2. -- A simple stack for integers.
  3. -- --
  4.  
  5. package Stack is
  6.  
  7.    type Stack ( Max : Natural ) is limited private;
  8.    
  9.    Empty_Stack : constant Stack;
  10.  
  11.    procedure Push ( The_Stack : in out Stack; Value : in Integer );
  12.  
  13.    procedure Pop  ( The_Stack : in out Stack; Value : out Integer );
  14.  
  15.    function  "="  ( The_Stack : in Stack; A_Stack : in Stack ) return Boolean;
  16.    
  17.    function Empty ( The_Stack : in Stack ) return Boolean;
  18.    
  19.    function Full  ( The_Stack : in Stack ) return Boolean;
  20.    
  21.    Underflow : exception;
  22.    Overflow  : exception;
  23.    
  24. pragma INLINE ( Empty, Full );
  25.  
  26. private
  27.  
  28.    type Vector is array ( Integer range <> ) of Integer;
  29.    
  30.    type Stack ( Max : Natural ) is record
  31.       S : Vector ( 1 .. Max );
  32.       Top : Natural := 0;       -- can't use Max, discriminant not allowed in range constraint
  33.    end record;
  34.    
  35.    -- there should be a discriminant on Stack here but the compiler barfs.
  36.    -- The following seems to work ok.
  37.    Empty_Stack : constant Stack := ( Max => 0, S => ( 1..0 => 0 ), Top => 0 );
  38.    
  39. end Stack;