Skip to content

Commit

Permalink
ADT: Move ArrayRef comparison operators into the class
Browse files Browse the repository at this point in the history
Summary:
This allows the implicit ArrayRef conversions to kick in when e.g.
comparing ArrayRef to a SmallVector.

Reviewers: zturner, dblaikie

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D48632

llvm-svn: 335839
  • Loading branch information
labath committed Jun 28, 2018
1 parent 057ce39 commit 33aa5c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
26 changes: 10 additions & 16 deletions llvm/include/llvm/ADT/ArrayRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ namespace llvm {
return std::vector<T>(Data, Data+Length);
}

/// @}
/// @name Comparison operators
/// @{

friend bool operator==(ArrayRef LHS, ArrayRef RHS) {
return LHS.equals(RHS);
}

friend bool operator!=(ArrayRef LHS, ArrayRef RHS) { return !(LHS == RHS); }

/// @}
};

Expand Down Expand Up @@ -510,22 +520,6 @@ namespace llvm {
return MutableArrayRef<T>(data, length);
}

/// @}
/// @name ArrayRef Comparison Operators
/// @{

template<typename T>
inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
return LHS.equals(RHS);
}

template<typename T>
inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
return !(LHS == RHS);
}

/// @}

// ArrayRefs can be treated like a POD type.
template <typename T> struct isPodLike;
template <typename T> struct isPodLike<ArrayRef<T>> {
Expand Down
4 changes: 4 additions & 0 deletions llvm/unittests/ADT/ArrayRefTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ TEST(ArrayRefTest, Equals) {
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}));

SmallVector<int, 8> V1{1, 2, 3, 4, 5, 6, 7, 8};
EXPECT_EQ(AR1, V1);
EXPECT_EQ(V1, AR1);
}

TEST(ArrayRefTest, EmptyEquals) {
Expand Down

0 comments on commit 33aa5c5

Please sign in to comment.