本文整理汇总了PHP中SplStack::serialize方法的典型用法代码示例。如果您正苦于以下问题:PHP SplStack::serialize方法的具体用法?PHP SplStack::serialize怎么用?PHP SplStack::serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplStack
的用法示例。
在下文中一共展示了SplStack::serialize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getReversePolishNotation
/**
* Rearranges tokens according to RPN (Reverse Polish Notation) or
* also known as Postfix Notation.
*
* @param array $tokens
* @return \SplQueue
* @throws \InvalidArgumentException
*/
public function getReversePolishNotation(array $tokens)
{
$queue = new \SplQueue();
$stack = new \SplStack();
$tokensCount = count($tokens);
for ($i = 0; $i < $tokensCount; $i++) {
if (is_numeric($tokens[$i])) {
// (string + 0) converts to int or float
$queue->enqueue($tokens[$i] + 0);
} else {
if (array_key_exists($tokens[$i], $this->_functions)) {
$stack->push($tokens[$i]);
} else {
if ($tokens[$i] === self::FUNC_ARG_SEPARATOR) {
// checking whether stack contains left parenthesis (dirty hack)
if (substr_count($stack->serialize(), '(') === 0) {
throw new \InvalidArgumentException('Parenthesis are misplaced');
}
while ($stack->top() != '(') {
$queue->enqueue($stack->pop());
}
} else {
if (in_array($tokens[$i], $this->_operators)) {
while ($stack->count() > 0 && in_array($stack->top(), $this->_operators) && ($this->isOperatorLeftAssociative($tokens[$i]) && $this->getOperatorPrecedence($tokens[$i]) === $this->getOperatorPrecedence($stack->top()) || $this->getOperatorPrecedence($tokens[$i]) < $this->getOperatorPrecedence($stack->top()))) {
$queue->enqueue($stack->pop());
}
$stack->push($tokens[$i]);
} else {
if ($tokens[$i] === '(') {
$stack->push('(');
} else {
if ($tokens[$i] === ')') {
// checking whether stack contains left parenthesis (dirty hack)
if (substr_count($stack->serialize(), '(') === 0) {
throw new \InvalidArgumentException('Parenthesis are misplaced');
}
while ($stack->top() != '(') {
$queue->enqueue($stack->pop());
}
$stack->pop();
if ($stack->count() > 0 && array_key_exists($stack->top(), $this->_functions)) {
$queue->enqueue($stack->pop());
}
}
}
}
}
}
}
}
while ($stack->count() > 0) {
$queue->enqueue($stack->pop());
}
return $queue;
}