Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/performance-audit' of https://github.com/EvanDo…
Browse files Browse the repository at this point in the history
  • Loading branch information
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/SplPriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function insert($datum, $priority)
parent::insert($datum, $priority);
}


/**
* Serialize to an array
*
Expand All @@ -55,46 +56,27 @@ public function insert($datum, $priority)
*/
public function toArray()
{
$this->setExtractFlags(self::EXTR_BOTH);
$array = array();
while ($this->valid()) {
$array[] = $this->current();
$this->next();
}
$this->setExtractFlags(self::EXTR_DATA);

// Iterating through a priority queue removes items
foreach ($array as $item) {
$this->insert($item['data'], $item['priority']);
}

// Return only the data
$return = array();
foreach ($array as $item) {
$return[] = $item['data'];
foreach (clone $this as $item) {
$array[] = $item;
}

return $return;
return $array;
}


/**
* Serialize
*
* @return string
*/
public function serialize()
{
$data = array();
$this->setExtractFlags(self::EXTR_BOTH);
while ($this->valid()) {
$data[] = $this->current();
$this->next();
}
$this->setExtractFlags(self::EXTR_DATA);
$clone = clone $this;
$clone->setExtractFlags(self::EXTR_BOTH);

// Iterating through a priority queue removes items
foreach ($data as $item) {
$this->insert($item['data'], $item['priority']);
$data = array();
foreach ($clone as $item) {
$data[] = $item;
}

return serialize($data);
Expand Down

0 comments on commit 4de87f2

Please sign in to comment.