KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > IntStack


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: IntStack.java,v 1.11 2004/02/17 04:21:14 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.utils;
20
21 import java.util.EmptyStackException JavaDoc;
22
23 /**
24  * Implement a stack of simple integers.
25  *
26  * %OPT%
27  * This is currently based on IntVector, which permits fast acess but pays a
28  * heavy recopying penalty if/when its size is increased. If we expect deep
29  * stacks, we should consider a version based on ChunkedIntVector.
30  * @xsl.usage internal
31  */

32 public class IntStack extends IntVector
33 {
34
35   /**
36    * Default constructor. Note that the default
37    * block size is very small, for small lists.
38    */

39   public IntStack()
40   {
41     super();
42   }
43
44   /**
45    * Construct a IntVector, using the given block size.
46    *
47    * @param blocksize Size of block to allocate
48    */

49   public IntStack(int blocksize)
50   {
51     super(blocksize);
52   }
53   
54   /**
55    * Copy constructor for IntStack
56    *
57    * @param v IntStack to copy
58    */

59   public IntStack (IntStack v)
60   {
61     super(v);
62   }
63
64   /**
65    * Pushes an item onto the top of this stack.
66    *
67    * @param i the int to be pushed onto this stack.
68    * @return the <code>item</code> argument.
69    */

70   public int push(int i)
71   {
72
73     if ((m_firstFree + 1) >= m_mapSize)
74     {
75       m_mapSize += m_blocksize;
76
77       int newMap[] = new int[m_mapSize];
78
79       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
80
81       m_map = newMap;
82     }
83
84     m_map[m_firstFree] = i;
85
86     m_firstFree++;
87
88     return i;
89   }
90
91   /**
92    * Removes the object at the top of this stack and returns that
93    * object as the value of this function.
94    *
95    * @return The object at the top of this stack.
96    */

97   public final int pop()
98   {
99     return m_map[--m_firstFree];
100   }
101
102   /**
103    * Quickly pops a number of items from the stack.
104    */

105
106   public final void quickPop(int n)
107   {
108     m_firstFree -= n;
109   }
110
111   /**
112    * Looks at the object at the top of this stack without removing it
113    * from the stack.
114    *
115    * @return the object at the top of this stack.
116    * @throws EmptyStackException if this stack is empty.
117    */

118   public final int peek()
119   {
120     try {
121       return m_map[m_firstFree - 1];
122     }
123     catch (ArrayIndexOutOfBoundsException JavaDoc e)
124     {
125       throw new EmptyStackException JavaDoc();
126     }
127   }
128
129   /**
130    * Looks at the object at the position the stack counting down n items.
131    *
132    * @param n The number of items down, indexed from zero.
133    * @return the object at n items down.
134    * @throws EmptyStackException if this stack is empty.
135    */

136   public int peek(int n)
137   {
138     try {
139       return m_map[m_firstFree-(1+n)];
140     }
141     catch (ArrayIndexOutOfBoundsException JavaDoc e)
142     {
143       throw new EmptyStackException JavaDoc();
144     }
145   }
146
147   /**
148    * Sets an object at a the top of the statck
149    *
150    *
151    * @param val object to set at the top
152    * @throws EmptyStackException if this stack is empty.
153    */

154   public void setTop(int val)
155   {
156     try {
157       m_map[m_firstFree - 1] = val;
158     }
159     catch (ArrayIndexOutOfBoundsException JavaDoc e)
160     {
161       throw new EmptyStackException JavaDoc();
162     }
163   }
164
165   /**
166    * Tests if this stack is empty.
167    *
168    * @return <code>true</code> if this stack is empty;
169    * <code>false</code> otherwise.
170    * @since JDK1.0
171    */

172   public boolean empty()
173   {
174     return m_firstFree == 0;
175   }
176
177   /**
178    * Returns where an object is on this stack.
179    *
180    * @param o the desired object.
181    * @return the distance from the top of the stack where the object is]
182    * located; the return value <code>-1</code> indicates that the
183    * object is not on the stack.
184    * @since JDK1.0
185    */

186   public int search(int o)
187   {
188
189     int i = lastIndexOf(o);
190
191     if (i >= 0)
192     {
193       return size() - i;
194     }
195
196     return -1;
197   }
198   
199   /**
200    * Returns clone of current IntStack
201    *
202    * @return clone of current IntStack
203    */

204   public Object JavaDoc clone()
205     throws CloneNotSupportedException JavaDoc
206   {
207     return (IntStack) super.clone();
208   }
209 }
210
Popular Tags