Skip to content

Commit

Permalink
Size comparison functions added for performance reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
DusanKasan committed May 27, 2016
1 parent 7824290 commit d306b61
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@

##6.1.0
- Filter can be called without arguments and it will remove falsy values

##6.2.0
- sizeIsExactly function added
- sizeIsGreaterThan function added
- sizeIsLessThan function added
- sizeIsBetween function added
48 changes: 48 additions & 0 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,54 @@ public function intersect(...$collections)
return intersect($this->getItems(), ...$collections);
}



/**
* Checks whether this collection has exactly $size items.
*
* @param int $size
* @return bool
*/
public function sizeIs($size)
{
return sizeIs($this->getItems(), $size);
}

/**
* Checks whether this collection has less than $size items.
*
* @param int $size
* @return bool
*/
public function sizeIsLessThan($size)
{
return sizeIsLessThan($this->getItems(), $size);
}

/**
* Checks whether this collection has more than $size items.
*
* @param int $size
* @return bool
*/
public function sizeIsGreaterThan($size)
{
return sizeIsGreaterThan($this->getItems(), $size);
}

/**
* Checks whether this collection has between $fromSize to $toSize items. $toSize can be
* smaller than $fromSize.
*
* @param int $fromSize
* @param int $toSize
* @return bool
*/
public function sizeIsBetween($fromSize, $toSize)
{
return sizeIsBetween($this->getItems(), $fromSize, $toSize);
}

/**
* @return array|\Traversable
*/
Expand Down
95 changes: 95 additions & 0 deletions src/collection_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1421,3 +1421,98 @@ function extract($collection, $keyPath)

return new Collection($generatorFactory);
}

/**
* Checks whether $collection has exactly $size items.
*
* @param array|Traversable $collection
* @param int $size
* @return bool
*/
function sizeIs($collection, $size)
{
$itemsTempCount = 0;

foreach ($collection as $key => $value) {
$itemsTempCount++;

if ($itemsTempCount > $size) {
return false;
}
}

return $itemsTempCount == $size;
}

/**
* Checks whether $collection has less than $size items.
*
* @param array|Traversable $collection
* @param int $size
* @return bool
*/
function sizeIsLessThan($collection, $size)
{
$itemsTempCount = 0;

foreach ($collection as $key => $value) {
$itemsTempCount++;

if ($itemsTempCount > $size) {
return false;
}
}

return $itemsTempCount < $size;
}

/**
* Checks whether $collection has more than $size items.
*
* @param array|Traversable $collection
* @param int $size
* @return bool
*/
function sizeIsGreaterThan($collection, $size)
{
$itemsTempCount = 0;

foreach ($collection as $key => $value) {
$itemsTempCount++;

if ($itemsTempCount > $size) {
return true;
}
}

return $itemsTempCount > $size;
}

/**
* Checks whether $collection has between $fromSize to $toSize items. $toSize can be
* smaller than $fromSize.
*
* @param array|Traversable $collection
* @param int $fromSize
* @param int $toSize
* @return bool
*/
function sizeIsBetween($collection, $fromSize, $toSize)
{
if ($fromSize > $toSize) {
$tmp = $toSize;
$toSize = $fromSize;
$fromSize = $tmp;
}

$itemsTempCount = 0;
foreach ($collection as $key => $value) {
$itemsTempCount++;

if ($itemsTempCount > $toSize) {
return false;
}
}

return $fromSize < $itemsTempCount && $itemsTempCount < $toSize;
}
35 changes: 34 additions & 1 deletion tests/spec/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ function it_can_use_the_utility_methods()
->values()
->toArray()
->shouldReturn([1, 2, 3]);

$this
->map('\DusanKasan\Knapsack\compare')
->toArray()
Expand Down Expand Up @@ -1139,5 +1139,38 @@ function it_can_use_the_utility_methods()
->reduce('\DusanKasan\Knapsack\concatenate', '')
->shouldReturn('103122');
}

function it_can_check_if_size_is_exactly_n()
{
$this->beConstructedWith([1, 2]);
$this->sizeIs(2)->shouldReturn(true);
$this->sizeIs(3)->shouldReturn(false);
$this->sizeIs(0)->shouldReturn(false);
}

function it_can_check_if_size_is_less_than_n()
{
$this->beConstructedWith([1, 2]);
$this->sizeIsLessThan(0)->shouldReturn(false);
$this->sizeIsLessThan(2)->shouldReturn(false);
$this->sizeIsLessThan(3)->shouldReturn(true);
}

function it_can_check_if_size_is_greater_than_n()
{
$this->beConstructedWith([1, 2]);
$this->sizeIsGreaterThan(2)->shouldReturn(false);
$this->sizeIsGreaterThan(1)->shouldReturn(true);
$this->sizeIsGreaterThan(0)->shouldReturn(true);
}

function it_can_check_if_size_is_between_n_and_m()
{
$this->beConstructedWith([1, 2]);
$this->sizeIsBetween(1, 3)->shouldReturn(true);
$this->sizeIsBetween(3, 4)->shouldReturn(false);
$this->sizeIsBetween(0, 0)->shouldReturn(false);
$this->sizeIsBetween(3, 1)->shouldReturn(true);
}
}

0 comments on commit d306b61

Please sign in to comment.