-
-
Notifications
You must be signed in to change notification settings - Fork 705
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
Fix Issue 10131 - To remove duplicates and keep order #5774
Conversation
|
Thanks for your pull request, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla references
|
|
|
|
@bbasile maybe it's just me, but deduplicate sounds very confusing. |
|
@wilzbach why is dscanner failing? The code in the unittest is valid user code. |
std/algorithm/iteration.d
Outdated
| { | ||
| import std.algorithm : canFind; | ||
| T[] result; | ||
| foreach (T elem; s) |
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.
first element of s can be inserted, always, unconditionally. (in the other else part of the static branch too)
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 would imply checking manually for an empty array, instead the foreach does that for you, plus the code is more readable this way
std/algorithm/iteration.d
Outdated
| } | ||
|
|
||
| /// | ||
| @system unittest |
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.
it's not safe to deduplicate an array of int ?
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.
By the way the content here is quite horrid for a documentation unittest
std/algorithm/iteration.d
Outdated
| if the $(D opEquals) operator is not overriden for classes, $(D noDupes) considers the class | ||
| pointer for duplication removal. | ||
| */ | ||
| T[] noDupes(T)(T[] s) |
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.
why not auto ref ?
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.
Aw and inout ? Since deduplication doesn't happend in place ?
std/algorithm/iteration.d
Outdated
| @@ -5185,3 +5185,100 @@ if (isRandomAccessRange!Range && hasLength!Range) | |||
| [1, 2, 0], | |||
| [2, 1, 0]])); | |||
| } | |||
| /* | |||
| Returns the array without any duplicates, keeping the initial order of appearance. | |||
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.
Params and Returns standard DDOC sections ?
std/algorithm/iteration.d
Outdated
| else | ||
| { | ||
| bool[T] set; | ||
| T[] result; |
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.
can't an Appender be possible here ? we know how many to reserve (top).
std/algorithm/iteration.d
Outdated
| foreach (T elem; s) | ||
| if (elem !in set) | ||
| { | ||
| set[elem] = true; |
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'm not sure if this strategy is something really clever with struct that are postblitable. The postblit will run once in the AA and then a second time in the result.
|
I have a custom function for this: auto distinct(Range)(Range r) if (isInputRange!Range)
{
bool[ForeachType!Range] justSeen;
return r.filter!(
(k) {
if (k in justSeen) return false;
return justSeen[k] = true;
}
);
}It works in a lazy way, so it works for infinite ranges too like: roundRobin(sequence!"n"(0), sequence!"n"(0)).distinct.take(10).writeln;and doesn't read the whole array if not needed. |
Have a look at the error messages: Regarding
|
|
@RazvanN7 please don't forget to add a link to your PR to the regarding Bugzilla issue, s.t. people in the future know about all work done and duplicate the work (I have added it for this one). See also: dlang/dmd#7205 |
|
Regarding circleCi : since the compiler allows having to hash without opEquals why does dscanner complain about that? I admit that having a non-const toHash, opCmp etc. is most likely a mistake. Other then that, I haven't thought about the @nogc aspect. I will put @trikko 's code into std/algorithm/experimental and hopefully that will close the bug. Thanks for the extensive explanation. I'm pretty noobish when it comes to function annotations and your explanations are extremely helpful. |
47a7c28
to
d8e8558
Compare
I'm not sure if this function should be added as an overload to uniq.