-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from KaririCode-Framework/develop
feat: Refactor ArrayQueue, Create CircularArrayQueue, Refactor ArrayD…
- Loading branch information
Showing
6 changed files
with
203 additions
and
140 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KaririCode\DataStructure\Queue; | ||
|
||
use KaririCode\Contract\DataStructure\Queue; | ||
|
||
/** | ||
* CircularArrayQueue implementation. | ||
* | ||
* This class provides the common functionality for array-based queues using a circular array. | ||
* | ||
* @category Queues | ||
* | ||
* @author Walmir Silva <walmir.silva@kariricode.org> | ||
* @license MIT | ||
* | ||
* @see https://kariricode.org/ | ||
*/ | ||
abstract class CircularArrayQueue implements Queue | ||
{ | ||
protected array $elements; | ||
protected int $front = 0; | ||
protected int $size = 0; | ||
protected int $capacity; | ||
|
||
public function __construct(int $initialCapacity = 16) | ||
{ | ||
$this->capacity = $initialCapacity; | ||
$this->elements = array_fill(0, $this->capacity, null); | ||
} | ||
|
||
public function isEmpty(): bool | ||
{ | ||
return $this->size === 0; | ||
} | ||
|
||
public function size(): int | ||
{ | ||
return $this->size; | ||
} | ||
|
||
public function clear(): void | ||
{ | ||
$this->elements = array_fill(0, $this->capacity, null); | ||
$this->front = 0; | ||
$this->size = 0; | ||
} | ||
|
||
protected function ensureCapacity(): void | ||
{ | ||
if ($this->size === $this->capacity) { | ||
$newCapacity = $this->capacity * 2; | ||
$newElements = array_fill(0, $newCapacity, null); | ||
for ($i = 0; $i < $this->size; ++$i) { | ||
$newElements[$i] = $this->elements[($this->front + $i) % $this->capacity]; | ||
} | ||
$this->elements = $newElements; | ||
$this->front = 0; | ||
$this->capacity = $newCapacity; | ||
} | ||
} | ||
|
||
public function peek(): mixed | ||
{ | ||
return $this->isEmpty() ? null : $this->elements[$this->front]; | ||
} | ||
|
||
public function dequeue(): mixed | ||
{ | ||
if ($this->isEmpty()) { | ||
return null; | ||
} | ||
$element = $this->elements[$this->front]; | ||
$this->elements[$this->front] = null; | ||
$this->front = ($this->front + 1) % $this->capacity; | ||
--$this->size; | ||
|
||
return $element; | ||
} | ||
|
||
public function enqueue(mixed $element): void | ||
{ | ||
$this->ensureCapacity(); | ||
$index = ($this->front + $this->size) % $this->capacity; | ||
$this->elements[$index] = $element; | ||
++$this->size; | ||
} | ||
|
||
public function getItems(): array | ||
{ | ||
$items = []; | ||
for ($i = 0; $i < $this->size; ++$i) { | ||
$items[] = $this->elements[($this->front + $i) % $this->capacity]; | ||
} | ||
return $items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.