/3rd_party/llvm/lib/Support/IntEqClasses.cpp

https://code.google.com/p/softart/ · C++ · 70 lines · 41 code · 7 blank · 22 comment · 20 complexity · 2bc230d188218b0329054cbbcbd19203 MD5 · raw file

  1. //===-- llvm/ADT/IntEqClasses.cpp - Equivalence Classes of Integers -------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  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.
  12. //
  13. // Initially each integer has its own equivalence class. Classes are joined by
  14. // passing a representative member of each class to join().
  15. //
  16. // Once the classes are built, compress() will number them 0 .. M-1 and prevent
  17. // further changes.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/ADT/IntEqClasses.h"
  21. using namespace llvm;
  22. void IntEqClasses::grow(unsigned N) {
  23. assert(NumClasses == 0 && "grow() called after compress().");
  24. EC.reserve(N);
  25. while (EC.size() < N)
  26. EC.push_back(EC.size());
  27. }
  28. void IntEqClasses::join(unsigned a, unsigned b) {
  29. assert(NumClasses == 0 && "join() called after compress().");
  30. unsigned eca = EC[a];
  31. unsigned ecb = EC[b];
  32. // Update pointers while searching for the leaders, compressing the paths
  33. // incrementally. The larger leader will eventually be updated, joining the
  34. // classes.
  35. while (eca != ecb)
  36. if (eca < ecb)
  37. EC[b] = eca, b = ecb, ecb = EC[b];
  38. else
  39. EC[a] = ecb, a = eca, eca = EC[a];
  40. }
  41. unsigned IntEqClasses::findLeader(unsigned a) const {
  42. assert(NumClasses == 0 && "findLeader() called after compress().");
  43. while (a != EC[a])
  44. a = EC[a];
  45. return a;
  46. }
  47. void IntEqClasses::compress() {
  48. if (NumClasses)
  49. return;
  50. for (unsigned i = 0, e = EC.size(); i != e; ++i)
  51. EC[i] = (EC[i] == i) ? NumClasses++ : EC[EC[i]];
  52. }
  53. void IntEqClasses::uncompress() {
  54. if (!NumClasses)
  55. return;
  56. SmallVector<unsigned, 8> Leader;
  57. for (unsigned i = 0, e = EC.size(); i != e; ++i)
  58. if (EC[i] < Leader.size())
  59. EC[i] = Leader[EC[i]];
  60. else
  61. Leader.push_back(EC[i] = i);
  62. NumClasses = 0;
  63. }