← All changes
|
includes/sdk/google/ramsey/collection/src/DoubleEndedQueue.php
+50
-64
1.2.10
→
1.4.2
View file →
| @@ -9,12 +9,15 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Collection; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection; | |
| 14 | 14 | |
| 15 | -use Dudlewebs\WPMCS\Ramsey\Collection\Exception\InvalidArgumentException; | |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Collection\Exception\NoSuchElementException; | |
| 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; | |
| 17 | 20 | /** |
| 18 | 21 | * This class provides a basic implementation of `DoubleEndedQueueInterface`, to |
| 19 | 22 | * minimize the effort required to implement this interface. |
| 20 | 23 | * |
| @@ -24,130 +27,113 @@ | ||
| 24 | 27 | */ |
| 25 | 28 | class DoubleEndedQueue extends Queue implements DoubleEndedQueueInterface |
| 26 | 29 | { |
| 27 | 30 | /** |
| 28 | - * Index of the last element in the queue. | |
| 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. | |
| 29 | 36 | */ |
| 30 | - private int $tail = -1; | |
| 31 | - /** | |
| 32 | - * @inheritDoc | |
| 33 | - */ | |
| 34 | - public function offsetSet($offset, $value): void | |
| 37 | + public function __construct(private readonly string $queueType, array $data = []) | |
| 35 | 38 | { |
| 36 | - if ($this->checkType($this->getType(), $value) === \false) { | |
| 37 | - throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($value)); | |
| 38 | - } | |
| 39 | - $this->tail++; | |
| 40 | - $this->data[$this->tail] = $value; | |
| 39 | + parent::__construct($this->queueType, $data); | |
| 41 | 40 | } |
| 42 | 41 | /** |
| 43 | 42 | * @throws InvalidArgumentException if $element is of the wrong type |
| 44 | - * | |
| 45 | - * @inheritDoc | |
| 46 | 43 | */ |
| 47 | - public function addFirst($element): bool | |
| 44 | + public function addFirst(mixed $element) : bool | |
| 48 | 45 | { |
| 49 | 46 | if ($this->checkType($this->getType(), $element) === \false) { |
| 50 | 47 | throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($element)); |
| 51 | 48 | } |
| 52 | - $this->index--; | |
| 53 | - $this->data[$this->index] = $element; | |
| 49 | + array_unshift($this->data, $element); | |
| 54 | 50 | return \true; |
| 55 | 51 | } |
| 56 | 52 | /** |
| 57 | - * @inheritDoc | |
| 53 | + * @throws InvalidArgumentException if $element is of the wrong type | |
| 58 | 54 | */ |
| 59 | - public function addLast($element): bool | |
| 55 | + public function addLast(mixed $element) : bool | |
| 60 | 56 | { |
| 61 | 57 | return $this->add($element); |
| 62 | 58 | } |
| 63 | - /** | |
| 64 | - * @inheritDoc | |
| 65 | - */ | |
| 66 | - public function offerFirst($element): bool | |
| 59 | + public function offerFirst(mixed $element) : bool | |
| 67 | 60 | { |
| 68 | 61 | try { |
| 69 | 62 | return $this->addFirst($element); |
| 70 | - } catch (InvalidArgumentException $e) { | |
| 63 | + } catch (InvalidArgumentException) { | |
| 71 | 64 | return \false; |
| 72 | 65 | } |
| 73 | 66 | } |
| 74 | - /** | |
| 75 | - * @inheritDoc | |
| 76 | - */ | |
| 77 | - public function offerLast($element): bool | |
| 67 | + public function offerLast(mixed $element) : bool | |
| 78 | 68 | { |
| 79 | 69 | return $this->offer($element); |
| 80 | 70 | } |
| 81 | 71 | /** |
| 82 | - * @inheritDoc | |
| 72 | + * @return T the first element in this queue. | |
| 73 | + * | |
| 74 | + * @throws NoSuchElementException if the queue is empty | |
| 83 | 75 | */ |
| 84 | - public function removeFirst() | |
| 76 | + public function removeFirst() : mixed | |
| 85 | 77 | { |
| 86 | 78 | return $this->remove(); |
| 87 | 79 | } |
| 88 | 80 | /** |
| 89 | - * @inheritDoc | |
| 81 | + * @return T the last element in this queue. | |
| 82 | + * | |
| 83 | + * @throws NoSuchElementException if this queue is empty. | |
| 90 | 84 | */ |
| 91 | - public function removeLast() | |
| 85 | + public function removeLast() : mixed | |
| 92 | 86 | { |
| 93 | - $tail = $this->pollLast(); | |
| 94 | - if ($tail === null) { | |
| 95 | - throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 96 | - } | |
| 97 | - return $tail; | |
| 87 | + return $this->pollLast() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 98 | 88 | } |
| 99 | 89 | /** |
| 100 | - * @inheritDoc | |
| 90 | + * @return T | null the head of this queue, or `null` if this queue is empty. | |
| 101 | 91 | */ |
| 102 | - public function pollFirst() | |
| 92 | + public function pollFirst() : mixed | |
| 103 | 93 | { |
| 104 | 94 | return $this->poll(); |
| 105 | 95 | } |
| 106 | 96 | /** |
| 107 | - * @inheritDoc | |
| 97 | + * @return T | null the tail of this queue, or `null` if this queue is empty. | |
| 108 | 98 | */ |
| 109 | - public function pollLast() | |
| 99 | + public function pollLast() : mixed | |
| 110 | 100 | { |
| 111 | - if ($this->count() === 0) { | |
| 112 | - return null; | |
| 113 | - } | |
| 114 | - $tail = $this[$this->tail]; | |
| 115 | - unset($this[$this->tail]); | |
| 116 | - $this->tail--; | |
| 117 | - return $tail; | |
| 101 | + return array_pop($this->data); | |
| 118 | 102 | } |
| 119 | 103 | /** |
| 120 | - * @inheritDoc | |
| 104 | + * @return T the head of this queue. | |
| 105 | + * | |
| 106 | + * @throws NoSuchElementException if this queue is empty. | |
| 121 | 107 | */ |
| 122 | - public function firstElement() | |
| 108 | + public function firstElement() : mixed | |
| 123 | 109 | { |
| 124 | 110 | return $this->element(); |
| 125 | 111 | } |
| 126 | 112 | /** |
| 127 | - * @inheritDoc | |
| 113 | + * @return T the tail of this queue. | |
| 114 | + * | |
| 115 | + * @throws NoSuchElementException if this queue is empty. | |
| 128 | 116 | */ |
| 129 | - public function lastElement() | |
| 117 | + public function lastElement() : mixed | |
| 130 | 118 | { |
| 131 | - if ($this->count() === 0) { | |
| 132 | - throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 133 | - } | |
| 134 | - return $this->data[$this->tail]; | |
| 119 | + return $this->peekLast() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 135 | 120 | } |
| 136 | 121 | /** |
| 137 | - * @inheritDoc | |
| 122 | + * @return T | null the head of this queue, or `null` if this queue is empty. | |
| 138 | 123 | */ |
| 139 | - public function peekFirst() | |
| 124 | + public function peekFirst() : mixed | |
| 140 | 125 | { |
| 141 | 126 | return $this->peek(); |
| 142 | 127 | } |
| 143 | 128 | /** |
| 144 | - * @inheritDoc | |
| 129 | + * @return T | null the tail of this queue, or `null` if this queue is empty. | |
| 145 | 130 | */ |
| 146 | - public function peekLast() | |
| 131 | + public function peekLast() : mixed | |
| 147 | 132 | { |
| 148 | - if ($this->count() === 0) { | |
| 133 | + $lastIndex = array_key_last($this->data); | |
| 134 | + if ($lastIndex === null) { | |
| 149 | 135 | return null; |
| 150 | 136 | } |
| 151 | - return $this->data[$this->tail]; | |
| 137 | + return $this->data[$lastIndex]; | |
| 152 | 138 | } |
| 153 | 139 | } |