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 How One Sorted Sequence Includes Another #38

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

Conversation

CTMacUser
Copy link
Contributor

Description

Adds a new API for detecting how two already sorted sequences overlap. It is a generalization of the includes function from C++. It returns a custom type instead of a Boolean.

Detailed Design

/// The manner two (multi-)sets may overlap, including degenerate cases.
enum Inclusion {
  case bothUninhabited, onlyFirstInhabited, onlySecondInhabited,
       dualExclusivesOnly, sharedOnly, firstExtendsSecond,
       secondExtendsFirst, dualExclusivesAndShared
}

extension Inclusion {
  /// Whether there are elements exclusive to the first source.
  var hasExclusivesToFirst: Bool { get }
  /// Whether there are elements exclusive to the second source.
  var hasExclusivesToSecond: Bool { get }
  /// Whether there are elements shared by both sources.
  var hasSharedElements: Bool { get }
  /// Whether the sources are identical.
  var areIdentical: Bool { get }
  /// Whether the first source contains everything from the second.
  var doesFirstIncludeSecond: Bool { get }
  /// Whether the second source contains everything from the first.
  var doesSecondIncludeFirst: Bool { get }
}

extension Sequence {
  /// Returns how this sequence and the given sequence overlap, assuming both
  /// are sorted according to the given predicate that can compare elements.
  func sortedOverlap<S: Sequence>(
    with other: S,
    by areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> Inclusion where S.Element == Element
}

extension Sequence where Element: Comparable {
  /// Returns how this sequence and the given sequence overlap, assuming both
  /// are sorted.
  func sortedOverlap<S: Sequence>(
    with other: S
  ) -> Inclusion where S.Element == Element
}

Documentation Plan

The methods, type, and the type's cases and properties have block documents. There is also a guide file.

Test Plan

A test file is included.

Source Impact

It adds 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 method to sequences that takes another sequence and a comparison predicate, assumes both sequences are sorted along that predicate, and reports how much the sequences overlap in element values.  Add an overload that defaults the comparison to the standard less-than operator.  Add a type to express the form of set overlap, making optional distinction to degenerate sources.
Rename the type expressing kinds of set overlap, to be less general.  Explicitly list the preconditions for the set-overlap detection methods.
Forgot to update a file for a type rename from the last change.
Add a reference to the pull-request link. Change location of summary.
Change the name of the "sortedOverlap" methods to "degreeOfInclusion," to clarify what is being calculated.
Change the name of the source, test, and guide files to match the updated method name. Update their link in the README file.
Change some paragraphs with better explanations on design and usage. Change punctuation spacing to match the project's style.
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