Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function getLastEntry() : EntryInterface
 {
     if (0 === $this->stack->count()) {
         throw new \RuntimeException('Trace empty');
     }
     return $this->stack->top();
 }
 public function endGroup()
 {
     if ($this->predicateStack->count() <= 1) {
         throw new \BadMethodCallException('Invalid operation: Not in a condition group.');
     }
     $this->predicateStack->pop();
     return $this;
 }
Exemplo n.º 3
0
 /**
  * A hook to the $system special variable. Returns the
  * compiled PHP code for the call.
  *
  * @internal
  * @param array $namespace The namespace to parse
  * @return string
  */
 public function processSystemVar($opt)
 {
     if ($this->_stack->count() == 0) {
         throw new Opt_SysVariableInvalidUse_Exception('$' . implode('.', $opt), 'components');
     }
     return $this->_stack->top() . '->get(\'' . $opt[2] . '\')';
 }
Exemplo n.º 4
0
 /**
  * Returns the form stack head element. If the stack is empty,
  * the method returns NULL.
  *
  * @internal
  * @static
  * @return Opf_Form
  */
 public static function topOfStack()
 {
     if (self::$_stack === null) {
         self::$_stack = new SplStack();
     }
     if (self::$_stack->count() == 0) {
         return null;
     }
     return self::$_stack->top();
 }
Exemplo n.º 5
0
 function infix_to_rpn($tokens)
 {
     $out_q = new SplQueue();
     $stack = new SplStack();
     $index = 0;
     while (count($tokens) > $index) {
         $t = $tokens[$index];
         switch ($t) {
             case !in_array($t, self::operators_dictionary):
                 $out_q->enqueue($t);
                 break;
             case $t == "not":
             case $t == "and":
             case $t == "or":
                 $stack->push($t);
                 break;
             case $t == "(":
                 $stack->push($t);
                 break;
             case $t == ")":
                 while ($stack->top() != "(") {
                     $out_q->enqueue($stack->pop());
                 }
                 $stack->pop();
                 if ($stack->count() > 0 && $stack->top() == "not") {
                     $out_q->enqueue($stack->pop());
                 }
                 break;
             default:
                 break;
         }
         ++$index;
     }
     while ($stack->count() > 0) {
         $out_q->enqueue($stack->pop());
     }
     $reversed_q = array();
     foreach ($out_q as $value) {
         $reversed_q[] = $value;
     }
     return array_reverse($reversed_q);
 }
Exemplo n.º 6
0
 /**
  * Removes the specified section from the stack. The name
  * is provided to check, if the order of the closing is
  * valid.
  *
  * @static
  * @internal
  * @param String $name The section name.
  */
 private static function _removeSection($name)
 {
     if (self::$_stack->count() == 0) {
         throw new Opt_ObjectNotExists_Exception('section', $name);
     }
     $name2 = self::$_stack->pop();
     if ($name != $name2) {
         throw new Opl_Debug_Generic_Exception('OPT: Invalid section name thrown from the stack. Expected: ' . $name . '; Actual: ' . $name2);
     }
     unset(self::$_sections[$name]);
 }
Exemplo n.º 7
0
 private function solve($formula)
 {
     $findSubFormula = false;
     $stringStart = 0;
     $stringLength = 0;
     $stack = new \SplStack();
     $subFormulas = [];
     for ($i = 0; $i < strlen($formula); $i++) {
         $char = $formula[$i];
         if ($this->isBracketOpen($char)) {
             if ($findSubFormula == false && $stack->count() == 0) {
                 $stringStart = $i;
             }
             $stack->push(1);
             $findSubFormula = true;
         }
         if ($findSubFormula) {
             $stringLength++;
         }
         if ($this->isBracketClose($char)) {
             $stack->pop();
             if ($stack->count() === 0 && $findSubFormula) {
                 $subFormulas[substr($formula, $stringStart, $stringLength)] = substr($formula, $stringStart, $stringLength);
                 $findSubFormula = false;
                 $stringLength = 0;
             }
         }
     }
     if (count($subFormulas) > 0) {
         foreach ($subFormulas as &$subFormula) {
             $temp = trim(substr($subFormula, 1, strlen($subFormula) - 2));
             $subFormula = $this->solve($temp);
         }
         $formula = str_replace(array_keys($subFormulas), array_values($subFormulas), $formula);
     }
     $elems = new \SplDoublyLinkedList();
     array_map(function ($item) use($elems) {
         if ($item != ' ') {
             $elems->push($item);
         }
     }, explode(' ', $formula));
     while ($elems->count() > 1) {
         $maxPriority = 0;
         $index = 0;
         foreach ($elems as $i => $el) {
             if (isset(static::$symbols[$el]) && static::$symbols[$el] > $maxPriority) {
                 $maxPriority = static::$symbols[$el];
                 $index = $i;
             }
         }
         $this->process($index, $elems);
     }
     return $elems->pop();
 }
