| 27 | import java.util.Arrays; |
| 28 | |
| 29 | public class IntStack implements Mutable { |
| 30 | private static final int DEFAULT_INITIAL_CAPACITY = 16; |
| 31 | private static final int MIN_INITIAL_CAPACITY = 8; |
| 32 | private static final int NO_ENTRY_VALUE = -1; |
| 33 | private int bottom; |
| 34 | private int[] elements; |
| 35 | private int head; |
| 36 | private int mask; |
| 37 | private int tail; |
| 38 | |
| 39 | public IntStack() { |
| 40 | this(DEFAULT_INITIAL_CAPACITY); |
| 41 | } |
| 42 | |
| 43 | public IntStack(int initialCapacity) { |
| 44 | allocateElements(initialCapacity); |
| 45 | } |
| 46 | |
| 47 | public int bottom() { |
| 48 | return bottom; |
| 49 | } |
| 50 | |
| 51 | public void clear() { |
| 52 | if (head != tail) { |
| 53 | head = tail = 0; |
| 54 | Arrays.fill(elements, NO_ENTRY_VALUE); |
| 55 | bottom = 0; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public boolean notEmpty() { |
| 60 | return size() > 0; |
| 61 | } |
| 62 | |
| 63 | public int peek() { |
| 64 | return peek(0); |
| 65 | } |
| 66 | |
| 67 | public int peek(int n) { |
| 68 | return n < size() ? elements[(head + n) & mask] : NO_ENTRY_VALUE; |
| 69 | } |
| 70 | |
| 71 | public int pollLast() { |
| 72 | if (bottom != 0) { |
| 73 | throw new IllegalStateException("pollLast() called while bottom != 0"); |
| 74 | } |
| 75 | final int[] elems = elements; |
| 76 | int newTail = tail; |
| 77 | if (head != newTail && --newTail < 0) { |
| 78 | newTail = mask; |
| 79 | } |
| 80 | final int elem = elems[newTail]; |
| 81 | tail = newTail; |
| 82 | elems[newTail] = NO_ENTRY_VALUE; |
| 83 | return elem; |
| 84 | } |
| 85 | |
| 86 | public int pop() { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…