[ADT] Add a remarkbly useful little helper routine to ArrayRef for
authorChandler Carruth <chandlerc@gmail.com>
Sun, 27 Jul 2014 01:11:19 +0000 (01:11 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sun, 27 Jul 2014 01:11:19 +0000 (01:11 +0000)
checking whether the ArrayRef is equal to an explicit list of arguments.

This is particularly easy to implement even without variadic templates
because ArrayRef happens to be homogeneously typed. As a consequence we
can use a "clever" wrapper type and default arguments to capture in
a single method many arguments as well as *how many* arguments the user
specified.

Thanks to Dave Blaikie for helping me pull together this little helper.
Suggestions for how to improve or generalize it are of course welcome.
I'll be using it immediately in my follow-up patch. =D

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

include/llvm/ADT/ArrayRef.h
unittests/ADT/ArrayRefTest.cpp

index 0fff505d8d012e8b49a5e31bba55ebbe9736b807..44400d591da9b1186a48f43043d5544948963e2a 100644 (file)
@@ -11,6 +11,7 @@
 #define LLVM_ADT_ARRAYREF_H
 
 #include "llvm/ADT/None.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include <vector>
 
@@ -43,6 +44,19 @@ namespace llvm {
     /// The number of elements.
     size_type Length;
 
+    /// \brief A dummy "optional" type that is only created by implicit
+    /// conversion from a reference to T.
+    ///
+    /// This type must *only* be used in a function argument or as a copy of
+    /// a function argument, as otherwise it will hold a pointer to a temporary
+    /// past that temporaries' lifetime.
+    struct TRefOrNothing {
+      const T *TPtr;
+
+      TRefOrNothing() : TPtr(nullptr) {}
+      TRefOrNothing(const T &TRef) : TPtr(&TRef) {}
+    };
+
   public:
     /// @name Constructors
     /// @{
@@ -175,6 +189,47 @@ namespace llvm {
       return std::vector<T>(Data, Data+Length);
     }
 
+    /// @}
+    /// @{
+    /// @name Convenience methods
+
+    /// @brief Predicate for testing that the array equals the exact sequence of
+    /// arguments.
+    ///
+    /// Will return false if the size is not equal to the exact number of
+    /// arguments given or if the array elements don't equal the argument
+    /// elements in order. Currently supports up to 16 arguments, but can
+    /// easily be extended.
+    bool equals(TRefOrNothing Arg0 = TRefOrNothing(),
+                TRefOrNothing Arg1 = TRefOrNothing(),
+                TRefOrNothing Arg2 = TRefOrNothing(),
+                TRefOrNothing Arg3 = TRefOrNothing(),
+                TRefOrNothing Arg4 = TRefOrNothing(),
+                TRefOrNothing Arg5 = TRefOrNothing(),
+                TRefOrNothing Arg6 = TRefOrNothing(),
+                TRefOrNothing Arg7 = TRefOrNothing(),
+                TRefOrNothing Arg8 = TRefOrNothing(),
+                TRefOrNothing Arg9 = TRefOrNothing(),
+                TRefOrNothing Arg10 = TRefOrNothing(),
+                TRefOrNothing Arg11 = TRefOrNothing(),
+                TRefOrNothing Arg12 = TRefOrNothing(),
+                TRefOrNothing Arg13 = TRefOrNothing(),
+                TRefOrNothing Arg14 = TRefOrNothing(),
+                TRefOrNothing Arg15 = TRefOrNothing()) {
+      TRefOrNothing Args[] = {Arg0,  Arg1,  Arg2,  Arg3, Arg4,  Arg5,
+                              Arg6,  Arg7,  Arg8,  Arg9, Arg10, Arg11,
+                              Arg12, Arg13, Arg14, Arg15};
+      if (size() > array_lengthof(Args))
+        return false;
+
+      for (unsigned i = 0, e = size(); i != e; ++i)
+        if (Args[i].TPtr == nullptr || (*this)[i] != *Args[i].TPtr)
+          return false;
+
+      // Either the size is exactly as many args, or the next arg must be null.
+      return size() == array_lengthof(Args) || Args[size()].TPtr == nullptr;
+    }
+
     /// @}
   };
 
index 293afc6ea37d25f1cc0ec8a1c1e77fb9a948724f..83a012d0cb1daec218392bef5c8c5efecfc91806 100644 (file)
@@ -36,5 +36,27 @@ TEST(ArrayRefTest, DropBack) {
   EXPECT_TRUE(AR1.drop_back().equals(AR2));
 }
 
+TEST(ArrayRefTest, Equals) {
+  static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
+  ArrayRef<int> AR1(A1);
+  EXPECT_TRUE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 8));
+  EXPECT_FALSE(AR1.equals(8, 1, 2, 4, 5, 6, 6, 7));
+  EXPECT_FALSE(AR1.equals(2, 4, 5, 6, 6, 7, 8, 1));
+  EXPECT_FALSE(AR1.equals(0, 1, 2, 4, 5, 6, 6, 7));
+  EXPECT_FALSE(AR1.equals(1, 2, 42, 4, 5, 6, 7, 8));
+  EXPECT_FALSE(AR1.equals(42, 2, 3, 4, 5, 6, 7, 8));
+  EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 42));
+  EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7));
+  EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 8, 9));
+
+  ArrayRef<int> AR1a = AR1.drop_back();
+  EXPECT_TRUE(AR1a.equals(1, 2, 3, 4, 5, 6, 7));
+  EXPECT_FALSE(AR1a.equals(1, 2, 3, 4, 5, 6, 7, 8));
+
+  ArrayRef<int> AR1b = AR1a.slice(2, 4);
+  EXPECT_TRUE(AR1b.equals(3, 4, 5, 6));
+  EXPECT_FALSE(AR1b.equals(2, 3, 4, 5, 6));
+  EXPECT_FALSE(AR1b.equals(3, 4, 5, 6, 7));
+}
 
 } // end anonymous namespace