LLVM 19.0.0git
SetOperations.h
Go to the documentation of this file.
1//===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines generic set operations that may be used on set's of
11/// different types, and different element types.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_SETOPERATIONS_H
16#define LLVM_ADT_SETOPERATIONS_H
17
18#include "llvm/ADT/STLExtras.h"
19
20namespace llvm {
21
22namespace detail {
23template <typename Set, typename Fn>
25 decltype(std::declval<Set>().remove_if(std::declval<Fn>()));
26
27template <typename Set, typename Fn>
28static constexpr bool HasMemberRemoveIf =
30} // namespace detail
31
32/// set_union(A, B) - Compute A := A u B, return whether A changed.
33///
34template <class S1Ty, class S2Ty> bool set_union(S1Ty &S1, const S2Ty &S2) {
35 bool Changed = false;
36
37 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); SI != SE;
38 ++SI)
39 if (S1.insert(*SI).second)
40 Changed = true;
41
42 return Changed;
43}
44
45/// set_intersect(A, B) - Compute A := A ^ B
46/// Identical to set_intersection, except that it works on set<>'s and
47/// is nicer to use. Functionally, this iterates through S1, removing
48/// elements that are not contained in S2.
49///
50template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
51 auto Pred = [&S2](const auto &E) { return !S2.count(E); };
52 if constexpr (detail::HasMemberRemoveIf<S1Ty, decltype(Pred)>) {
53 S1.remove_if(Pred);
54 } else {
55 for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
56 const auto &E = *I;
57 ++I;
58 if (!S2.count(E))
59 S1.erase(E); // Erase element if not in S2
60 }
61 }
62}
63
64template <class S1Ty, class S2Ty>
65S1Ty set_intersection_impl(const S1Ty &S1, const S2Ty &S2) {
66 S1Ty Result;
67 for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end(); SI != SE;
68 ++SI)
69 if (S2.count(*SI))
70 Result.insert(*SI);
71 return Result;
72}
73
74/// set_intersection(A, B) - Return A ^ B
75template <class S1Ty, class S2Ty>
76S1Ty set_intersection(const S1Ty &S1, const S2Ty &S2) {
77 if (S1.size() < S2.size())
78 return set_intersection_impl(S1, S2);
79 else
80 return set_intersection_impl(S2, S1);
81}
82
83/// set_difference(A, B) - Return A - B
84///
85template <class S1Ty, class S2Ty>
86S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
87 S1Ty Result;
88 for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end(); SI != SE;
89 ++SI)
90 if (!S2.count(*SI)) // if the element is not in set2
91 Result.insert(*SI);
92 return Result;
93}
94
95/// set_subtract(A, B) - Compute A := A - B
96///
97template <class S1Ty, class S2Ty> void set_subtract(S1Ty &S1, const S2Ty &S2) {
98 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); SI != SE;
99 ++SI)
100 S1.erase(*SI);
101}
102
103/// set_subtract(A, B, C, D) - Compute A := A - B, set C to the elements of B
104/// removed from A (A ^ B), and D to the elements of B not found in and removed
105/// from A (B - A).
106template <class S1Ty, class S2Ty>
107void set_subtract(S1Ty &S1, const S2Ty &S2, S1Ty &Removed, S1Ty &Remaining) {
108 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); SI != SE;
109 ++SI)
110 if (S1.erase(*SI))
111 Removed.insert(*SI);
112 else
113 Remaining.insert(*SI);
114}
115
116/// set_is_subset(A, B) - Return true iff A in B
117///
118template <class S1Ty, class S2Ty>
119bool set_is_subset(const S1Ty &S1, const S2Ty &S2) {
120 if (S1.size() > S2.size())
121 return false;
122 for (const auto It : S1)
123 if (!S2.count(It))
124 return false;
125 return true;
126}
127
128} // namespace llvm
129
130#endif
static const LLT S1
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains some templates that are useful if you are working with the STL at all.
decltype(std::declval< Set >().remove_if(std::declval< Fn >())) check_has_member_remove_if_t
Definition: SetOperations.h:25
static constexpr bool HasMemberRemoveIf
Definition: SetOperations.h:28
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void set_intersect(S1Ty &S1, const S2Ty &S2)
set_intersect(A, B) - Compute A := A ^ B Identical to set_intersection, except that it works on set<>...
Definition: SetOperations.h:50
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
void set_subtract(S1Ty &S1, const S2Ty &S2)
set_subtract(A, B) - Compute A := A - B
Definition: SetOperations.h:97
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
Definition: STLExtras.h:79
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
Definition: SetOperations.h:34
S1Ty set_intersection(const S1Ty &S1, const S2Ty &S2)
set_intersection(A, B) - Return A ^ B
Definition: SetOperations.h:76
S1Ty set_difference(const S1Ty &S1, const S2Ty &S2)
set_difference(A, B) - Return A - B
Definition: SetOperations.h:86
S1Ty set_intersection_impl(const S1Ty &S1, const S2Ty &S2)
Definition: SetOperations.h:65