10b57cec5SDimitry Andric //===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
81fd87a68SDimitry Andric ///
91fd87a68SDimitry Andric /// \file
101fd87a68SDimitry Andric /// This file defines generic set operations that may be used on set's of
111fd87a68SDimitry Andric /// different types, and different element types.
121fd87a68SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #ifndef LLVM_ADT_SETOPERATIONS_H
160b57cec5SDimitry Andric #define LLVM_ADT_SETOPERATIONS_H
170b57cec5SDimitry Andric
18*0fca6ea1SDimitry Andric #include "llvm/ADT/STLExtras.h"
19*0fca6ea1SDimitry Andric
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric
22*0fca6ea1SDimitry Andric namespace detail {
23*0fca6ea1SDimitry Andric template <typename Set, typename Fn>
24*0fca6ea1SDimitry Andric using check_has_member_remove_if_t =
25*0fca6ea1SDimitry Andric decltype(std::declval<Set>().remove_if(std::declval<Fn>()));
26*0fca6ea1SDimitry Andric
27*0fca6ea1SDimitry Andric template <typename Set, typename Fn>
28*0fca6ea1SDimitry Andric static constexpr bool HasMemberRemoveIf =
29*0fca6ea1SDimitry Andric is_detected<check_has_member_remove_if_t, Set, Fn>::value;
30*0fca6ea1SDimitry Andric
31*0fca6ea1SDimitry Andric template <typename Set>
32*0fca6ea1SDimitry Andric using check_has_member_erase_iter_t =
33*0fca6ea1SDimitry Andric decltype(std::declval<Set>().erase(std::declval<Set>().begin()));
34*0fca6ea1SDimitry Andric
35*0fca6ea1SDimitry Andric template <typename Set>
36*0fca6ea1SDimitry Andric static constexpr bool HasMemberEraseIter =
37*0fca6ea1SDimitry Andric is_detected<check_has_member_erase_iter_t, Set>::value;
38*0fca6ea1SDimitry Andric
39*0fca6ea1SDimitry Andric } // namespace detail
40*0fca6ea1SDimitry Andric
410b57cec5SDimitry Andric /// set_union(A, B) - Compute A := A u B, return whether A changed.
420b57cec5SDimitry Andric ///
set_union(S1Ty & S1,const S2Ty & S2)43*0fca6ea1SDimitry Andric template <class S1Ty, class S2Ty> bool set_union(S1Ty &S1, const S2Ty &S2) {
440b57cec5SDimitry Andric bool Changed = false;
450b57cec5SDimitry Andric
46*0fca6ea1SDimitry Andric for (const auto &E : S2)
47*0fca6ea1SDimitry Andric if (S1.insert(E).second)
480b57cec5SDimitry Andric Changed = true;
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric return Changed;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric /// set_intersect(A, B) - Compute A := A ^ B
540b57cec5SDimitry Andric /// Identical to set_intersection, except that it works on set<>'s and
550b57cec5SDimitry Andric /// is nicer to use. Functionally, this iterates through S1, removing
560b57cec5SDimitry Andric /// elements that are not contained in S2.
570b57cec5SDimitry Andric ///
set_intersect(S1Ty & S1,const S2Ty & S2)58*0fca6ea1SDimitry Andric template <class S1Ty, class S2Ty> void set_intersect(S1Ty &S1, const S2Ty &S2) {
59*0fca6ea1SDimitry Andric auto Pred = [&S2](const auto &E) { return !S2.count(E); };
60*0fca6ea1SDimitry Andric if constexpr (detail::HasMemberRemoveIf<S1Ty, decltype(Pred)>) {
61*0fca6ea1SDimitry Andric S1.remove_if(Pred);
62*0fca6ea1SDimitry Andric } else {
63*0fca6ea1SDimitry Andric typename S1Ty::iterator Next;
64*0fca6ea1SDimitry Andric for (typename S1Ty::iterator I = S1.begin(); I != S1.end(); I = Next) {
65*0fca6ea1SDimitry Andric Next = std::next(I);
66*0fca6ea1SDimitry Andric if (!S2.count(*I))
67*0fca6ea1SDimitry Andric S1.erase(I); // Erase element if not in S2
68*0fca6ea1SDimitry Andric }
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric
7206c3fb27SDimitry Andric template <class S1Ty, class S2Ty>
set_intersection_impl(const S1Ty & S1,const S2Ty & S2)7306c3fb27SDimitry Andric S1Ty set_intersection_impl(const S1Ty &S1, const S2Ty &S2) {
7406c3fb27SDimitry Andric S1Ty Result;
75*0fca6ea1SDimitry Andric for (const auto &E : S1)
76*0fca6ea1SDimitry Andric if (S2.count(E))
77*0fca6ea1SDimitry Andric Result.insert(E);
7806c3fb27SDimitry Andric return Result;
7906c3fb27SDimitry Andric }
8006c3fb27SDimitry Andric
8106c3fb27SDimitry Andric /// set_intersection(A, B) - Return A ^ B
8206c3fb27SDimitry Andric template <class S1Ty, class S2Ty>
set_intersection(const S1Ty & S1,const S2Ty & S2)8306c3fb27SDimitry Andric S1Ty set_intersection(const S1Ty &S1, const S2Ty &S2) {
8406c3fb27SDimitry Andric if (S1.size() < S2.size())
8506c3fb27SDimitry Andric return set_intersection_impl(S1, S2);
8606c3fb27SDimitry Andric else
8706c3fb27SDimitry Andric return set_intersection_impl(S2, S1);
8806c3fb27SDimitry Andric }
8906c3fb27SDimitry Andric
900b57cec5SDimitry Andric /// set_difference(A, B) - Return A - B
910b57cec5SDimitry Andric ///
920b57cec5SDimitry Andric template <class S1Ty, class S2Ty>
set_difference(const S1Ty & S1,const S2Ty & S2)930b57cec5SDimitry Andric S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
940b57cec5SDimitry Andric S1Ty Result;
95*0fca6ea1SDimitry Andric for (const auto &E : S1)
96*0fca6ea1SDimitry Andric if (!S2.count(E)) // if the element is not in set2
97*0fca6ea1SDimitry Andric Result.insert(E);
980b57cec5SDimitry Andric return Result;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric /// set_subtract(A, B) - Compute A := A - B
1020b57cec5SDimitry Andric ///
103*0fca6ea1SDimitry Andric /// Selects the set to iterate based on the relative sizes of A and B for better
104*0fca6ea1SDimitry Andric /// efficiency.
105*0fca6ea1SDimitry Andric ///
set_subtract(S1Ty & S1,const S2Ty & S2)106*0fca6ea1SDimitry Andric template <class S1Ty, class S2Ty> void set_subtract(S1Ty &S1, const S2Ty &S2) {
107*0fca6ea1SDimitry Andric // If S1 is smaller than S2, iterate on S1 provided that S2 supports efficient
108*0fca6ea1SDimitry Andric // lookups via contains(). Note that a couple callers pass a vector for S2,
109*0fca6ea1SDimitry Andric // which doesn't support contains(), and wouldn't be efficient if it did.
110*0fca6ea1SDimitry Andric using ElemTy = decltype(*S1.begin());
111*0fca6ea1SDimitry Andric if constexpr (detail::HasMemberContains<S2Ty, ElemTy>) {
112*0fca6ea1SDimitry Andric auto Pred = [&S2](const auto &E) { return S2.contains(E); };
113*0fca6ea1SDimitry Andric if constexpr (detail::HasMemberRemoveIf<S1Ty, decltype(Pred)>) {
114*0fca6ea1SDimitry Andric if (S1.size() < S2.size()) {
115*0fca6ea1SDimitry Andric S1.remove_if(Pred);
116*0fca6ea1SDimitry Andric return;
117*0fca6ea1SDimitry Andric }
118*0fca6ea1SDimitry Andric } else if constexpr (detail::HasMemberEraseIter<S1Ty>) {
119*0fca6ea1SDimitry Andric if (S1.size() < S2.size()) {
120*0fca6ea1SDimitry Andric typename S1Ty::iterator Next;
121*0fca6ea1SDimitry Andric for (typename S1Ty::iterator SI = S1.begin(), SE = S1.end(); SI != SE;
122*0fca6ea1SDimitry Andric SI = Next) {
123*0fca6ea1SDimitry Andric Next = std::next(SI);
124*0fca6ea1SDimitry Andric if (S2.contains(*SI))
125*0fca6ea1SDimitry Andric S1.erase(SI);
126*0fca6ea1SDimitry Andric }
127*0fca6ea1SDimitry Andric return;
128*0fca6ea1SDimitry Andric }
129*0fca6ea1SDimitry Andric }
130*0fca6ea1SDimitry Andric }
131*0fca6ea1SDimitry Andric
132*0fca6ea1SDimitry Andric for (const auto &E : S2)
133*0fca6ea1SDimitry Andric S1.erase(E);
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric
13606c3fb27SDimitry Andric /// set_subtract(A, B, C, D) - Compute A := A - B, set C to the elements of B
13706c3fb27SDimitry Andric /// removed from A (A ^ B), and D to the elements of B not found in and removed
13806c3fb27SDimitry Andric /// from A (B - A).
13906c3fb27SDimitry Andric template <class S1Ty, class S2Ty>
set_subtract(S1Ty & S1,const S2Ty & S2,S1Ty & Removed,S1Ty & Remaining)14006c3fb27SDimitry Andric void set_subtract(S1Ty &S1, const S2Ty &S2, S1Ty &Removed, S1Ty &Remaining) {
141*0fca6ea1SDimitry Andric for (const auto &E : S2)
142*0fca6ea1SDimitry Andric if (S1.erase(E))
143*0fca6ea1SDimitry Andric Removed.insert(E);
14406c3fb27SDimitry Andric else
145*0fca6ea1SDimitry Andric Remaining.insert(E);
14606c3fb27SDimitry Andric }
14706c3fb27SDimitry Andric
1485ffd83dbSDimitry Andric /// set_is_subset(A, B) - Return true iff A in B
1495ffd83dbSDimitry Andric ///
1505ffd83dbSDimitry Andric template <class S1Ty, class S2Ty>
set_is_subset(const S1Ty & S1,const S2Ty & S2)1515ffd83dbSDimitry Andric bool set_is_subset(const S1Ty &S1, const S2Ty &S2) {
1525ffd83dbSDimitry Andric if (S1.size() > S2.size())
1535ffd83dbSDimitry Andric return false;
154fe6060f1SDimitry Andric for (const auto It : S1)
1555ffd83dbSDimitry Andric if (!S2.count(It))
1565ffd83dbSDimitry Andric return false;
1575ffd83dbSDimitry Andric return true;
1585ffd83dbSDimitry Andric }
1595ffd83dbSDimitry Andric
160*0fca6ea1SDimitry Andric } // namespace llvm
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric #endif
163