Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions std/container/binaryheap.d
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change would be cleaner if it was written in the following way

        else static if (isDynamicArray!Store)
        {
            if (_store.length == 0)
                _store.length = 10;
            if (length == _store.length)
                _store.length = length * 2;
            _store[_length] = value;
        }
        else
        {
            // can't grow
            enforce(length < _store.length,
                    "Cannot grow a heap created over a range");
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know the reason for setting the element after an exception was thrown, so I didn't change the code, but thanks for the heads-up -> changed :)

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 @@ -445,3 +456,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);
}