-
Notifications
You must be signed in to change notification settings - Fork 0
/
prop.c
181 lines (163 loc) · 4.33 KB
/
prop.c
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
177
178
179
180
/*
* prop.c
* Copyright (C) 2013 National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "solver.h"
#define MAX_PRIORITY 8
/*
* The propagator queue.
*/
struct propqueue_s
{
prop_t head;
prop_t tail;
};
static struct propqueue_s propqueue[MAX_PRIORITY] = {{0}};
static uint_t priority = MAX_PRIORITY;
static prop_t current = NULL;
/*
* Reset the propagator queue.
*/
extern void solver_reset_prop_queue(void)
{
for (size_t i = 0; i < MAX_PRIORITY; i++)
propqueue[i].head = propqueue[i].tail = NULL;
current = NULL;
priority = MAX_PRIORITY;
}
/*
* Schedule a propagator.
*/
extern void solver_schedule_prop(prop_t prop)
{
if (solver_is_prop_scheduled(prop))
{
if (prop != current)
return;
current = NULL;
}
cons_t c = constraint(prop);
if (ispurged(c))
return;
sym_t sym = c->sym;
propinfo_t info = sym->propinfo;
prop->next = prop;
if (propqueue[info->priority].head == NULL)
{
propqueue[info->priority].head = prop;
propqueue[info->priority].tail = prop;
}
else
{
propqueue[info->priority].tail->next = prop;
propqueue[info->priority].tail = prop;
}
priority = (priority <= info->priority? priority: info->priority);
debug("!rSCHEDULE!d (%u) %s [%zu]",
(prop->info & INFO_IDX_MASK) >> INFO_IDX_SHIFT, show_cons(c),
priority);
}
/*
* Wake a propagator.
*/
extern bool solver_wake_prop(void)
{
prop_t prop;
cons_t c;
while (true)
{
if (priority == MAX_PRIORITY)
return false;
prop = propqueue[priority].head;
debug("!rWAKE!d (%u) %s [%zu]",
(prop->info & INFO_IDX_MASK) >> INFO_IDX_SHIFT,
show_cons(constraint(prop)), priority);
if (prop->next == prop)
{
propqueue[priority].head = NULL;
propqueue[priority].tail = NULL;
while (priority < MAX_PRIORITY && propqueue[priority].head == NULL)
priority++;
}
else
propqueue[priority].head = prop->next;
if (iskilled(prop))
{
prop->next = NULL;
continue;
}
c = constraint(prop);
if (!ispurged(c))
break;
prop->next = NULL;
}
sym_t sym = c->sym;
propinfo_t info = sym->propinfo + solver_propinfo_index(prop);
current = prop;
info->handler(prop);
if (current != NULL)
{
current = NULL;
prop->next = NULL;
}
while (priority < MAX_PRIORITY && propqueue[priority].head == NULL)
priority++;
return (priority != MAX_PRIORITY);
}
/*
* Check if the propagation queue is empty.
*/
extern bool solver_is_queue_empty(void)
{
return (priority == MAX_PRIORITY);
}
/*
* Flush the propagator queue.
*/
extern void solver_flush_queue(void)
{
while (priority < MAX_PRIORITY)
{
prop_t prop = propqueue[priority].head;
if (prop == NULL)
{
priority++;
continue;
}
while (prop->next != prop)
{
debug("!rFLUSH!d %s [%zu]", show_cons(constraint(prop)),
priority);
prop_t old_prop = prop;
prop = prop->next;
old_prop->next = NULL;
}
prop->next = NULL;
debug("!rFLUSH!d %s [%zu]", show_cons(constraint(prop)), priority);
propqueue[priority].head = propqueue[priority].tail = NULL;
priority++;
}
if (current != NULL)
{
debug("!rFLUSH!d %s [%zu]", show_cons(constraint(current)), priority);
current->next = NULL;
current = NULL;
}
}