Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * Class BlockStack
- * Implements character block stack and operations upon it.
- * <p>
- * $Revision: 1.4 $
- * $Last Revision Date: 2017/02/08 $
- *
- * @author Serguei A. Mokhov, mokhov@cs.concordia.ca;
- * Inspired by an earlier code by Prof. D. Probst
- */
- class BlockStack {
- /**
- * # of letters in the English alphabet + 2
- */
- public static final int MAX_SIZE = 28;
- /**
- * Default stack size
- */
- public static final int DEFAULT_SIZE = 6;
- /**
- * Current size of the stack
- */
- public int iSize = DEFAULT_SIZE;
- /**
- * Current top of the stack
- */
- public int iTop = 3;
- /**
- * stack[0:5] with four defined values
- */
- public char acStack[] = new char[]{'a', 'b', 'c', 'd', '$', '$'};
- private AtomicInteger accessCounter = new AtomicInteger();
- /**
- * Default constructor
- */
- public BlockStack() {
- }
- /**
- * Supplied size
- */
- public BlockStack(final int piSize) {
- if (piSize != DEFAULT_SIZE) {
- this.acStack = new char[piSize];
- // Fill in with letters of the alphabet and keep
- // 2 free blocks
- for (int i = 0; i < piSize - 2; i++)
- this.acStack[i] = (char) ('a' + i);
- this.acStack[piSize - 2] = this.acStack[piSize - 1] = '$';
- this.iTop = piSize - 3;
- this.iSize = piSize;
- }
- }
- /**
- * Picks a value from the top without modifying the stack
- *
- * @return top element of the stack, char
- */
- public char pick() {
- accessCounter.incrementAndGet();
- return this.acStack[this.iTop];
- }
- /**
- * Returns arbitrary value from the stack array
- *
- * @return the element, char
- */
- public char getAt(final int piPosition) {
- accessCounter.incrementAndGet();
- return this.acStack[piPosition];
- }
- /**
- * Standard push operation
- */
- public void push(final char pcBlock) {
- accessCounter.incrementAndGet();
- this.acStack[++this.iTop] = pcBlock;
- }
- /**
- * Standard pop operation
- *
- * @return ex-top element of the stack, char
- */
- public char pop() {
- accessCounter.incrementAndGet();
- char cBlock = this.acStack[this.iTop];
- this.acStack[this.iTop--] = '$'; // Leave prev. value undefined
- return cBlock;
- }
- public AtomicInteger getAccessCounter() {
- return accessCounter;
- }
- }
- // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement