-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Optimize contains method implementation in CxxSet
#85488
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,22 @@ | |
| public protocol CxxSet<Element>: ExpressibleByArrayLiteral { | ||
| associatedtype Element | ||
| associatedtype Size: BinaryInteger | ||
| associatedtype RawIterator: UnsafeCxxInputIterator | ||
| where RawIterator.Pointee == Element | ||
| associatedtype RawMutableIterator: UnsafeCxxInputIterator | ||
| where RawMutableIterator.Pointee == Element | ||
|
|
||
| // std::pair<iterator, bool> for std::set and std::unordered_set | ||
| // iterator for std::multiset | ||
| associatedtype InsertionResult | ||
| associatedtype InsertionResult | ||
|
|
||
| init() | ||
|
|
||
| func __endUnsafe() -> RawIterator | ||
| func __findUnsafe(_ value: Element) -> RawIterator | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out! I removed mutating because neither That said, I recognize this is a breaking change to the public API. Though, since these methods are prefixed with I’d like to align with the project’s preferences here—would you recommend restoring the original signatures to preserve compatibility, or keeping the semantically accurate version? Happy to implement whichever approach makes the most sense.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Cxx and CxxStdlib overlays aren't part of the Swift's ABI stability guarantees, so it's totally fine to change their interfaces. For this reason these two libraries are distributed as part of the toolchain, not part of the SDK, and are version-locked to the compiler. As long as making these functions non- |
||
|
|
||
| @discardableResult | ||
| mutating func __insertUnsafe(_ element: Element) -> InsertionResult | ||
|
|
||
| func count(_ element: Element) -> Size | ||
| } | ||
|
|
||
|
|
@@ -56,7 +62,7 @@ extension CxxSet { | |
| /// in the set. | ||
| @inlinable | ||
| public func contains(_ element: Element) -> Bool { | ||
| return count(element) > 0 | ||
| return self.__findUnsafe(element) != self.__endUnsafe() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -65,23 +71,11 @@ extension CxxSet { | |
| /// C++ standard library types such as `std::set` and `std::unordered_set` | ||
| /// conform to this protocol. | ||
| public protocol CxxUniqueSet<Element>: CxxSet { | ||
| override associatedtype Element | ||
| override associatedtype Size: BinaryInteger | ||
| associatedtype RawIterator: UnsafeCxxInputIterator | ||
| where RawIterator.Pointee == Element | ||
| associatedtype RawMutableIterator: UnsafeCxxInputIterator | ||
| where RawMutableIterator.Pointee == Element | ||
| override associatedtype InsertionResult | ||
| where InsertionResult: CxxPair<RawMutableIterator, Bool> | ||
|
|
||
| @discardableResult | ||
| mutating func __findUnsafe(_ value: Element) -> RawIterator | ||
|
|
||
| @discardableResult | ||
| mutating func __eraseUnsafe(_ iter: RawIterator) -> RawMutableIterator | ||
|
|
||
| @discardableResult | ||
| mutating func __endUnsafe() -> RawIterator | ||
| } | ||
|
|
||
| extension CxxUniqueSet { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be
UnsafeCxxMutableInputIterator?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::setiterators are indeed immutable since modifying set elements would break the internal ordering. This aligns with the existing StdSet implementation in Swift's ClangImporter