llvm.org GIT mirror llvm / 87a991e
Implementation of Equivalence Classes git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6422 91177308-0d34-0410-b5e6-96231b3b80d8 Sumant Kowshik 17 years ago
2 changed file(s) with 186 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- Support/EquivalenceClasses.h -------------------------*- C++ -*--=//
1 //
2 // Generic implementation of equivalence classes and implementation of
3 // union-find algorithms
4 // A not-so-fancy implementation: 2 level tree i.e root and one more level
5 // Overhead of a union = size of the equivalence class being attached
6 // Overhead of a find = 1.
7 //
8 //===------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_EQUIVALENCE_CLASSES_H
11 #define LLVM_SUPPORT_EQUIVALENCE_CLASSES_H
12
13 #include
14 #include
15 #include
16 using std::map;
17 using std::set;
18 using std::vector;
19
20 template
21 class EquivalenceClasses {
22 // Maps each element to the element that is the leader of its
23 // equivalence class.
24 map Elem2ECLeaderMap;
25
26 // Make Element2 the leader of the union of classes Element1 and Element2
27 // Element1 and Element2 are presumed to be leaders of their respective
28 // equivalence classes.
29 void attach(ElemTy Element1, ElemTy Element2) {
30 for (typename map::iterator ElemI =
31 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
32 ElemI != ElemE; ++ElemI) {
33 if (ElemI->second == Element1)
34 Elem2ECLeaderMap[ElemI->first] = Element2;
35 }
36 }
37
38 public:
39
40 void addElement (ElemTy NewElement) {
41 if (Elem2ECLeaderMap.find(NewElement) == Elem2ECLeaderMap.end())
42 Elem2ECLeaderMap[NewElement] = NewElement;
43 }
44
45 ElemTy findClass(ElemTy Element) {
46 if (Elem2ECLeaderMap.find(Element) == Elem2ECLeaderMap.end())
47 return 0;
48 else
49 return Elem2ECLeaderMap[Element];
50 }
51
52 /// Attach the set with Element1 to the set with Element2 adding Element1 and
53 /// Element2 to the set of equivalence classes if they are not there already.
54 /// Implication: Make Element1 the element in the smaller set.
55 void unionElements(ElemTy Element1, ElemTy Element2) {
56 // If either Element1 or Element2 does not already exist, include it
57 if (Elem2ECLeaderMap.find(Element1) == Elem2ECLeaderMap.end())
58 Elem2ECLeaderMap[Element1] = Element1;
59 if (Elem2ECLeaderMap.find(Element2) == Elem2ECLeaderMap.end())
60 Elem2ECLeaderMap[Element2] = Element2;
61
62 attach(Elem2ECLeaderMap[Element1], Elem2ECLeaderMap[Element2]);
63 }
64
65 // Returns a vector containing all the elements in the equivalent class
66 // including Element1
67 vector getEqClass(ElemTy Element1) {
68 vector EqClass;
69
70 if (Elem2ECLeaderMap.find(EqClass) == Elem2ECLeaderMap.end())
71 return EqClass;
72
73 ElemTy classLeader = Elem2ECLeaderMap[Element1];
74
75 for (typename map::iterator ElemI =
76 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
77 ElemI != ElemE; ++ElemI) {
78 if (ElemI->second == classLeader)
79 EqClass.push_back(ElemI->first);
80 }
81
82 return EqClass;
83
84 }
85
86 map getLeaderMap() {
87 return Elem2ECLeaderMap ;
88 }
89
90 };
91
92 #endif
0 //===-- Support/EquivalenceClasses.h -------------------------*- C++ -*--=//
1 //
2 // Generic implementation of equivalence classes and implementation of
3 // union-find algorithms
4 // A not-so-fancy implementation: 2 level tree i.e root and one more level
5 // Overhead of a union = size of the equivalence class being attached
6 // Overhead of a find = 1.
7 //
8 //===------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_EQUIVALENCE_CLASSES_H
11 #define LLVM_SUPPORT_EQUIVALENCE_CLASSES_H
12
13 #include
14 #include
15 #include
16 using std::map;
17 using std::set;
18 using std::vector;
19
20 template
21 class EquivalenceClasses {
22 // Maps each element to the element that is the leader of its
23 // equivalence class.
24 map Elem2ECLeaderMap;
25
26 // Make Element2 the leader of the union of classes Element1 and Element2
27 // Element1 and Element2 are presumed to be leaders of their respective
28 // equivalence classes.
29 void attach(ElemTy Element1, ElemTy Element2) {
30 for (typename map::iterator ElemI =
31 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
32 ElemI != ElemE; ++ElemI) {
33 if (ElemI->second == Element1)
34 Elem2ECLeaderMap[ElemI->first] = Element2;
35 }
36 }
37
38 public:
39
40 void addElement (ElemTy NewElement) {
41 if (Elem2ECLeaderMap.find(NewElement) == Elem2ECLeaderMap.end())
42 Elem2ECLeaderMap[NewElement] = NewElement;
43 }
44
45 ElemTy findClass(ElemTy Element) {
46 if (Elem2ECLeaderMap.find(Element) == Elem2ECLeaderMap.end())
47 return 0;
48 else
49 return Elem2ECLeaderMap[Element];
50 }
51
52 /// Attach the set with Element1 to the set with Element2 adding Element1 and
53 /// Element2 to the set of equivalence classes if they are not there already.
54 /// Implication: Make Element1 the element in the smaller set.
55 void unionElements(ElemTy Element1, ElemTy Element2) {
56 // If either Element1 or Element2 does not already exist, include it
57 if (Elem2ECLeaderMap.find(Element1) == Elem2ECLeaderMap.end())
58 Elem2ECLeaderMap[Element1] = Element1;
59 if (Elem2ECLeaderMap.find(Element2) == Elem2ECLeaderMap.end())
60 Elem2ECLeaderMap[Element2] = Element2;
61
62 attach(Elem2ECLeaderMap[Element1], Elem2ECLeaderMap[Element2]);
63 }
64
65 // Returns a vector containing all the elements in the equivalent class
66 // including Element1
67 vector getEqClass(ElemTy Element1) {
68 vector EqClass;
69
70 if (Elem2ECLeaderMap.find(EqClass) == Elem2ECLeaderMap.end())
71 return EqClass;
72
73 ElemTy classLeader = Elem2ECLeaderMap[Element1];
74
75 for (typename map::iterator ElemI =
76 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
77 ElemI != ElemE; ++ElemI) {
78 if (ElemI->second == classLeader)
79 EqClass.push_back(ElemI->first);
80 }
81
82 return EqClass;
83
84 }
85
86 map getLeaderMap() {
87 return Elem2ECLeaderMap ;
88 }
89
90 };
91
92 #endif