本文整理汇总了PHP中SplStack::push方法的典型用法代码示例。如果您正苦于以下问题:PHP SplStack::push方法的具体用法?PHP SplStack::push怎么用?PHP SplStack::push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplStack
的用法示例。
在下文中一共展示了SplStack::push方法的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: pushHandler
/**
* @inheritdoc
*/
public function pushHandler(HandlerInterface ...$handlers)
{
$this->initHandlers();
foreach ($handlers as $handler) {
$this->handlers->push($handler);
}
}
示例3: 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();
}
示例4: pushProcessor
/**
* @inheritdoc
*/
public function pushProcessor(ProcessorInterface ...$processors)
{
$this->initProcessors();
foreach ($processors as $processor) {
$this->processors->push($processor);
}
}
示例5: startOrGroup
public function startOrGroup()
{
$predicate = new OrPredicate(new NullPredicate());
$this->predicateStack->top()->pushPredicate($predicate);
$this->predicateStack->push($predicate);
return $this;
}
示例6: LoadV2
/**
* 加载指定目录下的配置并生成[绝对路径=>配置]
* (备用)
*
* @param $filePath
* @param string $ext
*/
private function LoadV2($filePath, $ext = '.php')
{
$resConfig = [];
$split = '.';
if (file_exists($filePath)) {
$key = basename($filePath, $ext);
$configs = (include $filePath);
$keyStack = new \SplStack();
$keyStack->push([$key, $configs]);
$whileCount = 0;
//防止意外进入死循环,限制最多循环1024层
while (!$keyStack->isEmpty() && $whileCount < 1024) {
$whileCount++;
$pair = $keyStack->pop();
foreach ($pair[1] as $pairKey => $pairVal) {
if (is_array($pairVal)) {
$keyStack->push([$pair[0] . $split . $pairKey, $pairVal]);
} else {
$resConfig[$pair[0] . $split . $pairKey] = $pairVal;
}
}
}
}
return $resConfig;
}
示例7: parse
public function parse($data)
{
$tokens = $this->tokenizer($data);
$this->stack = new \SplStack();
$this->stack->push(0);
while (true) {
$token = $tokens[0];
$state = $this->stack->top();
$terminal = $token[0];
if (!isset($this->action[$state][$terminal])) {
throw new \Exception('Token not allowed here (' . $token[1] . ')');
}
$action = $this->action[$state][$terminal];
if ($action[0] === 0) {
$this->stack->push($token[1]);
$this->stack->push($action[1]);
array_shift($tokens);
} elseif ($action[0] === 1) {
$value = $this->reduce($action[2], $action[1]);
array_unshift($tokens, array($action[3], $value));
} elseif ($action[0] === 2) {
$this->stack->pop();
return $this->stack->pop();
} else {
throw new \RuntimeException('Cannot compile');
}
}
throw new \RuntimeException('Cannot compile. EOF');
}
示例8: onElementStart
/**
* {@inheritdoc}
*/
protected function onElementStart($parser, $name, $attributes)
{
$this->stack->push($name);
if ($name === 'ITEM') {
$this->currentRate = array();
}
}
示例9: testPrintTraversable
function testPrintTraversable()
{
$stack = new \SplStack();
$stack->push('foo');
$stack->push('bar');
$this->assertEquals("<SplStack>['bar', 'foo']", ValuePrinter::serialize($stack));
}
示例10: registerCssFile
public function registerCssFile($filename)
{
if (!isset($this->css_files)) {
$this->css_files = new SplStack();
}
$this->css_files->push($filename);
}
示例11: 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();
}
示例12: startVisiting
public function startVisiting($object)
{
if (!is_object($object)) {
return;
}
$this->visitingSet->attach($object);
$this->visitingStack->push($object);
}
示例13: start
/**
* start profiling a block of code
*
* @param string $title the name you are giving to this block of profiled code
* @return boolean pretty much always returns true
*/
public function start($title)
{
$profile_map = array();
$profile_map['start'] = microtime(true);
$profile_map['title'] = $title;
$this->stack_started->push($profile_map);
return true;
}
示例14: withMiddleware
/**
* {@inheritdoc}
*/
public function withMiddleware($middleware) : StackContract
{
if ($middleware instanceof MiddlewareInterface || $middleware instanceof ServerMiddlewareInterface) {
$this->stack->push($this->isContainerAware($middleware));
return $this;
}
throw new LogicException('Unsupported middleware type.');
}
示例15: enterNode
/**
* {@inheritdoc}
*/
public function enterNode(Node $node)
{
if ($node instanceof Node\FunctionLike) {
$this->loopStacks->push(new \SplStack());
} elseif ($this->isTargetLoopNode($node)) {
$this->getCurrentLoopStack()->push($node);
}
}