Fix issue 23993: Discard Rebindable before passing extremum to comparator.

This commit is contained in:
FeepingCreature 2023-06-15 19:05:46 +02:00
parent f22cc260f8
commit f715941036

View file

@ -1349,13 +1349,23 @@ if (isInputRange!Range && !isInfinite!Range &&
// if we only have one statement in the loop, it can be optimized a lot better
static if (__traits(isSame, map, a => a))
{
CommonElement getExtremeElement()
{
static if (is(typeof(extremeElement) == T[], T))
{
return extremeElement;
}
else
{
return extremeElement.get;
}
}
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
if (selectorFun(r[i], getExtremeElement))
{
extremeElement = r[i];
}
@ -1365,7 +1375,7 @@ if (isInputRange!Range && !isInfinite!Range &&
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
if (selectorFun(r.front, getExtremeElement))
{
extremeElement = r.front;
}
@ -3880,6 +3890,14 @@ if (isInputRange!Range && !isInfinite!Range &&
assert(arr.maxElement!"a.val".val == 1);
}
// https://issues.dlang.org/show_bug.cgi?id=23993
@safe unittest
{
import std.bigint : BigInt;
assert([BigInt(2), BigInt(3)].maxElement == BigInt(3));
}
// minPos
/**
Computes a subrange of `range` starting at the first occurrence of `range`'s