php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53710 Data registered with equal priority not returned in expected order
Submitted: 2011-01-10 22:23 UTC Modified: 2011-07-12 18:10 UTC
Votes:6
Avg. Score:4.3 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:4 (80.0%)
From: weierophinney@php.net Assigned: colder (profile)
Status: Wont fix Package: SPL related
PHP Version: 5.3.5 OS: Linux 32-bit
Private report: No CVE-ID: None
 [2011-01-10 22:23 UTC] weierophinney@php.net
Description:
------------
SplPriorityQueue does not behave as expected when data is registered with equal 
priority. One would expect one of the following situations:

 * The data retain the same order as how they were registered
 * The data are ordered based on value

Neither of these are the case, however. From empirical analysis, it appears that 
the following happens:

 * The first item registered at that priority will always be the first returned
 * The remaining items are returned in the reverse order in which they were 
enqueued.

Test script:
---------------
$queue = new SplPriorityQueue();
$queue->insert('foo', 100);
$queue->insert('bar', 100);
$queue->insert('baz', 100);
$queue->insert('bat', 100);

foreach ($queue as $data) {
    echo $data, "\n";
}

Expected result:
----------------
// IDEALLY order in which they are registered:
foo
bar
baz
bat

// OR following same rules as priority queue -- higher values == higher priority
foo
baz
bat
bar



Actual result:
--------------
foo
bat
baz
bar

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-14 01:01 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: colder
 [2011-01-17 19:44 UTC] daniel at berste dot in
Reproduced on OSX (PHP compiled from MacPorts):

$ php --version
PHP 5.3.5 (cli) (built: Jan  9 2011 20:15:19) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

$ cat test.php 
<?php
$queue = new SplPriorityQueue();
$queue->insert('foo', 100);
$queue->insert('bar', 100);
$queue->insert('baz', 100);
$queue->insert('bat', 100);

foreach ($queue as $data) {
    echo $data, "\n";
}

$ php test.php 
foo
bat
baz
bar
 [2011-07-12 18:10 UTC] colder@php.net
-Status: Assigned +Status: Wont fix
 [2011-07-12 18:10 UTC] colder@php.net
There is no such guarantee. The only guarantee that you'll get from 
SplPriorityQueue is that you won't get an element out of order. Elements with the 
same priority are extracted in arbitrary order, the rest is implementation 
dependant.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 08 15:01:30 2024 UTC