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 $
|
2001-09-19 23:33:52 +00:00
|
|
|
* $Date: 2001/09/19 23:33:52 $
|
|
|
|
* $Revision: 1.3 $
|
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
|
|
|
|
// =============================================================
|
|
|
|
|
2001-09-19 23:33:52 +00:00
|
|
|
public final class IntStack implements Comparable {
|
2001-08-30 20:50:18 +00:00
|
|
|
private int[] values;
|
|
|
|
private int top = 0;
|
|
|
|
|
|
|
|
public IntStack(int initialSize) {
|
|
|
|
values = new int[initialSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
public void push(int value) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int pop() {
|
|
|
|
if (top > 0) return values[--top];
|
|
|
|
throw new IllegalArgumentException("Stack underflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int get(int index) {
|
|
|
|
if (0 <= index && index < top) return values[index];
|
|
|
|
throw new IllegalArgumentException("Stack index out of bounds");
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTop() {
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return top == 0;
|
|
|
|
}
|
2001-09-19 23:33:52 +00:00
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
top = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Object other) {
|
|
|
|
IntStack that = (IntStack) other;
|
|
|
|
int min = top;
|
|
|
|
if (min < that.top) min = that.top;
|
|
|
|
for (int i = 0; i < min; ++i) {
|
|
|
|
int result = values[i] - that.values[i];
|
|
|
|
if (result != 0) return result;
|
|
|
|
}
|
|
|
|
return top - that.top;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object other) {
|
|
|
|
return compareTo(other) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
int result = top;
|
|
|
|
for (int i = 0; i < top; ++i) {
|
|
|
|
result = result * 37 + values[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2001-08-30 20:50:18 +00:00
|
|
|
}
|