1 28 29 package org.jibx.binding.util; 30 31 import java.lang.reflect.Array ; 32 33 44 45 public class IntStack 46 { 47 48 public static final int DEFAULT_SIZE = 8; 49 50 51 protected int m_countLimit; 52 53 54 protected int m_countPresent; 55 56 57 protected int m_maximumGrowth; 58 59 60 protected int[] m_baseArray; 61 62 69 70 public IntStack(int size, int growth) { 71 m_countLimit = size; 72 m_maximumGrowth = growth; 73 m_baseArray = new int[size]; 74 } 75 76 82 83 public IntStack(int size) { 84 this(size, Integer.MAX_VALUE); 85 } 86 87 90 91 public IntStack() { 92 this(DEFAULT_SIZE); 93 } 94 95 100 101 public IntStack(IntStack base) { 102 this(base.m_countLimit, base.m_maximumGrowth); 103 System.arraycopy(base.m_baseArray, 0, m_baseArray, 0, 104 base.m_countPresent); 105 m_countPresent = base.m_countPresent; 106 } 107 108 113 114 public IntStack(int[] ints) { 115 this(ints.length); 116 System.arraycopy(ints, 0, m_baseArray, 0, ints.length); 117 m_countPresent = ints.length; 118 } 119 120 128 129 private void resizeCopy(Object base, Object grown) { 130 System.arraycopy(base, 0, grown, 0, Array.getLength(base)); 131 } 132 133 144 145 private void growArray(int required) { 146 int size = Math.max(required, 147 m_countLimit + Math.min(m_countLimit, m_maximumGrowth)); 148 int[] grown = new int[size]; 149 resizeCopy(m_baseArray, grown); 150 m_countLimit = size; 151 m_baseArray = grown; 152 } 153 154 160 161 public final void ensureCapacity(int min) { 162 if (min > m_countLimit) { 163 growArray(min); 164 } 165 } 166 167 172 173 public void push(int value) { 174 int index = getAddIndex(); 175 m_baseArray[index] = value; 176 } 177 178 184 185 public int pop() { 186 if (m_countPresent > 0) { 187 return m_baseArray[--m_countPresent]; 188 } else { 189 throw new ArrayIndexOutOfBoundsException 190 ("Attempt to pop empty stack"); 191 } 192 } 193 194 204 205 public int pop(int count) { 206 if (count <= 0) { 207 throw new IllegalArgumentException ("Count must be greater than 0"); 208 } else if (m_countPresent >= count) { 209 m_countPresent -= count; 210 return m_baseArray[m_countPresent]; 211 } else { 212 throw new ArrayIndexOutOfBoundsException 213 ("Attempt to pop past end of stack"); 214 } 215 } 216 217 226 227 public int peek(int depth) { 228 if (m_countPresent > depth) { 229 return m_baseArray[m_countPresent - depth - 1]; 230 } else { 231 throw new ArrayIndexOutOfBoundsException 232 ("Attempt to peek past end of stack"); 233 } 234 } 235 236 243 244 public int peek() { 245 return peek(0); 246 } 247 248 256 257 public int[] toArray() { 258 int[] copy = new int[m_countPresent]; 259 System.arraycopy(m_baseArray, 0, copy, 0, m_countPresent); 260 return copy; 261 } 262 263 268 269 public Object clone() { 270 return new IntStack(this); 271 } 272 273 281 282 private int getAddIndex() { 283 int index = m_countPresent++; 284 if (m_countPresent > m_countLimit) { 285 growArray(m_countPresent); 286 } 287 return index; 288 } 289 290 295 public int size() { 296 return m_countPresent; 297 } 298 299 304 305 public boolean isEmpty() { 306 return m_countPresent == 0; 307 } 308 309 312 313 public void clear() { 314 m_countPresent = 0; 315 } 316 } 317 | Popular Tags |