summaryrefslogtreecommitdiff
path: root/include/llvm
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2002-02-05 03:35:10 +0000
committerChris Lattner <sabre@nondot.org>2002-02-05 03:35:10 +0000
commitedcea4ba4ae049d5bb5cdb2931a04200ac1f0e38 (patch)
tree627e985dd7a5d4f52d08a0e38127172a36990c7d /include/llvm
parentc980c50dc1e7c12601b2ea93da74fd3abd47d9e4 (diff)
downloadllvm-edcea4ba4ae049d5bb5cdb2931a04200ac1f0e38.tar.gz
llvm-edcea4ba4ae049d5bb5cdb2931a04200ac1f0e38.tar.bz2
llvm-edcea4ba4ae049d5bb5cdb2931a04200ac1f0e38.tar.xz
New Support file for operations on set like objects
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1714 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm')
-rw-r--r--include/llvm/ADT/SetOperations.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/include/llvm/ADT/SetOperations.h b/include/llvm/ADT/SetOperations.h
new file mode 100644
index 0000000000..d79d386380
--- /dev/null
+++ b/include/llvm/ADT/SetOperations.h
@@ -0,0 +1,60 @@
+//===-- Support/SetOperations.h - Generic Set Operations ---------*- C++ -*--=//
+//
+// This file defines generic set operations that may be used on set's of
+// different types, and different element types.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_SET_OPERATIONS_H
+#define LLVM_SUPPORT_SET_OPERATIONS_H
+
+// set_union(A, B) - Compute A := A u B, return whether A changed.
+//
+template <class S1Ty, class S2Ty>
+bool set_union(S1Ty &S1, const S2Ty &S2) {
+ 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;
+}
+
+// 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 <template<class S1ElTy> class S1Ty, class ETy, class S2Ty>
+void set_intersect(S1Ty<ETy> &S1, const S2Ty &S2) {
+ for (typename S1Ty<ETy>::iterator I = S1.begin(); I != S1.end();) {
+ const ETy &E = *I;
+ ++I;
+ if (!S2.count(E)) S1.erase(E); // Erase element if not in S2
+ }
+}
+
+// set_difference(A, B) - Return A - B
+//
+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;
+}
+
+// set_subtract(A, B) - Compute A := A - B
+//
+template <class S1Ty, class S2Ty>
+void set_subtract(S1Ty &S1, const S2Ty &S2) {
+ for (typename S2Ty::const_iterator SI = S2.begin(), SE = S2.end();
+ SI != SE; ++SI)
+ S1.erase(*SI);
+}
+
+#endif