Stack.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * MODIFICATIONS
  4. * - Renamed with prefix to avoid naming collisions.
  5. */
  6. /**
  7. * Basic Stack Class.
  8. *
  9. * Created for use with eqEOS. May eventually be replaced with native
  10. * PHP functions `array_pop()`, `array_push()`, and `end()`
  11. *
  12. * @author Jon Lawrence <jlawrence11@gmail.com>
  13. * @copyright Copyright �2005-2013 Jon Lawrence
  14. * @license http://opensource.org/licenses/LGPL-2.1 LGPL 2.1 License
  15. * @package Math
  16. * @subpackage EOS
  17. * @version 2.0
  18. */
  19. class NF_EOS_Stack {
  20. private $index;
  21. private $locArray;
  22. /**
  23. * Constructor
  24. *
  25. * Initializes the stack
  26. */
  27. public function __construct() {
  28. //define the private vars
  29. $this->locArray = array();
  30. $this->index = -1;
  31. }
  32. /**
  33. * Peek
  34. *
  35. * Will view the last element of the stack without removing it
  36. *
  37. * @return Mixed An element of the array or false if none exist
  38. */
  39. public function peek() {
  40. if($this->index > -1)
  41. return $this->locArray[$this->index];
  42. else
  43. return false;
  44. }
  45. /**
  46. * Poke
  47. *
  48. * Will add an element to the end of the stack
  49. *
  50. * @param Mixed $data Element to add
  51. */
  52. public function poke($data) {
  53. $this->locArray[++$this->index] = $data;
  54. }
  55. /**
  56. * Push
  57. *
  58. * Alias of {@see Stack::poke()}
  59. * Adds element to the stack
  60. *
  61. * @param Mixed $data Element to add
  62. */
  63. public function push($data) {
  64. //allias for 'poke'
  65. $this->poke($data);
  66. }
  67. /**
  68. * Pop
  69. *
  70. * Retrives an element from the end of the stack, and removes it from
  71. * the stack at the same time. If no elements, returns boolean false
  72. *
  73. * @return Mixed Element at end of stack or false if none exist
  74. */
  75. public function pop() {
  76. if($this->index > -1)
  77. {
  78. $this->index--;
  79. return $this->locArray[$this->index+1];
  80. }
  81. else
  82. return false;
  83. }
  84. /**
  85. * Clear
  86. *
  87. * Clears the stack to be reused.
  88. */
  89. public function clear() {
  90. $this->index = -1;
  91. $this->locArray = array();
  92. }
  93. /**
  94. * Get Stack
  95. *
  96. * Returns the array of stack elements, keeping all, indexed at 0
  97. *
  98. * @return Mixed Array of stack elements or false if none exist.
  99. */
  100. public function getStack() {
  101. if($this->index > -1)
  102. {
  103. return array_values($this->locArray);
  104. }
  105. else
  106. return false;
  107. }
  108. }
  109. ?>