-
-
Notifications
You must be signed in to change notification settings - Fork 741
Add Specializations for SortedRange #3534
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
Hilarious. It's sill O(1), the number just means it's |
I recall what the problem we had with it was - literally things like It doesn't block anything but should be taken into consideration, a compiler ought to try and match lambas with identical bodies at least token-wise identical. |
I kind of the new that ;) |
For instance static assert(is(typeof(binaryFun!"a<b") ==
typeof(binaryFun!"a<b")));
static assert(is(typeof(binaryFun!"a<b") ==
typeof(binaryFun!"a < b"))); both passes but static assert(!is(typeof(binaryFun!"a<b") ==
typeof(binaryFun!"a>b"))); fails. What to do about this?...should we extract and reuse the CT-parsing logic of
Is this lambda-comparison just a wish or is there something that can be used already?
|
7ac09c3
to
644087d
Compare
@@ -2777,23 +2777,30 @@ smallest element and with the same ending as $(D range). The function | |||
can actually be used for finding the maximum or any other ordering | |||
predicate (that's why $(D maxPos) is not provided). | |||
*/ | |||
Range minPos(alias pred = "a < b", Range)(Range range) | |||
auto minPos(alias pred = "a < b", Range)(Range range) |
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.
The return type is Range
for all paths; explicit return type is better for documented functions as it provides important information to readers.
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.
Done.
Functions that return subranges often already them as the same type as the whole range; this already works with Comparing predicates has been discussed a lot before, particularly in the context of string lambdas. One such thread: |
I suggest to break this PR up into smaller pieces so that it's easier to review, and the non-controversial parts can be merged first while we work on the other parts. Otherwise this will go very slowly and take too long to get merged. |
As a start, I'd say do a separate PR per overload set, as a rough guideline. I think that's much easier to review, and safe to merge piecemeal. |
ping @nordlow |
@quickfur Should I start with an initial pull for the new One way to move forward with this PR is to restrict logic to a specific set of expressions such as
which should cover most uses. All other inputs should trigger a compile-time error with a nice descriptive message. |
Hmm. On second thoughts, there is no good way to compare two lambdas (whether string or function literal -- the latter because the compiler has no implementation of such a comparison). So there is no good solution for moving this PR forward. The problem is that comparing two arbitrary lambdas is, in the most general case, uncomputable. For most practical purposes, though, we can reduce the problem to something tractable by ignoring non-trivial equivalences such as But in any case, this requires compiler support, and I agree with the sentiment that we should not promote string lambdas anymore so it's kinda pointless to support these functions just for string lambdas. Let's wait until we have a DIP on how to implement lambda comparisons. |
I'm not sure if you're saying that this is bad, but I'd consider that to be a pretty good thing. |
I was just thinking aloud. Obviously, it would be very bad if the two lambdas in the following code compared equal!
|
I have a question: Why not to add specializations directly into |
That might be cleaner, thanks to UFCS and because AFAIK: Lambda comparison problem will remain, though. |
Actually, if the specializations are added as member functions of Given the current state of things, that could potentially be the better way to go right now. |
Ok, I'll look into it. I guess we should be begin with calls such as:
where I guess Anymore low-hanging fruits? But note that this, of course, only works for calls to |
There's slight problem. For example, if range were sorted with |
|
@sigod We can't possibly know about all these special cases. It would be up to user code to detect such a case and use (Not to mention that determining whether two predicates are the opposite of each other, generally speaking, is uncomputable. It's easy to detect built-in operators |
This didn't occurred to me. Then I think such detection is out of question.
We probably could easily add support for the same |
@nordlow
What else was blocking this PR? |
But we still need Should these overloads be implemented as free functions take a I'll restrict the overloads in this pull to only operate on |
|
I vote for |
How do you avoid that SortedRanges match the other template? Won't you have to add something like |
It's intended for
I hope not. |
But to support overloading of minElement(R)(R r)
if (isSortedRange!R)
{ ... } ? So if is defined as member function in |
Yes, for such use we need free function overload. To be honest I didn't consider it. I always use UFCS.
No. Only |
So, AFAICT, the only current solution that harmonizes with Phobos' own Update: Can't we still use |
That or we need to deal with comparison of predicates. I'd go with former. |
Closing due to inactivity. @nordlow If you want to reopen this and continue working on it, please let me know. |
Add new predicate function
std.range.primitives.isSortedRange
: DONEAdd specializations for
SortedRange
in the followings algorithms:sort
: just return input argument ifpred
parameter matches: DONEsort
-alikes: just return input argument ifpred
parameter matches:isSorted
: returntrue
: DONEfind
and alikes, should do some kind of binary search. These could take an extra paramSearchPolicy
for sorted ranges that defaults to binary search.remove
,strip
andsplit
at least could.minPos
: return input as is, ifpred
matches: DONEIn compliance with C++ STL also add
minElement
: O(1) forSortedRange
, O(n) otherwise: DONEmaxElement
: O(1) forSortedRange!BidirectionalRange
, O(n) otherwise: DONEminmaxElement
: O(1) forSortedRange!BidirectionalRange
, O(n) otherwise.See also: http://forum.dlang.org/post/yenezfjjteokzyvgmzcf@forum.dlang.org
See also: https://issues.dlang.org/show_bug.cgi?id=11667
See also: http://forum.dlang.org/post/mqskao$28vh$1@digitalmars.com
See also: http://en.cppreference.com/w/cpp/algorithm/min_element