blob: 4a976dcefc65fc24f61599b949d7c8016260fa91 [file] [log] [blame]
Jakob Stoklund Olesenbaee6552010-12-21 00:04:46 +00001//===-- llvm/ADT/IntEqClasses.cpp - Equivalence Classes of Integers -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jakob Stoklund Olesenbaee6552010-12-21 00:04:46 +00006//
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
20#include "llvm/ADT/IntEqClasses.h"
21
22using namespace llvm;
23
24void IntEqClasses::grow(unsigned N) {
25 assert(NumClasses == 0 && "grow() called after compress().");
Jakob Stoklund Olesen4c278f82010-12-21 00:48:17 +000026 EC.reserve(N);
Jakob Stoklund Olesenbaee6552010-12-21 00:04:46 +000027 while (EC.size() < N)
28 EC.push_back(EC.size());
29}
30
Matthias Braun7c66afb2016-01-08 01:16:39 +000031unsigned IntEqClasses::join(unsigned a, unsigned b) {
Jakob Stoklund Olesenbaee6552010-12-21 00:04:46 +000032 assert(NumClasses == 0 && "join() called after compress().");
33 unsigned eca = EC[a];
34 unsigned ecb = EC[b];
35 // Update pointers while searching for the leaders, compressing the paths
36 // incrementally. The larger leader will eventually be updated, joining the
37 // classes.
38 while (eca != ecb)
Richard Trieu7a083812016-02-18 22:09:30 +000039 if (eca < ecb) {
40 EC[b] = eca;
41 b = ecb;
42 ecb = EC[b];
43 } else {
44 EC[a] = ecb;
45 a = eca;
46 eca = EC[a];
47 }
Matthias Braun7c66afb2016-01-08 01:16:39 +000048
49 return eca;
Jakob Stoklund Olesenbaee6552010-12-21 00:04:46 +000050}
51
52unsigned IntEqClasses::findLeader(unsigned a) const {
53 assert(NumClasses == 0 && "findLeader() called after compress().");
54 while (a != EC[a])
55 a = EC[a];
56 return a;
57}
58
59void IntEqClasses::compress() {
60 if (NumClasses)
61 return;
62 for (unsigned i = 0, e = EC.size(); i != e; ++i)
63 EC[i] = (EC[i] == i) ? NumClasses++ : EC[EC[i]];
64}
65
66void IntEqClasses::uncompress() {
67 if (!NumClasses)
68 return;
69 SmallVector<unsigned, 8> Leader;
70 for (unsigned i = 0, e = EC.size(); i != e; ++i)
71 if (EC[i] < Leader.size())
72 EC[i] = Leader[EC[i]];
73 else
74 Leader.push_back(EC[i] = i);
75 NumClasses = 0;
76}