本文整理汇总了PHP中SplStack::pop方法的典型用法代码示例。如果您正苦于以下问题:PHP SplStack::pop方法的具体用法?PHP SplStack::pop怎么用?PHP SplStack::pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplStack
的用法示例。
在下文中一共展示了SplStack::pop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: translate
/**
* Translate array sequence of tokens from infix to
* Reverse Polish notation (RPN) which representing mathematical expression.
*
* @param array $tokens Collection of Token intances
* @return array Collection of Token intances
* @throws InvalidArgumentException
*/
public function translate(array $tokens)
{
$this->operatorStack = new SplStack();
$this->outputQueue = new SplQueue();
for ($i = 0; $i < count($tokens); $i++) {
$token = $tokens[$i];
switch ($token->getType()) {
case Token::T_OPERAND:
$this->outputQueue->enqueue($token);
break;
case Token::T_OPERATOR:
case Token::T_FUNCTION:
$o1 = $token;
$isUnary = $this->isPreviousTokenOperator($tokens, $i) || $this->isPreviousTokenLeftParenthesis($tokens, $i) || $this->hasPreviousToken($i) === false;
if ($isUnary) {
if ($o1->getValue() === '-' || $o1->getValue() === '+') {
$o1 = new Operator($o1->getValue() . 'u', 3, Operator::O_NONE_ASSOCIATIVE);
} else {
if (!$o1 instanceof FunctionToken) {
throw new \InvalidArgumentException("Syntax error: operator '" . $o1->getValue() . "' cannot be used as a unary operator or with an operator as an operand.");
}
}
} else {
while ($this->hasOperatorInStack() && ($o2 = $this->operatorStack->top()) && $o1->hasLowerPriority($o2)) {
$this->outputQueue->enqueue($this->operatorStack->pop());
}
}
$this->operatorStack->push($o1);
break;
case Token::T_LEFT_BRACKET:
$this->operatorStack->push($token);
break;
case Token::T_RIGHT_BRACKET:
if ($this->isPreviousTokenOperator($tokens, $i)) {
throw new \InvalidArgumentException('Syntax error: an operator cannot be followed by a right parenthesis.');
} elseif ($this->isPreviousTokenLeftParenthesis($tokens, $i)) {
throw new \InvalidArgumentException('Syntax error: empty parenthesis.');
}
while (!$this->operatorStack->isEmpty() && Token::T_LEFT_BRACKET != $this->operatorStack->top()->getType()) {
$this->outputQueue->enqueue($this->operatorStack->pop());
}
if ($this->operatorStack->isEmpty()) {
throw new \InvalidArgumentException('Syntax error: mismatched parentheses.');
}
$this->operatorStack->pop();
break;
default:
throw new \InvalidArgumentException(sprintf('Invalid token detected: %s.', $token));
break;
}
}
while ($this->hasOperatorInStack()) {
$this->outputQueue->enqueue($this->operatorStack->pop());
}
if (!$this->operatorStack->isEmpty()) {
throw new InvalidArgumentException('Syntax error: mismatched parentheses or misplaced number.');
}
return iterator_to_array($this->outputQueue);
}
示例2: evaluateRPN
/**
* Evaluate array sequence of tokens in Reverse Polish notation (RPN)
* representing mathematical expression.
*
* @param array $expressionTokens
* @return float
* @throws \InvalidArgumentException
*/
private function evaluateRPN(array $expressionTokens)
{
$stack = new \SplStack();
foreach ($expressionTokens as $token) {
$tokenValue = $token->getValue();
if (is_numeric($tokenValue)) {
$stack->push((double) $tokenValue);
continue;
}
switch ($tokenValue) {
case '+':
$stack->push($stack->pop() + $stack->pop());
break;
case '-':
$n = $stack->pop();
$stack->push($stack->pop() - $n);
break;
case '*':
$stack->push($stack->pop() * $stack->pop());
break;
case '/':
$n = $stack->pop();
$stack->push($stack->pop() / $n);
break;
case '%':
$n = $stack->pop();
$stack->push($stack->pop() % $n);
break;
default:
throw new \InvalidArgumentException(sprintf('Invalid operator detected: %s', $tokenValue));
break;
}
}
return $stack->top();
}
示例3: enable
/**
* That nested case will be corrected and fully supported
* {% softdeleteable %}
* <span>
* {% softdeleteable 'Acme\Bundle\CoreBundle\Entity\Foo' %}
* {% softdeleteable %}
* {% softdeleteable 'Acme\Bundle\CoreBundle\Entity\Bar' %}
* {{ object.owner.fullName }}
* {% endsoftdeleteable %}
* {% endsoftdeleteable %}
* {% endsoftdeleteable %}
* </span>
* {% endsoftdeleteable %}
* @param null $class
* @return bool
*/
public function enable($class = null)
{
if (!$this->entityManager->getFilters()->isEnabled('softdeleteable')) {
if ($class !== null) {
// Nested tags case.
// {% softdeleteable 'FQCN' %} inside {% softdeleteable %}
// So, just pop classes stack
$this->classStack->pop();
} else {
// When we enable globally we need to restore disabled entities
$this->entityManager->getFilters()->enable('softdeleteable');
// Populate from stack of disabled entities
/** @var SoftDeleteableFilter $filter */
$filter = $this->entityManager->getFilters()->getFilter('softdeleteable');
foreach ($this->classStack as $class) {
$filter->disableForEntity($class);
}
}
} else {
if ($class !== null) {
/** @var SoftDeleteableFilter $filter */
$filter = $this->entityManager->getFilters()->getFilter('softdeleteable');
$filter->enableForEntity($class);
$this->classStack->pop();
}
}
}
示例4: calculate
public function calculate(array $variables)
{
$stack = new \SplStack();
foreach ($this->reversePolandNotation as $token) {
if ($token->getExpressionType() == ExpressionElementInterface::CONSTANT) {
/** @var $token Constant */
$stack->push($token->getValue());
}
if ($token->getExpressionType() == ExpressionElementInterface::VARIABLE) {
/** @var $token Variable*/
$variableName = $token->getValue();
if (isset($variables[$variableName])) {
$stack->push($variables[$variableName]);
} else {
throw new ExpressionParserException("Undefined variable: " . $variableName);
}
}
if ($token->getExpressionType() == ExpressionElementInterface::OPERATOR) {
/** @var $token OperatorInterface */
$arg1 = $stack->pop();
$arg2 = $stack->pop();
$stack->push($token->calculate($arg1, $arg2));
}
}
return $stack->top();
}
示例5: leaveNode
/**
* {@inheritdoc}
*/
public function leaveNode(Node $node)
{
if ($node instanceof Node\FunctionLike) {
$this->loopStacks->pop();
} elseif ($this->isTargetLoopNode($node)) {
$this->getCurrentLoopStack()->pop();
}
}
示例6: stopVisiting
public function stopVisiting($object)
{
$this->visitingSet->detach($object);
$poppedObject = $this->visitingStack->pop();
if ($object !== $poppedObject) {
throw new RuntimeException('Context visitingStack not working well');
}
}
示例7: endGroup
public function endGroup()
{
if ($this->predicateStack->count() <= 1) {
throw new \BadMethodCallException('Invalid operation: Not in a condition group.');
}
$this->predicateStack->pop();
return $this;
}
示例8: postorder
function postorder($tree)
{
$lst = array();
if (!$tree) {
return $lst;
}
$stack = new SplStack();
$stack->push($tree);
$stack->rewind();
$prev = null;
while ($stack->valid()) {
$curr = $stack->current();
// go down the tree.
//check if current node is leaf, if so, process it and pop stack,
//otherwise, keep going down
if (!$prev || @$prev->left->key == @$curr->key || @$prev->right->key == @$curr->key) {
//prev == null is the situation for the root node
if ($curr->left) {
$stack->push($curr->left);
$stack->rewind();
} else {
if ($curr->right) {
$stack->push($curr->right);
$stack->rewind();
} else {
$stack->pop();
$stack->rewind();
$lst[] = $curr->key;
}
}
//go up the tree from left node
//need to check if there is a right child
//if yes, push it to stack
//otherwise, process parent and pop stack
} else {
if (@$curr->left->key == @$prev->key) {
if (@$curr->right->key) {
$stack->push($curr->right);
$stack->rewind();
} else {
$stack->pop();
$stack->rewind();
$lst[] = $curr->key;
}
} else {
if (@$curr->right->key == @$prev->key) {
$stack->pop();
$stack->rewind();
$lst[] = $curr->key;
}
}
}
$prev = $curr;
}
return $lst;
}
示例9: stop
/**
* stop a profiling session
*
* profiling sessions are saved in a stack (LIFO) so this will pop the newest session
* off the top and stop it
*
* @return array the information about the profiling session
*/
public function stop()
{
// canary...
if ($this->stack_started->isEmpty()) {
return array();
}
//if
$profile_map = $this->stack_started->pop();
$profile_map['stop'] = microtime(true);
$profile_map['time'] = $this->getTime($profile_map['start'], $profile_map['stop']);
$profile_map['summary'] = sprintf('%s = %s ms', $profile_map['title'], $profile_map['time']);
if ($this->stack_started->isEmpty()) {
// the stack is empty, so this was a top level call, so it should be merged into an already
// existing key with the same name (because we've maybe stopped children), or a new key if no
// children were ever started and stopped.
if (isset($this->stopped_map[$profile_map['title']])) {
$this->stopped_map[$profile_map['title']] = array_merge($this->stopped_map[$profile_map['title']], $profile_map);
} else {
$this->stopped_map[$profile_map['title']] = $profile_map;
}
//if/else
// we add to total here because otherwise it will get counted twice
$this->total += $profile_map['time'];
} else {
// this is a child, so if there were 3 nested calls: Foo -> Bar -> che, and we
// are stopping che, we should build a map that has a Foo key, with a Bar child, and
// that Bar child will have a Che child, that's where this profile map will go
// go through and build a path, by the time this is done, we'll know where $profile_map goes
$current_map =& $this->stopped_map;
// stack iteration is fixes as LIFO now, so we need to go through it backward
$stack_key = count($this->stack_started) - 1;
while (isset($this->stack_started[$stack_key])) {
$map = $this->stack_started[$stack_key--];
if (!isset($current_map[$map['title']])) {
$current_map[$map['title']] = array('time' => 0, 'children' => array());
} else {
if (!isset($current_map[$map['title']]['children'])) {
$current_map[$map['title']]['children'] = array();
}
//if
}
//if/else
$current_map =& $current_map[$map['title']]['children'];
}
//while
if (isset($current_map[$profile_map['title']])) {
$current_map[$profile_map['title']] = array_merge($current_map[$profile_map['title']], $profile_map);
} else {
$current_map[$profile_map['title']] = $profile_map;
}
//if/else
}
//if/else
return $profile_map;
}
示例10: clearCachesOfRegisteredPageIds
/**
* Walks through the pageIdStack, collects all pageIds
* as array and passes them on to clearPageCache.
*
* @return void
*/
public function clearCachesOfRegisteredPageIds()
{
if (!$this->pageIdStack->isEmpty()) {
$pageIds = array();
while (!$this->pageIdStack->isEmpty()) {
$pageIds[] = (int) $this->pageIdStack->pop();
}
$pageIds = array_values(array_unique($pageIds));
$this->clearPageCache($pageIds);
}
}
示例11: _checkLevel
/**
* @param \PHP\Manipulator\Token $token
*/
protected function _checkLevel(Token $token)
{
if ($this->isOpeningCurlyBrace($token)) {
$this->_level++;
$this->_maxLevel = max(array($this->_level, $this->_maxLevel));
}
if ($this->isClosingCurlyBrace($token)) {
$this->_level--;
if (!$this->_classStack->isEmpty() && $this->_level === $this->_classStack[count($this->_classStack) - 1]) {
$this->_classStack->pop();
}
}
}
示例12: build
/**
* Converts the current expression into a single matcher, applying
* coordination operators to operands according to their binding rules
*
* @throws \RuntimeException
* @return \Hamcrest_Matcher
*/
public function build()
{
// Apply Shunting Yard algorithm to convert the infix expression
// into Reverse Polish Notation. Since we have a very limited
// set of operators and binding rules, the implementation becomes
// really simple
$ops = new \SplStack();
$rpn = array();
foreach ($this->parts as $token) {
if ($token instanceof Operator) {
while (!$ops->isEmpty() && $token->compare($ops->top()) <= 0) {
$rpn[] = $ops->pop();
}
$ops->push($token);
} else {
$rpn[] = $token;
}
}
// Append the remaining operators
while (!$ops->isEmpty()) {
$rpn[] = $ops->pop();
}
// Walk the RPN expression to create AnyOf and AllOf matchers
$stack = new \splStack();
foreach ($rpn as $token) {
if ($token instanceof Operator) {
// Our operators always need two operands
if ($stack->count() < 2) {
throw new \RuntimeException('Unable to build a valid expression. Not enough operands available.');
}
$operands = array($stack->pop(), $stack->pop());
// Check what kind of matcher we need to create
if ($token->getKeyword() === 'OR') {
$matcher = new \Hamcrest_Core_AnyOf($operands);
} else {
// AND, BUT
$matcher = new \Hamcrest_Core_AllOf($operands);
}
$stack->push($matcher);
} else {
$stack[] = $token;
}
}
if ($stack->count() !== 1) {
throw new \RuntimeException('Unable to build a valid expression. The RPN stack should have just one item.');
}
return $stack->pop();
}
示例13: _applyModifiers
/**
* Apply modifiers to the modifiers stack
*
* @param string $modifiers Modifiers to apply
*
* @return void
*/
protected function _applyModifiers($modifiers)
{
$currentModifiers = $this->_modifiersStack->top();
//If $modifiers is an empty string just take current modifiers and
//add them to the modifiers stack
//Othwerwise
if ($modifiers !== "") {
//Explode modifiers with the dash, the first group of modifiers
//represents the modifiers to add, every following group represents
//the modifiers to subtract
$groups = explode("-", $modifiers);
foreach ($groups as $k => $group) {
if (!$group) {
continue;
}
$len = strlen($group);
for ($i = 0; $i < $len; $i++) {
$contains = strpos($currentModifiers, $group[$i]) !== false;
if (!$k && !$contains) {
$currentModifiers .= $group[$i];
} elseif ($k && $contains) {
$currentModifiers = str_replace($group[$i], "", $currentModifiers);
}
}
}
//Remove the last entry in modifiers stack
$this->_modifiersStack->pop();
}
//Add calculated modifiers to the top of the modifiers stack
$this->_modifiersStack->push($currentModifiers);
}
示例14: stackedCoroutine
/**
* Resolves yield calls tree
* and gives a return value if outcome of yield is CoroutineReturnValue instance
*
* @param \Generator $coroutine nested coroutine tree
* @return \Generator
*/
private static function stackedCoroutine(\Generator $coroutine)
{
$stack = new \SplStack();
while (true) {
$value = $coroutine->current();
// nested generator/coroutine
if ($value instanceof \Generator) {
$stack->push($coroutine);
$coroutine = $value;
continue;
}
// coroutine end or value is a value object instance
if (!$coroutine->valid() || $value instanceof CoroutineReturnValue) {
// if till this point, there are no coroutines in a stack thatn stop here
if ($stack->isEmpty()) {
return;
}
$coroutine = $stack->pop();
$value = $value instanceof CoroutineReturnValue ? $value->getValue() : null;
$coroutine->send($value);
continue;
}
$coroutine->send((yield $coroutine->key() => $value));
}
}
示例15: group
public function group()
{
$args = func_get_args();
$argc = count($args);
$uri = '';
$closure;
$options = [];
if ($argc >= 3) {
$uri = array_shift($args);
$closure = array_pop($args);
$options = array_shift($args);
} elseif ($argc == 2) {
$uri = array_shift($args);
$closure = array_pop($args);
$options = [];
} elseif ($argc == 1) {
$uri = '';
$closure = array_pop($args);
$options = [];
} elseif ($argc == 0) {
throw new ClearException("Calling Whitout Argument", 1);
}
$this->groups->push(['uri' => $uri, 'options' => $options]);
if (is_callable($closure)) {
call_user_func($closure);
}
$this->groups->pop();
return $this;
}