Bug 5462 – std.container.BinaryHeap enforce message + pop
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-01-19T14:34:00Z
Last change time
2014-02-09T12:55:24Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-01-19T14:34:44Z
A D2 program:
import std.container;
void main() {
auto h = BinaryHeap!(int[])([]);
h.removeFront();
}
It throws the exception:
object.Exception@...\dmd\src\phobos\std\container.d(2592): Enforcement failed
In a larger program such error message is bad because it doesn't give a lot of information. To improve the situation a little I suggest to modify the enforce at line 2592:
void removeFront()
{
enforce(!empty);
Adding an error message that helps understand what the error is:
enforce(!empty, "Attempt to remove the front of an empty heap");
(The future stack trace on Windows will probably also give the line number 4 of the user program.)
-------------------------
In code that uses the BinaryHeap I've often used two lines of code like this:
auto item = heap.front();
heap.removeFront();
So I suggest to add to BinaryHeap a handy member function pop() that does both things:
auto item = heap.pop();
Comment #1 by github-bugzilla — 2014-01-25T12:32:10Z
Comment #2 by bearophile_hugs — 2014-02-09T12:30:03Z
Is the heap.pop(); a WONTFIX?
Comment #3 by peter.alexander.au — 2014-02-09T12:37:08Z
Sorry, missed that.
(would probably be better if this was two separate enhancements)
Comment #4 by bearophile_hugs — 2014-02-09T12:55:24Z
(In reply to comment #3)
> Sorry, missed that.
>
> (would probably be better if this was two separate enhancements)
Yes, this ER is from some years ago, and since then I've learnt to avoid packing two or more enhancements or bug reports in the same issue report, unless they are really tied together.
So I close this issue again, and split it into another (but I remember some issues against the idea of having a pop() function in C++).