| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/collection library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidArgumentException; |
| 16 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\NoSuchElementException; |
| 17 |
use function array_key_last; |
| 18 |
use function array_pop; |
| 19 |
use function array_unshift; |
| 20 |
/** |
| 21 |
* This class provides a basic implementation of `DoubleEndedQueueInterface`, to |
| 22 |
* minimize the effort required to implement this interface. |
| 23 |
* |
| 24 |
* @template T |
| 25 |
* @extends Queue<T> |
| 26 |
* @implements DoubleEndedQueueInterface<T> |
| 27 |
*/ |
| 28 |
class DoubleEndedQueue extends Queue implements DoubleEndedQueueInterface |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Constructs a double-ended queue (dequeue) object of the specified type, |
| 32 |
* optionally with the specified data. |
| 33 |
* |
| 34 |
* @param string $queueType The type or class name associated with this dequeue. |
| 35 |
* @param array<array-key, T> $data The initial items to store in the dequeue. |
| 36 |
*/ |
| 37 |
public function __construct(private readonly string $queueType, array $data = []) |
| 38 |
{ |
| 39 |
parent::__construct($this->queueType, $data); |
| 40 |
} |
| 41 |
/** |
| 42 |
* @throws InvalidArgumentException if $element is of the wrong type |
| 43 |
*/ |
| 44 |
public function addFirst(mixed $element) : bool |
| 45 |
{ |
| 46 |
if ($this->checkType($this->getType(), $element) === \false) { |
| 47 |
throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($element)); |
| 48 |
} |
| 49 |
array_unshift($this->data, $element); |
| 50 |
return \true; |
| 51 |
} |
| 52 |
/** |
| 53 |
* @throws InvalidArgumentException if $element is of the wrong type |
| 54 |
*/ |
| 55 |
public function addLast(mixed $element) : bool |
| 56 |
{ |
| 57 |
return $this->add($element); |
| 58 |
} |
| 59 |
public function offerFirst(mixed $element) : bool |
| 60 |
{ |
| 61 |
try { |
| 62 |
return $this->addFirst($element); |
| 63 |
} catch (InvalidArgumentException) { |
| 64 |
return \false; |
| 65 |
} |
| 66 |
} |
| 67 |
public function offerLast(mixed $element) : bool |
| 68 |
{ |
| 69 |
return $this->offer($element); |
| 70 |
} |
| 71 |
/** |
| 72 |
* @return T the first element in this queue. |
| 73 |
* |
| 74 |
* @throws NoSuchElementException if the queue is empty |
| 75 |
*/ |
| 76 |
public function removeFirst() : mixed |
| 77 |
{ |
| 78 |
return $this->remove(); |
| 79 |
} |
| 80 |
/** |
| 81 |
* @return T the last element in this queue. |
| 82 |
* |
| 83 |
* @throws NoSuchElementException if this queue is empty. |
| 84 |
*/ |
| 85 |
public function removeLast() : mixed |
| 86 |
{ |
| 87 |
return $this->pollLast() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 88 |
} |
| 89 |
/** |
| 90 |
* @return T | null the head of this queue, or `null` if this queue is empty. |
| 91 |
*/ |
| 92 |
public function pollFirst() : mixed |
| 93 |
{ |
| 94 |
return $this->poll(); |
| 95 |
} |
| 96 |
/** |
| 97 |
* @return T | null the tail of this queue, or `null` if this queue is empty. |
| 98 |
*/ |
| 99 |
public function pollLast() : mixed |
| 100 |
{ |
| 101 |
return array_pop($this->data); |
| 102 |
} |
| 103 |
/** |
| 104 |
* @return T the head of this queue. |
| 105 |
* |
| 106 |
* @throws NoSuchElementException if this queue is empty. |
| 107 |
*/ |
| 108 |
public function firstElement() : mixed |
| 109 |
{ |
| 110 |
return $this->element(); |
| 111 |
} |
| 112 |
/** |
| 113 |
* @return T the tail of this queue. |
| 114 |
* |
| 115 |
* @throws NoSuchElementException if this queue is empty. |
| 116 |
*/ |
| 117 |
public function lastElement() : mixed |
| 118 |
{ |
| 119 |
return $this->peekLast() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 120 |
} |
| 121 |
/** |
| 122 |
* @return T | null the head of this queue, or `null` if this queue is empty. |
| 123 |
*/ |
| 124 |
public function peekFirst() : mixed |
| 125 |
{ |
| 126 |
return $this->peek(); |
| 127 |
} |
| 128 |
/** |
| 129 |
* @return T | null the tail of this queue, or `null` if this queue is empty. |
| 130 |
*/ |
| 131 |
public function peekLast() : mixed |
| 132 |
{ |
| 133 |
$lastIndex = array_key_last($this->data); |
| 134 |
if ($lastIndex === null) { |
| 135 |
return null; |
| 136 |
} |
| 137 |
return $this->data[$lastIndex]; |
| 138 |
} |
| 139 |
} |
| 140 |
|