IntEqClasses.h 3.0 KB

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