Bug 6515 – Support for a basic BinaryHeap operation
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-08-16T14:42:17Z
Last change time
2017-10-19T08:04:12Z
Keywords
bootcamp
Assigned to
No Owner
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-08-16T14:42:17Z
I'd like to create an empty heap, and then add to it an arbitrary (and statically unknown) number of items, keeping the heap invariant valid all the time (this means the heap invariant is valid after each item is added to the heap, so I am free to pop items in any moment). Is this possible with std.container.BinaryHeap?
If this is not possible then I think this is a common and really basic operation that needs to be possible. If it's already possible, then I suggest to add a little example to the std.container.BinaryHeap module docs that shows how you do it.
Comment #1 by safety0ff.bugz — 2016-10-15T00:29:55Z
import std.container.binaryheap;
import std.container.array;
import std.random;
void main()
{
BinaryHeap!(Array!uint) heap;
uint n = uniform(100, 2000);
foreach (_;0..n)
heap.insert(uniform(0,uint.max));
}
I didn't get it to work with uint[], perhaps there's a bug.
It kept saying "Cannot grow a heap created over a range," but as you can see, it should work with uint[] as the following static if should evaluate to true.
https://github.com/dlang/phobos/blob/master/std/container/binaryheap.d#L279
Comment #2 by razvan.nitu1305 — 2017-10-19T08:04:12Z
Using the method insert as pointed out in comment 2 is the way to go. Closing as invalid.