PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.13
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.13
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / vendor / ramsey / collection / src / DoubleEndedQueue.php

DoubleEndedQueue.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.13, 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 <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 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