ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
union_find.h
Go to the documentation of this file.
1#ifndef CPROVER_UNION_FIND_H
2#define CPROVER_UNION_FIND_H
3
4#include <cassert>
6#include <vector>
7
9{
10public:
11 void make_union(unsigned a, unsigned b);
12 unsigned find(unsigned a) const;
13
15
16 // remove from any sets
17 void isolate(unsigned a);
18
20 {
21 other.nodes.swap(nodes);
22 }
23
24 void resize(unsigned size)
25 {
26 // only enlarge
27 assert(nodes.size() <= size);
28 nodes.resize(size);
29 }
30
31 void clear()
32 {
33 nodes.clear();
34 }
35
36 bool is_root(unsigned a) const
37 {
38 if (a >= size())
39 return true;
40 return nodes[a].root;
41 }
42
43 bool same_set(unsigned a, unsigned b) const
44 {
45 return find(a) == find(b);
46 }
47
48 unsigned size() const
49 {
50 return nodes.size();
51 }
52
53 unsigned count(unsigned a) const
54 {
55 if (a >= size())
56 return 1;
57 return nodes[find(a)].count;
58 }
59
60 void check_index(unsigned a)
61 {
62 if (a >= size())
63 resize(a + 1);
64 }
65
66 unsigned count_roots() const
67 {
68 unsigned c = 0;
69 for (auto node : nodes)
70 if (node.root)
71 c++;
72 return c;
73 }
74
75 void re_root(unsigned old, unsigned new_root);
76
77 unsigned get_other(unsigned a);
78
79protected:
80 struct nodet
81 {
82 // these two could be shared
83 unsigned count;
84 unsigned parent;
85
86 bool root;
87
88 nodet() : count(1), parent(0), root(true)
89 {
90 }
91 };
92
93 std::vector<nodet> nodes;
94};
95
96template <typename T>
97class union_find : protected numbering<T>
98{
99public:
100 // true == already in same set
101 bool make_union(const T &a, const T &b)
102 {
103 unsigned na = number(a), nb = number(b);
104 bool is_union = find_number(na) == find_number(nb);
105 uuf.make_union(na, nb);
106 return is_union;
107 }
108
109 const T &find(const T &a)
110 {
111 return find(number(a));
112 }
113
114 unsigned find_number(unsigned a) const
115 {
116 return uuf.find(a);
117 }
118
119 unsigned find_number(const T &a)
120 {
121 return uuf.find(number(a));
122 }
123
124 bool is_root_number(unsigned a) const
125 {
126 return uuf.is_root(a);
127 }
128
129 bool is_root(const T &a)
130 {
131 return is_root(number(a));
132 }
133
134 unsigned number(const T &a)
135 {
136 unsigned n = subt::number(a);
137
138 if (n >= uuf.size())
139 uuf.resize(this->size());
140
141 assert(uuf.size() == this->size());
142
143 return n;
144 }
145
146 void clear()
147 {
148 uuf.clear();
149 subt::clear();
150 }
151
152protected:
155};
156
157#endif
void clear()
Definition numbering.h:47
size_t size()
Definition numbering.h:53
numbering()
Definition numbering.h:15
unsigned number(const T &a)
Definition numbering.h:20
Definition union_find.h:98
unsigned find_number(const T &a)
Definition union_find.h:119
unsigned find_number(unsigned a) const
Definition union_find.h:114
const T & find(const T &a)
Definition union_find.h:109
bool make_union(const T &a, const T &b)
Definition union_find.h:101
unsigned_union_find uuf
Definition union_find.h:153
numbering< T > subt
Definition union_find.h:154
bool is_root(const T &a)
Definition union_find.h:129
unsigned number(const T &a)
Definition union_find.h:134
void clear()
Definition union_find.h:146
bool is_root_number(unsigned a) const
Definition union_find.h:124
Definition union_find.h:9
void intersection(const unsigned_union_find &other)
Definition union_find.cpp:118
void resize(unsigned size)
Definition union_find.h:24
void re_root(unsigned old, unsigned new_root)
Definition union_find.cpp:68
std::vector< nodet > nodes
Definition union_find.h:93
unsigned find(unsigned a) const
Definition union_find.cpp:138
unsigned count(unsigned a) const
Definition union_find.h:53
void clear()
Definition union_find.h:31
bool same_set(unsigned a, unsigned b) const
Definition union_find.h:43
void swap(unsigned_union_find &other)
Definition union_find.h:19
unsigned size() const
Definition union_find.h:48
bool is_root(unsigned a) const
Definition union_find.h:36
void isolate(unsigned a)
Definition union_find.cpp:34
void check_index(unsigned a)
Definition union_find.h:60
unsigned count_roots() const
Definition union_find.h:66
void make_union(unsigned a, unsigned b)
Definition union_find.cpp:4
unsigned get_other(unsigned a)
Definition union_find.cpp:102
@ other
Definition converter_compare.cpp:887
#define T
Definition exp2f.c:24
#define true
Definition stdbool.h:11
nodet()
Definition union_find.h:88
unsigned count
Definition union_find.h:83
bool root
Definition union_find.h:86
unsigned parent
Definition union_find.h:84