Skip to content

[SetOperations] Support set containers with remove_if #96613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions llvm/include/llvm/ADT/SetOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@
#ifndef LLVM_ADT_SETOPERATIONS_H
#define LLVM_ADT_SETOPERATIONS_H

#include "llvm/ADT/STLExtras.h"

namespace llvm {

namespace detail {
template <typename Set, typename Fn>
using check_has_member_remove_if_t =
decltype(std::declval<Set>().remove_if(std::declval<Fn>()));

template <typename Set, typename Fn>
static constexpr bool HasMemberRemoveIf =
is_detected<check_has_member_remove_if_t, Set, Fn>::value;
} // namespace detail

/// set_union(A, B) - Compute A := A u B, return whether A changed.
///
template <class S1Ty, class S2Ty> bool set_union(S1Ty &S1, const S2Ty &S2) {
Expand All @@ -36,11 +48,16 @@ template <class S1Ty, class S2Ty> bool set_union(S1Ty &S1, const S2Ty &S2) {
/// elements that are not contained in S2.
///
template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
const auto &E = *I;
++I;
if (!S2.count(E))
S1.erase(E); // Erase element if not in S2
if constexpr (detail::HasMemberRemoveIf<S1Ty,
bool (*)(decltype(*S2.begin()))>) {
S1.remove_if([S2](const auto &E) { return !S2.count(E); });
} else {
for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
const auto &E = *I;
++I;
if (!S2.count(E))
S1.erase(E); // Erase element if not in S2
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/ADT/SetOperationsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SetVector.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -65,6 +66,16 @@ TEST(SetOperationsTest, SetIntersect) {
// is empty as they are non-overlapping.
EXPECT_THAT(Set1, IsEmpty());
EXPECT_EQ(ExpectedSet2, Set2);

// Check that set_intersect works on SetVector via remove_if.
SmallSetVector<int, 4> SV;
SV.insert(3);
SV.insert(6);
SV.insert(4);
SV.insert(5);
set_intersect(SV, Set2);
// SV should contain only 6 and 5 now.
EXPECT_EQ(SV.getArrayRef(), ArrayRef({6, 5}));
}

TEST(SetOperationsTest, SetIntersection) {
Expand Down
Loading