Skip to content

Commit

Permalink
Fix Issue 7287 - BinaryHeap example
Browse files Browse the repository at this point in the history
Two changes:

1. Fixed the array in the final assert. Not sure where the original array came from -- not Intro to Algorithms, I checked my copy!
2. Moved to documented unit test so we make sure it actually passes.

https://d.puremagic.com/issues/show_bug.cgi?id=7287
  • Loading branch information
Poita committed Feb 12, 2014
1 parent 4a7f1a9 commit 80ef8ef
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions std/container.d
Expand Up @@ -3742,17 +3742,6 @@ If $(D Store) is a range, the $(D BinaryHeap) cannot grow beyond the
size of that range. If $(D Store) is a container that supports $(D
insertBack), the $(D BinaryHeap) may grow by adding elements to the
container.
Example:
----
// Example from "Introduction to Algorithms" Cormen et al, p 146
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
auto h = heapify(a);
// largest element
assert(h.front == 16);
// a has the heap property
assert(equal(a, [ 16, 14, 10, 9, 8, 7, 4, 3, 2, 1 ]));
----
*/
struct BinaryHeap(Store, alias less = "a < b")
if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))
Expand Down Expand Up @@ -4089,6 +4078,18 @@ must be collected.
}
}

///
unittest
{
// Example from "Introduction to Algorithms" Cormen et al, p 146
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
auto h = heapify(a);
// largest element
assert(h.front == 16);
// a has the heap property
assert(equal(a, [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]));
}

/**
Convenience function that returns a $(D BinaryHeap!Store) object
initialized with $(D s) and $(D initialSize).
Expand Down

0 comments on commit 80ef8ef

Please sign in to comment.