aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/collection/src/DoubleEndedQueue.php
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2024-03-14 09:35:09 +0000
committerMario <mario@mariovavti.com>2024-03-14 09:35:09 +0000
commit6bf61dfa6b585db01b607a79bd64ec9c583a9c10 (patch)
tree78698101aa58d918568dfc0020650fc337e8d3e0 /vendor/ramsey/collection/src/DoubleEndedQueue.php
parent0e59cfb8390e4c6aee29ef73b53a4dc6b7fb581e (diff)
downloadvolse-hubzilla-6bf61dfa6b585db01b607a79bd64ec9c583a9c10.tar.gz
volse-hubzilla-6bf61dfa6b585db01b607a79bd64ec9c583a9c10.tar.bz2
volse-hubzilla-6bf61dfa6b585db01b607a79bd64ec9c583a9c10.zip
composer update and use the fixed streams php-jcs library until the floats issue will be fixed upstream. see here for reference https://codeberg.org/streams/streams/issues/151
Diffstat (limited to 'vendor/ramsey/collection/src/DoubleEndedQueue.php')
-rw-r--r--vendor/ramsey/collection/src/DoubleEndedQueue.php129
1 files changed, 54 insertions, 75 deletions
diff --git a/vendor/ramsey/collection/src/DoubleEndedQueue.php b/vendor/ramsey/collection/src/DoubleEndedQueue.php
index c9c59502d..62947a24f 100644
--- a/vendor/ramsey/collection/src/DoubleEndedQueue.php
+++ b/vendor/ramsey/collection/src/DoubleEndedQueue.php
@@ -17,6 +17,10 @@ namespace Ramsey\Collection;
use Ramsey\Collection\Exception\InvalidArgumentException;
use Ramsey\Collection\Exception\NoSuchElementException;
+use function array_key_last;
+use function array_pop;
+use function array_unshift;
+
/**
* This class provides a basic implementation of `DoubleEndedQueueInterface`, to
* minimize the effort required to implement this interface.
@@ -28,160 +32,135 @@ use Ramsey\Collection\Exception\NoSuchElementException;
class DoubleEndedQueue extends Queue implements DoubleEndedQueueInterface
{
/**
- * Index of the last element in the queue.
+ * Constructs a double-ended queue (dequeue) object of the specified type,
+ * optionally with the specified data.
*
- * @var int
- */
- private $tail = -1;
-
- /**
- * @inheritDoc
+ * @param string $queueType The type or class name associated with this dequeue.
+ * @param array<array-key, T> $data The initial items to store in the dequeue.
*/
- public function offsetSet($offset, $value): void
+ public function __construct(private readonly string $queueType, array $data = [])
{
- if ($this->checkType($this->getType(), $value) === false) {
- throw new InvalidArgumentException(
- 'Value must be of type ' . $this->getType() . '; value is '
- . $this->toolValueToString($value)
- );
- }
-
- $this->tail++;
-
- $this->data[$this->tail] = $value;
+ parent::__construct($this->queueType, $data);
}
/**
- * @inheritDoc
+ * @throws InvalidArgumentException if $element is of the wrong type
*/
- public function addFirst($element): bool
+ public function addFirst(mixed $element): bool
{
if ($this->checkType($this->getType(), $element) === false) {
throw new InvalidArgumentException(
'Value must be of type ' . $this->getType() . '; value is '
- . $this->toolValueToString($element)
+ . $this->toolValueToString($element),
);
}
- $this->index--;
-
- $this->data[$this->index] = $element;
+ array_unshift($this->data, $element);
return true;
}
/**
- * @inheritDoc
+ * @throws InvalidArgumentException if $element is of the wrong type
*/
- public function addLast($element): bool
+ public function addLast(mixed $element): bool
{
return $this->add($element);
}
- /**
- * @inheritDoc
- */
- public function offerFirst($element): bool
+ public function offerFirst(mixed $element): bool
{
try {
return $this->addFirst($element);
- } catch (InvalidArgumentException $e) {
+ } catch (InvalidArgumentException) {
return false;
}
}
- /**
- * @inheritDoc
- */
- public function offerLast($element): bool
+ public function offerLast(mixed $element): bool
{
return $this->offer($element);
}
/**
- * @inheritDoc
+ * @return T the first element in this queue.
+ *
+ * @throws NoSuchElementException if the queue is empty
*/
- public function removeFirst()
+ public function removeFirst(): mixed
{
return $this->remove();
}
/**
- * @inheritDoc
+ * @return T the last element in this queue.
+ *
+ * @throws NoSuchElementException if this queue is empty.
*/
- public function removeLast()
+ public function removeLast(): mixed
{
- $tail = $this->pollLast();
-
- if ($tail === null) {
- throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
- }
-
- return $tail;
+ return $this->pollLast() ?? throw new NoSuchElementException(
+ 'Can\'t return element from Queue. Queue is empty.',
+ );
}
/**
- * @inheritDoc
+ * @return T | null the head of this queue, or `null` if this queue is empty.
*/
- public function pollFirst()
+ public function pollFirst(): mixed
{
return $this->poll();
}
/**
- * @inheritDoc
+ * @return T | null the tail of this queue, or `null` if this queue is empty.
*/
- public function pollLast()
+ public function pollLast(): mixed
{
- if ($this->count() === 0) {
- return null;
- }
-
- $tail = $this[$this->tail];
-
- unset($this[$this->tail]);
- $this->tail--;
-
- return $tail;
+ return array_pop($this->data);
}
/**
- * @inheritDoc
+ * @return T the head of this queue.
+ *
+ * @throws NoSuchElementException if this queue is empty.
*/
- public function firstElement()
+ public function firstElement(): mixed
{
return $this->element();
}
/**
- * @inheritDoc
+ * @return T the tail of this queue.
+ *
+ * @throws NoSuchElementException if this queue is empty.
*/
- public function lastElement()
+ public function lastElement(): mixed
{
- if ($this->count() === 0) {
- throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
- }
-
- return $this->data[$this->tail];
+ return $this->peekLast() ?? throw new NoSuchElementException(
+ 'Can\'t return element from Queue. Queue is empty.',
+ );
}
/**
- * @inheritDoc
+ * @return T | null the head of this queue, or `null` if this queue is empty.
*/
- public function peekFirst()
+ public function peekFirst(): mixed
{
return $this->peek();
}
/**
- * @inheritDoc
+ * @return T | null the tail of this queue, or `null` if this queue is empty.
*/
- public function peekLast()
+ public function peekLast(): mixed
{
- if ($this->count() === 0) {
+ $lastIndex = array_key_last($this->data);
+
+ if ($lastIndex === null) {
return null;
}
- return $this->data[$this->tail];
+ return $this->data[$lastIndex];
}
}