| 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\Ramsey\Collection; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\Ramsey\Collection\Exception\InvalidArgumentException; |
| 16 |
use Dudlewebs\WPMCS\Ramsey\Collection\Exception\NoSuchElementException; |
| 17 |
/** |
| 18 |
* This class provides a basic implementation of `DoubleEndedQueueInterface`, to |
| 19 |
* minimize the effort required to implement this interface. |
| 20 |
* |
| 21 |
* @template T |
| 22 |
* @extends Queue<T> |
| 23 |
* @implements DoubleEndedQueueInterface<T> |
| 24 |
*/ |
| 25 |
class DoubleEndedQueue extends Queue implements DoubleEndedQueueInterface |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Index of the last element in the queue. |
| 29 |
*/ |
| 30 |
private int $tail = -1; |
| 31 |
/** |
| 32 |
* @inheritDoc |
| 33 |
*/ |
| 34 |
public function offsetSet($offset, $value): void |
| 35 |
{ |
| 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; |
| 41 |
} |
| 42 |
/** |
| 43 |
* @throws InvalidArgumentException if $element is of the wrong type |
| 44 |
* |
| 45 |
* @inheritDoc |
| 46 |
*/ |
| 47 |
public function addFirst($element): bool |
| 48 |
{ |
| 49 |
if ($this->checkType($this->getType(), $element) === \false) { |
| 50 |
throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($element)); |
| 51 |
} |
| 52 |
$this->index--; |
| 53 |
$this->data[$this->index] = $element; |
| 54 |
return \true; |
| 55 |
} |
| 56 |
/** |
| 57 |
* @inheritDoc |
| 58 |
*/ |
| 59 |
public function addLast($element): bool |
| 60 |
{ |
| 61 |
return $this->add($element); |
| 62 |
} |
| 63 |
/** |
| 64 |
* @inheritDoc |
| 65 |
*/ |
| 66 |
public function offerFirst($element): bool |
| 67 |
{ |
| 68 |
try { |
| 69 |
return $this->addFirst($element); |
| 70 |
} catch (InvalidArgumentException $e) { |
| 71 |
return \false; |
| 72 |
} |
| 73 |
} |
| 74 |
/** |
| 75 |
* @inheritDoc |
| 76 |
*/ |
| 77 |
public function offerLast($element): bool |
| 78 |
{ |
| 79 |
return $this->offer($element); |
| 80 |
} |
| 81 |
/** |
| 82 |
* @inheritDoc |
| 83 |
*/ |
| 84 |
public function removeFirst() |
| 85 |
{ |
| 86 |
return $this->remove(); |
| 87 |
} |
| 88 |
/** |
| 89 |
* @inheritDoc |
| 90 |
*/ |
| 91 |
public function removeLast() |
| 92 |
{ |
| 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; |
| 98 |
} |
| 99 |
/** |
| 100 |
* @inheritDoc |
| 101 |
*/ |
| 102 |
public function pollFirst() |
| 103 |
{ |
| 104 |
return $this->poll(); |
| 105 |
} |
| 106 |
/** |
| 107 |
* @inheritDoc |
| 108 |
*/ |
| 109 |
public function pollLast() |
| 110 |
{ |
| 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; |
| 118 |
} |
| 119 |
/** |
| 120 |
* @inheritDoc |
| 121 |
*/ |
| 122 |
public function firstElement() |
| 123 |
{ |
| 124 |
return $this->element(); |
| 125 |
} |
| 126 |
/** |
| 127 |
* @inheritDoc |
| 128 |
*/ |
| 129 |
public function lastElement() |
| 130 |
{ |
| 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]; |
| 135 |
} |
| 136 |
/** |
| 137 |
* @inheritDoc |
| 138 |
*/ |
| 139 |
public function peekFirst() |
| 140 |
{ |
| 141 |
return $this->peek(); |
| 142 |
} |
| 143 |
/** |
| 144 |
* @inheritDoc |
| 145 |
*/ |
| 146 |
public function peekLast() |
| 147 |
{ |
| 148 |
if ($this->count() === 0) { |
| 149 |
return null; |
| 150 |
} |
| 151 |
return $this->data[$this->tail]; |
| 152 |
} |
| 153 |
} |
| 154 |
|