1e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao//===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===// 2e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// 3e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// The LLVM Compiler Infrastructure 4e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// 5e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// This file is distributed under the University of Illinois Open Source 6e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// License. See LICENSE.TXT for details. 7e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// 8e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao//===----------------------------------------------------------------------===// 9e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// 10e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// This file defines generic set operations that may be used on set's of 11e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// different types, and different element types. 12e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao// 13e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao//===----------------------------------------------------------------------===// 14e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 15e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao#ifndef LLVM_ADT_SETOPERATIONS_H 16e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao#define LLVM_ADT_SETOPERATIONS_H 17e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 18e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaonamespace llvm { 19e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 20e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// set_union(A, B) - Compute A := A u B, return whether A changed. 21e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// 22e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaotemplate <class S1Ty, class S2Ty> 23e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaobool set_union(S1Ty &S1, const S2Ty &S2) { 24e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao bool Changed = false; 25e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 26e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); 27e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao SI != SE; ++SI) 28e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao if (S1.insert(*SI).second) 29e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao Changed = true; 30e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 31e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao return Changed; 32e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao} 33e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 34e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// set_intersect(A, B) - Compute A := A ^ B 35e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// Identical to set_intersection, except that it works on set<>'s and 36e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// is nicer to use. Functionally, this iterates through S1, removing 37e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// elements that are not contained in S2. 38e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// 39e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaotemplate <class S1Ty, class S2Ty> 40e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaovoid set_intersect(S1Ty &S1, const S2Ty &S2) { 41e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) { 42cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar const auto &E = *I; 43e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao ++I; 44e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao if (!S2.count(E)) S1.erase(E); // Erase element if not in S2 45e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao } 46e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao} 47e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 48e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// set_difference(A, B) - Return A - B 49e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// 50e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaotemplate <class S1Ty, class S2Ty> 51e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei LiaoS1Ty set_difference(const S1Ty &S1, const S2Ty &S2) { 52e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao S1Ty Result; 53e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end(); 54e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao SI != SE; ++SI) 55e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao if (!S2.count(*SI)) // if the element is not in set2 56e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao Result.insert(*SI); 57e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao return Result; 58e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao} 59e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 60e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// set_subtract(A, B) - Compute A := A - B 61e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao/// 62e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaotemplate <class S1Ty, class S2Ty> 63e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liaovoid set_subtract(S1Ty &S1, const S2Ty &S2) { 64e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end(); 65e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao SI != SE; ++SI) 66e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao S1.erase(*SI); 67e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao} 68e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 69e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao} // End llvm namespace 70e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao 71e264f62ca09a8f65c87a46d562a4d0f9ec5d457Shih-wei Liao#endif 72