OSDN Git Service

STLExtras: Add convenience is_contained() function.
authorDevin Coughlin <dcoughlin@apple.com>
Sat, 4 Jun 2016 00:49:46 +0000 (00:49 +0000)
committerDevin Coughlin <dcoughlin@apple.com>
Sat, 4 Jun 2016 00:49:46 +0000 (00:49 +0000)
This commit adds a convenience is_contained() function
which checks if an element exists in a container. It is part of a larger
series of patches adding an MPI checker to the clang static analyzer.

Reviewers: dblaikie,bkramer

A patch by Alexander Droste!

Differential Revision:http://reviews.llvm.org/D16053

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271757 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/STLExtras.h

index 11be196..72682f8 100644 (file)
@@ -412,6 +412,13 @@ auto remove_if(R &&Range, UnaryPredicate &&P) -> decltype(Range.begin()) {
   return std::remove_if(Range.begin(), Range.end(), P);
 }
 
+/// Wrapper function around std::find to detect if an element exists
+/// in a container.
+template <typename R, typename E>
+bool is_contained(R &&Range, const E &Element) {
+  return std::find(Range.begin(), Range.end(), Element) != Range.end();
+}
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <memory>
 //===----------------------------------------------------------------------===//