Exemplo n.º 8
0
 /**
  * Returns the array of logs.
  *
  * @return array
  */
 public function info()
 {
     if ($this->working) {
         throw new LogicException("It is not possible get info on a working Pipeline.");
     }
     $info = ['context' => $this->context];
     while ($this->DTOs->count()) {
         $info[] = $this->DTOs->pop()->toArray($this);
     }
     return $info;
 }
Exemplo n.º 9
0
 /**
  * Close current block.
  *
  * @param  string|null $which
  * @return string
  */
 public function close($which = null)
 {
     if (!$this->buffers->count()) {
         throw new LogicException('It is not possible to close a never opened block.');
     }
     list($buffer, $name) = $this->buffers->pop();
     if (!is_null($which) && $which !== $name) {
         throw new InvalidArgumentException("Please close blocks in order: you need to close other block(s) before \"{$which}\".");
     }
     $output = $buffer instanceof Block ? $buffer->close() : '';
     return $output;
 }
Exemplo n.º 10
0
 /**
  * Bubbles the stack.
  *
  * Whenever another level in the render array has been rendered, the stack
  * must be bubbled, to merge its rendering metadata with that of the parent
  * element.
  */
 protected function bubbleStack()
 {
     // If there's only one frame on the stack, then this is the root call, and
     // we can't bubble up further. Reset the stack for the next root call.
     if (static::$stack->count() === 1) {
         $this->resetStack();
         return;
     }
     // Merge the current and the parent stack frame.
     $current = static::$stack->pop();
     $parent = static::$stack->pop();
     static::$stack->push($current->merge($parent));
 }
 /**
  * @param \SplStack $metadataStack
  * @return null|string
  */
 protected function getStackAsString(\SplStack $metadataStack)
 {
     if ($metadataStack->count() > 1) {
         $propertyNames = [];
         foreach ($metadataStack as $metadata) {
             if ($metadata instanceof PropertyMetadata) {
                 $propertyNames[] = $metadata->name;
             }
         }
         $propertyNames = array_reverse($propertyNames);
         return implode('.', $propertyNames);
     }
     return null;
 }
Exemplo n.º 12
0
function isPalindrome($word)
{
    $word = strtolower(str_replace(" ", "", $word));
    $stack = new SplStack();
    $cnt = strlen($word);
    for ($i = 0; $i < $cnt; ++$i) {
        $stack->push($word[$i]);
    }
    $rword = "";
    while ($stack->count() > 0) {
        $rword .= $stack->pop();
    }
    return $word == $rword;
}
Exemplo n.º 13
0
 public function endVisiting($data, Type $type, Context $context)
 {
     $nodes = $this->currentNodes ?: [];
     $this->currentNodes = $this->nodeStack->pop();
     if ($this->nodeStack->count() === 0 && $this->document->documentElement === null) {
         $rootNode = $this->document->createElement('result');
         $this->document->appendChild($rootNode);
         $this->currentNodes = $rootNode;
     }
     if (!is_array($nodes) && !$nodes instanceof \DOMNodeList) {
         $this->currentNodes->appendChild($nodes);
         return;
     }
     foreach ($nodes as $node) {
         $this->currentNodes->appendChild($node);
     }
 }
