IntEqClasses.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===-- llvm/ADT/IntEqClasses.h - Equiv. Classes of Integers ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Equivalence classes for small integers. This is a mapping of the integers
  10. // 0 .. N-1 into M equivalence classes numbered 0 .. M-1.
  11. //
  12. // Initially each integer has its own equivalence class. Classes are joined by
  13. // passing a representative member of each class to join().
  14. //
  15. // Once the classes are built, compress() will number them 0 .. M-1 and prevent
  16. // further changes.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_INTEQCLASSES_H
  20. #define LLVM_ADT_INTEQCLASSES_H
  21. #include "llvm/ADT/SmallVector.h"
  22. namespace llvm {
  23. class IntEqClasses {
  24. /// EC - When uncompressed, map each integer to a smaller member of its
  25. /// equivalence class. The class leader is the smallest member and maps to
  26. /// itself.
  27. ///
  28. /// When compressed, EC[i] is the equivalence class of i.
  29. SmallVector<unsigned, 8> EC;
  30. /// NumClasses - The number of equivalence classes when compressed, or 0 when
  31. /// uncompressed.
  32. unsigned NumClasses;
  33. public:
  34. /// IntEqClasses - Create an equivalence class mapping for 0 .. N-1.
  35. IntEqClasses(unsigned N = 0) : NumClasses(0) { grow(N); }
  36. /// grow - Increase capacity to hold 0 .. N-1, putting new integers in unique
  37. /// equivalence classes.
  38. /// This requires an uncompressed map.
  39. void grow(unsigned N);
  40. /// clear - Clear all classes so that grow() will assign a unique class to
  41. /// every integer.
  42. void clear() {
  43. EC.clear();
  44. NumClasses = 0;
  45. }
  46. /// Join the equivalence classes of a and b. After joining classes,
  47. /// findLeader(a) == findLeader(b). This requires an uncompressed map.
  48. /// Returns the new leader.
  49. unsigned join(unsigned a, unsigned b);
  50. /// findLeader - Compute the leader of a's equivalence class. This is the
  51. /// smallest member of the class.
  52. /// This requires an uncompressed map.
  53. unsigned findLeader(unsigned a) const;
  54. /// compress - Compress equivalence classes by numbering them 0 .. M.
  55. /// This makes the equivalence class map immutable.
  56. void compress();
  57. /// getNumClasses - Return the number of equivalence classes after compress()
  58. /// was called.
  59. unsigned getNumClasses() const { return NumClasses; }
  60. /// operator[] - Return a's equivalence class number, 0 .. getNumClasses()-1.
  61. /// This requires a compressed map.
  62. unsigned operator[](unsigned a) const {
  63. assert(NumClasses && "operator[] called before compress()");
  64. return EC[a];
  65. }
  66. /// uncompress - Change back to the uncompressed representation that allows
  67. /// editing.
  68. void uncompress();
  69. };
  70. } // End llvm namespace
  71. #endif