1 //===-- llvm/ADT/IntEqClasses.cpp - Equivalence Classes of Integers -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Equivalence classes for small integers. This is a mapping of the integers
11 // 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
13 // Initially each integer has its own equivalence class. Classes are joined by
14 // passing a representative member of each class to join().
16 // Once the classes are built, compress() will number them 0 .. M-1 and prevent
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ADT/IntEqClasses.h"
25 void IntEqClasses::grow(unsigned N) {
26 assert(NumClasses == 0 && "grow() called after compress().");
29 EC.push_back(EC.size());
32 void IntEqClasses::join(unsigned a, unsigned b) {
33 assert(NumClasses == 0 && "join() called after compress().");
36 // Update pointers while searching for the leaders, compressing the paths
37 // incrementally. The larger leader will eventually be updated, joining the
41 EC[b] = eca, b = ecb, ecb = EC[b];
43 EC[a] = ecb, a = eca, eca = EC[a];
46 unsigned IntEqClasses::findLeader(unsigned a) const {
47 assert(NumClasses == 0 && "findLeader() called after compress().");
53 void IntEqClasses::compress() {
56 for (unsigned i = 0, e = EC.size(); i != e; ++i)
57 EC[i] = (EC[i] == i) ? NumClasses++ : EC[EC[i]];
60 void IntEqClasses::uncompress() {
63 SmallVector<unsigned, 8> Leader;
64 for (unsigned i = 0, e = EC.size(); i != e; ++i)
65 if (EC[i] < Leader.size())
66 EC[i] = Leader[EC[i]];
68 Leader.push_back(EC[i] = i);