comparison vendor/zendframework/zend-stdlib/src/FastPriorityQueue.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
55 protected $subPriorities = []; 55 protected $subPriorities = [];
56 56
57 /** 57 /**
58 * Max priority 58 * Max priority
59 * 59 *
60 * @var integer|null
61 */
62 protected $maxPriority = null;
63
64 /**
65 * Total number of elements in the queue
66 *
60 * @var integer 67 * @var integer
61 */ 68 */
62 protected $maxPriority = 0; 69 protected $count = 0;
63 70
64 /** 71 /**
65 * Total number of elements in the queue 72 * Index of the current element in the queue
66 * 73 *
67 * @var integer 74 * @var integer
68 */ 75 */
69 protected $count = 0; 76 protected $index = 0;
70 77
71 /** 78 /**
72 * Index of the current element in the queue 79 * Sub index of the current element in the same priority level
73 * 80 *
74 * @var integer 81 * @var integer
75 */ 82 */
76 protected $index = 0;
77
78 /**
79 * Sub index of the current element in the same priority level
80 *
81 * @var integer
82 */
83 protected $subIndex = 0; 83 protected $subIndex = 0;
84 84
85 /** 85 /**
86 * Insert an element in the queue with a specified priority 86 * Insert an element in the queue with a specified priority
87 * 87 *
88 * @param mixed $value 88 * @param mixed $value
89 * @param integer $priority a positive integer 89 * @param integer $priority
90 */ 90 */
91 public function insert($value, $priority) 91 public function insert($value, $priority)
92 { 92 {
93 if (! is_int($priority)) { 93 if (! is_int($priority)) {
94 throw new Exception\InvalidArgumentException('The priority must be an integer'); 94 throw new Exception\InvalidArgumentException('The priority must be an integer');
95 } 95 }
96 $this->values[$priority][] = $value; 96 $this->values[$priority][] = $value;
97 if (! isset($this->priorities[$priority])) { 97 if (! isset($this->priorities[$priority])) {
98 $this->priorities[$priority] = $priority; 98 $this->priorities[$priority] = $priority;
99 $this->maxPriority = max($priority, $this->maxPriority); 99 $this->maxPriority = $this->maxPriority === null ? $priority : max($priority, $this->maxPriority);
100 } 100 }
101 ++$this->count; 101 ++$this->count;
102 } 102 }
103 103
104 /** 104 /**
130 * @param mixed $datum 130 * @param mixed $datum
131 * @return bool False if the item was not found, true otherwise. 131 * @return bool False if the item was not found, true otherwise.
132 */ 132 */
133 public function remove($datum) 133 public function remove($datum)
134 { 134 {
135 $currentIndex = $this->index;
136 $currentSubIndex = $this->subIndex;
137 $currentPriority = $this->maxPriority;
138
135 $this->rewind(); 139 $this->rewind();
136 while ($this->valid()) { 140 while ($this->valid()) {
137 if (current($this->values[$this->maxPriority]) === $datum) { 141 if (current($this->values[$this->maxPriority]) === $datum) {
138 $index = key($this->values[$this->maxPriority]); 142 $index = key($this->values[$this->maxPriority]);
139 unset($this->values[$this->maxPriority][$index]); 143 unset($this->values[$this->maxPriority][$index]);
144
145 // The `next()` method advances the internal array pointer, so we need to use the `reset()` function,
146 // otherwise we would lose all elements before the place the pointer points.
147 reset($this->values[$this->maxPriority]);
148
149 $this->index = $currentIndex;
150 $this->subIndex = $currentSubIndex;
151
152 // If the array is empty we need to destroy the unnecessary priority,
153 // otherwise we would end up with an incorrect value of `$this->count`
154 // {@see \Zend\Stdlib\FastPriorityQueue::nextAndRemove()}.
155 if (empty($this->values[$this->maxPriority])) {
156 unset($this->values[$this->maxPriority]);
157 unset($this->priorities[$this->maxPriority]);
158 if ($this->maxPriority === $currentPriority) {
159 $this->subIndex = 0;
160 }
161 }
162
163 $this->maxPriority = empty($this->priorities) ? null : max($this->priorities);
140 --$this->count; 164 --$this->count;
141 return true; 165 return true;
142 } 166 }
143 $this->next(); 167 $this->next();
144 } 168 }
189 * Set the iterator pointer to the next element in the queue 213 * Set the iterator pointer to the next element in the queue
190 * removing the previous element 214 * removing the previous element
191 */ 215 */
192 protected function nextAndRemove() 216 protected function nextAndRemove()
193 { 217 {
218 $key = key($this->values[$this->maxPriority]);
219
194 if (false === next($this->values[$this->maxPriority])) { 220 if (false === next($this->values[$this->maxPriority])) {
195 unset($this->priorities[$this->maxPriority]); 221 unset($this->priorities[$this->maxPriority]);
196 unset($this->values[$this->maxPriority]); 222 unset($this->values[$this->maxPriority]);
197 $this->maxPriority = empty($this->priorities) ? 0 : max($this->priorities); 223 $this->maxPriority = empty($this->priorities) ? null : max($this->priorities);
198 $this->subIndex = -1; 224 $this->subIndex = -1;
225 } else {
226 unset($this->values[$this->maxPriority][$key]);
199 } 227 }
200 ++$this->index; 228 ++$this->index;
201 ++$this->subIndex; 229 ++$this->subIndex;
202 --$this->count; 230 --$this->count;
203 } 231 }
209 public function next() 237 public function next()
210 { 238 {
211 if (false === next($this->values[$this->maxPriority])) { 239 if (false === next($this->values[$this->maxPriority])) {
212 unset($this->subPriorities[$this->maxPriority]); 240 unset($this->subPriorities[$this->maxPriority]);
213 reset($this->values[$this->maxPriority]); 241 reset($this->values[$this->maxPriority]);
214 $this->maxPriority = empty($this->subPriorities) ? 0 : max($this->subPriorities); 242 $this->maxPriority = empty($this->subPriorities) ? null : max($this->subPriorities);
215 $this->subIndex = -1; 243 $this->subIndex = -1;
216 } 244 }
217 ++$this->index; 245 ++$this->index;
218 ++$this->subIndex; 246 ++$this->subIndex;
219 } 247 }