Code Duplication    Length = 161-164 lines in 2 locations

src/Queue.php 1 location

@@ 10-170 (lines=161) @@
7
 * Queue
8
 * @package Ds
9
 */
10
final class Queue implements \IteratorAggregate, \ArrayAccess, Collection
11
{
12
    use Traits\Collection;
13
14
    const MIN_CAPACITY = 8;
15
16
    /**
17
     * @var Deque
18
     */
19
    private $internal;
20
21
    /**
22
     * Creates an instance using the values of an array or Traversable object.
23
     *
24
     * @param array|\Traversable $values
25
     */
26
    public function __construct($values = null)
27
    {
28
        $this->internal = new Deque($values ?: []);
29
    }
30
31
    /**
32
     * Ensures that enough memory is allocated for a specified capacity. This
33
     * potentially reduces the number of reallocations as the size increases.
34
     *
35
     * @param int $capacity The number of values for which capacity should be
36
     *                      allocated. Capacity will stay the same if this value
37
     *                      is less than or equal to the current capacity.
38
     */
39
    public function allocate(int $capacity)
40
    {
41
        $this->internal->allocate($capacity);
42
    }
43
44
    /**
45
     * Returns the current capacity of the queue.
46
     *
47
     * @return int
48
     */
49
    public function capacity(): int
50
    {
51
        return $this->internal->capacity();
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function clear()
58
    {
59
        $this->internal->clear();
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function copy(): \Ds\Collection
66
    {
67
        return new self($this->internal);
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function count(): int
74
    {
75
        return count($this->internal);
76
    }
77
78
    /**
79
     * Returns the value at the front of the queue without removing it.
80
     *
81
     * @return
82
     */
83
    public function peek()
84
    {
85
        return $this->internal->first();
86
    }
87
88
    /**
89
     * Returns and removes the value at the front of the Queue.
90
     *
91
     * @return mixed
92
     */
93
    public function pop()
94
    {
95
        return $this->internal->shift();
96
    }
97
98
    /**
99
     * Pushes zero or more values into the front of the queue.
100
     *
101
     * @param mixed ...$values
102
     */
103
    public function push(...$values)
104
    {
105
        $this->internal->push(...$values);
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function toArray(): array
112
    {
113
        return $this->internal->toArray();
114
    }
115
116
    /**
117
     * Get iterator
118
     */
119
    public function getIterator()
120
    {
121
        while ( ! $this->isEmpty()) {
122
            yield $this->pop();
123
        }
124
    }
125
126
127
    /**
128
     * @inheritdoc
129
     *
130
     * @throws OutOfBoundsException
131
     */
132
    public function offsetSet($offset, $value)
133
    {
134
        if ($offset === null) {
135
            $this->push($value);
136
        } else {
137
            throw new OutOfBoundsException();
138
        }
139
    }
140
141
    /**
142
     * @inheritdoc
143
     *
144
     * @throws Error
145
     */
146
    public function offsetGet($offset)
147
    {
148
        throw new Error();
149
    }
150
151
    /**
152
     * @inheritdoc
153
     *
154
     * @throws Error
155
     */
156
    public function offsetUnset($offset)
157
    {
158
        throw new Error();
159
    }
160
161
    /**
162
     * @inheritdoc
163
     *
164
     * @throws Error
165
     */
166
    public function offsetExists($offset)
167
    {
168
        throw new Error();
169
    }
170
}
171

src/Stack.php 1 location

@@ 12-175 (lines=164) @@
9
 *
10
 * @package Ds
11
 */
12
final class Stack implements \IteratorAggregate, \ArrayAccess, Collection
13
{
14
    use Traits\Collection;
15
16
    /**
17
     * @var Vector
18
     */
19
    private $internal;
20
21
    /**
22
     * Creates an instance using the values of an array or Traversable object.
23
     *
24
     * @param array|\Traversable $values
25
     */
26
    public function __construct($values = null)
27
    {
28
        $this->internal = new Vector($values ?: []);
29
    }
30
31
    /**
32
     * Clear all elements in the Stack
33
     */
34
    public function clear()
35
    {
36
        $this->internal->clear();
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function copy(): Collection
43
    {
44
        return new self($this->internal);
45
    }
46
47
    /**
48
     * Returns the number of elements in the Stack
49
     *
50
     * @return int
51
     */
52
    public function count(): int
53
    {
54
        return count($this->internal);
55
    }
56
57
    /**
58
     * Ensures that enough memory is allocated for a specified capacity. This
59
     * potentially reduces the number of reallocations as the size increases.
60
     *
61
     * @param int $capacity The number of values for which capacity should be
62
     *                      allocated. Capacity will stay the same if this value
63
     *                      is less than or equal to the current capacity.
64
     */
65
    public function allocate(int $capacity)
66
    {
67
        $this->internal->allocate($capacity);
68
    }
69
70
    /**
71
     * Returns the current capacity of the stack.
72
     *
73
     * @return int
74
     */
75
    public function capacity(): int
76
    {
77
        return $this->internal->capacity();
78
    }
79
80
    /**
81
     * Returns the value at the top of the stack without removing it.
82
     *
83
     * @return mixed
84
     *
85
     * @throws UnderflowException if the stack is empty.
86
     */
87
    public function peek()
88
    {
89
        return $this->internal->last();
90
    }
91
92
    /**
93
     * Returns and removes the value at the top of the stack.
94
     *
95
     * @return mixed
96
     *
97
     * @throws UnderflowException if the stack is empty.
98
     */
99
    public function pop()
100
    {
101
        return $this->internal->pop();
102
    }
103
104
    /**
105
     * Pushes zero or more values onto the top of the stack.
106
     *
107
     * @param mixed ...$values
108
     */
109
    public function push(...$values)
110
    {
111
        $this->internal->push(...$values);
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function toArray(): array
118
    {
119
        return array_reverse($this->internal->toArray());
120
    }
121
122
    /**
123
     *
124
     */
125
    public function getIterator()
126
    {
127
        while ( ! $this->isEmpty()) {
128
            yield $this->pop();
129
        }
130
    }
131
132
    /**
133
     * @inheritdoc
134
     *
135
     * @throws OutOfBoundsException
136
     */
137
    public function offsetSet($offset, $value)
138
    {
139
        if ($offset === null) {
140
            $this->push($value);
141
        } else {
142
            throw new OutOfBoundsException();
143
        }
144
    }
145
146
    /**
147
     * @inheritdoc
148
     *
149
     * @throws Error
150
     */
151
    public function offsetGet($offset)
152
    {
153
        throw new Error();
154
    }
155
156
    /**
157
     * @inheritdoc
158
     *
159
     * @throws Error
160
     */
161
    public function offsetUnset($offset)
162
    {
163
        throw new Error();
164
    }
165
166
    /**
167
     * @inheritdoc
168
     *
169
     * @throws Error
170
     */
171
    public function offsetExists($offset)
172
    {
173
        throw new Error();
174
    }
175
}
176