D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6515 - Support for a basic BinaryHeap operation
Summary: Support for a basic BinaryHeap operation
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2011-08-16 14:42 UTC by bearophile_hugs
Modified: 2017-10-19 08:04 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-08-16 14:42:17 UTC
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 safety0ff.bugz 2016-10-15 00:29:55 UTC
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 RazvanN 2017-10-19 08:04:12 UTC
Using the method insert as pointed out in comment 2 is the way to go. Closing as invalid.