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