You are here

class ctools_math_expr_stack in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/math-expr.inc \ctools_math_expr_stack

Class implementing a simple stack structure, used by ctools_math_expr.

Hierarchy

Expanded class hierarchy of ctools_math_expr_stack

File

includes/math-expr.inc, line 818
=============================================================================.

View source
class ctools_math_expr_stack {

  /**
   * The stack.
   *
   * @var array
   */
  private $stack;

  /**
   * The stack pointer, points at the first empty space.
   *
   * @var int
   */
  private $count;

  /**
   * Ctools_math_expr_stack constructor.
   */
  public function __construct() {
    $this->stack = array();
    $this->count = 0;
  }

  /**
   * Push the value onto the stack.
   *
   * @param mixed $val
   */
  public function push($val) {
    $this->stack[$this->count] = $val;
    $this->count++;
  }

  /**
   * Remove the most recently pushed value and return it.
   *
   * @return mixed|null
   *   The most recently pushed value, or NULL if the stack was empty.
   */
  public function pop() {
    if ($this->count > 0) {
      $this->count--;
      return $this->stack[$this->count];
    }
    return NULL;
  }

  /**
   * "Peek" the stack, or Return a value from the stack without removing it.
   *
   * @param int $n
   *   Integer indicating which value to return. 1 is the topmost (i.e. the
   *   value that pop() would return), 2 indicates the next, 3 the third, etc.
   *
   * @return mixed|null
   *   A value pushed onto the stack at the nth position, or NULL if the stack
   *   was empty.
   */
  public function last($n = 1) {
    return !empty($this->stack[$this->count - $n]) ? $this->stack[$this->count - $n] : NULL;
  }

  /**
   * Return the number of items on the stack.
   *
   * @return int
   *   The number of items.
   */
  public function count() {
    return $this->count;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ctools_math_expr_stack::$count private property The stack pointer, points at the first empty space.
ctools_math_expr_stack::$stack private property The stack.
ctools_math_expr_stack::count public function Return the number of items on the stack.
ctools_math_expr_stack::last public function "Peek" the stack, or Return a value from the stack without removing it.
ctools_math_expr_stack::pop public function Remove the most recently pushed value and return it.
ctools_math_expr_stack::push public function Push the value onto the stack.
ctools_math_expr_stack::__construct public function Ctools_math_expr_stack constructor.