2001-08-31 00:20:40 +00:00
|
|
|
/**
|
|
|
|
*******************************************************************************
|
|
|
|
* Copyright (C) 1996-2001, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* $Source: /xsrl/Nsvn/icu/unicodetools/com/ibm/text/utility/IntStack.java,v $
|
2002-06-15 02:47:14 +00:00
|
|
|
* $Date: 2002/06/15 02:47:14 $
|
|
|
|
* $Revision: 1.4 $
|
2001-08-31 00:20:40 +00:00
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2001-08-30 20:50:18 +00:00
|
|
|
package com.ibm.text.utility;
|
|
|
|
|
|
|
|
// =============================================================
|
|
|
|
// Simple stack mechanism, with push, pop and access
|
|
|
|
// =============================================================
|
|
|
|
|
2002-06-15 02:47:14 +00:00
|
|
|
public final class IntStack implements Comparable, Cloneable {
|
2001-08-30 20:50:18 +00:00
|
|
|
private int[] values;
|
|
|
|
private int top = 0;
|
2002-06-15 02:47:14 +00:00
|
|
|
private int first = 0;
|
2001-08-30 20:50:18 +00:00
|
|
|
|
|
|
|
public IntStack(int initialSize) {
|
|
|
|
values = new int[initialSize];
|
|
|
|
}
|
2002-06-15 02:47:14 +00:00
|
|
|
|
|
|
|
public IntStack append(IntStack other) {
|
|
|
|
// TODO speed up by copying arrays
|
|
|
|
for (int i = 0; i < other.getTop(); ++i) {
|
|
|
|
push(other.get(i));
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntStack append(int value) {
|
|
|
|
return push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int length() {
|
|
|
|
return top - first;
|
|
|
|
}
|
2001-08-30 20:50:18 +00:00
|
|
|
|
2002-06-15 02:47:14 +00:00
|
|
|
public IntStack push(int value) {
|
2001-08-30 20:50:18 +00:00
|
|
|
if (top >= values.length) { // must grow?
|
|
|
|
int[] temp = new int[values.length*2];
|
|
|
|
System.arraycopy(values,0,temp,0,values.length);
|
|
|
|
values = temp;
|
|
|
|
}
|
|
|
|
values[top++] = value;
|
2002-06-15 02:47:14 +00:00
|
|
|
return this;
|
2001-08-30 20:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int pop() {
|
2002-06-15 02:47:14 +00:00
|
|
|
if (top > first) {
|
|
|
|
int result = values[--top];
|
|
|
|
if (top == first && first > 0) {
|
|
|
|
top = first = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Stack underflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int popFront() {
|
|
|
|
if (top > first) {
|
|
|
|
int result = values[first++];
|
|
|
|
if (top == first) {
|
|
|
|
top = first = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2001-08-30 20:50:18 +00:00
|
|
|
throw new IllegalArgumentException("Stack underflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int get(int index) {
|
2002-06-15 02:47:14 +00:00
|
|
|
if (first <= index && index < top) return values[index];
|
2001-08-30 20:50:18 +00:00
|
|
|
throw new IllegalArgumentException("Stack index out of bounds");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTop() {
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
2002-06-15 02:47:14 +00:00
|
|
|
return top - first == 0;
|
2001-08-30 20:50:18 +00:00
|
|
|
}
|
2001-09-19 23:33:52 +00:00
|
|
|
|
|
|
|
public void clear() {
|
2002-06-15 02:47:14 +00:00
|
|
|
top = first = 0;
|
2001-09-19 23:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Object other) {
|
|
|
|
IntStack that = (IntStack) other;
|
2002-06-15 02:47:14 +00:00
|
|
|
int myLen = top - first;
|
|
|
|
int thatLen = that.top - that.first;
|
|
|
|
int limit = first + ((myLen < thatLen) ? myLen : thatLen);
|
|
|
|
int delta = that.first - first;
|
|
|
|
for (int i = first; i < limit; ++i) {
|
|
|
|
int result = values[i] - that.values[i + delta];
|
2001-09-19 23:33:52 +00:00
|
|
|
if (result != 0) return result;
|
|
|
|
}
|
2002-06-15 02:47:14 +00:00
|
|
|
return myLen - thatLen;
|
2001-09-19 23:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
return compareTo(other) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
int result = top;
|
2002-06-15 02:47:14 +00:00
|
|
|
for (int i = first; i < top; ++i) {
|
2001-09-19 23:33:52 +00:00
|
|
|
result = result * 37 + values[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2002-06-15 02:47:14 +00:00
|
|
|
|
|
|
|
public Object clone() {
|
|
|
|
try {
|
|
|
|
IntStack result = (IntStack) (super.clone());
|
|
|
|
result.values = (int[]) result.values.clone();
|
|
|
|
return result;
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
throw new IllegalArgumentException("Will never happen");
|
|
|
|
}
|
|
|
|
}
|
2001-08-30 20:50:18 +00:00
|
|
|
}
|