Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DoubleEndedQueue.php

class DoubleEndedQueue

This class provides a basic implementation of `DoubleEndedQueueInterface`, to minimize the effort required to implement this interface.

@template T @extends Queue<T> @implements DoubleEndedQueueInterface<T>

Hierarchy

  • class \Ramsey\Collection\AbstractArray implements \Ramsey\Collection\ArrayInterface
    • class \Ramsey\Collection\Queue extends \Ramsey\Collection\AbstractArray implements \Ramsey\Collection\QueueInterface uses \Ramsey\Collection\Tool\TypeTrait, \Ramsey\Collection\Tool\ValueToStringTrait
      • class \Ramsey\Collection\DoubleEndedQueue extends \Ramsey\Collection\Queue implements \Ramsey\Collection\DoubleEndedQueueInterface

Expanded class hierarchy of DoubleEndedQueue

File

vendor/ramsey/collection/src/DoubleEndedQueue.php, line 32

Namespace

Ramsey\Collection
View source
class DoubleEndedQueue extends Queue implements DoubleEndedQueueInterface {
    
    /**
     * Constructs a double-ended queue (dequeue) object of the specified type,
     * optionally with the specified data.
     *
     * @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 __construct(string $queueType, array $data = []) {
        parent::__construct($this->queueType, $data);
    }
    
    /**
     * @throws InvalidArgumentException if $element is of the wrong type
     */
    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));
        }
        array_unshift($this->data, $element);
        return true;
    }
    
    /**
     * @throws InvalidArgumentException if $element is of the wrong type
     */
    public function addLast(mixed $element) : bool {
        return $this->add($element);
    }
    public function offerFirst(mixed $element) : bool {
        try {
            return $this->addFirst($element);
        } catch (InvalidArgumentException) {
            return false;
        }
    }
    public function offerLast(mixed $element) : bool {
        return $this->offer($element);
    }
    
    /**
     * @return T the first element in this queue.
     *
     * @throws NoSuchElementException if the queue is empty
     */
    public function removeFirst() : mixed {
        return $this->remove();
    }
    
    /**
     * @return T the last element in this queue.
     *
     * @throws NoSuchElementException if this queue is empty.
     */
    public function removeLast() : mixed {
        return $this->pollLast() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
    }
    
    /**
     * @return T | null the head of this queue, or `null` if this queue is empty.
     */
    public function pollFirst() : mixed {
        return $this->poll();
    }
    
    /**
     * @return T | null the tail of this queue, or `null` if this queue is empty.
     */
    public function pollLast() : mixed {
        return array_pop($this->data);
    }
    
    /**
     * @return T the head of this queue.
     *
     * @throws NoSuchElementException if this queue is empty.
     */
    public function firstElement() : mixed {
        return $this->element();
    }
    
    /**
     * @return T the tail of this queue.
     *
     * @throws NoSuchElementException if this queue is empty.
     */
    public function lastElement() : mixed {
        return $this->peekLast() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
    }
    
    /**
     * @return T | null the head of this queue, or `null` if this queue is empty.
     */
    public function peekFirst() : mixed {
        return $this->peek();
    }
    
    /**
     * @return T | null the tail of this queue, or `null` if this queue is empty.
     */
    public function peekLast() : mixed {
        $lastIndex = array_key_last($this->data);
        if ($lastIndex === null) {
            return null;
        }
        return $this->data[$lastIndex];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractArray::$data protected property The items of this array.
AbstractArray::clear public function Removes all items from this array. Overrides ArrayInterface::clear
AbstractArray::count public function Returns the number of items in this array.
AbstractArray::getIterator public function Returns an iterator for this array. 2
AbstractArray::isEmpty public function Returns `true` if this array is empty. Overrides ArrayInterface::isEmpty
AbstractArray::offsetExists public function Returns `true` if the given offset exists in this array.
AbstractArray::offsetGet public function Returns the value at the specified offset.
AbstractArray::offsetUnset public function Removes the given offset and its value from the array.
AbstractArray::toArray public function @inheritDoc Overrides ArrayInterface::toArray 1
AbstractArray::__serialize public function Returns data suitable for PHP serialization. 1
AbstractArray::__unserialize public function Adds unserialized data to the object.
DoubleEndedQueue::addFirst public function Overrides DoubleEndedQueueInterface::addFirst
DoubleEndedQueue::addLast public function Overrides DoubleEndedQueueInterface::addLast
DoubleEndedQueue::firstElement public function Overrides DoubleEndedQueueInterface::firstElement
DoubleEndedQueue::lastElement public function Overrides DoubleEndedQueueInterface::lastElement
DoubleEndedQueue::offerFirst public function Inserts the specified element at the front of this queue if it is
possible to do so immediately without violating capacity restrictions.
Overrides DoubleEndedQueueInterface::offerFirst
DoubleEndedQueue::offerLast public function Inserts the specified element at the end of this queue if it is possible
to do so immediately without violating capacity restrictions.
Overrides DoubleEndedQueueInterface::offerLast
DoubleEndedQueue::peekFirst public function Overrides DoubleEndedQueueInterface::peekFirst
DoubleEndedQueue::peekLast public function Overrides DoubleEndedQueueInterface::peekLast
DoubleEndedQueue::pollFirst public function Overrides DoubleEndedQueueInterface::pollFirst
DoubleEndedQueue::pollLast public function Overrides DoubleEndedQueueInterface::pollLast
DoubleEndedQueue::removeFirst public function Overrides DoubleEndedQueueInterface::removeFirst
DoubleEndedQueue::removeLast public function Overrides DoubleEndedQueueInterface::removeLast
DoubleEndedQueue::__construct public function Constructs a double-ended queue (dequeue) object of the specified type,
optionally with the specified data.
Overrides Queue::__construct
Queue::add public function Overrides QueueInterface::add
Queue::element public function Overrides QueueInterface::element
Queue::getType public function Returns the type associated with this queue. Overrides QueueInterface::getType
Queue::offer public function Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions.
Overrides QueueInterface::offer
Queue::offsetSet public function Since arbitrary offsets may not be manipulated in a queue, this method
serves only to fulfill the `ArrayAccess` interface requirements. It is
invoked by other operations when adding values to the queue.
Overrides AbstractArray::offsetSet
Queue::peek public function Overrides QueueInterface::peek
Queue::poll public function Overrides QueueInterface::poll
Queue::remove public function Overrides QueueInterface::remove
TypeTrait::checkType protected function Returns `true` if value is of the specified type.
ValueToStringTrait::toolValueToString protected function Returns a string representation of the value.
RSS feed
Powered by Drupal