a_stack.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
---------------------------> Sather 1.1 source file <--------------------------
-- a_stack.sa: Array based stack
-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: a_stack.sa,v 1.5 1996/04/09 10:04:38 borisv Exp $
--
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.

--!!! THIS FILE HAS BEEN CREATED FROM a_stack.sa, DO NOT EDIT IT !!! class STACK{T} < $STACK{T} is include A_STACK{T} end; --NR: ---NR: class NR_STACK{T} < $NR_STACK{T} is include NR_A_STACK{T} end;

class A_STACK{T} < $STACK{T

class A_STACK{T} < $STACK{T} --NR: class NR_A_STACK{T} < $NR_STACK{T} is -- An array-based stack implemented by delegation to an FLIST{T}, -- which allocates space by amortized doubling. private attr s: FLIST{T}; create: SAME is res ::= new; res.s := #FLIST{T};-- A_STACK{1}::s FLIST{1}::create return(res); end; create(e: $ELT{T}): SAME is -- Push the elements of "e" onto the stack. -- If "e" is an ordered collection this will push elements -- such that the last element is left at the top of the stack res ::= #SAME; loop res.push(e.elt!) end; return res; end; create_from(a: ARRAY{T}): SAME is return create(a); end; create_capacity(n: INT): SAME is -- Preallocate n empty elements res ::= new; res.s := #FLIST{T}(n); return(res); end; remove:T is return pop; end; current: T is return top ; end; push(e: T) pre ~void(self) is-- BOOL::not s := s.push(e);-- A_STACK{1}::s A_STACK{1}::s FLIST{1}::push end; pop: T pre ~void(self) and ~is_empty is -- BOOL::not A_STACK{1}::is_empty BOOL::not return(s.pop);-- A_STACK{1}::s FLIST{1}::pop end; top: T pre ~void(self) and ~is_empty is-- BOOL::not A_STACK{1}::is_empty BOOL::not -- Return the top element of the stack return(s.top);-- A_STACK{1}::s FLIST{1}::top end; reverse_elt!: T pre ~void(self) is-- BOOL::not -- Yield the elements of the stack in reverse order i.e. -- ending with "top". loop yield(s.elt!) end;-- A_STACK{1}::s FLIST{1}::elt! end; elt!: T pre ~void(self) is loop yield s[(s.size-1).downto!(0)]; end; end; top!: T pre ~void(self) is-- BOOL::not -- Same as elt! loop yield s[(s.size-1).downto!(0)]; end;-- A_STACK{1}::s FLIST{1}::aget A_STACK{1}::s FLIST{1}::size INT::minus INT::downto! end; size: INT pre ~void(self) is return(s.size) end;-- BOOL::not A_STACK{1}::s FLIST{1}::size is_empty: BOOL pre ~void(self) is return(size = 0) end;-- BOOL::not A_STACK{1}::size INT::is_eq has(e: T): BOOL pre ~void(self) is return(s.has(e)); end; str: STR is return s.str;-- A_STACK{1}::s FLIST{1}::str end; copy: $STACK{T} pre ~void(self) is --NR: --NR: copy: $NR_STACK{T} pre ~void(self) is res ::= create_capacity(size); res.s := s.copy; return res; end; end;