Skip to content

[cxx-interop] Add std::set initializer that takes a Swift Sequence #65087

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

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/ClangImporter/ClangDerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,33 @@ void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
if (!valueType || !sizeType)
return;

auto insertId = ctx.getIdentifier("__insertUnsafe");
auto inserts = lookupDirectWithoutExtensions(decl, insertId);
FuncDecl *insert = nullptr;
for (auto candidate : inserts) {
if (auto candidateMethod = dyn_cast<FuncDecl>(candidate)) {
if (!candidateMethod->hasParameterList())
continue;
auto params = candidateMethod->getParameters();
if (params->size() != 1)
continue;
auto param = params->front();
if (param->getType()->getCanonicalType() !=
valueType->getUnderlyingType()->getCanonicalType())
continue;
insert = candidateMethod;
break;
}
}
if (!insert)
return;

impl.addSynthesizedTypealias(decl, ctx.Id_Element,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Size"),
sizeType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("InsertionResult"),
insert->getResultInterfaceType());
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxSet});
}

Expand Down
20 changes: 20 additions & 0 deletions stdlib/public/Cxx/CxxSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,31 @@
public protocol CxxSet<Element> {
associatedtype Element
associatedtype Size: BinaryInteger
associatedtype InsertionResult // std::pair<iterator, bool>

init()

@discardableResult
mutating func __insertUnsafe(_ element: Element) -> InsertionResult

func count(_ element: Element) -> Size
}

extension CxxSet {
/// Creates a C++ set containing the elements of a Swift Sequence.
///
/// This initializes the set by copying every element of the sequence.
///
/// - Complexity: O(*n*), where *n* is the number of elements in the Swift
/// sequence
@inlinable
public init<S: Sequence>(_ sequence: S) where S.Element == Element {
self.init()
for item in sequence {
self.__insertUnsafe(item)
}
}

@inlinable
public func contains(_ element: Element) -> Bool {
return count(element) > 0
Expand Down
14 changes: 14 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ StdSetTestSuite.test("MultisetOfCInt.contains") {
expectFalse(s.contains(3))
}

StdSetTestSuite.test("SetOfCInt.init()") {
let s = SetOfCInt([1, 3, 5])
expectTrue(s.contains(1))
expectFalse(s.contains(2))
expectTrue(s.contains(3))
}

StdSetTestSuite.test("UnorderedSetOfCInt.init()") {
let s = UnorderedSetOfCInt([1, 3, 5])
expectTrue(s.contains(1))
expectFalse(s.contains(2))
expectTrue(s.contains(3))
}

runAllTests()