LLVM API Documentation
00001 //===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines generic set operations that may be used on set's of 00011 // different types, and different element types. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_ADT_SETOPERATIONS_H 00016 #define LLVM_ADT_SETOPERATIONS_H 00017 00018 namespace llvm { 00019 00020 /// set_union(A, B) - Compute A := A u B, return whether A changed. 00021 /// 00022 template <class S1Ty, class S2Ty> 00023 bool set_union(S1Ty &S1, const S2Ty &S2) { 00024 bool Changed = false; 00025 00026 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); 00027 SI != SE; ++SI) 00028 if (S1.insert(*SI).second) 00029 Changed = true; 00030 00031 return Changed; 00032 } 00033 00034 /// set_intersect(A, B) - Compute A := A ^ B 00035 /// Identical to set_intersection, except that it works on set<>'s and 00036 /// is nicer to use. Functionally, this iterates through S1, removing 00037 /// elements that are not contained in S2. 00038 /// 00039 template <class S1Ty, class S2Ty> 00040 void set_intersect(S1Ty &S1, const S2Ty &S2) { 00041 for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) { 00042 const typename S1Ty::key_type &E = *I; 00043 ++I; 00044 if (!S2.count(E)) S1.erase(E); // Erase element if not in S2 00045 } 00046 } 00047 00048 /// set_difference(A, B) - Return A - B 00049 /// 00050 template <class S1Ty, class S2Ty> 00051 S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) { 00052 S1Ty Result; 00053 for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end(); 00054 SI != SE; ++SI) 00055 if (!S2.count(*SI)) // if the element is not in set2 00056 Result.insert(*SI); 00057 return Result; 00058 } 00059 00060 /// set_subtract(A, B) - Compute A := A - B 00061 /// 00062 template <class S1Ty, class S2Ty> 00063 void set_subtract(S1Ty &S1, const S2Ty &S2) { 00064 for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); 00065 SI != SE; ++SI) 00066 S1.erase(*SI); 00067 } 00068 00069 } // End llvm namespace 00070 00071 #endif