Skip to content

Add filter method for ordered and unordered map #67501

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 14 commits into from
Oct 5, 2023

Conversation

mohanadkandil
Copy link
Contributor

@mohanadkandil mohanadkandil commented Jul 25, 2023

Implemented a filter method like the Swift stdlib one
Resolves #67400

@mohanadkandil
Copy link
Contributor Author

@swift-ci Please test

@finagolfin
Copy link
Member

If you modify the original comment to add "Resolves #67400," as shown when you submitted this pull, that issue will be linked to this pull.

@egorzhdan
Copy link
Contributor

@mohanadkandil thanks for the PR! You won't be able to run the CI just yet, this is fine, running the CI requires write access to the repo. I can run the CI for you in the future.

Let me know if you need any help with compiling this locally on your machine.

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohanadkandil please try building and running this on your machine

@mohanadkandil mohanadkandil marked this pull request as ready for review August 30, 2023 20:22
@CrazyFanFan
Copy link
Contributor

I have some personal insights regarding filter that I would like to share. Given that CxxDictionary does not require its Key to conform to Hashable, I believe it would be more reasonable for the filter function to return Self. Consequently, we need a method to create a new instance of Self using either std::map or std::unordered_map. I have attempted to write the code for this; however, I have yet to find a suitable method for implementing the init function.

@egorzhdan
Copy link
Contributor

I believe it would be more reasonable for the filter function to return Self

I agree. Another reason why it should return Self is that there is already a way to filter a std::map into a Swift Dictionary: Dictionary(myCxxMap).filter { ... }. However, there is no way to filter a std::map into a std::map, which is what you might want to do when working with a C++ library from Swift.

@mohanadkandil
Copy link
Contributor Author

mohanadkandil commented Sep 17, 2023

Hi @egorzhdan,
A̶f̶t̶e̶r̶ ̶p̶o̶s̶t̶i̶n̶g̶ ̶o̶n̶ ̶f̶o̶r̶u̶m̶s̶ ̶a̶n̶d̶ ̶c̶h̶a̶n̶g̶i̶n̶g̶ ̶t̶h̶e̶ ̶c̶o̶d̶e̶,̶ ̶b̶u̶i̶l̶d̶ ̶e̶r̶r̶o̶r̶s̶ ̶a̶r̶e̶ ̶n̶o̶t̶ ̶r̶e̶l̶a̶t̶e̶d̶ ̶t̶o̶ ̶m̶y̶ ̶c̶h̶a̶n̶g̶e̶s̶,̶ ̶I̶ ̶j̶u̶s̶t̶ ̶t̶r̶i̶e̶d̶ ̶a̶ ̶c̶o̶m̶p̶l̶e̶t̶e̶ ̶d̶i̶f̶f̶e̶r̶e̶n̶t̶ ̶b̶r̶a̶n̶c̶h̶ ̶a̶n̶d̶ ̶i̶t̶'̶s̶ ̶s̶t̶i̶l̶l̶ ̶g̶e̶t̶t̶i̶n̶g̶ ̶̶9̶ ̶e̶r̶r̶o̶r̶s̶ ̶g̶e̶n̶e̶r̶a̶t̶e̶d̶̶.̶ ̶H̶o̶w̶e̶v̶e̶r̶,̶ ̶t̶h̶e̶ ̶c̶h̶a̶n̶g̶e̶s̶ ̶a̶b̶o̶v̶e̶ ̶l̶i̶k̶e̶ ̶r̶e̶t̶u̶r̶n̶i̶n̶g̶ ̶s̶e̶l̶f̶ ̶a̶r̶e̶ ̶a̶l̶r̶e̶a̶d̶y̶ ̶c̶o̶m̶p̶l̶e̶t̶e̶d̶.̶ ̶

Also, I posted a more detailed post on the forum so maybe anyone could tell me about a workaround to sole DAK errors. It will be solved asap

All issues resolved ✅

@mohanadkandil mohanadkandil force-pushed the filter-method-for-std-map branch from b420685 to 62cf45f Compare September 18, 2023 11:02
@mohanadkandil
Copy link
Contributor Author

mohanadkandil commented Sep 18, 2023

Hi @egorzhdan
Just added a test for the filter and passed it.

Should we run the checks ?

Screenshot 2023-09-18 at 6 34 52 PM

@egorzhdan
Copy link
Contributor

@mohanadkandil awesome, let's run the CI!

@egorzhdan
Copy link
Contributor

@swift-ci please smoke test

@mohanadkandil
Copy link
Contributor Author

@egorzhdan Passed!
Do you have any comments in code before merge ?

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly looks good to me, thanks @mohanadkandil!
Left a couple comments.

@egorzhdan
Copy link
Contributor

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor

@swift-ci please smoke test

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Oct 5, 2023
@egorzhdan
Copy link
Contributor

@swift-ci please smoke test

Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me now, thank you @mohanadkandil!

@egorzhdan
Copy link
Contributor

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor

@swift-ci please smoke test macOS

@egorzhdan
Copy link
Contributor

@swift-ci please smoke test Linux

@egorzhdan
Copy link
Contributor

I'm going to merge this now. @mohanadkandil next time could you please squash all your commits in a PR into one commit 😉

@egorzhdan egorzhdan merged commit d58943a into swiftlang:main Oct 5, 2023
meg-gupta pushed a commit to meg-gupta/swift that referenced this pull request Oct 13, 2023
@mrousavy
Copy link

Sorry this is maybe the wrong place to ask this, but does Swift's umbrella header expose a swift::Dictionary<K, V> type, or how should passing around maps/dictionaries between Swift <-> C++ be handled?

@egorzhdan
Copy link
Contributor

does Swift's umbrella header expose a swift::Dictionary<K, V> type

Not at the moment, no.

how should passing around maps/dictionaries between Swift <-> C++ be handled

You could pass around an instance of std::map, that way you could access it from both Swift and C++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C++ std::map should provide a filter method
6 participants