-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add a cpl::contains(container, value) helper #10464
Conversation
template <typename C, typename V> | ||
inline bool contains(const C &container, const V &value) | ||
{ | ||
return container.count(value) != 0; |
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.
Hm, doesn't count
always need to do too much? It cannot stop at the first match.
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.
not for std::set and std::map, which are up to now the only users of that
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.
not for std::set and std::map
... unless some call triggers the second overload of count? (Taking an "equivalent" key instead of an exact one.)
up to now the only users of that
This is a very generic function interface, but the implementation is specific. It gets attention (read: review) when added or modified. It is unlikely to get attention when a different use is added elsewhere. Including in software using GDAL.
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.
Thanks for pointing me to this new C++14 second overload / "equivalent" key concept. I have a bit of a hard time fully understanding the impact of that , but my understanding of https://stackoverflow.com/questions/20317413/what-are-transparent-comparators, is that it is only available if you define something like a std::key or std::map specially constructed with a std::less<>, so that shouldn't occur with the current state of the code base. That said in #10518 I'm proposing to use a std::find() based implementation to avoid any performance issue.
It is unlikely to get attention when a different use is added elsewhere. Including in software using GDAL.
This can't as this is protected by a ifdef GDAL_COMPILATION. So for GDAL internal use only
and use it in gcore
@dbaston Thoughts? I'm quite tired of writing code like
container.find(key) != containers.end()
. As of C++17, we could usecontainer.count(key)
, as the closest alternative to C++20container.contains(key)
, but using count() might complicate future modernization when we bump to C++20. Thiscpl::contains(container, value)
should be easily greppable to automate a change tocontainer.contains(value)