/** * {@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); }
/** * {@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(); } }
/** * Class constructor * * @param array $processorConfiguration Configuration array, parsed from app/config/services.yml * @param KernelInterface $kernel The Symfony HttpKernel. */ public function __construct(array $processorConfiguration, KernelInterface $kernel) { $this->kernel = $kernel; $this->interval = $processorConfiguration['interval']; $this->port = $processorConfiguration['port']; $this->host = $processorConfiguration['host']; $this->timeout = $processorConfiguration['timeout']; $this->queue = new \SplPriorityQueue(); $this->queue->setExtractFlags(\SplPriorityQueue::EXTR_DATA); $this->workers = new \SplFixedArray($processorConfiguration['workers']); $this->phpPath = (new PhpExecutableFinder())->find(); $this->eventDispatcher = $kernel->getContainer()->get('event_dispatcher'); }
function encode($symb2freq) { $heap = new SplPriorityQueue(); // Instancia fila de prioridades utilizando max heap. $heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH); // define o modo de extração no caso extrai array com a prioridade foreach ($symb2freq as $sym => $wt) { $heap->insert(array($sym => ''), -$wt); } while ($heap->count() > 1) { $lo = $heap->extract(); // extraio o minimo $hi = $heap->extract(); // extraio o minimo foreach ($lo['data'] as &$x) { $x = '0' . $x; } foreach ($hi['data'] as &$x) { $x = '1' . $x; } $heap->insert($lo['data'] + $hi['data'], $lo['priority'] + $hi['priority']); } $result = $heap->extract(); return $result['data']; }
/** * Detach the listener from the events manager * * @param string eventType * @param object handler */ public function detach($eventType, $handler) { if (!is_object($handler)) { throw new \Exception("Event handler must be an Object"); } if (isset($this->_events[$eventType])) { if (is_object($this->_events[$eventType])) { // SplPriorityQueue hasn't method for element deletion, so we need to rebuild queue $newPriorityQueue = new PriorityQueue(); $newPriorityQueue->setExtractFlags(\SplPriorityQueue::EXTR_DATA); $priorityQueue->setExtractFlags(PriorityQueue::EXTR_BOTH); $priorityQueue->top(); while ($priorityQueue->valid()) { $data = $priorityQueue->current(); $priorityQueue->next(); if ($data['data'] !== $handler) { $newPriorityQueue->insert($data['data'], $data['priority']); } } $this->_events[$eventType] = $newPriorityQueue; } else { $key = array_search($handler, $priorityQueue, true); if ($key !== false) { unset($priorityQueue[$key]); } $this->_events[$eventType] = $priorityQueue; } } }
private function findNeighbors($type, $keywords, $id) { $queue = new SplPriorityQueue(); $queue->setExtractFlags(SplPriorityQueue::EXTR_BOTH); $cursor = $this->m->websites->find([]); foreach ($cursor as $doc) { if ($doc['_id'] != new MongoId($id)) { $d = $this->distance($type, $keywords, $doc['type'], $doc['keywords']); $queue->insert($doc, $d); } } $res = []; for ($i = 0; $i < 20 && !$queue->isEmpty(); $i++) { $res[] = $queue->extract(); } return $res; }
public static function deactive(Block $source, $updateLevel) { $queue = new \SplPriorityQueue(); $queue->setExtractFlags(\SplPriorityQueue::EXTR_BOTH); if (!$source->isPowerSource()) { return; } self::addToDeactiveQueue($queue, $source, $updateLevel); while (!$queue->isEmpty()) { $ex = $queue->extract(); $updating = $ex["data"]; $power = $ex["priority"]; /** @var Block $updating */ $updating->setPowerLevel(0); $updating->level->setBlock($updating, $updating, true); self::addToDeactiveQueue($queue, $updating, $power); } }
function encode($symb2freq) { $heap = new SplPriorityQueue(); $heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH); foreach ($symb2freq as $sym => $wt) { $heap->insert(array($sym => ''), -$wt); } while ($heap->count() > 1) { $lo = $heap->extract(); $hi = $heap->extract(); foreach ($lo['data'] as &$x) { $x = '0' . $x; } foreach ($hi['data'] as &$x) { $x = '1' . $x; } $heap->insert($lo['data'] + $hi['data'], $lo['priority'] + $hi['priority']); } $result = $heap->extract(); return $result['data']; }
/** * {@inheritdoc} */ public function remove($type, $handler) { // Handle variable type errors if (!is_string($type)) { throw new Exception("Event type must be a string"); } if (!is_object($handler)) { throw new Exception("Event handler must be an object"); } // Proceed if type exists if (isset($this->events[$type])) { $priorityQueue = $this->events[$type]; if (is_object($priorityQueue)) { // Create a temporary queue $tmpPriorityQueue = new \SplPriorityQueue(); $tmpPriorityQueue->setExtractFlags(\SplPriorityQueue::EXTR_DATA); // Prepare the current queue $priorityQueue->setExtractFlags(\SplPriorityQueue::EXTR_BOTH); $priorityQueue->top(); // Recreate a queue without the handle while ($priorityQueue->valid()) { $data = $priorityQueue->current(); $priorityQueue->next(); if ($data["data"] !== $handler) { $tmpPriorityQueue->insert($data["data"], $data["priority"]); } } // Replace the event queue with the new one $this->events[$type] = $tmpPriorityQueue; } else { // Search array for the handler and remove it if (array_search($handler, $priorityQueue, true) !== false) { unset($priorityQueue[$key]); } // Replace the event queue with the new one $this->events[$type] = $priorityQueue; } } }
} echo "EXTR_BOTH\n"; $pq1 = new SplPriorityQueue(); $pq1->setExtractFlags(SplPriorityQueue::EXTR_BOTH); $pq1->insert("a", 1); $pq1->insert("b", 2); $pq1->insert("c", 0); foreach ($pq1 as $k => $v) { echo "{$k}=>" . print_r($v, 1) . "\n"; } echo "EXTR_DATA\n"; $pq2 = new SplPriorityQueue(); $pq2->setExtractFlags(SplPriorityQueue::EXTR_DATA); $pq2->insert("a", 1); $pq2->insert("b", 2); $pq2->insert("c", 0); foreach ($pq2 as $k => $v) { echo "{$k}=>" . print_r($v, 1) . "\n"; } echo "EXTR_PRIORITY\n"; $pq3 = new SplPriorityQueue(); $pq3->setExtractFlags(SplPriorityQueue::EXTR_PRIORITY); $pq3->insert("a", 1); $pq3->insert("b", 2); $pq3->insert("c", 0); foreach ($pq3 as $k => $v) { echo "{$k}=>" . print_r($v, 1) . "\n"; } ?> ===DONE===
/** * @see Events\EventInterface::clearAllTimer() */ public function clearAllTimer() { $this->_scheduler = new \SplPriorityQueue(); $this->_scheduler->setExtractFlags(\SplPriorityQueue::EXTR_BOTH); $this->_task = array(); }
<?php $pq = new SplPriorityQueue(); $pq->insert('Clear drains', 3); $pq->insert('Feed cat', 4); $pq->insert('Make tea', 5); $pq->insert('Solve RC tasks', 1); $pq->insert('Tax return', 2); // This line causes extract() to return both the data and priority (in an associative array), // Otherwise it would just return the data $pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH); while (!$pq->isEmpty()) { print_r($pq->extract()); }
public function detach($eventType, $handler) { if (!is_string($eventType) || !is_object($handler)) { throw new Exception("Invalid parameter!", 1); } if (isset($this->_events[$eventType])) { $priorityQueue = $this->_events[$eventType]; if (is_object($priorityQueue)) { # 有SplPriorityQueue无法删除元素,因此我们需要创建一个新的 $newPriorityQueue = new PriorityQueue(); $newPriorityQueue->setExtractFlags(PriorityQueue::EXTR_DATA); $priorityQueue->setExtractFlags(PriorityQueue::EXTR_BOTH); $priorityQueue->top(); // 将优先队列的指针指向起始位置 while ($priorityQueue->valid()) { $data = $priorityQueue->current(); // 此时既输出了数据,有输出了优先级 $priorityQueue->next(); if ($data['data'] !== $handler) { $newPriorityQueue->insert($data['data'], $data['priority']); } } $this->_events[$eventType] = $newPriorityQueue; } else { $key = array_search($handler, $priorityQueue); if ($key !== false) { unset($priorityQueue[$key]); } $this->_events[$eventType] = $priorityQueue; } } }
<?php $priorityQueue = new SplPriorityQueue(); var_dump($priorityQueue->getExtractFlags()); $priorityQueue->insert("a", 1); $priorityQueue->insert("b", 2); $priorityQueue->insert("c", 0); echo "EXTR DEFAULT", PHP_EOL; echo "value: ", $priorityQueue->top(), PHP_EOL; $priorityQueue->setExtractFlags(SplPriorityQueue::EXTR_PRIORITY); var_dump($priorityQueue->getExtractFlags() & SplPriorityQueue::EXTR_PRIORITY); echo "EXTR_PRIORITY", PHP_EOL; echo "priority: ", $priorityQueue->top(), PHP_EOL; $priorityQueue->setExtractFlags(SplPriorityQueue::EXTR_BOTH); echo "EXTR_BOTH", PHP_EOL; print_r($priorityQueue->top()); echo "EXTR_DATA", PHP_EOL; $priorityQueue->setExtractFlags(SplPriorityQueue::EXTR_DATA); echo "value: ", $priorityQueue->top(), PHP_EOL;
<?php $sp = new SplPriorityQueue(); $sp->setExtractFlags(1, 1); // Should throw a warning as setExtractFlags expects only 1 argument
function calculate_score() { global $score_board; global $TOTAL_P_SCORE; global $score_P_ratio; $qCur = new SplPriorityQueue(); $qExpire = new SplPriorityQueue(); $total_p_score = $TOTAL_P_SCORE; $multiplier = 0.01; $qCur->setExtractFlags(SplPriorityQueue::EXTR_DATA); $qExpire->setExtractFlags(SplPriorityQueue::EXTR_DATA); foreach ($score_board as $k => $r) { ///// f_score if (IsValidScoreboardEntry($k)) { $score_board[$k]["func"] = $score_board[$k]["check_pattern"] * pow(0.9, $score_board[$k]["mem_error"]); if ($score_board[$k]["mem_heap"] > 0) { $score_board[$k]["func"] *= ($score_board[$k]["mem_heap"] - $score_board[$k]["heap_lost"]) / $score_board[$k]["mem_heap"]; } if ($r["priority"] > 0) { $qCur->insert($k, $r["priority"]); } } else { $score_board[$k]["func"] = 0; } ////// init p_score $score_board[$k]["p_score"] = 0; } while ($qCur->valid() && $total_p_score > 0) { while ($qCur->valid()) { $idx = $qCur->extract(); $s = $score_board[$idx]["priority"] * $multiplier; // alloted score in the scheduling cycle if ($s > 100 - $score_board[$idx]["p_score"]) { $s = 100 - $score_board[$idx]["p_score"]; } $score_board[$idx]["p_score"] += $s; $total_p_score -= $s; if ($score_board[$idx]["p_score"] < 100) { $qExpire->insert($idx, $score_board[$idx]["priority"]); } } $qCur = $qExpire; $qExpire = new SplPriorityQueue(); $qExpire->setExtractFlags(SplPriorityQueue::EXTR_DATA); } foreach ($score_board as $k => $r) { $score_board[$k]["score"] = 100 * $score_board[$k]["func"] * (1 - $score_P_ratio + $score_board[$k]["p_score"] * $score_P_ratio / 100); } ////////sort by score $tmp = array(); foreach ($score_board as &$ma) { $tmp[] =& $ma["score"]; } array_multisort($tmp, SORT_DESC, $score_board); }
<?php $slist = new \ION\Data\SkipList(); $alist = new \ArrayObject(); $plist = new \SplPriorityQueue(); $plist->setExtractFlags($plist::EXTR_BOTH); // $this->assertSame(0, $slist->count()); $memory = memory_get_usage(1); $top = $item = null; $i = 0; $time = microtime(1); for ($i = 0; $i < 1000; $i++) { $slist->add($i, "zero#{$i}"); } printf("\nset 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[$i] = "zero#{$i}"; } printf("\nnset 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++) { $plist->insert("zero#{$i}", -$i); } printf("\nnset 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++) { $top = $slist->first();