本文整理汇总了PHP中SplPriorityQueue::top方法的典型用法代码示例。如果您正苦于以下问题:PHP SplPriorityQueue::top方法的具体用法?PHP SplPriorityQueue::top怎么用?PHP SplPriorityQueue::top使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplPriorityQueue
的用法示例。
在下文中一共展示了SplPriorityQueue::top方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct()
{
$this->queue = new \SplPriorityQueue();
$this->timer = Loop\periodic(1, function () {
$time = time();
while (!$this->queue->isEmpty()) {
$key = $this->queue->top();
if (isset($this->expire[$key])) {
if ($time <= $this->expire[$key]) {
break;
}
unset($this->data[$key], $this->expire[$key], $this->ttl[$key]);
}
$this->queue->extract();
}
if ($this->queue->isEmpty()) {
$this->timer->stop();
}
});
$this->timer->stop();
$this->timer->unreference();
}
示例2: pop
/**
* @see QueueInterface::pop()
*/
public function pop()
{
if (!$this->innerQueue->isEmpty()) {
$eta = $this->innerQueue->top()->getEta();
if (!$eta || $eta->getTimestamp() <= time()) {
return $this->innerQueue->extract();
}
}
return false;
}
示例3: pop
/**
* {@inheritdoc}
*/
public function pop()
{
if (!$this->queue->isEmpty()) {
$this->queue->setExtractFlags(\SplPriorityQueue::EXTR_PRIORITY);
$priority = $this->queue->top();
if (time() + $priority[0] >= 0) {
$this->queue->setExtractFlags(\SplPriorityQueue::EXTR_DATA);
return $this->queue->extract();
}
}
throw new NoItemAvailableException($this);
}
示例4: pop
/**
* {@inheritdoc}
*/
public function pop()
{
if ($this->queue->isEmpty()) {
return;
}
$this->queue->setExtractFlags(\SplPriorityQueue::EXTR_PRIORITY);
$priority = $this->queue->top();
if (time() + $priority[0] >= 0) {
$this->queue->setExtractFlags(\SplPriorityQueue::EXTR_DATA);
return $this->queue->extract();
}
}
示例5: trigger
/**
* @param string $event
* @param array $params
* @param string $context
*/
public function trigger($event, $params = [], $context = '')
{
if (empty($this->events[$event])) {
return;
}
$queue = new \SplPriorityQueue();
foreach ($this->events[$event] as $index => $action) {
$queue->insert($index, $action['prio']);
}
$queue->top();
while ($queue->valid()) {
$index = $queue->current();
if (!empty($context)) {
$contexts = explode(',', $this->events[$event][$index]['contexts']);
$current_context = false;
foreach ($contexts as $route) {
if (fnmatch(trim($route), $context)) {
$current_context = true;
break;
}
}
} else {
$current_context = true;
}
if ($current_context && is_callable($this->events[$event][$index]['fn'])) {
if (call_user_func_array($this->events[$event][$index]['fn'], $params) === false) {
break;
}
}
$queue->next();
}
}
示例6: tick
/**
* 检查是否有可执行的定时任务,有的话执行
* @return void
*/
protected function tick()
{
while (!$this->_scheduler->isEmpty()) {
$scheduler_data = $this->_scheduler->top();
$timer_id = $scheduler_data['data'];
$next_run_time = -$scheduler_data['priority'];
$time_now = microtime(true);
if ($time_now >= $next_run_time) {
$this->_scheduler->extract();
// 如果任务不存在,则是对应的定时器已经删除
if (!isset($this->_task[$timer_id])) {
continue;
}
// 任务数据[func, args, flag, timer_interval]
$task_data = $this->_task[$timer_id];
// 如果是持续的定时任务,再把任务加到定时队列
if ($task_data[2] === self::EV_TIMER) {
$next_run_time = $time_now + $task_data[3];
$this->_scheduler->insert($timer_id, -$next_run_time);
}
// 尝试执行任务
try {
call_user_func_array($task_data[0], $task_data[1]);
} catch (\Exception $e) {
echo $e;
}
continue;
} else {
// 设定超时时间
$this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
return;
}
}
$this->_selectTimeout = 100000000;
}
示例7: tick
/**
* Executes any pending timers. Returns the number of timers executed.
*
* @return int
*
* @internal
*/
public function tick() : int
{
$count = 0;
$time = microtime(true);
while (!$this->queue->isEmpty()) {
list($timer, $timeout) = $this->queue->top();
if (!$this->timers->contains($timer) || $timeout !== $this->timers[$timer]) {
$this->queue->extract();
// Timer was removed from queue.
continue;
}
if ($this->timers[$timer] > $time) {
// Timer at top of queue has not expired.
return $count;
}
// Remove and execute timer. Replace timer if persistent.
$this->queue->extract();
if ($timer->isPeriodic()) {
$timeout = $time + $timer->getInterval();
$this->queue->insert([$timer, $timeout], -$timeout);
$this->timers[$timer] = $timeout;
} else {
$this->timers->detach($timer);
}
// Execute the timer.
$timer->call();
++$count;
}
return $count;
}
示例8: tick
/**
* Tick for timer.
* @return void
*/
protected function tick()
{
while (!$this->_scheduler->isEmpty()) {
$scheduler_data = $this->_scheduler->top();
$timer_id = $scheduler_data['data'];
$next_run_time = -$scheduler_data['priority'];
$time_now = microtime(true);
if ($time_now >= $next_run_time) {
$this->_scheduler->extract();
if (!isset($this->_task[$timer_id])) {
continue;
}
// [func, args, flag, timer_interval]
$task_data = $this->_task[$timer_id];
if ($task_data[2] === self::EV_TIMER) {
$next_run_time = $time_now + $task_data[3];
$this->_scheduler->insert($timer_id, -$next_run_time);
}
call_user_func_array($task_data[0], $task_data[1]);
if (isset($this->_task[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
$this->del($timer_id, self::EV_TIMER_ONCE);
}
continue;
} else {
$this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
return;
}
}
$this->_selectTimeout = 100000000;
}
示例9: nextTimeout
/**
* Get the number of microseconds until the next timer watcher is due.
*
* @return int or null when no timers are pending.
*/
protected function nextTimeout()
{
$time = \microtime(true);
while (!$this->scheduler->isEmpty()) {
list($watcher, $scheduled) = $this->scheduler->top();
if ($watcher->enabled && $watcher->scheduled === $scheduled) {
return (int) \max(0, ($watcher->due - $time) * 1000000);
}
}
}
示例10: __construct
/**
* Method to instantiate the view.
*
* @param JModel $model The model object.
* @param SplPriorityQueue $paths The paths queue.
*
* @since 12.1
*/
public function __construct(JModel $model, SplPriorityQueue $paths = null)
{
parent::__construct($model);
// Setup dependencies.
$this->paths = isset($paths) ? $paths : $this->loadPaths();
/* T3: Add T3 html path to the priority paths of component view */
// T3 html path
$component = JApplicationHelper::getComponentName();
$component = preg_replace('/[^A-Z0-9_\\.-]/i', '', $component);
$t3Path = T3_PATH . '/html/' . $component . '/' . $this->getName();
// Setup the template path
$this->paths->top();
$defaultPath = $this->paths->current();
$this->paths->next();
$templatePath = $this->paths->current();
// add t3 path
$this->paths->insert($t3Path, 3);
$this->_path['template'] = array($defaultPath, $t3Path, $templatePath);
/* //T3 */
}
示例11: getTimeout
/**
* @return int Milliseconds until next timer expires or -1 if there are no pending times.
*/
private function getTimeout()
{
while (!$this->timerQueue->isEmpty()) {
list($watcher, $expiration) = $this->timerQueue->top();
$id = $watcher->id;
if (!isset($this->timerExpires[$id]) || $expiration !== $this->timerExpires[$id]) {
$this->timerQueue->extract();
// Timer was removed from queue.
continue;
}
$expiration -= (int) (\microtime(true) * self::MILLISEC_PER_SEC);
if ($expiration < 0) {
return 0;
}
return $expiration;
}
return -1;
}
示例12: apply
public function apply($filter, $value, $args = [])
{
if (!isset($this->filters[$filter])) {
return $value;
}
if (!count($this->filters[$filter])) {
return $this;
}
$queue = new \SplPriorityQueue();
foreach ($this->filters[$filter] as $index => $action) {
$queue->insert($index, $action["prio"]);
}
$queue->top();
while ($queue->valid()) {
$index = $queue->current();
if (is_callable($this->filters[$filter][$index]["fn"])) {
$value = call_user_func_array($this->filters[$filter][$index]["fn"], [$value, $args]);
}
$queue->next();
}
return $value;
}
示例13: microtime
$memory = memory_get_usage(1);
$time = microtime(1);
for ($i = 0; $i < 1000; $i++) {
$top = $slist->first();
}
printf("\ntop slist: time: %f, memory: %d KB\n", microtime(1) - $time, round((memory_get_usage(1) - $memory) / 1000, 2));
$memory = memory_get_usage(1);
$time = microtime(1);
for ($i = 0; $i < 1000; $i++) {
$alist->asort();
$top = key($alist);
}
printf("\ntop alist: time: %f, memory: %d KB\n", microtime(1) - $time, round((memory_get_usage(1) - $memory) / 1000, 2));
$memory = memory_get_usage(1);
$time = microtime(1);
for ($i = 0; $i < 1000; $i++) {
$top = $plist->top();
}
printf("\ntop plist: time: %f, memory: %d KB\n", microtime(1) - $time, round((memory_get_usage(1) - $memory) / 1000, 2));
$memory = memory_get_usage(1);
$time = microtime(1);
for ($i = 0; $i < 1000; $i++) {
$item = $slist->get($i);
}
printf("\nget slist: time: %f, memory: %d KB\n", microtime(1) - $time, round((memory_get_usage(1) - $memory) / 1000, 2));
$memory = memory_get_usage(1);
$time = microtime(1);
for ($i = 0; $i < 1000; $i++) {
$item = $alist[$i];
}
printf("\nget alist: time: %f, memory: %d KB\n", microtime(1) - $time, round((memory_get_usage(1) - $memory) / 1000, 2));
示例14: SplPriorityQueue
<?php
$priorityQueue = new SplPriorityQueue();
try {
$priorityQueue->top();
} catch (RuntimeException $e) {
echo "Exception: " . $e->getMessage() . PHP_EOL;
}
示例15: split
$school_same = split(";", $school);
$response = json_decode(file_get_contents('http://maps.googleapis.com/maps/api/distancematrix/json?origins=' . urlencode($home) . '&destinations=' . urlencode($school_same[0]) . '&mode=driving&language=nl-BE&sensor=false'));
$distance = -(int) $response->rows[0]->elements[0]->distance->text;
$pq->insert($school, $distance);
$pref_array2[$id][$school] = $distance;
}
$pref_array[$id] = $pq;
}
$engage_array = [];
$avn_array = [];
$id_enagage = [];
while (count($engage_array) < count($school_array)) {
foreach ($pref_array as $id => $pq) {
if (!isset($id_enagage[$id]) || !$id_enagage[$id]) {
if (!$pq->isEmpty()) {
$top = $pq->top();
if (!isset($engage_array[$top])) {
$engage_array[$top] = $id;
$id_enagage[$id] = true;
//echo "$top***$id<br>";
} else {
$old = $engage_array[$top];
if (!isset($avnarray[$old])) {
$avnarray[$old] = avn($old, $conn, $pref_array2, $id_home);
//echo "###$avnarray[$old]<br>";
}
if (!isset($avnarray[$id])) {
$avnarray[$id] = avn($id, $conn, $pref_array2, $id_home);
//echo "!!!!!$avnarray[$id]<br>";
}
if ($avnarray[$old] > $avnarray[$id]) {