1
|
|
|
<?php namespace Mbh\Collection\Traits; |
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\FixedArray; |
12
|
|
|
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface; |
13
|
|
|
use SplFixedArray; |
14
|
|
|
use SplStack; |
15
|
|
|
use LimitIterator; |
16
|
|
|
|
17
|
|
|
trait Sort |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Perform a bottom-up, non-recursive, in-place mergesort. |
21
|
|
|
* Efficient for very-large objects, and written without recursion |
22
|
|
|
* since PHP isn't well optimized for large recursion stacks. |
23
|
|
|
* |
24
|
|
|
* @param callable $callback The callback for comparison |
25
|
|
|
* @return SequenceableInterface |
26
|
|
|
*/ |
27
|
|
|
public function mergeSort(callable $callback): SequenceableInterface |
28
|
|
|
{ |
29
|
|
|
$count = count($this); |
|
|
|
|
30
|
|
|
$result = new SplFixedArray($count); |
31
|
|
|
for ($k = 1; $k < $count; $k = $k << 1) { |
32
|
|
|
for ($left = 0; ($left + $k) < $count; $left += $k << 1) { |
33
|
|
|
$right = $left + $k; |
34
|
|
|
$rend = min($right + $k, $count); |
35
|
|
|
$m = $left; |
36
|
|
|
$i = $left; |
37
|
|
|
$j = $right; |
38
|
|
|
while ($i < $right && $j < $rend) { |
39
|
|
|
if ($callback($this[$i], $this[$j]) <= 0) { |
40
|
|
|
$result[$m] = $this[$i]; |
41
|
|
|
$i++; |
42
|
|
|
} else { |
43
|
|
|
$result[$m] = $this[$j]; |
44
|
|
|
$j++; |
45
|
|
|
} |
46
|
|
|
$m++; |
47
|
|
|
} |
48
|
|
|
while ($i < $right) { |
49
|
|
|
$result[$m] = $this[$i]; |
50
|
|
|
$i++; |
51
|
|
|
$m++; |
52
|
|
|
} |
53
|
|
|
while ($j < $rend) { |
54
|
|
|
$result[$m] = $this[$j]; |
55
|
|
|
$j++; |
56
|
|
|
$m++; |
57
|
|
|
} |
58
|
|
|
for ($m = $left; $m < $rend; $m++) { |
59
|
|
|
$this[$m] = $result[$m]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sort by applying a CallbackHeap and building a new heap |
69
|
|
|
* Can be efficient for sorting large stored objects. |
70
|
|
|
* |
71
|
|
|
* @param callable $callback The comparison callback |
72
|
|
|
* @return SequenceableInterface |
73
|
|
|
*/ |
74
|
|
|
public function heapSort(callable $callback): SequenceableInterface |
75
|
|
|
{ |
76
|
|
|
$h = new CallbackHeap($callback); |
|
|
|
|
77
|
|
|
foreach ($this as $elem) { |
78
|
|
|
$h->insert($elem); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return static::fromItems($h); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Fallback behaviour to use the builtin array sort functions |
86
|
|
|
* |
87
|
|
|
* @param callable $callback The callback for comparison |
88
|
|
|
* @return SequenceableInterface |
89
|
|
|
*/ |
90
|
|
|
abstract public function arraySort(callable $callback = null): SequenceableInterface; |
91
|
|
|
} |
92
|
|
|
|