Skip to content

[REG 2.073.0] std.algorithm.searching.find on a SortedRange assumes that the comparison is the default #10883

@schveiguy

Description

@schveiguy

A SortedRange may carry with it a predicate, which means that the data is sorted according to the predicate, not the default comparison.

However, std.algorithm.searching.find has special casing for SortedRange where it assumes it can use the specialized search functions in it if the find predicate is the default. However, the correct comparison should that the predicates are identical!

A counter example:

import std.typecons;
import std.algorithm;
import std.range;

void main()
{
    auto arr = [tuple(2, 2), tuple(2, 1)];
    auto sr = arr.sort!((a, b) => a[0] < b[0]);
    assert(!arr.find(tuple(2, 1)).empty); // ok, linear search
    assert(!sr.find(tuple(2, 1)).empty); // fails
}

The relevant code is here:

static if (is(InputRange : SortedRange!TT, TT) && isDefaultPred)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Severity:RegressionIssues that are regressions/PRs that fix regressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions