1 //===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines generic set operations that may be used on set's of
10 // different types, and different element types.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SETOPERATIONS_H
15 #define LLVM_ADT_SETOPERATIONS_H
19 /// set_union(A, B) - Compute A := A u B, return whether A changed.
21 template <class S1Ty
, class S2Ty
>
22 bool set_union(S1Ty
&S1
, const S2Ty
&S2
) {
25 for (typename
S2Ty::const_iterator SI
= S2
.begin(), SE
= S2
.end();
27 if (S1
.insert(*SI
).second
)
33 /// set_intersect(A, B) - Compute A := A ^ B
34 /// Identical to set_intersection, except that it works on set<>'s and
35 /// is nicer to use. Functionally, this iterates through S1, removing
36 /// elements that are not contained in S2.
38 template <class S1Ty
, class S2Ty
>
39 void set_intersect(S1Ty
&S1
, const S2Ty
&S2
) {
40 for (typename
S1Ty::iterator I
= S1
.begin(); I
!= S1
.end();) {
43 if (!S2
.count(E
)) S1
.erase(E
); // Erase element if not in S2
47 /// set_difference(A, B) - Return A - B
49 template <class S1Ty
, class S2Ty
>
50 S1Ty
set_difference(const S1Ty
&S1
, const S2Ty
&S2
) {
52 for (typename
S1Ty::const_iterator SI
= S1
.begin(), SE
= S1
.end();
54 if (!S2
.count(*SI
)) // if the element is not in set2
59 /// set_subtract(A, B) - Compute A := A - B
61 template <class S1Ty
, class S2Ty
>
62 void set_subtract(S1Ty
&S1
, const S2Ty
&S2
) {
63 for (typename
S2Ty::const_iterator SI
= S2
.begin(), SE
= S2
.end();
68 } // End llvm namespace