scuffed-code/tools/unicodetools/com/ibm/text/utility/IntStack.java

128 lines
3.5 KiB
Java
Raw Normal View History

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 $
* $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
// =============================================================
public final class IntStack implements Comparable, Cloneable {
2001-08-30 20:50:18 +00:00
private int[] values;
private int top = 0;
private int first = 0;
2001-08-30 20:50:18 +00:00
public IntStack(int initialSize) {
values = new int[initialSize];
}
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
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;
return this;
2001-08-30 20:50:18 +00:00
}
public int pop() {
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) {
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() {
return top - first == 0;
2001-08-30 20:50:18 +00:00
}
2001-09-19 23:33:52 +00:00
public void clear() {
top = first = 0;
2001-09-19 23:33:52 +00:00
}
public int compareTo(Object other) {
IntStack that = (IntStack) other;
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;
}
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;
for (int i = first; i < top; ++i) {
2001-09-19 23:33:52 +00:00
result = result * 37 + values[i];
}
return result;
}
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
}