blob: dab5d73257c9b7f5eb1c351d9ced4e656b354497 [file] [log] [blame]
Chris Lattner48486892003-09-30 18:37:50 +00001//===-- Support/EquivalenceClasses.h ----------------------------*- C++ -*-===//
Sumant Kowshik87a991e2003-05-29 22:44:25 +00002//
Chris Lattner48486892003-09-30 18:37:50 +00003// Generic implementation of equivalence classes and implementation of
4// union-find algorithms A not-so-fancy implementation: 2 level tree i.e root
5// and one more level Overhead of a union = size of the equivalence class being
6// attached Overhead of a find = 1.
Sumant Kowshik87a991e2003-05-29 22:44:25 +00007//
Chris Lattner48486892003-09-30 18:37:50 +00008//===----------------------------------------------------------------------===//
Sumant Kowshik87a991e2003-05-29 22:44:25 +00009
Brian Gaekea9f6e4a2003-06-17 00:35:55 +000010#ifndef SUPPORT_EQUIVALENCECLASSES_H
11#define SUPPORT_EQUIVALENCECLASSES_H
Sumant Kowshik87a991e2003-05-29 22:44:25 +000012
13#include <map>
Sumant Kowshik87a991e2003-05-29 22:44:25 +000014#include <vector>
Sumant Kowshik87a991e2003-05-29 22:44:25 +000015
16template <class ElemTy>
17class EquivalenceClasses {
18 // Maps each element to the element that is the leader of its
19 // equivalence class.
Sumant Kowshika3e57642003-06-04 08:00:05 +000020 std::map<ElemTy, ElemTy> Elem2ECLeaderMap;
Sumant Kowshik87a991e2003-05-29 22:44:25 +000021
22 // Make Element2 the leader of the union of classes Element1 and Element2
23 // Element1 and Element2 are presumed to be leaders of their respective
24 // equivalence classes.
25 void attach(ElemTy Element1, ElemTy Element2) {
Sumant Kowshika3e57642003-06-04 08:00:05 +000026 for (typename std::map<ElemTy, ElemTy>::iterator ElemI =
Sumant Kowshik87a991e2003-05-29 22:44:25 +000027 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
28 ElemI != ElemE; ++ElemI) {
29 if (ElemI->second == Element1)
30 Elem2ECLeaderMap[ElemI->first] = Element2;
31 }
32 }
33
34public:
35
36 void addElement (ElemTy NewElement) {
37 if (Elem2ECLeaderMap.find(NewElement) == Elem2ECLeaderMap.end())
38 Elem2ECLeaderMap[NewElement] = NewElement;
39 }
40
41 ElemTy findClass(ElemTy Element) {
42 if (Elem2ECLeaderMap.find(Element) == Elem2ECLeaderMap.end())
43 return 0;
44 else
45 return Elem2ECLeaderMap[Element];
46 }
47
48 /// Attach the set with Element1 to the set with Element2 adding Element1 and
49 /// Element2 to the set of equivalence classes if they are not there already.
50 /// Implication: Make Element1 the element in the smaller set.
Sumant Kowshika3e57642003-06-04 08:00:05 +000051 void unionSetsWith(ElemTy Element1, ElemTy Element2) {
Sumant Kowshik87a991e2003-05-29 22:44:25 +000052 // If either Element1 or Element2 does not already exist, include it
53 if (Elem2ECLeaderMap.find(Element1) == Elem2ECLeaderMap.end())
54 Elem2ECLeaderMap[Element1] = Element1;
55 if (Elem2ECLeaderMap.find(Element2) == Elem2ECLeaderMap.end())
56 Elem2ECLeaderMap[Element2] = Element2;
57
58 attach(Elem2ECLeaderMap[Element1], Elem2ECLeaderMap[Element2]);
59 }
60
61 // Returns a vector containing all the elements in the equivalent class
62 // including Element1
Sumant Kowshika3e57642003-06-04 08:00:05 +000063 std::vector<ElemTy> getEqClass(ElemTy Element1) {
64 std::vector<ElemTy> EqClass;
Sumant Kowshik87a991e2003-05-29 22:44:25 +000065
66 if (Elem2ECLeaderMap.find(EqClass) == Elem2ECLeaderMap.end())
67 return EqClass;
68
69 ElemTy classLeader = Elem2ECLeaderMap[Element1];
Sumant Kowshika3e57642003-06-04 08:00:05 +000070 for (typename std::map<ElemTy, ElemTy>::iterator ElemI =
Sumant Kowshik87a991e2003-05-29 22:44:25 +000071 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
72 ElemI != ElemE; ++ElemI) {
73 if (ElemI->second == classLeader)
74 EqClass.push_back(ElemI->first);
75 }
76
77 return EqClass;
Sumant Kowshik87a991e2003-05-29 22:44:25 +000078 }
79
Sumant Kowshika3e57642003-06-04 08:00:05 +000080 std::map<ElemTy, ElemTy>& getLeaderMap() {
Sumant Kowshik87a991e2003-05-29 22:44:25 +000081 return Elem2ECLeaderMap ;
82 }
Sumant Kowshik87a991e2003-05-29 22:44:25 +000083};
84
85#endif