blob: 46e626b69b585456001980dec42334d2662424a3 [file] [log] [blame]
Chris Lattner48486892003-09-30 18:37:50 +00001//===-- Support/EquivalenceClasses.h ----------------------------*- C++ -*-===//
Sumant Kowshik87a991e2003-05-29 22:44:25 +00002//
John Criswellb2109ce2003-10-20 19:46:57 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner48486892003-09-30 18:37:50 +000010// Generic implementation of equivalence classes and implementation of
11// union-find algorithms A not-so-fancy implementation: 2 level tree i.e root
12// and one more level Overhead of a union = size of the equivalence class being
13// attached Overhead of a find = 1.
Sumant Kowshik87a991e2003-05-29 22:44:25 +000014//
Chris Lattner48486892003-09-30 18:37:50 +000015//===----------------------------------------------------------------------===//
Sumant Kowshik87a991e2003-05-29 22:44:25 +000016
Brian Gaekea9f6e4a2003-06-17 00:35:55 +000017#ifndef SUPPORT_EQUIVALENCECLASSES_H
18#define SUPPORT_EQUIVALENCECLASSES_H
Sumant Kowshik87a991e2003-05-29 22:44:25 +000019
20#include <map>
Sumant Kowshik87a991e2003-05-29 22:44:25 +000021#include <vector>
Sumant Kowshik87a991e2003-05-29 22:44:25 +000022
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
24
Sumant Kowshik87a991e2003-05-29 22:44:25 +000025template <class ElemTy>
26class EquivalenceClasses {
27 // Maps each element to the element that is the leader of its
28 // equivalence class.
Sumant Kowshika3e57642003-06-04 08:00:05 +000029 std::map<ElemTy, ElemTy> Elem2ECLeaderMap;
Sumant Kowshik87a991e2003-05-29 22:44:25 +000030
31 // Make Element2 the leader of the union of classes Element1 and Element2
32 // Element1 and Element2 are presumed to be leaders of their respective
33 // equivalence classes.
34 void attach(ElemTy Element1, ElemTy Element2) {
Sumant Kowshika3e57642003-06-04 08:00:05 +000035 for (typename std::map<ElemTy, ElemTy>::iterator ElemI =
Sumant Kowshik87a991e2003-05-29 22:44:25 +000036 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
37 ElemI != ElemE; ++ElemI) {
38 if (ElemI->second == Element1)
39 Elem2ECLeaderMap[ElemI->first] = Element2;
40 }
41 }
42
43public:
44
45 void addElement (ElemTy NewElement) {
46 if (Elem2ECLeaderMap.find(NewElement) == Elem2ECLeaderMap.end())
47 Elem2ECLeaderMap[NewElement] = NewElement;
48 }
49
50 ElemTy findClass(ElemTy Element) {
51 if (Elem2ECLeaderMap.find(Element) == Elem2ECLeaderMap.end())
52 return 0;
53 else
54 return Elem2ECLeaderMap[Element];
55 }
56
57 /// Attach the set with Element1 to the set with Element2 adding Element1 and
58 /// Element2 to the set of equivalence classes if they are not there already.
59 /// Implication: Make Element1 the element in the smaller set.
Sumant Kowshika3e57642003-06-04 08:00:05 +000060 void unionSetsWith(ElemTy Element1, ElemTy Element2) {
Sumant Kowshik87a991e2003-05-29 22:44:25 +000061 // If either Element1 or Element2 does not already exist, include it
62 if (Elem2ECLeaderMap.find(Element1) == Elem2ECLeaderMap.end())
63 Elem2ECLeaderMap[Element1] = Element1;
64 if (Elem2ECLeaderMap.find(Element2) == Elem2ECLeaderMap.end())
65 Elem2ECLeaderMap[Element2] = Element2;
66
67 attach(Elem2ECLeaderMap[Element1], Elem2ECLeaderMap[Element2]);
68 }
69
70 // Returns a vector containing all the elements in the equivalent class
71 // including Element1
Sumant Kowshika3e57642003-06-04 08:00:05 +000072 std::vector<ElemTy> getEqClass(ElemTy Element1) {
73 std::vector<ElemTy> EqClass;
Sumant Kowshik87a991e2003-05-29 22:44:25 +000074
75 if (Elem2ECLeaderMap.find(EqClass) == Elem2ECLeaderMap.end())
76 return EqClass;
77
78 ElemTy classLeader = Elem2ECLeaderMap[Element1];
Sumant Kowshika3e57642003-06-04 08:00:05 +000079 for (typename std::map<ElemTy, ElemTy>::iterator ElemI =
Sumant Kowshik87a991e2003-05-29 22:44:25 +000080 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
81 ElemI != ElemE; ++ElemI) {
82 if (ElemI->second == classLeader)
83 EqClass.push_back(ElemI->first);
84 }
85
86 return EqClass;
Sumant Kowshik87a991e2003-05-29 22:44:25 +000087 }
88
Sumant Kowshika3e57642003-06-04 08:00:05 +000089 std::map<ElemTy, ElemTy>& getLeaderMap() {
Sumant Kowshik87a991e2003-05-29 22:44:25 +000090 return Elem2ECLeaderMap ;
91 }
Sumant Kowshik87a991e2003-05-29 22:44:25 +000092};
93
Brian Gaeked0fde302003-11-11 22:41:34 +000094} // End llvm namespace
95
Sumant Kowshik87a991e2003-05-29 22:44:25 +000096#endif