public function testForValidValues() { $var = new \SplMaxHeap(); $var->insert(1); $var->insert(2); $var->insert(3); $data = new VariableWrapper(get_class($var), $var); $result = $this->inspector->get($data); $this->assertInstanceOf('Ladybug\\Plugin\\Extra\\Type\\CollectionType', $result); $this->assertCount(3, $result); }
/** * function - getTopNIntegersFromChunk * Loops over every integer in the chunk and pulls out the top N. N is specified in the constructor. * @param - $numbers - the chunk containing the numbers * @param - $n - the number of integers we want to get back. This is set in the constructor * * @return - array - the N highest numbers from the chunk */ public function getTopNIntegersFromChunk(array $numbers, $n) { $maxHeap = new SplMaxHeap(); foreach ($numbers as $number) { $maxHeap->insert($number); } return iterator_to_array(new LimitIterator($maxHeap, 0, $n)); }
<?php $h = new SplMaxHeap(); $h->insert(1); function myfunc($a, $b) { error_log($a . $b); } class MyClass { function myMeth($a, $b) { error_log($a . $b); } }
<?php $heap = new SplMinHeap(); $heap->insert([1, 1]); $heap->insert([2, 2]); $heap->insert([1, 2]); $heap->insert([2, 1]); foreach ($heap as $item) { echo implode(' ', $item) . "\n"; } $heap = new SplMaxHeap(); $heap->insert([1, 1]); $heap->insert([2, 2]); $heap->insert([1, 2]); $heap->insert([2, 1]); foreach ($heap as $item) { echo implode(' ', $item) . "\n"; }
<?php $a = new SplMaxHeap(); for ($i = 0; $i < 5000; $i++) { $a->insert(rand(1, 5000)); } for ($i = 0; $i < 5000; $i++) { $a->extract(); }
<?php /** * A kulcsot nem tartja meg */ $data = [2, 4, 1, 76, 42, 23, 0, 10, 2]; $heap = new SplMinHeap(); foreach ($data as $v) { $heap->insert($v); } printf("TOP: %s\n", $heap->top()); foreach ($heap as $k => $v) { printf("%s => %s\n", $k, $v); } $heap = new SplMaxHeap(); foreach ($data as $v) { $heap->insert($v); } printf("TOP: %s\n", $heap->top()); foreach ($heap as $k => $v) { printf("%s => %s\n", $k, $v); }
<?php //============================================================================== // PHP SEIDS: Supplementary, Easily Interchangeable Data Structures // // Copyright 2015, Daniel A.C. Martin // Distributed under the MIT License. // (See LICENSE file for details.) //============================================================================== define('DS_SIZE', 10000); //////////////////////////////////////////////////////////////////////////////// // Take initial memory usage $initial_memory = memory_get_usage(); // Build data structure $ds = new SplMaxHeap(); $n = 0; while ($n++ < DS_SIZE) { $ds->insert(mt_rand()); } // Calculate memory usage of data structure $memory_usage = memory_get_usage() - $initial_memory; // Output results echo 'Memory usage: ' . $memory_usage . ' bytes' . "\n";
<?php $h = new SplMaxHeap(); echo "Checking a new heap is empty: "; var_dump($h->isEmpty()) . "\n"; $h->insert(2); echo "Checking after insert: "; var_dump($h->isEmpty()) . "\n"; $h->extract(); echo "Checking after extract: "; var_dump($h->isEmpty()) . "\n";
/** * An adaptation of the Zhang-Oles 2001 CLG algorithm by Genkin et al. to * use the Laplace prior for parameter regularization. On completion, * optimizes the beta vector to maximize the likelihood of the data set. * * @param object $X SparseMatrix representing the training dataset * @param array $y array of known labels corresponding to the rows of $X */ function train($X, $y) { $invX = new InvertedData($X); $this->lambda = $this->estimateLambdaNorm($invX); $m = $invX->rows(); $n = $invX->columns(); $this->beta = array_fill(0, $n, 0.0); $beta =& $this->beta; $lambda = $this->lambda; $d = array_fill(0, $n, 1.0); $r = array_fill(0, $m, 0.0); $converged = false; $drSum = 0.0; $rSum = 0.0; $change = 0.0; $score = 0.0; $minDrj = $this->epsilon; $prevDrj = $this->epsilon; $schedule = new SplMaxHeap(); $nextSchedule = new SplMaxHeap(); for ($j = 0; $j < $n; $j++) { $schedule->insert(array($this->epsilon, $j)); } for ($k = 0; !$converged; $k++) { $prevR = $r; $var = 1; while (!$schedule->isEmpty()) { list($drj, $j) = $schedule->top(); if ($drj < $minDrj) { break; } else { $schedule->extract(); $prevDrj = $drj; } $Xj = $invX->iterateColumn($j); list($numer, $denom) = $this->computeApproxLikelihood($Xj, $y, $r, $d[$j]); // Compute tentative step $dvj if ($beta[$j] == 0) { $dvj = ($numer - $lambda) / $denom; if ($dvj <= 0) { $dvj = ($numer + $lambda) / $denom; if ($dvj >= 0) { $dvj = 0; } } } else { $s = $beta[$j] > 0 ? 1 : -1; $dvj = ($numer - $s * $lambda) / $denom; if ($s * ($beta[$j] + $dvj) < 0) { $dvj = -$beta[$j]; } } if ($dvj == 0) { $d[$j] /= 2; $nextSchedule->insert(array($this->epsilon, $j, $k)); } else { // Compute delta for beta[j], constrained to trust region. $dbetaj = min(max($dvj, -$d[$j]), $d[$j]); // Update our cached dot product by the delta. $drj = 0.0; foreach ($Xj as $cell) { list($_, $i, $Xij) = $cell; $dr = $dbetaj * $Xij; $drj += $dr; $r[$i] += $dr; } $drj = abs($drj); $nextSchedule->insert(array($drj, $j, $k)); $beta[$j] += $dbetaj; // Update the trust region. $d[$j] = max(2 * abs($dbetaj), $d[$j] / 2); } if ($this->debug > 1) { $score = $this->score($r, $y, $beta); } $this->log(sprintf("itr = %3d, j = %4d (#%d), score = %6.2f, change = %6.4f", $k + 1, $j, $var, $score, $change)); $var++; } // Update $converged $drSum = 0.0; $rSum = 0.0; for ($i = 0; $i < $m; $i++) { $drSum += abs($r[$i] - $prevR[$i]); $rSum += abs($r[$i]); } $change = $drSum / (1 + $rSum); $converged = $change <= $this->epsilon; while (!$schedule->isEmpty()) { list($drj, $j) = $schedule->extract(); $nextSchedule->insert(array($drj * 4, $j)); } $tmp = $schedule; $schedule = $nextSchedule; $nextSchedule = $tmp; $minDrj *= 2; } }
* 最小堆和最大堆是堆,所以,所有双向链表的方法都可以被栈和队列使用。调用方法也一致。 * SplHeap只能通过继承的方式来调用该类方法。可以直接new SplMinHeap或者new SplMaxHeap * * 优先队列堆会按照插入时给定的优先值建立二叉树。 * * * 堆是一个完全二叉树,高度是O(lg n)。特点是父节点的值大于(小于)两个子节点的值,分别称为大顶!d=====( ̄▽ ̄*)b堆和小顶堆 * 最常见的应用的堆配需,事件复杂度是O(N lg N)。如果从小到大排序,用大顶堆,从大到小排序,用小顶堆 */ // compare // 比较值,一般用来建立自己的堆类,通过比较返回值作为最大堆或者最小堆 // $heap = new SplMaxHeap(); // insert($value) // 正常堆插入新值 $heap->insert('3'); $heap->insert('1'); $heap->insert('2'); $heap->insert('5'); $heap->insert('4'); // $heap = new SplPriorityQueue(); // insert($value, $priority) // 优先队列插入新值 //$heap->insert('A', '3'); //$heap->insert('B', '1'); //$heap->insert('C', '2'); //$heap->insert('D', '5'); //$heap->insert('E', '4'); // isEmpty() // 判断该堆是否为空 $heap->isEmpty();
try { foreach ($heap as &$item) { } } catch (RuntimeException $e) { echo $e->getMessage(), "\n"; } } // 1. SplMinHeap emtpy $heap = new SplMinHeap(); testForException($heap); // 2. SplMinHeap non-emtpy $heap = new SplMinHeap(); $heap->insert(1); testForException($heap); // 3. SplMaxHeap emtpy $heap = new SplMaxHeap(); testForException($heap); // 4. SplMaxHeap non-emtpy $heap = new SplMaxHeap(); $heap->insert(1); testForException($heap); // 5. SplPriorityQueue empty $heap = new SplPriorityQueue(); testForException($heap); // 6. SplPriorityQueue non-empty $heap = new SplPriorityQueue(); $heap->insert(1, 2); testForException($heap); ?> ==DONE==
<?php require_once __DIR__ . '/../vendor/autoload.php'; $numbers = array(10, 15, 1, 56, 3); // Heaps $maxHeap = new \SplMaxHeap(); foreach ($numbers as $item) { $maxHeap->insert($item); } $minHeap = new \SplMinHeap(); foreach ($numbers as $item) { $minHeap->insert($item); } // Stacks $stack = new \SplStack(); foreach ($numbers as $item) { $stack->push($item); } // Queues $queue = new \SplQueue(); foreach ($numbers as $item) { $queue->push($item); } $ladybug = new \Ladybug\Dumper(); echo $ladybug->dump($maxHeap, $minHeap, $stack, $queue);
<?php $h = new SplMaxHeap(); var_dump($h->insert());
<?php $a = new SplMaxHeap(); $a->insert($a); var_dump($a); ?> ===DONE===
<?php $h = new SplMaxHeap(); $h->insert(1); $h->insert(5); $h->insert(0); $h->insert(4); $h->rewind(); echo "count(\$h) = " . count($h) . "\n"; echo "\$h->count() = " . $h->count() . "\n"; while ($h->valid()) { $k = $h->key(); $v = $h->current(); echo "{$k}=>{$v}\n"; $h->next(); } ?> ===DONE===
<?php $obj = new SplMaxHeap(); $obj->insert(1); $obj->insert(2); var_dump(count($obj)); class SplMaxHeap2 extends SplMaxHeap { public function count() { return -parent::count(); } } $obj = new SplMaxHeap2(); $obj->insert(1); $obj->insert(2); var_dump(count($obj));
<?php $input = range(1, 100); shuffle($input); $h = new SplMaxHeap(); foreach ($input as $i) { $h->insert($i); } foreach ($h as $k => $o) { echo "{$k} => {$o}\n"; } ?> ===DONE===
<?php $h = new SplMaxHeap(); // errors try { $h->extract(); } catch (RuntimeException $e) { echo "Exception: " . $e->getMessage() . "\n"; } $h->insert(1); $h->insert(2); $h->insert(3); $h->insert(3); $h->insert(3); echo $h->count() . "\n"; echo $h->extract() . "\n"; echo $h->extract() . "\n"; echo $h->extract() . "\n"; echo $h->extract() . "\n"; echo $h->extract() . "\n"; echo $h->count() . "\n"; echo "--\n"; $b = 4; $h->insert($b); $b = 5; $h2 = clone $h; echo $h->extract() . "\n"; echo $h2->extract() . "\n"; ?> ===DONE=== <?php
<?php $heap = new SplMaxHeap(); $heap->insert(42); foreach ($heap as $key => $value) { var_dump($key); var_dump($value); break; } $heap = new SplMaxHeap(); $heap->insert(42); var_dump($heap->key()); var_dump($heap->current());