forked from OSchip/llvm-project
Add ADT/IntEqClasses.h as a light-weight implementation of EquivalenceClasses.h.
This implementation already exists as ConnectedVNInfoEqClasses in LiveInterval.cpp, and it seems to be generally useful to have a light-weight way of forming equivalence classes of small integers. IntEqClasses doesn't allow enumeration of the elements in a class. llvm-svn: 122293
This commit is contained in:
parent
830837da53
commit
baee655c5e
|
@ -0,0 +1,81 @@
|
||||||
|
//===-- llvm/ADT/IntEqClasses.h - Equiv. Classes of Integers ----*- C++ -*-===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// Equivalence classes for small integers. This is a mapping of the integers
|
||||||
|
// 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
|
||||||
|
//
|
||||||
|
// Initially each integer has its own equivalence class. Classes are joined by
|
||||||
|
// passing a representative member of each class to join().
|
||||||
|
//
|
||||||
|
// Once the classes are built, compress() will number them 0 .. M-1 and prevent
|
||||||
|
// further changes.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_ADT_INTEQCLASSES_H
|
||||||
|
#define LLVM_ADT_INTEQCLASSES_H
|
||||||
|
|
||||||
|
#include "llvm/ADT/SmallVector.h"
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
|
||||||
|
class IntEqClasses {
|
||||||
|
/// EC - When uncompressed, map each integer to a smaller member of its
|
||||||
|
/// equivalence class. The class leader is the smallest member and maps to
|
||||||
|
/// itself.
|
||||||
|
///
|
||||||
|
/// When compressed, EC[i] is the equivalence class of i.
|
||||||
|
SmallVector<unsigned, 8> EC;
|
||||||
|
|
||||||
|
/// NumClasses - The number of equivalence classes when compressed, or 0 when
|
||||||
|
/// uncompressed.
|
||||||
|
unsigned NumClasses;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// IntEqClasses - Create an equivalence class mapping for 0 .. N-1.
|
||||||
|
IntEqClasses(unsigned N) : NumClasses(0) { grow(N); }
|
||||||
|
|
||||||
|
/// grow - Increase capacity to hold 0 .. N-1, putting new integers in unique
|
||||||
|
/// equivalence classes.
|
||||||
|
/// This requires an uncompressed map.
|
||||||
|
void grow(unsigned N);
|
||||||
|
|
||||||
|
/// join - Join the equivalence classes of a and b. After joining classes,
|
||||||
|
/// findLeader(a) == findLeader(b).
|
||||||
|
/// This requires an uncompressed map.
|
||||||
|
void join(unsigned a, unsigned b);
|
||||||
|
|
||||||
|
/// findLeader - Compute the leader of a's equivalence class. This is the
|
||||||
|
/// smallest member of the class.
|
||||||
|
/// This requires an uncompressed map.
|
||||||
|
unsigned findLeader(unsigned a) const;
|
||||||
|
|
||||||
|
/// compress - Compress equivalence classes by numbering them 0 .. M.
|
||||||
|
/// This makes the equivalence class map immutable.
|
||||||
|
void compress();
|
||||||
|
|
||||||
|
/// getNumClasses - Return the number of equivalence classes after compress()
|
||||||
|
/// was called.
|
||||||
|
unsigned getNumClasses() const { return NumClasses; }
|
||||||
|
|
||||||
|
/// operator[] - Return a's equivalence class number, 0 .. getNumClasses()-1.
|
||||||
|
/// This requires a compressed map.
|
||||||
|
unsigned operator[](unsigned a) const {
|
||||||
|
assert(NumClasses && "operator[] called before compress()");
|
||||||
|
return EC[a];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// uncompress - Change back to the uncompressed representation that allows
|
||||||
|
/// editing.
|
||||||
|
void uncompress();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // End llvm namespace
|
||||||
|
|
||||||
|
#endif
|
|
@ -22,6 +22,7 @@ add_llvm_library(LLVMSupport
|
||||||
FoldingSet.cpp
|
FoldingSet.cpp
|
||||||
FormattedStream.cpp
|
FormattedStream.cpp
|
||||||
GraphWriter.cpp
|
GraphWriter.cpp
|
||||||
|
IntEqClasses.cpp
|
||||||
IntervalMap.cpp
|
IntervalMap.cpp
|
||||||
IsInf.cpp
|
IsInf.cpp
|
||||||
IsNAN.cpp
|
IsNAN.cpp
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
//===-- llvm/ADT/IntEqClasses.cpp - Equivalence Classes of Integers -------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// Equivalence classes for small integers. This is a mapping of the integers
|
||||||
|
// 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
|
||||||
|
//
|
||||||
|
// Initially each integer has its own equivalence class. Classes are joined by
|
||||||
|
// passing a representative member of each class to join().
|
||||||
|
//
|
||||||
|
// Once the classes are built, compress() will number them 0 .. M-1 and prevent
|
||||||
|
// further changes.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/ADT/IntEqClasses.h"
|
||||||
|
|
||||||
|
using namespace llvm;
|
||||||
|
|
||||||
|
void IntEqClasses::grow(unsigned N) {
|
||||||
|
assert(NumClasses == 0 && "grow() called after compress().");
|
||||||
|
while (EC.size() < N)
|
||||||
|
EC.push_back(EC.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntEqClasses::join(unsigned a, unsigned b) {
|
||||||
|
assert(NumClasses == 0 && "join() called after compress().");
|
||||||
|
unsigned eca = EC[a];
|
||||||
|
unsigned ecb = EC[b];
|
||||||
|
// Update pointers while searching for the leaders, compressing the paths
|
||||||
|
// incrementally. The larger leader will eventually be updated, joining the
|
||||||
|
// classes.
|
||||||
|
while (eca != ecb)
|
||||||
|
if (eca < ecb)
|
||||||
|
EC[b] = eca, b = ecb, ecb = EC[b];
|
||||||
|
else
|
||||||
|
EC[a] = ecb, a = eca, eca = EC[a];
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned IntEqClasses::findLeader(unsigned a) const {
|
||||||
|
assert(NumClasses == 0 && "findLeader() called after compress().");
|
||||||
|
while (a != EC[a])
|
||||||
|
a = EC[a];
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntEqClasses::compress() {
|
||||||
|
if (NumClasses)
|
||||||
|
return;
|
||||||
|
for (unsigned i = 0, e = EC.size(); i != e; ++i)
|
||||||
|
EC[i] = (EC[i] == i) ? NumClasses++ : EC[EC[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntEqClasses::uncompress() {
|
||||||
|
if (!NumClasses)
|
||||||
|
return;
|
||||||
|
SmallVector<unsigned, 8> Leader;
|
||||||
|
for (unsigned i = 0, e = EC.size(); i != e; ++i)
|
||||||
|
if (EC[i] < Leader.size())
|
||||||
|
EC[i] = Leader[EC[i]];
|
||||||
|
else
|
||||||
|
Leader.push_back(EC[i] = i);
|
||||||
|
NumClasses = 0;
|
||||||
|
}
|
|
@ -0,0 +1,107 @@
|
||||||
|
//===---- ADT/IntEqClassesTest.cpp - IntEqClasses unit tests ----*- C++ -*-===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/ADT/IntEqClasses.h"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
using namespace llvm;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
TEST(IntEqClasses, Simple) {
|
||||||
|
IntEqClasses ec(10);
|
||||||
|
|
||||||
|
ec.join(0, 1);
|
||||||
|
ec.join(3, 2);
|
||||||
|
ec.join(4, 5);
|
||||||
|
ec.join(7, 6);
|
||||||
|
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(0));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(1));
|
||||||
|
EXPECT_EQ(2u, ec.findLeader(2));
|
||||||
|
EXPECT_EQ(2u, ec.findLeader(3));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(4));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(5));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(6));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(7));
|
||||||
|
EXPECT_EQ(8u, ec.findLeader(8));
|
||||||
|
EXPECT_EQ(9u, ec.findLeader(9));
|
||||||
|
|
||||||
|
// join two non-leaders.
|
||||||
|
ec.join(1, 3);
|
||||||
|
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(0));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(1));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(2));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(3));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(4));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(5));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(6));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(7));
|
||||||
|
EXPECT_EQ(8u, ec.findLeader(8));
|
||||||
|
EXPECT_EQ(9u, ec.findLeader(9));
|
||||||
|
|
||||||
|
// join two leaders.
|
||||||
|
ec.join(4, 8);
|
||||||
|
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(0));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(1));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(2));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(3));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(4));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(5));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(6));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(7));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(8));
|
||||||
|
EXPECT_EQ(9u, ec.findLeader(9));
|
||||||
|
|
||||||
|
// join mixed.
|
||||||
|
ec.join(9, 1);
|
||||||
|
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(0));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(1));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(2));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(3));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(4));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(5));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(6));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(7));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(8));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(9));
|
||||||
|
|
||||||
|
// compressed map.
|
||||||
|
ec.compress();
|
||||||
|
EXPECT_EQ(3u, ec.getNumClasses());
|
||||||
|
|
||||||
|
EXPECT_EQ(0u, ec[0]);
|
||||||
|
EXPECT_EQ(0u, ec[1]);
|
||||||
|
EXPECT_EQ(0u, ec[2]);
|
||||||
|
EXPECT_EQ(0u, ec[3]);
|
||||||
|
EXPECT_EQ(1u, ec[4]);
|
||||||
|
EXPECT_EQ(1u, ec[5]);
|
||||||
|
EXPECT_EQ(2u, ec[6]);
|
||||||
|
EXPECT_EQ(2u, ec[7]);
|
||||||
|
EXPECT_EQ(1u, ec[8]);
|
||||||
|
EXPECT_EQ(0u, ec[9]);
|
||||||
|
|
||||||
|
// uncompressed map.
|
||||||
|
ec.uncompress();
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(0));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(1));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(2));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(3));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(4));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(5));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(6));
|
||||||
|
EXPECT_EQ(6u, ec.findLeader(7));
|
||||||
|
EXPECT_EQ(4u, ec.findLeader(8));
|
||||||
|
EXPECT_EQ(0u, ec.findLeader(9));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end anonymous namespace
|
|
@ -54,6 +54,7 @@ add_llvm_unittest(ADT
|
||||||
ADT/FoldingSet.cpp
|
ADT/FoldingSet.cpp
|
||||||
ADT/ilistTest.cpp
|
ADT/ilistTest.cpp
|
||||||
ADT/ImmutableSetTest.cpp
|
ADT/ImmutableSetTest.cpp
|
||||||
|
ADT/IntEqClassesTest.cpp
|
||||||
ADT/IntervalMapTest.cpp
|
ADT/IntervalMapTest.cpp
|
||||||
ADT/SmallBitVectorTest.cpp
|
ADT/SmallBitVectorTest.cpp
|
||||||
ADT/SmallStringTest.cpp
|
ADT/SmallStringTest.cpp
|
||||||
|
|
Loading…
Reference in New Issue