kodkod.util.collections
Class Stack<T>

java.lang.Object
  extended by kodkod.util.collections.Stack<T>
All Implemented Interfaces:
java.lang.Iterable<T>
Direct Known Subclasses:
ArrayStack, LinkedStack

public abstract class Stack<T>
extends java.lang.Object
implements java.lang.Iterable<T>

Represents a last-in-first-out (LIFO) stack of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and an iterator over the elements in the stack (that does not support removal). When a stack is first created, it contains no items.

Author:
Emina Torlak
specfield:
size: int
elems: [0..size)->one T

Method Summary
abstract  boolean empty()
          Returns true if the stack is empty; otherwise returns false.
 boolean equals(java.lang.Object o)
          Returns true if o is a stack containing the same elements as this stack, in the same order.
 int hashCode()
          Returns the hashcode for this stack.
abstract  java.util.Iterator<T> iterator()
          Iterates over the items in this Stack, starting at the top of the stack and working its way down.
abstract  T peek()
          Looks at the object at the top of this stack without removing it from the stack.
abstract  T pop()
          Removes the object at the top of this stack and returns that object as the value of this function.
abstract  T push(T item)
          Pushes an item onto the top of this stack and returns it.
abstract  int search(java.lang.Object o)
          Returns the 1-based position where an object is on this stack.
abstract  int size()
          Returns the size of this stack.
 java.lang.String toString()
          Returns a string represention of this stack.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

size

public abstract int size()
Returns the size of this stack.

Returns:
this.size

push

public abstract T push(T item)
Pushes an item onto the top of this stack and returns it.

Returns:
item
effects:
this.size' = this.size + 1 && this.elems'[0] = item && all i: [0..this.size) | this.elems'[i+1] = this.elems[i]

pop

public abstract T pop()
Removes the object at the top of this stack and returns that object as the value of this function.

Returns:
this.elems[0]
Throws:
java.util.EmptyStackException - - no this.elems
effects:
this.size' = this.size - 1 && all i: [1..this.size) | this.elems'[i-1] = this.elems[i]

peek

public abstract T peek()
Looks at the object at the top of this stack without removing it from the stack.

Returns:
this.elems[0]
Throws:
java.util.EmptyStackException - - no this.elems

search

public abstract int search(java.lang.Object o)
Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack.

Returns:
o in this.elems[int] => min(this.elems.o) + 1, -1

empty

public abstract boolean empty()
Returns true if the stack is empty; otherwise returns false.

Returns:
no this.elems

iterator

public abstract java.util.Iterator<T> iterator()
Iterates over the items in this Stack, starting at the top of the stack and working its way down.

Specified by:
iterator in interface java.lang.Iterable<T>
Returns:
iterator over the elements in this stack.

equals

public boolean equals(java.lang.Object o)
Returns true if o is a stack containing the same elements as this stack, in the same order.

Overrides:
equals in class java.lang.Object
Returns:
o in Stack && this.elems = o.elems

hashCode

public int hashCode()
Returns the hashcode for this stack.

Overrides:
hashCode in class java.lang.Object
Returns:
the hashcode for this stack.

toString

public java.lang.String toString()
Returns a string represention of this stack.

Overrides:
toString in class java.lang.Object