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

Detection of One Sorted Sequence Being a Subset of Another #234

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

CTMacUser
Copy link
Contributor

Description

This function is an adaptation of the includes function from C++. This pull request is a sequel to "Detection of How One Sorted Sequence Includes Another" (#38).

I started with a new version of the Inclusion type and the sortedOverlap function from Pull #38. I added another function, includes, that calls the overlap detector then translates the exact overlap degree to a simple Bool result.

Then I figured that no one actually cares about the precise overlapping factor. So the precise-overlap function and support type were removed, and its code was moved into includes directly. Since a general answer wasn't required, I could add short-circuit logic.

Detailed Design

One primary function extends Sequence to test if a given sequence is a subset of the receiver, assuming both are sorted according to the given predicate. (The given sequence's elements need not be contiguous in the receiver.) The variant function removes the predicate parameter for a default of the less-than operator (<), at the cost of requiring Comparable conformance.

extension Sequence {
    public func
    includes<T: Sequence>(sorted other: T, sortedBy areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows -> Bool
    where T.Element == Element
}

extension Sequence where Element: Comparable {
    @inlinable public func
    includes<T: Sequence>(sorted other: T) -> Bool
    where T.Element == Element
}

Documentation Plan

Both introductory documentation and a guide were added.

Test Plan

A test file was added.

Source Impact

The functions are an additive change, not otherwise affecting the API.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Add a function for a sequence that takes another sequence and a predicate, assumes that both sequences are sorted to that predicate, and checks if the argument sequence is a subset of the receiver.

Add a function that takes the above function, but defaults the predicate to the less-than operator.

Add tests and documentation.
@CTMacUser
Copy link
Contributor Author

I made a discussion thread.

Change the overlap domain flags from a triplet of Boolean to a bit-masking integer. Make some variable names longer.
The changes to the return type in commit

    ba38c7a

were not updated in the documentation
Change the 3 Boolean inputs in the `overlap` functions that control early exits to one value of a custom type.

Add the custom type to the tests. Update the documentation.
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.

1 participant