www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can someone help me make a binary heap with std.container.binaryheap?

reply Enjoys Math <enjoysmath gmail.com> writes:
Init:

programResultsQ = heapify!(compareResults, 
Array!(Results!(O,I)))(Array!(Results!(O,I))([Results!(O,I)()]), 
1);

Decl:

alias ProgramResultsQueue(O,I) = 
BinaryHeap!(Array!(Results!(O,I)), compareResults);

Error:

assert error in std.container.array.d (line 381)

upon running.  Compiles fine.  I'd like to initialize the heap to 
empty if possible.
Sep 25 2015
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/25/2015 08:22 PM, Enjoys Math wrote:
 Init:

 programResultsQ = heapify!(compareResults,
 Array!(Results!(O,I)))(Array!(Results!(O,I))([Results!(O,I)()]), 1);

 Decl:

 alias ProgramResultsQueue(O,I) = BinaryHeap!(Array!(Results!(O,I)),
 compareResults);

 Error:

 assert error in std.container.array.d (line 381)

 upon running.  Compiles fine.  I'd like to initialize the heap to empty
 if possible.
import std.container; import std.algorithm; import std.traits; void main() { // The data to start with auto data = [ 10, 5, -7, 20, 0, 3 ]; // This will move elements around to make a binary heap auto heap = heapify(data); // Yes, it is a BinaryHeap: static assert(isInstanceOf!(BinaryHeap, typeof(heap))); // Yes, the elements are in binary heap order: assert(data == [20, 10, 3, 5, 0, -7]); /* As is the case with binary heaps, although the data is ordered al a binary heap, that data is the equivalent of the following binary search tree: 20 / \ / \ 10 3 / \ / 5 0 -7 */ // Let's visit the elements through the binary heap // range. They should appear in descending order: assert(heap.equal([20, 10, 5, 3, 0, -7 ])); }
 I'd like to initialize the heap to empty
 if possible.
The following works (at least for int elements): import std.container; import std.algorithm; import std.traits; void main() { // Let's start with an empty array: Array!int data; auto heap = heapify(data); assert(heap.empty); heap.insert(42); heap.insert(-10); heap.insert(7); assert(heap.equal([ 42, 7, -10 ])); } Ali
Sep 28 2015