| @@ 26-200 (lines=175) @@ | ||
| 23 | * @author Ulises Jeremias Cornejo Fandos <[email protected]> |
|
| 24 | */ |
|
| 25 | ||
| 26 | class Queue implements ArrayAccess, CollectionInterface, IteratorAggregate |
|
| 27 | { |
|
| 28 | use Traits\Collection; |
|
| 29 | ||
| 30 | const MIN_CAPACITY = 8; |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @var Deque internal deque to store values. |
|
| 34 | */ |
|
| 35 | private $deque; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Creates an instance using the values of an array or Traversable object. |
|
| 39 | * |
|
| 40 | * @param array|Traversable $values |
|
| 41 | */ |
|
| 42 | public function __construct($values = []) |
|
| 43 | { |
|
| 44 | $this->deque = Deque::fromArray([]); |
|
| 45 | ||
| 46 | $this->pushAll($values); |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * Ensures that enough memory is allocated for a specified capacity. This |
|
| 51 | * potentially reduces the number of reallocations as the size increases. |
|
| 52 | * |
|
| 53 | * @param int $capacity The number of values for which capacity should be |
|
| 54 | * allocated. Capacity will stay the same if this value |
|
| 55 | * is less than or equal to the current capacity. |
|
| 56 | */ |
|
| 57 | public function allocate(int $capacity) |
|
| 58 | { |
|
| 59 | $this->deque->allocate($capacity); |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Returns the current capacity of the queue. |
|
| 64 | * |
|
| 65 | * @return int |
|
| 66 | */ |
|
| 67 | public function capacity(): int |
|
| 68 | { |
|
| 69 | return $this->deque->capacity(); |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * @inheritDoc |
|
| 74 | */ |
|
| 75 | public function clear() |
|
| 76 | { |
|
| 77 | $this->deque->clear(); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * @inheritDoc |
|
| 82 | */ |
|
| 83 | public function copy() |
|
| 84 | { |
|
| 85 | return new self($this->deque); |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * @inheritDoc |
|
| 90 | */ |
|
| 91 | public function count(): int |
|
| 92 | { |
|
| 93 | return count($this->deque); |
|
| 94 | } |
|
| 95 | ||
| 96 | /** |
|
| 97 | * Returns the value at the front of the queue without removing it. |
|
| 98 | * |
|
| 99 | * @return |
|
| 100 | */ |
|
| 101 | public function peek() |
|
| 102 | { |
|
| 103 | return $this->deque->first(); |
|
| 104 | } |
|
| 105 | ||
| 106 | /** |
|
| 107 | * Returns and removes the value at the front of the Queue. |
|
| 108 | * |
|
| 109 | * @return mixed |
|
| 110 | */ |
|
| 111 | public function pop() |
|
| 112 | { |
|
| 113 | return $this->deque->shift(); |
|
| 114 | } |
|
| 115 | ||
| 116 | /** |
|
| 117 | * Pushes zero or more values into the front of the queue. |
|
| 118 | * |
|
| 119 | * @param mixed ...$values |
|
| 120 | */ |
|
| 121 | public function push(...$values) |
|
| 122 | { |
|
| 123 | $this->deque->push(...$values); |
|
| 124 | } |
|
| 125 | ||
| 126 | /** |
|
| 127 | * Creates associations for all keys and corresponding values of either an |
|
| 128 | * array or iterable object. |
|
| 129 | * |
|
| 130 | * @param Traversable|array $values |
|
| 131 | */ |
|
| 132 | private function pushAll($values) |
|
| 133 | { |
|
| 134 | foreach ($values as &$value) { |
|
| 135 | $this[] = $value; |
|
| 136 | } |
|
| 137 | } |
|
| 138 | ||
| 139 | /** |
|
| 140 | * @inheritDoc |
|
| 141 | */ |
|
| 142 | public function toArray(): array |
|
| 143 | { |
|
| 144 | return $this->deque->toArray(); |
|
| 145 | } |
|
| 146 | ||
| 147 | /** |
|
| 148 | * Get iterator |
|
| 149 | */ |
|
| 150 | public function getIterator() |
|
| 151 | { |
|
| 152 | while (!$this->isEmpty()) { |
|
| 153 | yield $this->pop(); |
|
| 154 | } |
|
| 155 | } |
|
| 156 | ||
| 157 | /** |
|
| 158 | * @inheritdoc |
|
| 159 | * |
|
| 160 | * @throws OutOfBoundsException |
|
| 161 | */ |
|
| 162 | public function offsetSet($offset, $value) |
|
| 163 | { |
|
| 164 | if ($offset === null) { |
|
| 165 | $this->push($value); |
|
| 166 | } else { |
|
| 167 | throw new OutOfBoundsException(); |
|
| 168 | } |
|
| 169 | } |
|
| 170 | ||
| 171 | /** |
|
| 172 | * @inheritdoc |
|
| 173 | * |
|
| 174 | * @throws Error |
|
| 175 | */ |
|
| 176 | public function offsetGet($offset) |
|
| 177 | { |
|
| 178 | throw new Error(); |
|
| 179 | } |
|
| 180 | ||
| 181 | /** |
|
| 182 | * @inheritdoc |
|
| 183 | * |
|
| 184 | * @throws Error |
|
| 185 | */ |
|
| 186 | public function offsetUnset($offset) |
|
| 187 | { |
|
| 188 | throw new Error(); |
|
| 189 | } |
|
| 190 | ||
| 191 | /** |
|
| 192 | * @inheritdoc |
|
| 193 | * |
|
| 194 | * @throws Error |
|
| 195 | */ |
|
| 196 | public function offsetExists($offset) |
|
| 197 | { |
|
| 198 | throw new Error(); |
|
| 199 | } |
|
| 200 | } |
|
| 201 | ||
| @@ 27-207 (lines=181) @@ | ||
| 24 | * @author Ulises Jeremias Cornejo Fandos <[email protected]> |
|
| 25 | */ |
|
| 26 | ||
| 27 | class Queue implements ArrayAccess, CollectionInterface, IteratorAggregate |
|
| 28 | { |
|
| 29 | use Traits\Collection; |
|
| 30 | ||
| 31 | const MIN_CAPACITY = 8; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @var FixedArray internal sfa to store values of the stack. |
|
| 35 | */ |
|
| 36 | private $sfa; |
|
| 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 = null) |
|
| 44 | { |
|
| 45 | $this->sfa = FixedArray::fromArray([]); |
|
| 46 | ||
| 47 | $this->pushAll($pairs); |
|
| 48 | } |
|
| 49 | ||
| 50 | /** |
|
| 51 | * Clear all elements in the Stack |
|
| 52 | */ |
|
| 53 | public function clear() |
|
| 54 | { |
|
| 55 | $this->sfa->clear(); |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * @inheritdoc |
|
| 60 | */ |
|
| 61 | public function copy() |
|
| 62 | { |
|
| 63 | return new self($this->sfa); |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Returns the number of elements in the Stack |
|
| 68 | * |
|
| 69 | * @return int |
|
| 70 | */ |
|
| 71 | public function count(): int |
|
| 72 | { |
|
| 73 | return count($this->sfa); |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Ensures that enough memory is allocated for a specified capacity. This |
|
| 78 | * potentially reduces the number of reallocations as the size increases. |
|
| 79 | * |
|
| 80 | * @param int $capacity The number of values for which capacity should be |
|
| 81 | * allocated. Capacity will stay the same if this value |
|
| 82 | * is less than or equal to the current capacity. |
|
| 83 | */ |
|
| 84 | public function allocate(int $capacity) |
|
| 85 | { |
|
| 86 | $this->sfa->allocate($capacity); |
|
| 87 | } |
|
| 88 | ||
| 89 | /** |
|
| 90 | * Returns the current capacity of the stack. |
|
| 91 | * |
|
| 92 | * @return int |
|
| 93 | */ |
|
| 94 | public function capacity(): int |
|
| 95 | { |
|
| 96 | return $this->sfa->capacity(); |
|
| 97 | } |
|
| 98 | ||
| 99 | /** |
|
| 100 | * Returns the value at the top of the stack without removing it. |
|
| 101 | * |
|
| 102 | * @return mixed |
|
| 103 | * |
|
| 104 | * @throws UnderflowException if the stack is empty. |
|
| 105 | */ |
|
| 106 | public function peek() |
|
| 107 | { |
|
| 108 | return $this->sfa->last(); |
|
| 109 | } |
|
| 110 | ||
| 111 | /** |
|
| 112 | * Returns and removes the value at the top of the stack. |
|
| 113 | * |
|
| 114 | * @return mixed |
|
| 115 | * |
|
| 116 | * @throws UnderflowException if the stack is empty. |
|
| 117 | */ |
|
| 118 | public function pop() |
|
| 119 | { |
|
| 120 | return $this->sfa->pop(); |
|
| 121 | } |
|
| 122 | ||
| 123 | /** |
|
| 124 | * Pushes zero or more values onto the top of the stack. |
|
| 125 | * |
|
| 126 | * @param mixed ...$values |
|
| 127 | */ |
|
| 128 | public function push(...$values) |
|
| 129 | { |
|
| 130 | $this->sfa->push(...$values); |
|
| 131 | } |
|
| 132 | ||
| 133 | /** |
|
| 134 | * Creates associations for all keys and corresponding values of either an |
|
| 135 | * array or iterable object. |
|
| 136 | * |
|
| 137 | * @param Traversable|array $values |
|
| 138 | */ |
|
| 139 | private function pushAll($values) |
|
| 140 | { |
|
| 141 | foreach ($values as &$value) { |
|
| 142 | $this[] = $value; |
|
| 143 | } |
|
| 144 | } |
|
| 145 | ||
| 146 | /** |
|
| 147 | * @inheritDoc |
|
| 148 | */ |
|
| 149 | public function toArray(): array |
|
| 150 | { |
|
| 151 | return array_reverse($this->sfa->toArray()); |
|
| 152 | } |
|
| 153 | ||
| 154 | /** |
|
| 155 | * |
|
| 156 | */ |
|
| 157 | public function getIterator() |
|
| 158 | { |
|
| 159 | while (! $this->isEmpty()) { |
|
| 160 | yield $this->pop(); |
|
| 161 | } |
|
| 162 | } |
|
| 163 | ||
| 164 | /** |
|
| 165 | * @inheritdoc |
|
| 166 | * |
|
| 167 | * @throws OutOfBoundsException |
|
| 168 | */ |
|
| 169 | public function offsetSet($offset, $value) |
|
| 170 | { |
|
| 171 | if ($offset === null) { |
|
| 172 | $this->push($value); |
|
| 173 | } else { |
|
| 174 | throw new OutOfBoundsException(); |
|
| 175 | } |
|
| 176 | } |
|
| 177 | ||
| 178 | /** |
|
| 179 | * @inheritdoc |
|
| 180 | * |
|
| 181 | * @throws Error |
|
| 182 | */ |
|
| 183 | public function offsetGet($offset) |
|
| 184 | { |
|
| 185 | throw new Error(); |
|
| 186 | } |
|
| 187 | ||
| 188 | /** |
|
| 189 | * @inheritdoc |
|
| 190 | * |
|
| 191 | * @throws Error |
|
| 192 | */ |
|
| 193 | public function offsetUnset($offset) |
|
| 194 | { |
|
| 195 | throw new Error(); |
|
| 196 | } |
|
| 197 | ||
| 198 | /** |
|
| 199 | * @inheritdoc |
|
| 200 | * |
|
| 201 | * @throws Error |
|
| 202 | */ |
|
| 203 | public function offsetExists($offset) |
|
| 204 | { |
|
| 205 | throw new Error(); |
|
| 206 | } |
|
| 207 | } |
|
| 208 | ||