Queue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace Mbh\Collection;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use Mbh\Interfaces\Allocated as AllocatedInterface;
13
use Traversable;
14
use ArrayAccess;
15
use IteratorAggregate;
16
use Error;
17
use OutOfBoundsException;
18
19
/**
20
 * A “first in, first out” or “FIFO” collection that only allows access to the
21
 * value at the front of the queue and iterates in that order, destructively.
22
 *
23
 * @package structures
24
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
25
 */
26
27
class Queue implements AllocatedInterface, ArrayAccess, CollectionInterface, IteratorAggregate
28
{
29
    use Traits\Collection;
30
31
    const MIN_CAPACITY = 8;
32
33
    /**
34
     * @var Deque internal deque to store values.
35
     */
36
    protected $deque;
37
38
    /**
39
     * Creates an instance using the values of an array or Traversable object.
40
     *
41
     * @param array|Traversable $values
42
     */
43
    public function __construct($values = [])
44
    {
45
        $this->deque = Deque::empty();
46
47
        $this->pushAll($values);
48
    }
49
50
    /**
51
     * Ensures that enough memory is allocated for a specified capacity. This
52
     * potentially reduces the number of reallocations as the size increases.
53
     *
54
     * @param int $capacity The number of values for which capacity should be
55
     *                      allocated. Capacity will stay the same if this value
56
     *                      is less than or equal to the current capacity.
57
     */
58
    public function allocate(int $capacity)
59
    {
60
        $this->deque->allocate($capacity);
61
    }
62
63
    /**
64
     * Returns the current capacity of the queue.
65
     *
66
     * @return int
67
     */
68
    public function capacity(): int
69
    {
70
        return $this->deque->capacity();
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function clear()
77
    {
78
        $this->deque->clear();
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function copy()
85
    {
86
        return new self($this->deque);
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function count(): int
93
    {
94
        return count($this->deque);
95
    }
96
97
    /**
98
     * Returns the value at the front of the queue without removing it.
99
     *
100
     * @return
101
     */
102
    public function peek()
103
    {
104
        return $this->deque->first();
105
    }
106
107
    /**
108
     * Returns and removes the value at the front of the Queue.
109
     *
110
     * @return mixed
111
     */
112
    public function pop()
113
    {
114
        return $this->deque->shift();
115
    }
116
117
    /**
118
     * Pushes zero or more values into the front of the queue.
119
     *
120
     * @param mixed ...$values
121
     */
122
    public function push(...$values)
123
    {
124
        $this->deque->push(...$values);
125
    }
126
127
    /**
128
     * Creates associations for all keys and corresponding values of either an
129
     * array or iterable object.
130
     *
131
     * @param Traversable|array $values
132
     */
133
    protected function pushAll($values)
134
    {
135
        foreach ($values as &$value) {
136
            $this[] = $value;
137
        }
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function toArray(): array
144
    {
145
        return $this->deque->toArray();
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151
    public function unserialize($values)
152
    {
153
        $values = unserialize($values);
154
        $this->deque = Deque::fromArray($values);
155
    }
156
157
    /**
158
     *
159
     */
160
    public function getIterator()
161
    {
162
        while (!$this->isEmpty()) {
163
            yield $this->pop();
164
        }
165
    }
166
167
    /**
168
     * @inheritdoc
169
     *
170
     * @throws OutOfBoundsException
171
     */
172
    public function offsetSet($offset, $value)
173
    {
174
        if ($offset === null) {
175
            $this->push($value);
176
        } else {
177
            throw new OutOfBoundsException();
178
        }
179
    }
180
181
    /**
182
     * @inheritdoc
183
     *
184
     * @throws Error
185
     */
186
    public function offsetGet($offset)
187
    {
188
        throw new Error();
189
    }
190
191
    /**
192
     * @inheritdoc
193
     *
194
     * @throws Error
195
     */
196
    public function offsetUnset($offset)
197
    {
198
        throw new Error();
199
    }
200
201
    /**
202
     * @inheritdoc
203
     *
204
     * @throws Error
205
     */
206
    public function offsetExists($offset)
207
    {
208
        throw new Error();
209
    }
210
}
211