1 22 23 package com.sosnoski.util.stack; 24 25 36 37 public class IntStack extends StackBase 38 { 39 40 protected int[] m_baseArray; 41 42 49 50 public IntStack(int size, int growth) { 51 super(size, growth, int.class); 52 } 53 54 60 61 public IntStack(int size) { 62 super(size, int.class); 63 } 64 65 68 69 public IntStack() { 70 this(DEFAULT_SIZE); 71 } 72 73 78 79 public IntStack(IntStack base) { 80 super(base); 81 } 82 83 89 90 protected Object getArray() { 91 return m_baseArray; 92 } 93 94 100 101 protected void setArray(Object array) { 102 m_baseArray = (int[]) array; 103 } 104 105 110 111 public void push(int value) { 112 int index = getAddIndex(); 113 m_baseArray[index] = value; 114 } 115 116 122 123 public int pop() { 124 if (m_countPresent > 0) { 125 return m_baseArray[--m_countPresent]; 126 } else { 127 throw new ArrayIndexOutOfBoundsException 128 ("Attempt to pop empty stack"); 129 } 130 } 131 132 142 143 public int pop(int count) { 144 if (count <= 0) { 145 throw new IllegalArgumentException ("Count must be greater than 0"); 146 } else if (m_countPresent >= count) { 147 m_countPresent -= count; 148 return m_baseArray[m_countPresent]; 149 } else { 150 throw new ArrayIndexOutOfBoundsException 151 ("Attempt to pop past end of stack"); 152 } 153 } 154 155 164 165 public int peek(int depth) { 166 if (m_countPresent > depth) { 167 return m_baseArray[m_countPresent - depth - 1]; 168 } else { 169 throw new ArrayIndexOutOfBoundsException 170 ("Attempt to peek past end of stack"); 171 } 172 } 173 174 181 182 public int peek() { 183 return peek(0); 184 } 185 186 194 195 public int[] toArray() { 196 return (int[]) buildArray(int.class); 197 } 198 199 204 205 public Object clone() { 206 return new IntStack(this); 207 } 208 } 209 | Popular Tags |