-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeque.php
More file actions
36 lines (32 loc) · 1.16 KB
/
Deque.php
File metadata and controls
36 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace Altair\Structure;
use Altair\Structure\Contracts\CapacityInterface;
use Altair\Structure\Contracts\SequenceInterface;
use ArrayAccess;
use IteratorAggregate;
/**
* Deque.
*
* A Deque ("deck") is a Sequence of values in a contiguous buffer that grows and shrinks automatically. The name is a
* common abbreviation of "double-ended queue" and is used internally by `Altair\Structure\Queue`.
*
* Two pointers are used to keep track of a head and a tail. The pointers can "wrap around" the end of the buffer, which
* avoids to the need to move other values around to make room.
*
* Accessing a value by index requires a translation between the index and its corresponding position in the buffer:
* ((head + position) % capacity).
*
* @link https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd#.gl62k1xqr
*/
class Deque implements IteratorAggregate, ArrayAccess, SequenceInterface, CapacityInterface
{
use Traits\SequenceTrait;
use Traits\SquaredCapacityTrait;
/**
* @inheritdoc
*/
public function __construct($values = null)
{
$this->pushAll($this->normalizeItems($values ?? []));
}
}