This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Implement {DenseMap,MapVector,StringMap}::contains
ClosedPublic

Authored by kazu on Mar 12 2023, 10:23 PM.

Details

Summary

This patch implements the C++20-style contains() for DenseMap,
MapVector, and StringMap.

With this patch, every set and map container type that has count()
also has contains().

Diff Detail

Event Timeline

kazu created this revision.Mar 12 2023, 10:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 12 2023, 10:23 PM
kazu requested review of this revision.Mar 12 2023, 10:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 12 2023, 10:23 PM
kuhar accepted this revision.Mar 13 2023, 8:04 AM

LGTM.

Separately, it would be cool if we could make llvm::is_contained use the contains member when available, instead of doing a linear scan. It seems like a bit of a foot gun to me to have 2 functions with almost identical names but vastly different performance characteristics.

This revision is now accepted and ready to land.Mar 13 2023, 8:04 AM
This revision was automatically updated to reflect the committed changes.
kazu added a comment.Mar 13 2023, 9:41 AM

LGTM.

Separately, it would be cool if we could make llvm::is_contained use the contains member when available, instead of doing a linear scan. It seems like a bit of a foot gun to me to have 2 functions with almost identical names but vastly different performance characteristics.

Thanks for the reivew! I might just migrate those to $set.contains() or $map.contains(). In theory, it's possible to write a template that calls llvm::is_contained so that it can work with either an array or a set, but I don't know how common that is.

With some template tricks, we could implement a check for the contains() method. If so, we could do static_assert(!sizeof(T*), "Use contains() instead.") or something, at least in our working source tree, maybe not in production.