Showing with 31 additions and 4 deletions.
  1. +31 −4 std/container/binaryheap.d
35 changes: 31 additions & 4 deletions std/container/binaryheap.d
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,21 @@ and $(D length == capacity), throws an exception.
}
else
{
// can't grow
enforce(length < _store.length,
"Cannot grow a heap created over a range");
_store[_length] = value;
import std.traits : isDynamicArray;
static if (isDynamicArray!Store)
{
if (_store.length == 0)
_store.length = 8;
else if (length == _store.length)
_store.length = length * 3 / 2;
_store[_length] = value;
}
else
{
// can't grow
enforce(length < _store.length,
"Cannot grow a heap created over a range");
}
}

// sink down the element
Expand Down Expand Up @@ -446,3 +457,19 @@ unittest // 15675
auto heap = heapify(elements);
assert(heap.front == 12);
}

unittest // 16072
{
auto q = heapify!"a > b"([2, 4, 5]);
q.insert(1);
q.insert(6);
assert(q.front == 1);

// test more multiple grows
int[] arr;
auto r = heapify!"a < b"(arr);
foreach (i; 0..100)
r.insert(i);

assert(r.front == 99);
}