Creating a Priority Queue: An Adventure

DarthCthulhu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 4 18:54:02 PDT 2015


On Wednesday, 5 August 2015 at 01:27:53 UTC, Steven Schveighoffer 
wrote:
> On 8/4/15 9:02 PM, DarthCthulhu wrote:
>
>>      writefln("PQ: %s", pq.queue); <- prints PQ: [Tuple!(int, 
>> string)(3,
>> "HELLO3"), Tuple!(int, string)(10, "HELLO10"), Tuple!(int, 
>> string)(11,
>> "HELLO11")]
>
> This is probably consuming your queue, popping all the data off 
> as it prints. If you print the length before hand, I'll bet 
> it's not zero.
>

Aha! Yes, you are correct. I didn't know writefln was popping 
elements off the heap. I thought it would've just walked down the 
heap without altering it at all. Interesting. Now I feel kinda 
silly.

> I don't know how to print the elements without removing them, 
> as binary heap doesn't have a range type, it seems to be the 
> range itself (an odd situation). Perhaps print the underlying 
> storage?
>
> -Steve

Yeah, I think the thing to do would be to make a helper function 
that would return the Array!(Tuple!) that the heap contains. 
Maybe as a const reference to make sure a user doesn't 
accidentally alter the array?

Thanks for your help!



More information about the Digitalmars-d-learn mailing list