1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*++
Copyright (c) 2006 Microsoft Corporation
Module Name:
id_var_list.h
Abstract:
Association list from theory id -> var
where id in [0..255] and var is 24 bit.
Author:
Leonardo de Moura (leonardo) 2008-02-19.
Revision History:
Extracted from smt_theory_var_list
--*/
#pragma once
#include "util/region.h"
#include "util/debug.h"
template <int null_id = -1, int null_var = -1>
class id_var_list {
int m_id:8;
int m_var:24;
id_var_list * m_next;
public:
id_var_list():
m_id(null_id),
m_var(null_var),
m_next(nullptr) {
}
id_var_list(int t, int v, id_var_list * n = nullptr):
m_id(t),
m_var(v),
m_next(n) {
}
int get_id() const {
return m_id;
}
int get_var() const {
return m_var;
}
bool empty() const {
return get_var() == null_var;
}
int find(int id) const {
if (empty())
return null_var;
auto l = this;
do {
if (id == l->get_id())
return l->get_var();
l = l->get_next();
}
while (l);
return null_var;
}
unsigned size() const {
if (empty())
return 0;
unsigned r = 0;
auto l = this;
while (l) {
++r;
l = l->get_next();
}
return r;
}
void add_var(int v, int id, region& r) {
SASSERT(find(id) == null_var);
if (get_var() == null_var) {
m_var = v;
m_id = id;
m_next = nullptr;
}
else {
auto l = this;
while (l->get_next()) {
SASSERT(l->get_id() != id);
l = l->get_next();
}
SASSERT(l);
SASSERT(!l->get_next());
auto * new_cell = new (r) id_var_list<null_id, null_var>(id, v);
l->set_next(new_cell);
}
SASSERT(find(id) == v);
}
/**
\brief Replace the entry (v', id) with the entry (v, id).
The enode must have an entry (v', id)
*/
void replace(int v, int id) {
SASSERT(find(id) != null_var);
auto l = this;
while (l) {
if (l->get_id() == id) {
l->set_var(v);
return;
}
l = l->get_next();
}
UNREACHABLE();
}
/**
\brief Delete theory variable. It assumes the
enode is associated with a variable of the given theory.
*/
void del_var(int id) {
SASSERT(find(id) != null_var);
if (get_id() == id) {
if (!m_next) {
// most common case
m_var = null_var;
m_id = null_id;
}
else {
m_var = m_next->get_var();
m_id = m_next->get_id();
m_next = m_next->get_next();
}
}
else {
auto* prev = this;
auto* l = prev->get_next();
while (l) {
SASSERT(prev->get_next() == l);
if (l->get_id() == id) {
prev->set_next(l->get_next());
return;
}
prev = l;
l = l->get_next();
}
UNREACHABLE();
}
}
id_var_list * get_next() const {
return m_next;
}
void set_id(int id) {
m_id = id;
}
void set_var(int v) {
m_var = v;
}
void set_next(id_var_list * next) {
m_next = next;
}
};
// 32 bit machine
static_assert(sizeof(unsigned*) != 4 || sizeof(id_var_list<>) == sizeof(id_var_list<> *) + sizeof(int), "32 bit");
// 64 bit machine
static_assert(sizeof(unsigned*) != 8 || sizeof(id_var_list<>) == sizeof(id_var_list<> *) + sizeof(int) + /* a structure must be aligned */ sizeof(int), "64 bit");