Skip to content

Commit

Permalink
Reverse iterate over items when removing to take advantage of sort or…
Browse files Browse the repository at this point in the history
…der. #182
  • Loading branch information
dvdoug committed Dec 21, 2019
1 parent 0fef056 commit 16ff9bd
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/ItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ public function insert(Item $item): void
*/
public function remove(Item $item): void
{
foreach ($this->list as $key => $itemToCheck) {
if ($itemToCheck === $item) {
unset($this->list[$key]);
break;
}
if (!$this->isSorted) {
usort($this->list, [$this, 'compare']);
$this->isSorted = true;
}

end($this->list);
do {
if (current($this->list) === $item) {
unset($this->list[key($this->list)]);
return;
}
} while (prev($this->list) !== false);

}

/**
Expand Down

0 comments on commit 16ff9bd

Please sign in to comment.