01: /*
02: * $Id: LongStack.java,v 1.3 2004/07/08 08:01:45 yuvalo Exp $
03: *
04: * (C) Copyright 2002-2004 by Yuval Oren. All rights reserved.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.bluecast.util;
20:
21: /**
22: * A stack of native longs.
23: *
24: * @author Yuval Oren, yuval@bluecast.com
25: * @version $Revision: 1.3 $
26: */
27: final public class LongStack {
28: private long[] stack;
29: private int pos; // position of the top of the stack
30:
31: public LongStack(int initialSize) {
32: stack = new long[initialSize];
33: pos = -1;
34: }
35:
36: public long pop() {
37: if (pos >= 0) {
38: return stack[pos--];
39: } else
40: throw new ArrayIndexOutOfBoundsException("stack underflow");
41: }
42:
43: public void push(long s) {
44: if (pos + 1 < stack.length) {
45: stack[++pos] = s;
46: } else {
47: setSize(stack.length * 2);
48: stack[++pos] = s;
49: }
50: }
51:
52: public void setSize(int newSize) {
53: if (newSize != stack.length) {
54: long[] newStack = new long[newSize];
55: System.arraycopy(stack, 0, newStack, 0, Math.min(
56: stack.length, newSize));
57: stack = newStack;
58: }
59: }
60:
61: public void clear() {
62: pos = -1;
63: }
64:
65: public int size() {
66: return pos + 1;
67: }
68: }
|