42bddd7bf5
X-SVN-Rev: 5824
81 lines
2.2 KiB
Java
81 lines
2.2 KiB
Java
/**
|
|
*******************************************************************************
|
|
* 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: 2001/09/19 23:33:52 $
|
|
* $Revision: 1.3 $
|
|
*
|
|
*******************************************************************************
|
|
*/
|
|
|
|
package com.ibm.text.utility;
|
|
|
|
// =============================================================
|
|
// Simple stack mechanism, with push, pop and access
|
|
// =============================================================
|
|
|
|
public final class IntStack implements Comparable {
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |