PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.8.1
Defender Security – Malware Scanner, Login Security & Firewall v5.8.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / ramsey / collection / src / DoubleEndedQueue.php

DoubleEndedQueue.php in Defender Security – Malware Scanner, Login Security & Firewall 5.8.1, at vendor/ramsey/collection/src/DoubleEndedQueue.php

188 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 <[email protected]>
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 private int $tail = -1;
34
35 /**
36 * @inheritDoc
37 */
38 public function offsetSet($offset, $value): void
39 {
40 if ($this->checkType($this->getType(), $value) === false) {
41 throw new InvalidArgumentException(
42 'Value must be of type ' . $this->getType() . '; value is '
43 . $this->toolValueToString($value),
44 );
45 }
46
47 $this->tail++;
48
49 $this->data[$this->tail] = $value;
50 }
51
52 /**
53 * @throws InvalidArgumentException if $element is of the wrong type
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