Exception
3 years ago
Map
3 years ago
Tool
3 years ago
AbstractArray.php
3 years ago
AbstractCollection.php
3 years ago
AbstractSet.php
3 years ago
ArrayInterface.php
3 years ago
Collection.php
3 years ago
CollectionInterface.php
3 years ago
DoubleEndedQueue.php
3 years ago
DoubleEndedQueueInterface.php
3 years ago
GenericArray.php
3 years ago
Queue.php
3 years ago
QueueInterface.php
3 years ago
Set.php
3 years ago
DoubleEndedQueue.php
188 lines
| 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 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Collection; |
| 16 | |
| 17 | use Ramsey\Collection\Exception\InvalidArgumentException; |
| 18 | use Ramsey\Collection\Exception\NoSuchElementException; |
| 19 | |
| 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 | * Index of the last element in the queue. |
| 32 | * |
| 33 | * @var int |
| 34 | */ |
| 35 | private $tail = -1; |
| 36 | |
| 37 | /** |
| 38 | * @inheritDoc |
| 39 | */ |
| 40 | public function offsetSet($offset, $value): void |
| 41 | { |
| 42 | if ($this->checkType($this->getType(), $value) === false) { |
| 43 | throw new InvalidArgumentException( |
| 44 | 'Value must be of type ' . $this->getType() . '; value is ' |
| 45 | . $this->toolValueToString($value) |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | $this->tail++; |
| 50 | |
| 51 | $this->data[$this->tail] = $value; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @inheritDoc |
| 56 | */ |
| 57 | public function addFirst($element): bool |
| 58 | { |
| 59 | if ($this->checkType($this->getType(), $element) === false) { |
| 60 | throw new InvalidArgumentException( |
| 61 | 'Value must be of type ' . $this->getType() . '; value is ' |
| 62 | . $this->toolValueToString($element) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | $this->index--; |
| 67 | |
| 68 | $this->data[$this->index] = $element; |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @inheritDoc |
| 75 | */ |
| 76 | public function addLast($element): bool |
| 77 | { |
| 78 | return $this->add($element); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @inheritDoc |
| 83 | */ |
| 84 | public function offerFirst($element): bool |
| 85 | { |
| 86 | try { |
| 87 | return $this->addFirst($element); |
| 88 | } catch (InvalidArgumentException $e) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @inheritDoc |
| 95 | */ |
| 96 | public function offerLast($element): bool |
| 97 | { |
| 98 | return $this->offer($element); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @inheritDoc |
| 103 | */ |
| 104 | public function removeFirst() |
| 105 | { |
| 106 | return $this->remove(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @inheritDoc |
| 111 | */ |
| 112 | public function removeLast() |
| 113 | { |
| 114 | $tail = $this->pollLast(); |
| 115 | |
| 116 | if ($tail === null) { |
| 117 | throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 118 | } |
| 119 | |
| 120 | return $tail; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @inheritDoc |
| 125 | */ |
| 126 | public function pollFirst() |
| 127 | { |
| 128 | return $this->poll(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @inheritDoc |
| 133 | */ |
| 134 | public function pollLast() |
| 135 | { |
| 136 | if ($this->count() === 0) { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | $tail = $this[$this->tail]; |
| 141 | |
| 142 | unset($this[$this->tail]); |
| 143 | $this->tail--; |
| 144 | |
| 145 | return $tail; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @inheritDoc |
| 150 | */ |
| 151 | public function firstElement() |
| 152 | { |
| 153 | return $this->element(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @inheritDoc |
| 158 | */ |
| 159 | public function lastElement() |
| 160 | { |
| 161 | if ($this->count() === 0) { |
| 162 | throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 163 | } |
| 164 | |
| 165 | return $this->data[$this->tail]; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @inheritDoc |
| 170 | */ |
| 171 | public function peekFirst() |
| 172 | { |
| 173 | return $this->peek(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @inheritDoc |
| 178 | */ |
| 179 | public function peekLast() |
| 180 | { |
| 181 | if ($this->count() === 0) { |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | return $this->data[$this->tail]; |
| 186 | } |
| 187 | } |
| 188 |