-
-
Notifications
You must be signed in to change notification settings - Fork 739
Fix Issue 5945: redBlackTree printing #3572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I think this is not the correct solution, as it allocates a temporary string that is immediately garbage. The correct solution, I think, is to print the range, as identified in the bug report. As there is no way for RedBlackTree to hook that, I think the work to be done is not in RedBlackTree, but in format/writefln. |
Can't you just |
It's better to use this overload of
|
I didn't know that could be done. Sounds like a good solution. |
@yebblies You mean like this?
|
He means the toString variant that @quickfur alluded to. I'm not sure how it works on classes though. |
@schveiguy But yebblies' comment was made before quickfur's comment. |
I know, both were referring to the new style of toString, which instead of creating a garbage strings, accepts a "sink" function to put all string output into. |
I don't understand that overload. Isn't |
http://dlang.org/phobos/std_format.html#.formatValue Scroll down until you see the overload that takes an aggregate with toString. It will explain how the overloads work. Again, I'm not 100% sure how it works for classes, but I think it should work just by adding that overload (may have to provide a string returning overload too). |
Ok, so I changed the method to:
But now I am running into linker errors from std.format again, even though the unit tests pass. Original bug report: https://issues.dlang.org/show_bug.cgi?id=14959 But now the errors are
Where test.d is
|
Just add the unittest and we'll see what the auto-tester says |
Well the problem is that the tests don't cover the |
No? |
e231ab7
to
c0a36b3
Compare
At the time I was referring to the linker errors and my inability to compile the program. But after I re-downloaded dmd and druntime I was finally able to compile the test program in the issue. Thank you for reminding me about this. I have added your unit test now. |
c0a36b3
to
864b59d
Compare
Otherwise LGTM |
864b59d
to
9b26a72
Compare
@DmitryOlshansky Added extra tests. |
@@ -20,6 +20,7 @@ Authors: Steven Schveighoffer, $(WEB erdani.com, Andrei Alexandrescu) | |||
module std.container.rbtree; | |||
|
|||
import std.functional : binaryFun; | |||
import std.format; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... Move that to toString method itself? Since RB-Tree is a template that should cut down on number of imports if RB-Tree is not instantiated but std.container is imported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) const
overload doesn't work if std.format
is a local import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could do something like:
template toString(DG)
{
import std.format;
void toString(scope DG sink, FormatSpec!char fmt) const
{
...
}
}
override string toString() const
{
auto app = appender!string();
toString(app, FormatSpec!char("%s"));
return app.data;
}
edit:
Err, scratch the runtime polymorphic overload, clearly it would still require FormatSpec
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we have to make do with global import for now
Leaving last word to @schveiguy |
Well, it's better than the current behavior, and adjusting the format later isn't going to make a huge difference. |
@@ -1669,6 +1670,12 @@ assert(equal(rbt[], [5])); | |||
} | |||
} | |||
|
|||
void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be documented? I'm honestly not sure, because I don't know what the policy is for documenting toString
functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing a search through Phobos I see examples of both documentation and no documentation, so your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I say err on the side of documentation for public functions :)
Talk about damning with faint praise ;) |
9b26a72
to
a3cb029
Compare
Added documentation. |
Returns a user-friendly string representation by writing to the $(D sink). | ||
For more info, see $(D std.format.formatValue). | ||
*/ | ||
void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns void, so starting the doc with "Returns" is not correct.
I think this might be acceptable:
Formats the RedBlackTree into a sink function. For more info see $(D std.format.formatValue)
Heh, I will rephrase: I don't like the redundant brackets, but the output is not confusing or misleading, it just looks ugly to me :) Bikeshedding shouldn't derail this. In any case, I appreciate the effort to fix the bug, and I think it's an acceptable solution. |
a3cb029
to
0f05183
Compare
Fixed documentation |
Auto-merge toggled on |
Woohoo! My first real contribution to Phobos! |
Congrats! ;) |
Fix Issue 5945: redBlackTree printing
Low hanging fruit:
https://issues.dlang.org/show_bug.cgi?id=5945
My solution is a little different than the original suggestion, as it isn't known if the redBlackTree factory function was used or not.