本文整理汇总了PHP中SplQueue::enqueue方法的典型用法代码示例。如果您正苦于以下问题:PHP SplQueue::enqueue方法的具体用法?PHP SplQueue::enqueue怎么用?PHP SplQueue::enqueue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplQueue
的用法示例。
在下文中一共展示了SplQueue::enqueue方法的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: bfs_path
function bfs_path($graph, $start, $end)
{
$queue = new SplQueue();
# Enqueue the path
$queue->enqueue([$start]);
$visited = [$start];
while ($queue->count() > 0) {
$path = $queue->dequeue();
# Get the last node on the path
# so we can check if we're at the end
$node = $path[sizeof($path) - 1];
if ($node === $end) {
return $path;
}
foreach ($graph[$node] as $neighbour) {
if (!in_array($neighbour, $visited)) {
$visited[] = $neighbour;
# Build new path appending the neighbour then and enqueue it
$new_path = $path;
$new_path[] = $neighbour;
$queue->enqueue($new_path);
}
}
}
return false;
}
示例3: enqueue
/**
* {@inheritdoc}
*/
public function enqueue(Task $task) : \Generator
{
if (!$this->context->isRunning()) {
throw new StatusError('The worker has not been started.');
}
if ($this->shutdown) {
throw new StatusError('The worker has been shut down.');
}
// If the worker is currently busy, store the task in a busy queue.
if (null !== $this->active) {
$delayed = new Delayed();
$this->busyQueue->enqueue($delayed);
(yield $delayed);
}
$this->active = new Coroutine($this->send($task));
try {
$result = (yield $this->active);
} catch (\Throwable $exception) {
$this->kill();
throw new WorkerException('Sending the task to the worker failed.', $exception);
} finally {
$this->active = null;
}
// We're no longer busy at the moment, so dequeue a waiting task.
if (!$this->busyQueue->isEmpty()) {
$this->busyQueue->dequeue()->resolve();
}
if ($result instanceof TaskFailure) {
throw $result->getException();
}
return $result;
}
示例4: log
/**
* @param $type
* @param $message
* @param null $data
*/
public function log($type, $message, $data = null)
{
if (!$this->traitErrorLog) {
$this->traitErrorLog = new \SplQueue();
}
$this->traitErrorLog->enqueue(['type' => $type, 'message' => $message, 'data' => $data]);
}
示例5: list_dependency_cycles
/**
* @return string[] names of any dependencies involved in dependency cycle[s] (or that depend
* upon those in the cycle)
*/
public function list_dependency_cycles()
{
$dep_counts = $this->dependency_counts();
$depends_on = $this->reverse_graph();
$deps_met_queue = new \SplQueue();
foreach ($dep_counts as $dependency => $count) {
if ($count === 0) {
$deps_met_queue->enqueue($dependency);
}
}
// Iteratively resolve dependencies
$num_removed = 0;
while (!$deps_met_queue->isEmpty()) {
$name = $deps_met_queue->dequeue();
$num_removed++;
if (!array_key_exists($name, $depends_on)) {
continue;
}
foreach ($depends_on[$name] as $dependant) {
$dep_counts[$dependant]--;
if ($dep_counts[$dependant] === 0) {
$deps_met_queue->enqueue($dependant);
}
}
}
// Find the dependencies that couldn't be resolved
$depends_on_cycle = [];
foreach ($dep_counts as $dependency => $count) {
if ($count > 0) {
$depends_on_cycle[] = $dependency;
}
}
return $depends_on_cycle;
}
示例6: dispatch
/**
* @param EventInterface $event
* @param bool $asynchronously
*/
public function dispatch(EventInterface $event, $asynchronously = false)
{
if (!$asynchronously) {
$this->scope->emit($event->getName(), [$event]);
} else {
$this->queue->enqueue($event);
$this->run();
}
}
示例7: enqueue
/**
* Adds an element to the queue
* @param mixed $value
*/
public function enqueue($value)
{
$hash = $this->generateHash($value);
if (!$this->hashes->in($hash)) {
$this->hashes->append($hash);
$this->queue->enqueue($value);
$this->queue->rewind();
}
}
示例8: open
/**
* @return \React\Promise\PromiseInterface
*/
public function open()
{
if ($this->current < $this->limit) {
$this->current++;
return new FulfilledPromise();
}
$deferred = new Deferred();
$this->promises->enqueue($deferred);
return $deferred->promise();
}
示例9: shouldShutDownMessenger
protected function shouldShutDownMessenger(Messenger $messenger)
{
if ($this->callQueue->count() == 0 && $this->pool->count() > $this->options['min_size']) {
unset($this->coreMessengerMapping[spl_object_hash($messenger)]);
$this->pool->detach($messenger);
$messenger->softTerminate();
return;
}
$this->readyPool->enqueue($messenger);
}
示例10: push
public static function push($msg, $code = self::GENERIC_ERROR, $severity = self::SEVERITY_ERROR)
{
self::init();
self::$errors->enqueue(new Error($msg, $code, $severity));
if ($severity == self::SEVERITY_ERROR) {
self::$logger->error($msg);
} else {
self::$logger->warn($msg);
}
}
示例11: addRoute
/**
*
* Adds a route to the router
*
* @param $callable
* @param $conditions
* @author Tim Perry
*/
public function addRoute($callable, $conditions)
{
$route = clone $this->routePrototype;
$route->setCallable($callable);
if (!is_array($conditions)) {
$conditions = array($conditions);
}
$route->addConditionsFromArray($this->replaceFilterFunctions($conditions));
$this->routes->enqueue($route);
}
示例12: getQuery
/**
* @inheritDoc
*/
public function getQuery(Driver $driver, $link)
{
if (!$this->pool->contains($link)) {
throw new \OutOfBoundsException(sprintf('Undefined %s in the pooling controller.', $driver->info($link)));
}
if (!$this->waiting->isEmpty()) {
return $this->waiting->dequeue();
}
$this->idle->enqueue($link);
}
示例13: releaseConnection
/**
* Once a connection has finished being used...
* @param Connection $connection
*/
public function releaseConnection(Connection $connection)
{
// If we have any promises waiting for the connection, pass it along.
if ($this->waiting->count() > 0) {
$cb = $this->waiting->dequeue();
$cb($connection);
return;
}
// Otherwise, move it to the idle queue.
$this->available->enqueue($connection);
}
示例14: run
public function run(TestRequest $test)
{
if (!$this->process->isRunning()) {
$this->process->stop();
$this->process->start();
}
$this->debug("Added " . $test->encode());
$this->testErr = '';
$this->pendingRequests->enqueue($test);
$this->process->write($test->encode());
}
示例15: start
/**
* {@inheritdoc}
*/
public function start()
{
if (isset($this->deferred)) {
throw new \RuntimeException('Tasks is already started');
}
$this->deferred = new Deferred();
$this->process->start(function () {
$this->outputBuffer->enqueue(func_get_args());
});
return $this->deferred->promise();
}