Exemplo n.º 14
0
 /**
  * Render a common_report_Report object into an HTML string output.
  * 
  * @param common_report_Report $report A report to be rendered
  * @return string The HTML rendering.
  */
 public static function render(common_report_Report $report)
 {
     $stack = new SplStack();
     $renderingStack = new SplStack();
     $traversed = array();
     $stack->push($report);
     $nesting = 0;
     while ($stack->count() > 0) {
         $current = $stack->pop();
         if (in_array($current, $traversed, true) === false && $current->hasChildren() === true) {
             $nesting++;
             // -- Hierarchical report, 1st pass (descending).
             // Repush report for a 2ndpass.
             $stack->push($current);
             // Tag as already traversed.
             $traversed[] = $current;
             // Push the children for a 1st pass.
             foreach ($current as $child) {
                 $stack->push($child);
             }
         } else {
             if (in_array($current, $traversed, true) === true && $current->hasChildren() === true) {
                 $nesting--;
                 // -- Hierachical report, 2nd pass (ascending).
                 // Get the nested renderings of the current report.
                 $children = array();
                 foreach ($current as $child) {
                     $children[] = $renderingStack->pop();
                 }
                 $renderingStack->push(self::renderReport($current, $children, $nesting));
             } else {
                 // -- Leaf report, 1st & single pass.
                 $renderingStack->push(self::renderReport($current, array(), $nesting, true));
             }
         }
     }
     return $renderingStack->pop();
 }
Exemplo n.º 15
0
 /**
  * @return int
  */
 public function countParentIds()
 {
     return $this->traversalStack->count();
 }
Exemplo n.º 16
0
$stack = new SplStack();
if (isset($list[0])) {
}
$stack->push($list[0]->depth);
foreach ($list as $k => $item) {
    if ($item->depth > $stack->top()) {
        $stack->push($item->depth);
        echo "<ol>\n";
    }
    while (!$stack->isEmpty() && $stack->top() > $item->depth) {
        $stack->pop();
        echo "</ol></li>\n";
    }
    echo "<li class=\"sortableListsOpen\" id=\"row-" . $item->id . "\"><div>{$item->id}." . $item->name . "</div>\n";
}
while ($stack->count() > 1) {
    $stack->pop();
    echo "</ol>\n</li>\n";
}
?>
        </ul>
    </div>
