a_stack.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-------------------------> GNU Sather - sourcefile <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute and/or modify it under the terms of the GNU Library General --
-- Public License (LGPL) as published by the Free Software Foundation; --
-- either version 2 of the license, or (at your option) any later version. --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details. --
-- The license text is also available from: Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
--------------> Please email comments to <bug-sather@gnu.org> <--------------
class STACK{ETP} < $STACK{ETP}
class STACK{ETP} < $STACK{ETP} is
-- This stack class is an implementation of the abstract class using
-- arrays -- merely by including that class.
-- Version 1.2 Mar 97. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 13 Jul 96 bg Original
-- 13 Mar 97 kh Adapted for style
include A_STACK{ETP}
end ; -- STACK
class A_STACK{ETP} < $STACK{ETP}
class A_STACK{ETP} < $STACK{ETP} is
-- This class implements the generic stack object by asing an array
-- based stack which is in turn implemented by an FLIST which allocates
-- space by amortized doubling.
-- Version 1.2 Nov 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 13 Jul 96 bg Original
-- 13 Mar 97 kh Adapted for CARD
-- 12 Nov 98 kh Revised for 1.2, added pre/post conditions.
--include VISITOR_MUTATOR ;
private attr list : FLIST{ETP} ;
create : SAME is
-- This routine creates a new stack which is empty.
me : SAME := new ;
-- me.init_visitor_mutator ;
me.list := FLIST{ETP}::create ;
return me
end ;
create(
arr : $ELT{ETP}
) : SAME is
-- This routine creates a stack which it fills by pushing the elements
-- of arr such that the last element is at the top of the stack.
me : SAME := create ;
loop
me.push(arr.elt!)
end ;
return me
end ;
create_from(
arr : ARRAY{ETP}
) : SAME is
-- This routine is the same as the one above, but permits the
-- specification of a stack with a literal argument.
return create(arr)
end ;
create_capacity(
cnt : CARD
) : SAME is
-- This creation routine creates a stack with the given number of
-- elements. The stack can, of course, grow later.
res : SAME := new ;
res.list := FLIST{ETP}::create(cnt) ;
-- res.init_visitor_mutator ;
return res
end ;
copy : SAME
pre ~void(self)
post true -- (result = self)
is
-- This routine returns a new copy of self.
-- lock visitor then
res : SAME := create_capacity(size) ;
res.list := list.copy ;
return res
-- end
end ;
is_empty : BOOL
pre ~void(self)
post true
is
-- This predicate returns true if and only if the stack is empty.
return size = 0
end ;
contains(
elem : ETP
) : BOOL
pre ~void(self) is
-- This predicate returns true if and only if the element elem is in
-- the stack.
-- lock visitor then
return(list.contains(elem))
-- end
end ;
-- The access/insertion routines are subject to locking in the
-- presence of multiple processes.
size : CARD
pre ~void(self)
post (result = list.size)
is
-- This routine returns the current size of the stack.
return list.size
end ;
pop : ETP
pre ~void(self)
and ~is_empty
post (result = initial(list.top))
is
-- This routine removes the item from the top of the stack and
-- returns it.
-- lock mutator then
return list.pop
-- end
end ;
top : ETP
pre ~void(self)
and ~is_empty
post (result = initial(list.top))
is
-- This routine returns the item at the top of the stack.
-- lock visitor then
return list.top
-- end
end ;
remove : ETP
pre ~void(self)
and ~is_empty
post (result = initial(list.top))
is
-- This routine is a synonym for pop.
-- lock mutator then
return pop
-- end
end ;
current : ETP
pre ~void(self)
and ~is_empty
post (result = initial(list.top))
is
-- This routine is a synonym for top.
-- lock visitor then
return top
-- end
end ;
push(
elem : ETP
)
pre ~void(self)
post (top = elem)
is
-- This routine locks the stack and then pushes an element onto it.
-- lock mutator then
list := list.push(elem)
-- end
end ;
rev! : ETP
pre ~void(self)
post contains(result)
is
-- This iter locks the stack and then yields elements of the stack in
-- reverse order (from bottom to top)!
-- lock visitor then
loop
yield(list.elt!)
end
-- end
end ;
elt! : ETP
pre ~void(self)
post contains(result)
is
-- This iter locks the stack and then yields elements of it in
-- the order top to bottom.
-- lock visitor then
loop
yield list[(list.size - 1).downto!(0)]
end
-- end
end ;
top! : ETP
pre ~void(self)
post contains(result)
is
-- This iter is synonmous with elt!
-- lock visitor then
loop
yield list[(list.size - 1).downto!(0)]
end
-- end
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(self)
and ~void(lib)
post ~void(result)
is
-- This routine returns a string representation of the stack -- which
-- is an FLIST string representation.
-- lock visitor then
return list.str
-- end
end ;
str : STR
pre ~void(self)
post ~void(result)
is
-- This routine returns a string representation of the stack -- which
-- is an FLIST string representation.
return str(LIBCHARS::default)
end ;
end ; -- A_STACK