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

Support "in" operator for STL containers #3652

Open
cmpute opened this issue May 30, 2020 · 5 comments · May be fixed by #5340
Open

Support "in" operator for STL containers #3652

cmpute opened this issue May 30, 2020 · 5 comments · May be fixed by #5340

Comments

@cmpute
Copy link

cmpute commented May 30, 2020

This is a feature request for supporting python-like "in" operator for STL containers (vector, map, etc.)

For example to test whether a set contains a value in python:

a = set([1, 2])
assert 1 in a

But right now similar operator in Cython is not supported:

from libcpp.set cimport set
cdef set a = [1, 2]
assert 1 in a

The compiler will complain Invalid types for 'in' or Invalid types for 'not_in'

This is not critically necessary but it's useful

@da-woods
Copy link
Contributor

The one thing against it is that it's wrapping an operation that doesn't quite exist in the C++ STL. Obviously you can do it with something like std::find(a.begin(), a.end(), 1) != a.end(), but the there isn't an std::contains for example.

@cmpute
Copy link
Author

cmpute commented May 30, 2020

Yes, but actually almost all containers support find function, which can be used to implement this feature which is common in python side

@scoder
Copy link
Contributor

scoder commented Jun 1, 2020

We support for … in <c++ iterable>, so I think supporting in via the same iteration mechanism is reasonable. PR welcome.

This can be implemented via a generated C++ function, one per container type. See the C++ container conversion functions in CppConvert.pyx, for example. This wouldn't be done through the type instance (like the container conversion), but through a function generated in PrimaryCmpNode, which handles the in and not in operators (and others). It can be implemented in Cython code.

@jhelgert
Copy link
Contributor

jhelgert commented Oct 23, 2022

Just stumbled upon this. I think it's worth mentioning that C++20 added .contains() methods for all the associated STL containers (map, multimap, set, multiset and the _unordered ones) making this less verbose.

@da-woods
Copy link
Contributor

Thanks @jhelgert - I suspect practically we'd want to support C++97 still for this (it seems like kind of a basic feature to require C++20 for).

I actually think this is easier than Scoder says and it's a very small number of template functions rather than needing generated code. I'll have a look at this sometime fairly soon.

@EpigeneMax EpigeneMax linked a pull request Mar 27, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants