Skip to content
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

Fix Issue 24541 - cartesianProduct should have a length for finite ranges #10657

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nicolo-mn
Copy link
Contributor

This Draft PR aims to address issue #9871 (Issue 24541 on BugZilla). As of now, I have added a length field to the cartesianProduct implementation with two finite forward ranges. Since I've already built the unittests for it I'm publishing this Draft PR in case anyone wants to give some feedback. I'm still working on the case of two finite ranges where one of them is not a forward range, particularly to replace the joiner returned in cartesianProduct with a sized range.

Changelog as of now:

  • Modified the Result struct inside cartesianProduct to include a length field.
  • Added checks on length in unittest regarding cartesianProduct with finite forward ranges.
  • Added a unittest to ensure the correct calculation of length when popping elements.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @nicolo-mn! 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#10657"

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the trailing whitespace to fix the style failures

@nicolo-mn nicolo-mn marked this pull request as ready for review March 4, 2025 14:37
@nicolo-mn nicolo-mn requested a review from PetarKirov as a code owner March 4, 2025 14:37
@nicolo-mn
Copy link
Contributor Author

length field in cartesianProduct with one finite input range and one finite forward range has been added

@nicolo-mn
Copy link
Contributor Author

@thewilsonator Could you check my changes when you got some time? Thanks in advance!

import std.array : join;
auto pairs = map!((ElementType!R1 a) => zip(repeat(a), range2.save))
(range1);
static if (!isInfinite!R1) return join(pairs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to turn cartesianProduct from a lazy range into an eager range. Surely we should check if hasLength!R1 && hasLength!R2 (after taking care of infinite ranges) and compute that.

@CyberShadow do you have a better solution?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of the same solution. Maybe we can use takeExactly to set the length if we can know it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hypothetical assumeLength range wrapper might be even better (like assumeSorted).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants