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

[SR-735] SetAlgebraType: Make isStrictSupersetOf and isStrictSubsetOf Overridable #43350

Closed
swift-ci opened this issue Feb 14, 2016 · 1 comment
Labels
improvement standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-735
Radar None
Original Reporter plx (JIRA User)
Type Improvement
Status Closed
Resolution Won't Do

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: f1ad7fc6ea03210e72e4cd979653ba18

Issue Description:

At present, the `SetAlgebraType` defines `isStrictSupersetOf` and `isStrictSubsetOf` extension methods, but these are not overridable.

In many cases it is possible to do a better job of checking for strict-subset/strict-superset status than the default implementations here:

  @warn_unused_result
  public func isStrictSupersetOf(other: Self) -> Bool {
    return self.isSupersetOf(other) && self != other
  }

  @warn_unused_result
  public func isStrictSubsetOf(other: Self) -> Bool {
    return other.isStrictSupersetOf(self)
  }

...in particular, note that the default implementations for `isSubsetOf` and `isSupersetOf` are here:

  @warn_unused_result
  public func isSubsetOf(other: Self) -> Bool {
    return self.intersect(other) == self
  }

  @warn_unused_result
  public func isSupersetOf(other: Self) -> Bool {
    return other.isSubsetOf(self)
  }

...(aside: why does `isSupersetOf` call `isSubsetOf`, but it's the other way around for `isStrictSupersetOf` (it gets called-by `isStrictSubsetOf`)?).

If you expand it out, this means that the default implementation of `isStrictSupersetOf` is equivalent to:

  @warn_unused_result
  public func isStrictSupersetOf(other: Self) -> Bool {
    return other.intersect(self) == other && self != other
  }

...which is very easy to beat in many cases (e.g. in any case for which the `==` and `!=` operations are not O(1)); put differently, it's easy to "fuse" the "subset" and the "strict" parts of these checks into a single iteration.

I've attached a file containing a sketch of one concrete type for which you can easily improve on the default implementation of `isStrictSuperset` and `isStrictSuperset`.

@swift-ci
Copy link
Collaborator Author

Comment by plx (JIRA)

Ultimately irrelevant.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant