# Is this correct behaviour with SplMinHeap?

- Thread: https://externals.io/message/58064
- Original archive: https://news-web.php.net/php.internals/58064
- Started by Dmitri Snytkine on 2012-02-25T13:21:39+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #58064 — Dmitri Snytkine — 2012-02-25T13:21:39+00:00

URL: https://externals.io/message/58064#58064

I am not sure if this is a bug or not, but something is strange with this:
I run this little test script, this one is posted on php.net in comments
section


class MySimpleHeap extends SplMaxHeap
{
    public function  compare( $value1, $value2 ) {
        return ( $value1 - $value2 );
    }
}

$obj = new MySimpleHeap();
$obj->insert( 4 );
$obj->insert( 8 );
$obj->insert( 1 );
$obj->insert( 0 );

foreach( $obj as $number ) {
    echo $number."\n";
}

The output is 8 4 1 0
Good.
Now I change the 
class MySimpleHeap extends SplMaxHeap
to
class MySimpleHeap extends SplMinHeap

run it again (now I expet to get lowest value first) and get exact same
results back.

Is this a bug?



Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"

---

## #58065 — Etienne Kneuss — 2012-02-25T14:02:42+00:00

URL: https://externals.io/message/58064#58065 · In reply to: #58064

Hi,

On Sat, Feb 25, 2012 at 14:21, Dmitri Snytkine
<dsnytkine@ultralogistics.com> wrote:
> I am not sure if this is a bug or not, but something is strange with this:
> I run this little test script, this one is posted on php.net in comments
> section
>
>
> class MySimpleHeap extends SplMaxHeap
> {
>    public function  compare( $value1, $value2 ) {
>        return ( $value1 - $value2 );
>    }
> }
>
> $obj = new MySimpleHeap();
> $obj->insert( 4 );
> $obj->insert( 8 );
> $obj->insert( 1 );
> $obj->insert( 0 );
>
> foreach( $obj as $number ) {
>    echo $number."\n";
> }
>
> The output is 8 4 1 0
> Good.
> Now I change the
> class MySimpleHeap extends SplMaxHeap
> to
> class MySimpleHeap extends SplMinHeap
>
> run it again (now I expet to get lowest value first) and get exact same
> results back.
>
> Is this a bug?

No, this is not a bug. SplMin/MaxHeap simply define their own compare
method, but you overwrite it anyway.

If you specify the compare method, I don't believe it is useful to
extends SplMin/MaxHeap if you overwrite the compare method completely,
you should extend SplHeap directly in such cases.

Best,

>
>
>
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
>
> "A Top 100 Logistics I.T. Provider in 2011"
>
>
>
>
> --
> 
> 
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #58066 — Dmitri Snytkine — 2012-02-25T14:08:40+00:00

URL: https://externals.io/message/58064#58066

This makes sense. 
Thank you.



Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"


-----Original Message-----
From: ekneuss@gmail.com [mailto:ekneuss@gmail.com] On Behalf Of Etienne Kneuss
Sent: Saturday, February 25, 2012 9:03 AM
To: Dmitri Snytkine
Cc: internals@lists.php.net
Subject: Re: [PHP-DEV] Is this correct behaviour with SplMinHeap?

Hi,

On Sat, Feb 25, 2012 at 14:21, Dmitri Snytkine
<dsnytkine@ultralogistics.com> wrote:
> I am not sure if this is a bug or not, but something is strange with this:
> I run this little test script, this one is posted on php.net in comments
> section
>
>
> class MySimpleHeap extends SplMaxHeap
> {
>    public function  compare( $value1, $value2 ) {
>        return ( $value1 - $value2 );
>    }
> }
>
> $obj = new MySimpleHeap();
> $obj->insert( 4 );
> $obj->insert( 8 );
> $obj->insert( 1 );
> $obj->insert( 0 );
>
> foreach( $obj as $number ) {
>    echo $number."\n";
> }
>
> The output is 8 4 1 0
> Good.
> Now I change the
> class MySimpleHeap extends SplMaxHeap
> to
> class MySimpleHeap extends SplMinHeap
>
> run it again (now I expet to get lowest value first) and get exact same
> results back.
>
> Is this a bug?

No, this is not a bug. SplMin/MaxHeap simply define their own compare
method, but you overwrite it anyway.

If you specify the compare method, I don't believe it is useful to
extends SplMin/MaxHeap if you overwrite the compare method completely,
you should extend SplHeap directly in such cases.

Best,

>
>
>
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
>
> "A Top 100 Logistics I.T. Provider in 2011"
>
>
>
>
> --
> 
> 
>



-- 
Etienne Kneuss
http://www.colder.ch
