stack.class.php 2.1 KB

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