blob: 01e8c5ef8209bd1d17916bde446724f3790f019f [file] [log] [blame]
Chris Lattner555eaf52003-09-30 18:37:50 +00001//===-- Support/EquivalenceClasses.h ----------------------------*- C++ -*-===//
Sumant Kowshik26182722003-05-29 22:44:25 +00002//
John Criswelldd043292003-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 Lattner555eaf52003-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 Kowshik26182722003-05-29 22:44:25 +000014//
Chris Lattner555eaf52003-09-30 18:37:50 +000015//===----------------------------------------------------------------------===//
Sumant Kowshik26182722003-05-29 22:44:25 +000016
Brian Gaekea7a50132003-06-17 00:35:55 +000017#ifndef SUPPORT_EQUIVALENCECLASSES_H
18#define SUPPORT_EQUIVALENCECLASSES_H
Sumant Kowshik26182722003-05-29 22:44:25 +000019
20#include <map>
Sumant Kowshik26182722003-05-29 22:44:25 +000021#include <vector>
Sumant Kowshik26182722003-05-29 22:44:25 +000022
23template <class ElemTy>
24class EquivalenceClasses {
25 // Maps each element to the element that is the leader of its
26 // equivalence class.
Sumant Kowshik061d1552003-06-04 08:00:05 +000027 std::map<ElemTy, ElemTy> Elem2ECLeaderMap;
Sumant Kowshik26182722003-05-29 22:44:25 +000028
29 // Make Element2 the leader of the union of classes Element1 and Element2
30 // Element1 and Element2 are presumed to be leaders of their respective
31 // equivalence classes.
32 void attach(ElemTy Element1, ElemTy Element2) {
Sumant Kowshik061d1552003-06-04 08:00:05 +000033 for (typename std::map<ElemTy, ElemTy>::iterator ElemI =
Sumant Kowshik26182722003-05-29 22:44:25 +000034 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
35 ElemI != ElemE; ++ElemI) {
36 if (ElemI->second == Element1)
37 Elem2ECLeaderMap[ElemI->first] = Element2;
38 }
39 }
40
41public:
42
43 void addElement (ElemTy NewElement) {
44 if (Elem2ECLeaderMap.find(NewElement) == Elem2ECLeaderMap.end())
45 Elem2ECLeaderMap[NewElement] = NewElement;
46 }
47
48 ElemTy findClass(ElemTy Element) {
49 if (Elem2ECLeaderMap.find(Element) == Elem2ECLeaderMap.end())
50 return 0;
51 else
52 return Elem2ECLeaderMap[Element];
53 }
54
55 /// Attach the set with Element1 to the set with Element2 adding Element1 and
56 /// Element2 to the set of equivalence classes if they are not there already.
57 /// Implication: Make Element1 the element in the smaller set.
Sumant Kowshik061d1552003-06-04 08:00:05 +000058 void unionSetsWith(ElemTy Element1, ElemTy Element2) {
Sumant Kowshik26182722003-05-29 22:44:25 +000059 // If either Element1 or Element2 does not already exist, include it
60 if (Elem2ECLeaderMap.find(Element1) == Elem2ECLeaderMap.end())
61 Elem2ECLeaderMap[Element1] = Element1;
62 if (Elem2ECLeaderMap.find(Element2) == Elem2ECLeaderMap.end())
63 Elem2ECLeaderMap[Element2] = Element2;
64
65 attach(Elem2ECLeaderMap[Element1], Elem2ECLeaderMap[Element2]);
66 }
67
68 // Returns a vector containing all the elements in the equivalent class
69 // including Element1
Sumant Kowshik061d1552003-06-04 08:00:05 +000070 std::vector<ElemTy> getEqClass(ElemTy Element1) {
71 std::vector<ElemTy> EqClass;
Sumant Kowshik26182722003-05-29 22:44:25 +000072
73 if (Elem2ECLeaderMap.find(EqClass) == Elem2ECLeaderMap.end())
74 return EqClass;
75
76 ElemTy classLeader = Elem2ECLeaderMap[Element1];
Sumant Kowshik061d1552003-06-04 08:00:05 +000077 for (typename std::map<ElemTy, ElemTy>::iterator ElemI =
Sumant Kowshik26182722003-05-29 22:44:25 +000078 Elem2ECLeaderMap.begin(), ElemE = Elem2ECLeaderMap.end();
79 ElemI != ElemE; ++ElemI) {
80 if (ElemI->second == classLeader)
81 EqClass.push_back(ElemI->first);
82 }
83
84 return EqClass;
Sumant Kowshik26182722003-05-29 22:44:25 +000085 }
86
Sumant Kowshik061d1552003-06-04 08:00:05 +000087 std::map<ElemTy, ElemTy>& getLeaderMap() {
Sumant Kowshik26182722003-05-29 22:44:25 +000088 return Elem2ECLeaderMap ;
89 }
Sumant Kowshik26182722003-05-29 22:44:25 +000090};
91
92#endif