1 //===-- Support/SetOperations.h - Generic Set Operations ---------*- C++ -*--=//
3 // This file defines generic set operations that may be used on set's of
4 // different types, and different element types.
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_SUPPORT_SET_OPERATIONS_H
9 #define LLVM_SUPPORT_SET_OPERATIONS_H
11 // set_union(A, B) - Compute A := A u B, return whether A changed.
13 template <class S1Ty, class S2Ty>
14 bool set_union(S1Ty &S1, const S2Ty &S2) {
17 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();
19 if (S1.insert(*SI).second)
25 // set_intersect(A, B) - Compute A := A ^ B
26 // Identical to set_intersection, except that it works on set<>'s and
27 // is nicer to use. Functionally, this iterates through S1, removing
28 // elements that are not contained in S2.
30 template <template<class S1ElTy> class S1Ty, class ETy, class S2Ty>
31 void set_intersect(S1Ty<ETy> &S1, const S2Ty &S2) {
32 for (typename S1Ty<ETy>::iterator I = S1.begin(); I != S1.end();) {
35 if (!S2.count(E)) S1.erase(E); // Erase element if not in S2
39 // set_difference(A, B) - Return A - B
41 template <class S1Ty, class S2Ty>
42 S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
44 for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end();
46 if (!S2.count(*SI)) // if the element is not in set2
51 // set_subtract(A, B) - Compute A := A - B
53 template <class S1Ty, class S2Ty>
54 void set_subtract(S1Ty &S1, const S2Ty &S2) {
55 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();