Skip to content

Commit

Permalink
[12.x] Improved algorithm for Number::pairs() (#52641)
Browse files Browse the repository at this point in the history
* Update algo

* Update Number.php

* Update SupportNumberTest.php
  • Loading branch information
hotmeteor authored Sep 4, 2024
1 parent c50ee30 commit 2f4ebf1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,24 @@ public static function clamp(int|float $number, int|float $min, int|float $max)
/**
* Split the given number into pairs of min/max values.
*
* @param int|float $to
* @param int|float $by
* @param int|float $offset
* @param int|float $to
* @param int|float $by
* @param int|float $start
* @param int|float $offset
* @return array
*/
public static function pairs(int|float $to, int|float $by, int|float $offset = 1)
public static function pairs(int|float $to, int|float $by, int|float $start = 0, int|float $offset = 1)
{
$output = [];

for ($lower = 0; $lower < $to; $lower += $by) {
$upper = $lower + $by;
for ($lower = $start; $lower < $to; $lower += $by) {
$upper = $lower + $by - $offset;

if ($upper > $to) {
$upper = $to;
}

$output[] = [$lower + $offset, $upper];
$output[] = [$lower, $upper];
}

return $output;
Expand Down
15 changes: 12 additions & 3 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,17 @@ public function testSummarize()

public function testPairs()
{
$this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10));
$this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0));
$this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0));
$this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0, 0));
$this->assertSame([[0, 9], [10, 19], [20, 25]], Number::pairs(25, 10, 0, 1));
$this->assertSame([[1, 11], [11, 21], [21, 25]], Number::pairs(25, 10, 1, 0));
$this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10, 1, 1));
$this->assertSame([[0, 1000], [1000, 2000], [2000, 2500]], Number::pairs(2500, 1000, 0, 0));
$this->assertSame([[0, 999], [1000, 1999], [2000, 2500]], Number::pairs(2500, 1000, 0, 1));
$this->assertSame([[1, 1001], [1001, 2001], [2001, 2500]], Number::pairs(2500, 1000, 1, 0));
$this->assertSame([[1, 1000], [1001, 2000], [2001, 2500]], Number::pairs(2500, 1000, 1, 1));
$this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0, 0));
$this->assertSame([[0, 2.0], [2.5, 4.5], [5.0, 7.0], [7.5, 9.5]], Number::pairs(10, 2.5, 0, 0.5));
$this->assertSame([[0.5, 3.0], [3.0, 5.5], [5.5, 8.0], [8.0, 10]], Number::pairs(10, 2.5, 0.5, 0));
$this->assertSame([[0.5, 2.5], [3.0, 5.0], [5.5, 7.5], [8.0, 10.0]], Number::pairs(10, 2.5, 0.5, 0.5));
}
}

0 comments on commit 2f4ebf1

Please sign in to comment.