2004-09-01 22:55:40 +00:00
|
|
|
//===-- llvm/ADT/SetOperations.h - Generic Set Operations -------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-05 03:35:10 +00:00
|
|
|
//
|
|
|
|
// This file defines generic set operations that may be used on set's of
|
|
|
|
// different types, and different element types.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#ifndef LLVM_ADT_SETOPERATIONS_H
|
|
|
|
#define LLVM_ADT_SETOPERATIONS_H
|
2002-02-05 03:35:10 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2004-10-13 15:09:21 +00:00
|
|
|
/// set_union(A, B) - Compute A := A u B, return whether A changed.
|
|
|
|
///
|
2002-02-05 03:35:10 +00:00
|
|
|
template <class S1Ty, class S2Ty>
|
2005-04-21 20:19:05 +00:00
|
|
|
bool set_union(S1Ty &S1, const S2Ty &S2) {
|
2002-02-05 03:35:10 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();
|
|
|
|
SI != SE; ++SI)
|
|
|
|
if (S1.insert(*SI).second)
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2004-10-13 15:09:21 +00:00
|
|
|
/// set_intersect(A, B) - Compute A := A ^ B
|
|
|
|
/// Identical to set_intersection, except that it works on set<>'s and
|
|
|
|
/// is nicer to use. Functionally, this iterates through S1, removing
|
|
|
|
/// elements that are not contained in S2.
|
|
|
|
///
|
|
|
|
template <class S1Ty, class S2Ty>
|
|
|
|
void set_intersect(S1Ty &S1, const S2Ty &S2) {
|
|
|
|
for (typename S1Ty::iterator I = S1.begin(); I != S1.end();) {
|
|
|
|
const typename S1Ty::key_type &E = *I;
|
|
|
|
++I;
|
|
|
|
if (!S2.count(E)) S1.erase(E); // Erase element if not in S2
|
|
|
|
}
|
2002-02-05 03:35:10 +00:00
|
|
|
}
|
|
|
|
|
2004-10-13 15:09:21 +00:00
|
|
|
/// set_difference(A, B) - Return A - B
|
|
|
|
///
|
2002-02-05 03:35:10 +00:00
|
|
|
template <class S1Ty, class S2Ty>
|
|
|
|
S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
|
|
|
|
S1Ty Result;
|
|
|
|
for (typename S1Ty::const_iterator SI = S1.begin(), SE = S1.end();
|
|
|
|
SI != SE; ++SI)
|
|
|
|
if (!S2.count(*SI)) // if the element is not in set2
|
|
|
|
Result.insert(*SI);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2004-10-13 15:09:21 +00:00
|
|
|
/// set_subtract(A, B) - Compute A := A - B
|
|
|
|
///
|
2002-02-05 03:35:10 +00:00
|
|
|
template <class S1Ty, class S2Ty>
|
2005-04-21 20:19:05 +00:00
|
|
|
void set_subtract(S1Ty &S1, const S2Ty &S2) {
|
2002-02-05 03:35:10 +00:00
|
|
|
for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();
|
2005-04-21 20:19:05 +00:00
|
|
|
SI != SE; ++SI)
|
2002-02-05 03:35:10 +00:00
|
|
|
S1.erase(*SI);
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-02-05 03:35:10 +00:00
|
|
|
#endif
|