phobos icon indicating copy to clipboard operation
phobos copied to clipboard

[std.algorithm.searching] Add extrema to compute min and max

Open ntrel opened this issue 1 year ago • 9 comments

Performs < 3n/2 comparisons, unlike the naive < 2n. Implemented for an input range. TODO add alias map = a => a parameter, once design is confirmed OK.

ntrel avatar Mar 25 '23 12:03 ntrel

Thanks for your pull request and interest in making D better, @ntrel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8727"

dlang-bot avatar Mar 25 '23 12:03 dlang-bot

@atilaneves This is a new public function

dkorpel avatar Mar 25 '23 14:03 dkorpel

unlike the naive < 2n.

Unlike the naive version done where?

atilaneves avatar Mar 27 '23 21:03 atilaneves

Unlike the naive version done where?

https://dlang.org/phobos/std_algorithm_iteration.html#fold

// Compute minimum and maximum at the same time writeln(arr.fold!(min, max)); // tuple(1, 5)

ntrel avatar Mar 29 '23 11:03 ntrel

Unlike the naive version done where?

https://dlang.org/phobos/std_algorithm_iteration.html#fold

// Compute minimum and maximum at the same time writeln(arr.fold!(min, max)); // tuple(1, 5)

Do you mean min and max? I don't understand what fold has to do with this.

atilaneves avatar Apr 07 '23 11:04 atilaneves

@atilaneves min and max take pairs of elements, not ranges. The versions that take ranges are called minElement and maxElement.

pbackus avatar Apr 20 '23 00:04 pbackus

@ntrel @atilaneves how do we move further with this?

RazvanN7 avatar May 01 '23 11:05 RazvanN7

I still don't know what this is a replacement for.

atilaneves avatar May 02 '23 11:05 atilaneves

When a user wants to compute the min and max value of a range with Phobos, currently they need to call minElement and maxElement separately, resulting in 2n comparisons. By using the new extrema function, they're only doing 1.5n comparisons. It's somewhat similar to the sincos function being more efficient than separate sin and cos calls.

dkorpel avatar May 02 '23 11:05 dkorpel