Skip to content

Commit

Permalink
[LLVM][ADT] Put both vesions of 'unique' into STLExtras.h (llvm#82312)
Browse files Browse the repository at this point in the history
Currently there are two versions of llvm::unique, one that requires a
predicate, and is in STLExtras.h; and one that does not require a
predicate, and is in GenericUniformityImpl.h. This moves the one from
GenericUniformityImp.h to STlExtras.h, so they are both together, and
can both be easily called from other places inside LLVM.
  • Loading branch information
cmtice authored Feb 20, 2024
1 parent 8ca351d commit d7a73c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 1 addition & 4 deletions llvm/include/llvm/ADT/GenericUniformityImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

#include "llvm/ADT/GenericUniformityInfo.h"

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SparseBitVector.h"
#include "llvm/ADT/StringExtras.h"
Expand All @@ -57,10 +58,6 @@

namespace llvm {

template <typename Range> auto unique(Range &&R) {
return std::unique(adl_begin(R), adl_end(R));
}

/// Construct a specially modified post-order traversal of cycles.
///
/// The ModifiedPO is contructed using a virtually modified CFG as follows:
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,12 @@ auto unique(Range &&R, Predicate P) {
return std::unique(adl_begin(R), adl_end(R), P);
}

/// Wrapper function around std::unique to allow calling unique on a
/// container without having to specify the begin/end iterators.
template <typename Range> auto unique(Range &&R) {
return std::unique(adl_begin(R), adl_end(R));
}

/// Wrapper function around std::equal to detect if pair-wise elements between
/// two ranges are the same.
template <typename L, typename R> bool equal(L &&LRange, R &&RRange) {
Expand Down
13 changes: 13 additions & 0 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,19 @@ TEST(STLExtras, Unique) {
EXPECT_EQ(3, V[3]);
}

TEST(STLExtras, UniqueNoPred) {
std::vector<uint32_t> V = {1, 5, 5, 4, 3, 3, 3};

auto I = llvm::unique(V);

EXPECT_EQ(I, V.begin() + 4);

EXPECT_EQ(1, V[0]);
EXPECT_EQ(5, V[1]);
EXPECT_EQ(4, V[2]);
EXPECT_EQ(3, V[3]);
}

TEST(STLExtrasTest, MakeVisitorOneCallable) {
auto IdentityLambda = [](auto X) { return X; };
auto IdentityVisitor = makeVisitor(IdentityLambda);
Expand Down

0 comments on commit d7a73c9

Please sign in to comment.