Fixing the SortedRange
Dmitry Olshansky
dmitry.olsh at gmail.com
Sat Mar 3 10:44:51 PST 2012
It's annoyingly strange that e.g. the following won't work as of yet:
auto sorted = assumeSorted(["hello", "world"]); //assume lots of strings
auto x = sorted.lowerBound("Try that?"d);
Yeah, I know the immutable(dchar)[] vs immutable(char)[] can't be
compared using straight <. That's acceptable, even if not convenient.
Ok, take 2, via std.algorithm cmp:
auto sorted = assumeSorted!"cmp(a,b) < 0"(["hello", "world"]);
auto x = sorted.lowerBound("Try that?"d);
Now that fails, because of lowerBound signature. And that is
auto lowerBound(SearchPolicy sp = SearchPolicy.binarySearch, V)(V value)
if (is(V : ElementType!Range))
Same goes for upperBound and others.
And why the hell I need to convert the value to type of range element?
It's not like there is any value = range[x]; necessary, and indeed it
works without the signature (+ a one liner fix).
Seeking the proper signature I've come up with the following:
auto lowerBound(SearchPolicy sp = SearchPolicy.binarySearch, V)(V value)
if (isTwoWayCompatible!(predFun, ElementType!Range, V))
isTwoWayCompatible!(pred,A,B) means ether of the following works:
pred(a,b) and pred(b,a) where a has type A, b typed as B
Seem fine to me, so I'm looking for a better name for this trait.
Does it sound like it belongs in std.traits or is there more general
better abstraction for this kind of thing?
--
Dmitry Olshansky
More information about the Digitalmars-d
mailing list