Пример #1
0
/**
 * Merge an array of arrays so that the result is in sorted order.
 *
 * @param array $arrays
 * @return array
 */
function mergeSortedArrays(array $arrays)
{
    if (empty($arrays)) {
        return [];
    }
    $heads = array_fill(0, count($arrays), 0);
    $minHeap = new \SplMinHeap();
    $result = [];
    // Insert the first value from each sorted array into the min heap
    for ($i = 0; $i < count($arrays); $i++) {
        if (!empty($arrays[$i])) {
            $minHeap->insert([$arrays[$i][0], $i]);
            $heads[$i] = 1;
        }
    }
    while (!$minHeap->isEmpty()) {
        $smallestArray = $arrays[$minHeap->top()[1]];
        $smallestHead = $heads[$minHeap->top()[1]];
        $result[] = $minHeap->top()[0];
        // If the smallest array has more values, put the next one into the heap
        if ($smallestHead < count($smallestArray)) {
            $minHeap->insert([$smallestArray[$smallestHead], $minHeap->top()[1]]);
            $heads[$minHeap->top()[1]] += 1;
        }
        $minHeap->extract();
    }
    return $result;
}
 public function testForValidValues()
 {
     $var = new \SplMinHeap();
     $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);
 }
Пример #3
0
 public function spl_min_and_max_heaps_are_available_to_do_sort_operations()
 {
     $max_heap = new SplMinHeap();
     $max_heap->insert(5);
     $max_heap->insert(10);
     $max_heap->insert(2);
     $max_heap->insert(20);
     $result = [];
     foreach ($max_heap as $value) {
         $result[] = $value;
     }
     assert_that($result)->is_identical_to(__);
 }
Пример #4
0
 /**
  * @return void
  */
 private function addExtensionInstance($extInstance)
 {
     // do not allow to add the same extension twice
     foreach ($this->getExtensions() as $loadedExt) {
         if ($loadedExt->getNamespace() == $extInstance->getNamespace()) {
             Exception\System\FrameworkRuntimeError::create('The extension "%s" is already loaded', null, $extInstance->getComposerName())->_throw();
         }
     }
     if ($extInstance->getPriority() === null) {
         $extInstance->setPriority($this->getPriority() - 1);
     }
     self::$extMinHeap->insert($extInstance);
     $this->addChild($extInstance);
 }
Пример #5
0
<?php

$h = new SplMinHeap();
try {
    $h->top();
} catch (Exception $e) {
    echo $e->getMessage();
}
Пример #6
0
<?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

$h = new SplMinHeap();
// 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;
echo $h->extract() . "\n";
?>
===DONE===
Пример #8
0
 protected function compare($a, $b)
 {
     return -parent::compare($a, $b);
 }
<?php

$a = new SplMinHeap();
for ($i = 0; $i < 5000; $i++) {
    $a->insert(rand(1, 5000));
}
for ($i = 0; $i < 5000; $i++) {
    $a->extract();
}
Пример #10
0
 /**
  * Uses the chi-squared feature selection algorithm to rank features by
  * informativeness, and return a map from old feature indices to new ones.
  *
  * @param object $features full feature set
  * @return array associative array mapping a subset of the original feature
  * indices to new indices
  */
 function select(Features $features)
 {
     $n = $features->numFeatures();
     $selected = new SplMinHeap();
     $allowed = isset($this->max) ? min($this->max, $n) : $n;
     $labels = array(-1, 1);
     /*
       Start with 1, since 0 is dedicated to the constant intercept term;
       <= $n because n is the last feature.
     */
     for ($j = 1; $j <= $n; $j++) {
         $max_chi2 = 0.0;
         foreach ($labels as $label) {
             /*
               t = term present
               l = document has label
               n = negation
             */
             $stats = $features->varStats($j, $label);
             list($t_l, $t_nl, $nt_l, $nt_nl) = $stats;
             $num = $t_l * $nt_nl - $t_nl * $nt_l;
             $den = ($t_l + $t_nl) * ($nt_l + $nt_nl);
             $chi2 = $den != 0 ? $num * $num / $den : INF;
             if ($chi2 > $max_chi2) {
                 $max_chi2 = $chi2;
             }
         }
         /*
           Keep track of top features in a heap, as we compute
           informativeness.
         */
         if ($allowed > 0) {
             $selected->insert(array($max_chi2, $j));
             $allowed -= 1;
         } else {
             list($other_chi2, $_) = $selected->top();
             if ($max_chi2 > $other_chi2) {
                 $selected->extract();
                 $selected->insert(array($max_chi2, $j));
             }
         }
     }
     return $this->buildMap($selected);
 }
Пример #11
0
<?php

function testForException($heap)
{
    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);
Пример #12
0
<?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);
Пример #13
0
//入栈
$stack->push("data1 \n");
$stack->push("data2 \n");
//出栈
echo $stack->pop();
echo $stack->pop();
// 队列 先进先出
$queue = new SplQueue();
// 入队列
$queue->enqueue("data1 \n");
$queue->enqueue("data2 \n");
// 出队列
echo $queue->dequeue();
echo $queue->dequeue();
// 堆
$heap = new SplMinHeap();
// 插入堆
$heap->insert("data5 \n");
$heap->insert("data6 \n");
// 输出
echo $heap->extract();
echo $heap->extract();
// 固定长度的数组
$array = new SplFixedArray(10);
$array[0] = 123;
$array[9] = 234;
var_dump($array);
/**
 * PSR-0规范
 *
 * 查看psr_0文件夹
Пример #14
0
 public function testCompare3()
 {
     return parent::compare(1, 2, 3);
 }
Пример #15
0
<?php

$pq = new SplMinHeap();
$pq->insert(array(3, 'Clear drains'));
$pq->insert(array(4, 'Feed cat'));
$pq->insert(array(5, 'Make tea'));
$pq->insert(array(1, 'Solve RC tasks'));
$pq->insert(array(2, 'Tax return'));
while (!$pq->isEmpty()) {
    print_r($pq->extract());
}
Пример #16
0
 public function compare($a, $b)
 {
     return -parent::notexist($a, $b);
 }
Пример #17
0
<?php

$a = new SplMinHeap();
for ($i = 0; $i < $argv[1]; $i++) {
    $a->insert(rand(1, $argv[1]));
}
for ($i = 0; $i < $argv[1]; $i++) {
    $a->extract();
}
Пример #18
0
<?php

$input = range(1, 100);
shuffle($input);
$h = new SplMinHeap();
foreach ($input as $i) {
    $h->insert($i);
}
foreach ($h as $k => $o) {
    echo "{$k} => {$o}\n";
}
?>
===DONE===
<?php 
exit(0);
Пример #19
0
<?php

$h = new SplMinHeap();
$h->insert(5);
// top doesn't take any args, lets see what happens if we give it one
$h->top('bogus');
Пример #20
0
 public function compare($a, $b)
 {
     return -parent::compare($a, $b);
 }
Пример #21
0
<?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);
}