LLVM API Documentation
00001 //===-- llvm/ADT/IntEqClasses.cpp - Equivalence Classes of Integers -------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Equivalence classes for small integers. This is a mapping of the integers 00011 // 0 .. N-1 into M equivalence classes numbered 0 .. M-1. 00012 // 00013 // Initially each integer has its own equivalence class. Classes are joined by 00014 // passing a representative member of each class to join(). 00015 // 00016 // Once the classes are built, compress() will number them 0 .. M-1 and prevent 00017 // further changes. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #include "llvm/ADT/IntEqClasses.h" 00022 00023 using namespace llvm; 00024 00025 void IntEqClasses::grow(unsigned N) { 00026 assert(NumClasses == 0 && "grow() called after compress()."); 00027 EC.reserve(N); 00028 while (EC.size() < N) 00029 EC.push_back(EC.size()); 00030 } 00031 00032 void IntEqClasses::join(unsigned a, unsigned b) { 00033 assert(NumClasses == 0 && "join() called after compress()."); 00034 unsigned eca = EC[a]; 00035 unsigned ecb = EC[b]; 00036 // Update pointers while searching for the leaders, compressing the paths 00037 // incrementally. The larger leader will eventually be updated, joining the 00038 // classes. 00039 while (eca != ecb) 00040 if (eca < ecb) 00041 EC[b] = eca, b = ecb, ecb = EC[b]; 00042 else 00043 EC[a] = ecb, a = eca, eca = EC[a]; 00044 } 00045 00046 unsigned IntEqClasses::findLeader(unsigned a) const { 00047 assert(NumClasses == 0 && "findLeader() called after compress()."); 00048 while (a != EC[a]) 00049 a = EC[a]; 00050 return a; 00051 } 00052 00053 void IntEqClasses::compress() { 00054 if (NumClasses) 00055 return; 00056 for (unsigned i = 0, e = EC.size(); i != e; ++i) 00057 EC[i] = (EC[i] == i) ? NumClasses++ : EC[EC[i]]; 00058 } 00059 00060 void IntEqClasses::uncompress() { 00061 if (!NumClasses) 00062 return; 00063 SmallVector<unsigned, 8> Leader; 00064 for (unsigned i = 0, e = EC.size(); i != e; ++i) 00065 if (EC[i] < Leader.size()) 00066 EC[i] = Leader[EC[i]]; 00067 else 00068 Leader.push_back(EC[i] = i); 00069 NumClasses = 0; 00070 }