Creating a growable binary heap or priority queue using Phobos?

SimonM user at example.net
Sun Nov 13 12:49:17 PST 2011


Hey, I'm trying to use the BinaryHeap in Phobos, but somehow can't get 
it working with the Array!(T) container so that the heap can actually be 
growable. I keep getting this error:

Error: template instance BinaryHeap!(myArray,"a > b") does not match 
template declaration BinaryHeap(Store,alias less = "a < b") if 
(isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))

The documentation 
(http://d-programming-language.org/phobos/std_container.html#BinaryHeap) 
says that BinaryHeap should be growable with any container that supports 
the insertBack function, which Array!(T) does 
(http://d-programming-language.org/phobos/std_container.html#insertBack).

I've found the following questions in the newsgroup that all asked the 
same question, without really resolving it (without using their own 
implementation):

2011, 28 March: 
http://www.digitalmars.com/d/archives/digitalmars/D/learn/Growable_BinaryHeap_use_w_Array_25362.html

2011, 6 March: 
http://www.digitalmars.com/d/archives/digitalmars/D/learn/How_do_you_use_BinaryHeap_with_Array_or_just_make_a_growable_heap_25928.html

2009, 27 April: 
http://www.digitalmars.com/d/archives/digitalmars/D/std.algorithm.BinaryHeap_88811.html

 From those threads, I found that the reason might be related to the 
fact that the isRandomAccessRange template returns false when used with 
an Array!(T) object, for example this
----------------------------------------
import std.range;
import std.container;

void main(){
	pragma(msg,isRandomAccessRange!(Array!(uint)));
}
----------------------------------------
prints out false during compilation.

Is there any way to use Array!(T) with a BinaryHeap, or any other way to 
have a growable BinaryHeap?


More information about the Digitalmars-d-learn mailing list