ImmutableSort::heapSort()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php namespace Mbh\Collection\Traits\Functional;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use Mbh\Collection\FixedArray;
13
use Mbh\Collection\CallbackHeap;
14
use Traversable;
15
use SplFixedArray;
16
use SplStack;
17
use LimitIterator;
18
19
trait ImmutableSort
20
{
21
    /**
22
     * Fallback behaviour to use the builtin array sort functions
23
     *
24
     * @param callable $callback The callback for comparison
25
     * @return CollectionInterface
26
     */
27
    public function arraySort(callable $callback = null)
28
    {
29
        $array = $this->toArray();
30
31
        if ($callback) {
32
            usort($array, $callback);
33
        } else {
34
            sort($array);
35
        }
36
37
        return SplFixedArray::fromArray($array);
38
    }
39
40
    /**
41
     * Sort by applying a CallbackHeap and building a new heap
42
     * Can be efficient for sorting large stored objects.
43
     *
44
     * @param callable $callback The comparison callback
45
     * @return CollectionInterface
46
     */
47
    public function heapSort(callable $callback)
48
    {
49
        $h = new CallbackHeap($callback);
50
        foreach ($this as $elem) {
51
            $h->insert($elem);
52
        }
53
54
        return SplFixedArray::fromArray($h->toArray());
55
    }
56
57
    /**
58
     * Perform a bottom-up, non-recursive, in-place mergesort.
59
     * Efficient for very-large objects, and written without recursion
60
     * since PHP isn't well optimized for large recursion stacks.
61
     *
62
     * @param callable $callback The callback for comparison
63
     * @return CollectionInterface
64
     */
65
    public function mergeSort(callable $callback)
66
    {
67
        $count = $this->count();
68
        $sfa = $this->getValues();
69
        
70
        $result = new SplFixedArray($count);
71
72
        for ($k = 1; $k < $count; $k = $k << 1) {
73
            for ($left = 0; ($left + $k) < $count; $left += $k << 1) {
74
                $right = $left + $k;
75
                $rend = min($right + $k, $count);
76
                $m = $left;
77
                $i = $left;
78
                $j = $right;
79
                while ($i < $right && $j < $rend) {
80
                    if ($callback($sfa[$i], $sfa[$j]) <= 0) {
81
                        $result[$m] = $sfa[$i];
82
                        $i++;
83
                    } else {
84
                        $result[$m] = $sfa[$j];
85
                        $j++;
86
                    }
87
                    $m++;
88
                }
89
                while ($i < $right) {
90
                    $result[$m] = $sfa[$i];
91
                    $i++;
92
                    $m++;
93
                }
94
                while ($j < $rend) {
95
                    $result[$m] = $sfa[$j];
96
                    $j++;
97
                    $m++;
98
                }
99
                for ($m = $left; $m < $rend; $m++) {
100
                    $sfa[$m] = $result[$m];
101
                }
102
            }
103
        }
104
105
        return new static($sfa);
0 ignored issues
show
Unused Code introduced by
The call to Mbh\Collection\Traits\Fu...ableSort::__construct() has too many arguments starting with $sfa. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        return /** @scrutinizer ignore-call */ new static($sfa);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
106
    }
107
108
    abstract public static function fromItems(Traversable $array);
109
110
    abstract public function copy();
111
112
    abstract public function count(): int;
113
114
    abstract public function toArray(): array;
115
116
    abstract protected function getValues(): Traversable;
117
}
118