mdh.hephaestus2.containers
Class IntStack

java.lang.Object
  |
  +--mdh.hephaestus2.containers.IntStack
All Implemented Interfaces:
Externalizable, IntStackReadOnly, Serializable

public class IntStack
extends Object
implements IntStackReadOnly, Externalizable

A dynamic array of int values, usable as a stack or a list. I usually use it to store id numbers. Quite fast for appending, direct access, and popping, but relatively slow at removing and inserting (no worse than Vector, though).

Not synchronized, so be incredibly careful in a multithreading situation. If you have any doubts, do a synchronize(stack) around your calls, because if you get this in an inconsistent state it will likely munch your data.

Author:
Copyright © 1998 by Mark Hughes <kamikaze@kuoi.asui.uidaho.edu>
See Also:
Serialized Form

Constructor Summary
IntStack()
          Creates a new stack with a starting size of 1 element.
IntStack(int size)
           
 
Method Summary
 void add(int value)
          pushes 'val' onto the top of the stack, where it can be retrieved with pop() and peek(), or just data_[ size()-1 ]
 void clear()
          removes all elements from the stack
 boolean contains(int value)
           
 IntEnumeration elements()
           
 void ensureCapacity(int newsize)
          Make sure there's room for at least 'size' elements in the stack.
 int get(int i)
           
 int indexOf(int value)
           
 void insert(int ins, int val)
          Inserts a value before position 'ins'.
 boolean isEmpty()
           
 int lastIndexOf(int value)
           
 int peek()
          Returns top element without removing it.
 int pop()
          Removes and returns top element.
 void push(int val)
          Just calls 'add', so that's a touch faster.
 void readExternal(ObjectInput in)
           
 int remove(int rm)
          Deletes the element at index 'rm' and pulls everything above it down by one.
 void set(int i, int value)
          Changes the value at index 'i' to 'value'.
 int size()
          The number of elements actually defined.
 String toString()
          Produces [#,...]
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntStack

public IntStack()
Creates a new stack with a starting size of 1 element.


IntStack

public IntStack(int size)
Method Detail

add

public final void add(int value)
pushes 'val' onto the top of the stack, where it can be retrieved with pop() and peek(), or just data_[ size()-1 ]


clear

public final void clear()
removes all elements from the stack


contains

public final boolean contains(int value)
Specified by:
contains in interface IntStackReadOnly

ensureCapacity

public final void ensureCapacity(int newsize)
Make sure there's room for at least 'size' elements in the stack.


elements

public IntEnumeration elements()

get

public final int get(int i)
Specified by:
get in interface IntStackReadOnly
Returns:
the value at position i.

indexOf

public final int indexOf(int value)
Specified by:
indexOf in interface IntStackReadOnly

insert

public final void insert(int ins,
                         int val)
Inserts a value before position 'ins'. ins=0 inserts at the start of the list, ins=size() appends. Insertion entails a lot of copying, so this is quite slow. Use a linked list or Hash if you need insertion to be FAST.


isEmpty

public final boolean isEmpty()
Specified by:
isEmpty in interface IntStackReadOnly
Returns:
true if the IntStack is empty.

lastIndexOf

public final int lastIndexOf(int value)
Specified by:
lastIndexOf in interface IntStackReadOnly

peek

public final int peek()
Returns top element without removing it.

Specified by:
peek in interface IntStackReadOnly

pop

public final int pop()
Removes and returns top element.


push

public final void push(int val)
Just calls 'add', so that's a touch faster.


remove

public final int remove(int rm)
Deletes the element at index 'rm' and pulls everything above it down by one.


set

public final void set(int i,
                      int value)
Changes the value at index 'i' to 'value'.


size

public final int size()
The number of elements actually defined. Use this instead of data_.length, as the latter counts the capacity, not how many are currently active.

Specified by:
size in interface IntStackReadOnly

toString

public String toString()
Produces [#,...]

Specified by:
toString in interface IntStackReadOnly
Overrides:
toString in class Object

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
IOException
ClassNotFoundException

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
IOException