Skip to content

Commit f4eecc9

Browse files
committed
optimize contains method in CxxSet
1 parent 33b40cb commit f4eecc9

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,6 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
996996
insert->getResultInterfaceType());
997997
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxSet});
998998

999-
// If this isn't a std::multiset, try to also synthesize the conformance to
1000-
// CxxUniqueSet.
1001-
if (!isStdDecl(clangDecl, {"set", "unordered_set"}))
1002-
return;
1003-
1004999
ProtocolDecl *cxxInputIteratorProto =
10051000
ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator);
10061001
if (!cxxInputIteratorProto)
@@ -1025,6 +1020,12 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
10251020
rawIteratorTy);
10261021
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawMutableIterator"),
10271022
rawMutableIteratorTy);
1023+
1024+
// If this isn't a std::multiset, try to also synthesize the conformance to
1025+
// CxxUniqueSet.
1026+
if (!isStdDecl(clangDecl, {"set", "unordered_set"}))
1027+
return;
1028+
10281029
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxUniqueSet});
10291030
}
10301031

stdlib/public/Cxx/CxxSet.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
public protocol CxxSet<Element>: ExpressibleByArrayLiteral {
2020
associatedtype Element
2121
associatedtype Size: BinaryInteger
22+
associatedtype RawIterator: UnsafeCxxInputIterator
23+
where RawIterator.Pointee == Element
24+
associatedtype RawMutableIterator: UnsafeCxxInputIterator
25+
where RawMutableIterator.Pointee == Element
2226

2327
// std::pair<iterator, bool> for std::set and std::unordered_set
2428
// iterator for std::multiset
25-
associatedtype InsertionResult
29+
associatedtype InsertionResult
2630

2731
init()
2832

33+
func __endUnsafe() -> RawIterator
34+
func __findUnsafe(_ value: Element) -> RawIterator
35+
2936
@discardableResult
3037
mutating func __insertUnsafe(_ element: Element) -> InsertionResult
31-
3238
func count(_ element: Element) -> Size
3339
}
3440

@@ -56,7 +62,7 @@ extension CxxSet {
5662
/// in the set.
5763
@inlinable
5864
public func contains(_ element: Element) -> Bool {
59-
return count(element) > 0
65+
return self.__findUnsafe(element) != self.__endUnsafe()
6066
}
6167
}
6268

@@ -65,23 +71,11 @@ extension CxxSet {
6571
/// C++ standard library types such as `std::set` and `std::unordered_set`
6672
/// conform to this protocol.
6773
public protocol CxxUniqueSet<Element>: CxxSet {
68-
override associatedtype Element
69-
override associatedtype Size: BinaryInteger
70-
associatedtype RawIterator: UnsafeCxxInputIterator
71-
where RawIterator.Pointee == Element
72-
associatedtype RawMutableIterator: UnsafeCxxInputIterator
73-
where RawMutableIterator.Pointee == Element
7474
override associatedtype InsertionResult
7575
where InsertionResult: CxxPair<RawMutableIterator, Bool>
7676

77-
@discardableResult
78-
mutating func __findUnsafe(_ value: Element) -> RawIterator
79-
8077
@discardableResult
8178
mutating func __eraseUnsafe(_ iter: RawIterator) -> RawMutableIterator
82-
83-
@discardableResult
84-
mutating func __endUnsafe() -> RawIterator
8579
}
8680

8781
extension CxxUniqueSet {

0 commit comments

Comments
 (0)