intervals.c
3.97 KB
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
/*
* intervals.c
*
* Created on: 26/07/2016
* Author: pedro
*/
#include "intervals.h"
#include "bitmaps.h"
#include "variables.h"
/*
* Set (*d).s[0] as the unsigned short val
* d - interval to set value
* val - value to set
*/
void i_set_1st_ushort(interval* d, unsigned short val) {
(*d).s[0] = val;
}
/*
* Copy d_src interval to d_dest
* d_dest - where to copy the values
* d_src - domain to copy
*/
void i_copy(interval* d_dest, interval* d_src) {
(*d_dest).s0 = (*d_src).s0;
(*d_dest).s1 = (*d_src).s1;
}
/*
* Do operation b = b & i
* b - domain to save operation
* i - domain to do AND with
*/
void i_and_b(bitmap* b, interval* i) {
bitmap b_new;
if ((*i).s[0] <= (*i).s[1]) {
b_new_range(&b_new, (*i).s[0], (*i).s[1]);
b_intersect_b(b, &b_new);
} else {
b_clear(b);
}
}
/*
* Return true if interval is empty, and false if not
* i - interval1 to check if empty
*/
bool i_is_empty(interval* i) {
if ((*i).s[0] > (*i).s[1]) {
return true;
} else {
return false;
}
}
/*
* Return the first unsigned int value stored in d[idx]
* d - interval to get value from
* idx - index of array to get value from
*/
unsigned short i_get_1st_ushort(interval* d) {
return (*d).s[0];
}
/*
* return the interval minimum value
* i - interval1 to get minimum value
*/
unsigned int i_get_min_val(interval* i) {
return (*i).s[0];
}
/*
* return the interval maximum value
* i - interval1 to get maximum value
*/
unsigned int i_get_max_val(interval* i) {
return (*i).s[1];
}
/*
* Return true if i1 has the same max and min as the bitmap domain in variable var
* d - interval1 to check if has the same max and min as var
* v - variable to compare
*/
bool i_eq_b_var(interval* d, var* v) {
if ((*d).s[0] == v->min && (*d).s[1] == v->max) {
return true;
}
return false;
}
/*
* Fill the interval domain and updates the number of values on all the CSP variables
*/
void set_interval_domains() {
unsigned int i;
for (i = 0; i < N_VS; i++) {
VS[i].domain_i.s[0] = VS[i].min;
VS[i].domain_i.s[1] = VS[i].max;
VS[i].n_vals = (unsigned short)(VS[i].max - VS[i].min + 1);
}
}
/*
* Convert and copy bitmaps to intervals
* intervals - destiny for intervals1 converted from bitmaps
* bitmaps - source bitmaps
* n_domains - number of bitmaps to convert
*/
void convert_bitmaps_to_intervals(interval* intervals, bitmap* bitmaps, unsigned int n_domains) {
unsigned int i;
for (i = 0; i < n_domains; i++) {
intervals[i].s[0] = (unsigned short)b_get_min_val(&bitmaps[i]);
intervals[i].s[1] = (unsigned short)b_get_max_val(&bitmaps[i]);
}
}
/*
* Convert and copy intervals to bitmaps
* bitmaps - destiny for bitmaps converted from intervals
* intervals - source intervals1 to convert to bitmaps
* n_domains - number of intervals to convert
*/
void convert_intervals_to_bitmaps(bitmap* bitmaps, interval* intervals, unsigned int n_domains) {
unsigned int i;
for (i = 0; i < n_domains; i++) {
if (intervals[i].s[1] >= intervals[i].s[0]) {
b_new_range(&bitmaps[i], intervals[i].s[0], intervals[i].s[1]);
} else {
b_clear(&bitmaps[i]);
}
}
}
/*
* Convert and copy intervals to variables
* variables - destiny for variables converted from intervals
* intervals - source intervals
* n_domains - number of intervals to convert
*/
void convert_intervals_to_vars(var* variables, interval* intervals, unsigned int n_domains) {
unsigned int i;
for (i = 0; i < n_domains; i++) {
if (intervals[i].s[0] < intervals[i].s[1]) {
b_new_range(&variables[i].domain_b, intervals[i].s[0], intervals[i].s[1]);
variables[i].min = intervals[i].s[0];
variables[i].max = intervals[i].s[1];
variables[i].n_vals = (unsigned short)(intervals[i].s[1] - intervals[i].s[0] + 1);
} else if (intervals[i].s[0] == intervals[i].s[1]) {
b_new_val(&variables[i].domain_b, intervals[i].s[0]);
variables[i].min = intervals[i].s[0];
variables[i].max = intervals[i].s[0];
variables[i].n_vals = 1;
} else {
b_clear(&variables[i].domain_b);
variables[i].n_vals = 0;
}
}
}