| Conditions | 9 |
| Paths | 128 |
| Total Lines | 81 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | protected function fillPartitions(Partitions $partitions, array $dataset, int $chunks): void |
||
| 46 | { |
||
| 47 | // An array S of non-negative numbers {s1, ... ,sn} |
||
| 48 | $s = \array_merge([null], \array_values($dataset)); // adapt indices here: [0..n-1] => [1..n] |
||
| 49 | // Integer K - number of ranges to split items into |
||
| 50 | $k = $chunks; |
||
| 51 | $n = \count($dataset); |
||
| 52 | // Let M[n,k] be the minimum possible cost over all partitionings of N elements to K ranges |
||
| 53 | $m = []; |
||
| 54 | // Let D[n,k] be the position of K-th divider |
||
| 55 | // which produces the minimum possible cost partitioning of N elements to K ranges |
||
| 56 | $d = []; |
||
| 57 | // Note: For code simplicity we don't use zero indices for `m` and `d` |
||
| 58 | // to make code match math formulas |
||
| 59 | // Let pi be the sum of first i elements (cost calculation optimization) |
||
| 60 | $p = []; |
||
| 61 | // 1) Init prefix sums array |
||
| 62 | // pi = sum of {s1, ..., si} |
||
| 63 | $p[0] = $this->getPartitionItemFactory()::create(0); |
||
| 64 | for ($i = 1; $i <= $n; ++$i) { |
||
| 65 | $p[$i] = $this->getPartitionItemFactory()::create($p[$i - 1]->getWeight() + $s[$i]->getWeight()); |
||
| 66 | } |
||
| 67 | // 2) Init boundaries |
||
| 68 | for ($i = 1; $i <= $n; ++$i) { |
||
| 69 | // The only possible partitioning of i elements to 1 range is a single all-elements range |
||
| 70 | // The cost of that partitioning is the sum of those i elements |
||
| 71 | $m[$i][1] = $p[$i]; // sum of {s1, ..., si} -- optimized using pi |
||
| 72 | } |
||
| 73 | for ($j = 1; $j <= $k; ++$j) { |
||
| 74 | // The only possible partitioning of 1 element into j ranges is a single one-element range |
||
| 75 | // The cost of that partitioning is the value of first element |
||
| 76 | $m[1][$j] = $s[1]; |
||
| 77 | } |
||
| 78 | // 3) Main recurrence (fill the rest of values in table M) |
||
| 79 | for ($i = 2; $i <= $n; ++$i) { |
||
| 80 | for ($j = 2; $j <= $k; ++$j) { |
||
| 81 | $solutions = []; |
||
| 82 | for ($x = 1; ($i - 1) >= $x; ++$x) { |
||
| 83 | $solutions[] = [ |
||
| 84 | 0 => $this->getPartitionItemFactory()::create( |
||
| 85 | \max( |
||
| 86 | $m[$x][$j - 1]->getWeight(), |
||
| 87 | $p[$i]->getWeight() - $p[$x]->getWeight() |
||
| 88 | ) |
||
| 89 | ), |
||
| 90 | 1 => $x, |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | |||
| 94 | \usort( |
||
| 95 | $solutions, |
||
| 96 | static function (array $x, array $y) { |
||
| 97 | return $x[0] <=> $y[0]; |
||
| 98 | } |
||
| 99 | ); |
||
| 100 | |||
| 101 | $best_solution = $solutions[0]; |
||
| 102 | $m[$i][$j] = $best_solution[0]; |
||
| 103 | $d[$i][$j] = $best_solution[1]; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // 4) Reconstruct partitioning |
||
| 108 | $i = $n; |
||
| 109 | $j = $k; |
||
| 110 | $partition = []; |
||
| 111 | while (0 < $j) { |
||
| 112 | // delimiter position |
||
| 113 | $dp = $d[$i][$j] ?? 0; |
||
| 114 | // Add elements after delimiter {sdp, ..., si} to resulting $partition. |
||
| 115 | $partition[] = \array_slice($s, $dp + 1, $i - $dp); |
||
| 116 | // Step forward: look for delimiter position for partitioning M[$dp, $j-1] |
||
| 117 | $i = $dp; |
||
| 118 | --$j; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Fix order as we reconstructed the partitioning from end to start |
||
| 122 | $partition = \array_reverse($partition); |
||
| 123 | |||
| 124 | foreach ($partition as $i => $p) { |
||
| 125 | $partitions->partition($i)->exchangeArray($p); |
||
| 126 | } |
||
| 129 |