IntEqClasses.h 3.0 KB

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