</div>
<?php 
$root_id = (int) $root->id;
$ajax_url = Url::to(['move']);
$asset_root = Yii::getAlias('@web');
$js = <<<JS
var options ={
    currElClass:'currEl',
\tplaceholderClass:'placeholder',
Exemplo n.º 17
0
 private function parseEquation()
 {
     $length = strlen($this->equation);
     $queue = new \SplQueue();
     $stack = new \SplStack();
     for ($i = 0; $i < $length; $i++) {
         $token = $this->getToken($this->equation, $i);
         if (is_numeric($token)) {
             $queue->enqueue($token);
         } else {
             // We will manage here operator precedence
             // Last operator in the stack
             $op2 = $stack->count() > 0 ? $stack->top() : '';
             while (in_array($op2, $this->validTokens)) {
                 if (self::$operators[$token]['assoc'] == 'left' && self::$operators[$token]['pre'] <= self::$operators[$op2]['pre']) {
                     $queue->enqueue(self::$operators[$op2]['function']);
                 } else {
                     break;
                 }
                 $stack->pop();
                 $op2 = $stack->count() > 0 ? $stack->top() : '';
             }
             $stack->push($token);
         }
     }
     // while items on the stack, push to the queue
     while ($stack->count() > 0) {
         $queue->enqueue(self::$operators[$stack->pop()]['function']);
     }
     $this->parsedEquation = $queue;
 }
Exemplo n.º 18
0
 /**
  * Calculates tokens ordered in RPN.
  *
  * @param  \SplQueue $queue
  * @return int|float Result of the calculation.
  * @throws \InvalidArgumentException
  */
 public function calculateFromRPN(\SplQueue $queue)
 {
     $stack = new \SplStack();
     while ($queue->count() > 0) {
         $currentToken = $queue->dequeue();
         if (is_numeric($currentToken)) {
             $stack->push($currentToken);
         } else {
             if (in_array($currentToken, $this->_operators)) {
                 if ($stack->count() < 2) {
                     throw new \InvalidArgumentException('Invalid expression');
                 }
                 $stack->push($this->executeOperator($currentToken, $stack->pop(), $stack->pop()));
             } else {
                 if (array_key_exists($currentToken, $this->_functions)) {
                     if ($stack->count() < $this->_functions[$currentToken]['paramsCount']) {
                         throw new \InvalidArgumentException('Invalid expression');
                     }
                     $params = [];
                     for ($i = 0; $i < $this->_functions[$currentToken]['paramsCount']; $i++) {
                         $params[] = $stack->pop();
                     }
                     $stack->push($this->executeFunction($currentToken, $params));
                 }
             }
         }
     }
     if ($stack->count() === 1) {
         return $stack->pop();
     }
     throw new \InvalidArgumentException('Invalid expression');
 }
Exemplo n.º 19
0
 /**
  * Construct all successor states to the given state.  A "successor"
  * state is any state which can be reached by a shift action.
  * @param PHP_ParserGenerator_Data
  * @param PHP_ParserGenerator_State The state from which successors are computed
  */
 private function buildshifts(PHP_ParserGenerator_State $stp)
 {
     // Avoid true recursion which breaks the parser with
     // "maximum nesting level too deep" in more complex grammars
     $stack = new SplStack();
     $action = 0;
     $argument = $stp;
     do {
         switch ($action) {
             case 0:
                 $result = $this->buildshiftsInitial($argument);
                 if (is_array($result)) {
                     $stack->push($result[0]);
                     $argument = $result[1];
                     $action = 0;
                 } else {
                     $action = $stack->count() == 0 ? 3 : 2;
                 }
                 break;
             case 1:
                 $result = $this->buildshiftsResumed($stack->pop());
                 if (is_array($result)) {
                     $stack->push($result[0]);
                     $argument = $result[1];
                     $action = 0;
                 } else {
                     $action = $stack->count() == 0 ? 3 : 2;
                 }
                 break;
             case 2:
                 $returned = $stack->pop();
                 $stack->push($this->buildshiftsFinalizer($returned));
                 $action = 1;
                 break;
         }
     } while ($action != 3);
 }
Exemplo n.º 20
0
 /**
  * Parses a group-open token
  *
  * @param  Jstewmc\Rtf\Token\Group\Open  $token  the group-open token
  * @param  SplStack                      $stack  the group stack
  * @param  Jstewmc\Rtf\Element\Group     $root   the root group (optional; if 
  *     omitted, defaults to null)
  * @return  void
  * @since  0.1.0
  */
 protected function parseGroupOpen(Token\Group\Open $token, \SplStack $stack)
 {
     $group = new Element\Group();
     // if the group is not the root
     if ($stack->count() > 0) {
         // set the parent-child and child-parent relationships
         $group->setParent($stack->top());
         $stack->top()->appendChild($group);
     }
     $stack->push($group);
     return;
 }
Exemplo n.º 21
0
function printOutput(SplStack $outputStack)
{
    while ($outputStack->count() !== 0) {
        echo $outputStack->shift();
    }
    echo PHP_EOL;
}
Exemplo n.º 22
0
 public function getDepth()
 {
     return $this->visitingStack->count();
 }
Exemplo n.º 23
0
 /**
  * Create a dependencies map for a class
  *
  * @param int $level Level for dependency
  * @param string $class Class wich tree will build
  */
 private function buildDependencyTree(int $level, string $class)
 {
     //create stack
     $stack = new \SplStack();
     while (true) {
         //initial back slash
         $class = strpos($class, '\\') !== 0 ? '\\' . $class : $class;
         //initialize array if not already initialized
         if (!isset($this->dependencyTree[$level][$class])) {
             $this->dependencyTree[$level][$class] = [];
         }
         //create reflection class
         $reflectionClass = new \ReflectionClass($class);
         //get parameter from constructor
         $param = $reflectionClass->getConstructor()->getParameters();
         //loop parameter
         foreach ($param as $key => $value) {
             //if there is parameter with callable type
             if ($value->hasType() === true && class_exists((string) $value->getType())) {
                 //if class are not already resolved
                 if (!in_array('\\' . $value->getClass()->name, $this->dependencyTree[$level][$class])) {
                     //push values in stack for simulate later recursive function
                     $stack->push([$level, $class]);
                     //store dependency
                     $this->dependencyTree[$level][$class][] = '\\' . $value->getClass()->name;
                     //update values for simulate recursive function
                     $level = $level + 1;
                     $class = $value->getClass()->name;
                     //return to main while
                     continue 2;
                 }
             }
         }
         //if stack is empty break while end exit from function
         if ($stack->count() === 0) {
             return;
         }
         //get last value pushed into stack;
         list($level, $class) = $stack->pop();
     }
 }
Exemplo n.º 24
0
 /**
  * Links the tree into a valid XML file with embedded PHP commands
  * from the tag buffers. The method is recursion-free.
  *
  * @internal
  * @param String &$output Where to store the output.
  * @param Opt_Xml_Node $node The initial node.
  */
 protected function _stage3(&$output, Opt_Xml_Node $node)
 {
     $queue = new SplQueue();
     $stack = new SplStack();
     $queue->enqueue($node);
     while (true) {
         $item = $queue->dequeue();
         if (!$item->get('hidden')) {
             // If the tag has the "commented" flag, we comment it
             // and its content.
             if ($item->get('commented')) {
                 $this->_comments++;
                 if ($this->_comments == 1) {
                     $this->_output .= '<!--';
                 }
             }
             // Now link the node.
             $item->preLink($this);
             if ($this->_newQueue !== null) {
                 // Starting next level.
                 $stack->push(array($item, $queue));
                 $queue = $this->_newQueue;
                 $this->_newQueue = null;
             } else {
                 $item->postLink($this);
                 $this->_closeComments($item);
             }
         }
         // Closing the current level.
         while ($queue->count() == 0) {
             if ($stack->count() == 0) {
                 break 2;
             }
             unset($queue);
             list($item, $queue) = $stack->pop();
             $item->postLink($this);
             $this->_closeComments($item);
         }
     }
     if ($this->_tpl->stripWhitespaces) {
         $output = rtrim($output);
     }
 }
Exemplo n.º 25
0
 private function constructAST(Queue $tokens) : AST\Node
 {
     if ($tokens->isEmpty()) {
         throw new ParseException("Unexpected end of file");
     }
     $stack = new Stack();
     while (!$tokens->isEmpty()) {
         $token = $tokens->bottom();
         if ($this->isNumber($token)) {
             $tokens->dequeue();
             $stack->push(new AST\Constant($token->getLine(), (double) $token->getContent()));
         } else {
             if ($this->isVariable($token)) {
                 $tokens->dequeue();
                 $stack->push(new AST\Variable($token->getLine(), $token->getContent()));
             } else {
                 if ($this->isOperator($token)) {
                     $tokens->dequeue();
                     $right = $stack->pop();
                     $left = $stack->pop();
                     $stack->push(new AST\Operator($left->getLine(), $token->getId(), $left, $right));
                 } else {
                     if ($this->isFunctionName($token)) {
                         $tokens->dequeue();
                         $arguments = [];
                         for ($i = 0; $i < FUNCTIONS[$token->getContent()]['arity']; $i++) {
                             if ($stack->isEmpty()) {
                                 throw new ParseException("Not enough arguments on stack for '{$token->getContent()}', some function is missing an argument");
                             }
                             $arguments[] = $stack->pop();
                         }
                         // sin(1,2) would be [2 1 sin] on the stack
                         $arguments = \array_reverse($arguments);
                         $stack->push(new AST\FunctionCall($token->getLine(), $token->getContent(), $arguments));
                     } else {
                         throw new ParseException("Unexpected token {$token->getName()} on line {$token->getLine()}");
                     }
                 }
             }
         }
     }
     if ($stack->isEmpty()) {
         throw new ParseException("??????");
     }
     if ($stack->count() !== 1) {
         throw new ParseException("Missing operator");
     }
     return $stack->pop();
 }
 public function endAllGroups()
 {
     while ($this->predicateStack->count() > 1) {
         $this->endGroup();
     }
 }
Exemplo n.º 27
0
 /**
  * {@inheritdoc}
  */
 public function count()
 {
     return $this->stack->count();
 }
<?php

$ingredientes = new SplStack();
$ingredientes->push('Peixe');
$ingredientes->push('Sal');
$ingredientes->push('Limão');
foreach ($ingredientes as $item) {
    print 'Item: ' . $item . '<br>' . PHP_EOL;
}
print PHP_EOL;
print $ingredientes->pop() . '<br>' . PHP_EOL;
print 'Count: ' . $ingredientes->count() . '<br>' . PHP_EOL;
print $ingredientes->pop() . '<br>' . PHP_EOL;
print 'Count: ' . $ingredientes->count() . '<br>' . PHP_EOL;
print $ingredientes->pop() . '<br>' . PHP_EOL;
print 'Count: ' . $ingredientes->count() . '<br>' . PHP_EOL;