JML

org.multijava.util
Class IntStack

java.lang.Object
  extended byorg.multijava.util.IntStack

public final class IntStack
extends Object

This class represents a simple stack of integer values. Unlike java.util.Stack it is not synchronized and instead of storing Objects it store int value. Both of these changes make it faster than using Stack.

Version:
$Revision: 1.6 $
Author:
Curtis Clifton

Field Summary
static int DEFAULT_CAPACITY
           
static float MIN_LOAD_FACTOR
           
private  int minimumCapacity
           
private  int shrinkIfTopLessThanThis
           
private  int[] storage
           
private  int top
           
 
Constructor Summary
IntStack()
          Constructs a stack with default initial capacity.
IntStack(int initialCapacity)
          Constructs a stack with the given initial capacity.
 
Method Summary
private  void adjustCapacity(int newCapacity)
          Create new storage array of size newCapacity.
 int capacity()
          Returns the capacity of the stack (i.e., the total number of items that the stack can contains without automatically allocating more storage space.
 boolean isEmpty()
          Returns true if this stack is empty.
private  void maybeExpandStorage()
          Expand the storage to accomodate more items.
private  void maybeShrinkStorage()
          Shrink the storage to release memory if the load factor of the storage falls below MIN_LOAD_FACTOR.
 int peek()
          Returns the top item in the stack without modifying the stack.
 int pop()
          Pops the top item from the stack and returns it.
 void push(int i)
          Pushes the given item onto the stack.
 int size()
          Returns the size of this stack (i.e., the total number of items in the stack.
private  void throwExceptionIfEmpty()
          Throws an empty stack exception is the stack is empty.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

storage

private int[] storage

top

private int top

minimumCapacity

private int minimumCapacity

shrinkIfTopLessThanThis

private int shrinkIfTopLessThanThis

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY

MIN_LOAD_FACTOR

public static final float MIN_LOAD_FACTOR
Constructor Detail

IntStack

public IntStack()
Constructs a stack with default initial capacity.


IntStack

public IntStack(int initialCapacity)
Constructs a stack with the given initial capacity.

Method Detail

isEmpty

public boolean isEmpty()
Returns true if this stack is empty.


peek

public int peek()
Returns the top item in the stack without modifying the stack.

Throws:
EmptyStackException - if the stack is empty

pop

public int pop()
Pops the top item from the stack and returns it.

Throws:
EmptyStackException - if the stack is empty

push

public void push(int i)
Pushes the given item onto the stack.


size

public int size()
Returns the size of this stack (i.e., the total number of items in the stack.


capacity

public int capacity()
Returns the capacity of the stack (i.e., the total number of items that the stack can contains without automatically allocating more storage space.


throwExceptionIfEmpty

private void throwExceptionIfEmpty()
Throws an empty stack exception is the stack is empty. This avoids duplicating code in peek and pop. Since the method is private it can be inlined by compilers or JVMs.


maybeShrinkStorage

private void maybeShrinkStorage()
Shrink the storage to release memory if the load factor of the storage falls below MIN_LOAD_FACTOR.


maybeExpandStorage

private void maybeExpandStorage()
Expand the storage to accomodate more items.

 assignable storage, shrinkIfTopLessThanThis;
 ensures size() < capacity();
 


adjustCapacity

private void adjustCapacity(int newCapacity)
Create new storage array of size newCapacity. Copy existing data to new array.

 requires newCapacity >= size() + 1;
 requires newCapacity >= minimumCapacity;
 assignable storage, shrinkIfTopLessThanThis;
 ensures storage.length == newCapacity;
 


JML

JML is Copyright (C) 1998-2002 by Iowa State University and is distributed under the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This release depends on code from the MultiJava project and is based in part on the Kopi project Copyright (C) 1990-99 DMS Decision Management Systems Ges.m.b.H.