Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import java.util.concurrent.atomic.AtomicInteger;
  2.  
  3. /**
  4. * Class BlockStack
  5. * Implements character block stack and operations upon it.
  6. * <p>
  7. * $Revision: 1.4 $
  8. * $Last Revision Date: 2017/02/08 $
  9. *
  10. * @author Serguei A. Mokhov, mokhov@cs.concordia.ca;
  11. * Inspired by an earlier code by Prof. D. Probst
  12. */
  13. class BlockStack {
  14. /**
  15. * # of letters in the English alphabet + 2
  16. */
  17. public static final int MAX_SIZE = 28;
  18.  
  19. /**
  20. * Default stack size
  21. */
  22. public static final int DEFAULT_SIZE = 6;
  23.  
  24. /**
  25. * Current size of the stack
  26. */
  27. public int iSize = DEFAULT_SIZE;
  28.  
  29. /**
  30. * Current top of the stack
  31. */
  32. public int iTop = 3;
  33.  
  34. /**
  35. * stack[0:5] with four defined values
  36. */
  37. public char acStack[] = new char[]{'a', 'b', 'c', 'd', '$', '$'};
  38.  
  39.  
  40. private AtomicInteger accessCounter = new AtomicInteger();
  41.  
  42.  
  43. /**
  44. * Default constructor
  45. */
  46. public BlockStack() {
  47. }
  48.  
  49. /**
  50. * Supplied size
  51. */
  52. public BlockStack(final int piSize) {
  53.  
  54.  
  55. if (piSize != DEFAULT_SIZE) {
  56. this.acStack = new char[piSize];
  57.  
  58. // Fill in with letters of the alphabet and keep
  59. // 2 free blocks
  60. for (int i = 0; i < piSize - 2; i++)
  61. this.acStack[i] = (char) ('a' + i);
  62.  
  63. this.acStack[piSize - 2] = this.acStack[piSize - 1] = '$';
  64.  
  65. this.iTop = piSize - 3;
  66. this.iSize = piSize;
  67. }
  68. }
  69.  
  70. /**
  71. * Picks a value from the top without modifying the stack
  72. *
  73. * @return top element of the stack, char
  74. */
  75. public char pick() {
  76. accessCounter.incrementAndGet();
  77. return this.acStack[this.iTop];
  78. }
  79.  
  80. /**
  81. * Returns arbitrary value from the stack array
  82. *
  83. * @return the element, char
  84. */
  85. public char getAt(final int piPosition) {
  86.  
  87. accessCounter.incrementAndGet();
  88. return this.acStack[piPosition];
  89. }
  90.  
  91. /**
  92. * Standard push operation
  93. */
  94. public void push(final char pcBlock) {
  95. accessCounter.incrementAndGet();
  96. this.acStack[++this.iTop] = pcBlock;
  97. }
  98.  
  99. /**
  100. * Standard pop operation
  101. *
  102. * @return ex-top element of the stack, char
  103. */
  104. public char pop() {
  105. accessCounter.incrementAndGet();
  106. char cBlock = this.acStack[this.iTop];
  107. this.acStack[this.iTop--] = '$'; // Leave prev. value undefined
  108. return cBlock;
  109. }
  110.  
  111. public AtomicInteger getAccessCounter() {
  112. return accessCounter;
  113. }
  114. }
  115.  
  116. // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement