70 lines
1.6 KiB
Java
70 lines
1.6 KiB
Java
|
/*******************************************************************************
|
||
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
||
|
* All rights reserved. This program and the accompanying materials
|
||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||
|
* which accompanies this distribution, and is available at
|
||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||
|
*
|
||
|
* Contributors:
|
||
|
* IBM Corporation - initial API and implementation
|
||
|
*******************************************************************************/
|
||
|
package com.ibm.wala.util.collections;
|
||
|
|
||
|
import com.ibm.wala.util.debug.Assertions;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* A stack of integer primitives. This should be more efficient than a java.util.Stack
|
||
|
*
|
||
|
* @author unknown
|
||
|
*/
|
||
|
public class IntStack {
|
||
|
|
||
|
/**
|
||
|
* Comment for <code>top</code>
|
||
|
*/
|
||
|
private int top = -1;
|
||
|
/**
|
||
|
* Comment for <code>state</code>
|
||
|
*/
|
||
|
private int state[] = new int[0];
|
||
|
|
||
|
/**
|
||
|
* @param i
|
||
|
*/
|
||
|
public void push(int i) {
|
||
|
if (state.length <= (top+1)) {
|
||
|
int newState[] = new int[ state.length*2 + 1 ];
|
||
|
System.arraycopy(state, 0, newState, 0, state.length);
|
||
|
state = newState;
|
||
|
}
|
||
|
|
||
|
state[++top] = i;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return the int at the top of the stack
|
||
|
*/
|
||
|
public int peek() {
|
||
|
Assertions._assert(! isEmpty());
|
||
|
return state[top];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* pop the stack
|
||
|
* @return the int at the top of the stack
|
||
|
*/
|
||
|
public int pop() {
|
||
|
Assertions._assert(! isEmpty());
|
||
|
return state[top--];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return true iff the stack is empty
|
||
|
*/
|
||
|
public boolean isEmpty() {
|
||
|
return top == -1;
|
||
|
}
|
||
|
|
||
|
}
|