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/// \file
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
21#ifndef LLVM_ADT_INTEQCLASSES_H
22#define LLVM_ADT_INTEQCLASSES_H
23
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/Compiler.h"
26
27namespace llvm {
28
29class 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
37 /// NumClasses - The number of equivalence classes when compressed, or 0 when
38 /// uncompressed.
39 unsigned NumClasses = 0;
40
41public:
42 /// IntEqClasses - Create an equivalence class mapping for 0 .. N-1.
43 IntEqClasses(unsigned N = 0) { grow(N); }
44
45 /// grow - Increase capacity to hold 0 .. N-1, putting new integers in unique
46 /// equivalence classes.
47 /// This requires an uncompressed map.
48 LLVM_ABI void grow(unsigned N);
49
50 /// clear - Clear all classes so that grow() will assign a unique class to
51 /// every integer.
52 void clear() {
53 EC.clear();
54 NumClasses = 0;
55 }
56
57 /// Join the equivalence classes of a and b. After joining classes,
58 /// findLeader(a) == findLeader(b). This requires an uncompressed map.
59 /// Returns the new leader.
60 LLVM_ABI unsigned join(unsigned a, unsigned b);
61
62 /// findLeader - Compute the leader of a's equivalence class. This is the
63 /// smallest member of the class.
64 /// This requires an uncompressed map.
65 LLVM_ABI unsigned findLeader(unsigned a) const;
66
67 /// compress - Compress equivalence classes by numbering them 0 .. M.
68 /// This makes the equivalence class map immutable.
69 LLVM_ABI void compress();
70
71 /// getNumClasses - Return the number of equivalence classes after compress()
72 /// was called.
73 unsigned getNumClasses() const { return NumClasses; }
74
75 /// operator[] - Return a's equivalence class number, 0 .. getNumClasses()-1.
76 /// This requires a compressed map.
77 unsigned operator[](unsigned a) const {
78 assert(NumClasses && "operator[] called before compress()");
79 return EC[a];
80 }
81
82 /// uncompress - Change back to the uncompressed representation that allows
83 /// editing.
84 LLVM_ABI void uncompress();
85};
86
87} // End llvm namespace
88
89#endif
90