Blame view

src/intervals.c 3.97 KB
94b2b13d   Pedro Roque   PHACT source
1
2
3
4
/*
 * intervals.c
 *
 *  Created on: 26/07/2016
4d26a735   Pedro Roque   Increased recogni...
5
 *      Author: pedro
94b2b13d   Pedro Roque   PHACT source
6
7
8
9
10
11
12
13
14
15
16
17
 */

#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
 */
4d26a735   Pedro Roque   Increased recogni...
18
void i_set_1st_ushort(interval* d, unsigned short val) {
94b2b13d   Pedro Roque   PHACT source
19
20
21
22
23
24
25
26
	(*d).s[0] = val;
}

/*
 * Copy d_src interval to d_dest
 * d_dest - where to copy the values
 * d_src - domain to copy
 */
4d26a735   Pedro Roque   Increased recogni...
27
void i_copy(interval* d_dest, interval* d_src) {
94b2b13d   Pedro Roque   PHACT source
28
29
30
31
32
33
34
35
36
	(*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
 */
4d26a735   Pedro Roque   Increased recogni...
37
void i_and_b(bitmap* b, interval* i) {
94b2b13d   Pedro Roque   PHACT source
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	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
 */
4d26a735   Pedro Roque   Increased recogni...
52
bool i_is_empty(interval* i) {
94b2b13d   Pedro Roque   PHACT source
53
54
55
56
57
58
59
60
61
62
	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
94b2b13d   Pedro Roque   PHACT source
63
 * idx - index of array to get value from
4d26a735   Pedro Roque   Increased recogni...
64
 */
94b2b13d   Pedro Roque   PHACT source
65
66
67
68
69
70
71
unsigned short i_get_1st_ushort(interval* d) {
	return (*d).s[0];
}

/*
 * return the interval minimum value
 * i - interval1 to get minimum value
4d26a735   Pedro Roque   Increased recogni...
72
 */
94b2b13d   Pedro Roque   PHACT source
73
74
75
76
77
78
79
unsigned int i_get_min_val(interval* i) {
	return (*i).s[0];
}

/*
 * return the interval maximum value
 * i - interval1 to get maximum value
4d26a735   Pedro Roque   Increased recogni...
80
 */
94b2b13d   Pedro Roque   PHACT source
81
82
83
84
85
86
87
88
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
4d26a735   Pedro Roque   Increased recogni...
89
 */
94b2b13d   Pedro Roque   PHACT source
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
4d26a735   Pedro Roque   Increased recogni...
106
		VS[i].domain_i.s[1] = VS[i].max;
94b2b13d   Pedro Roque   PHACT source
107
108
109
110
111
112
113
114
115
		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
4d26a735   Pedro Roque   Increased recogni...
116
 */
94b2b13d   Pedro Roque   PHACT source
117
118
119
void convert_bitmaps_to_intervals(interval* intervals, bitmap* bitmaps, unsigned int n_domains) {
	unsigned int i;

4d26a735   Pedro Roque   Increased recogni...
120
121
	for (i = 0; i < n_domains; i++) {
		intervals[i].s[0] = (unsigned short)b_get_min_val(&bitmaps[i]);
94b2b13d   Pedro Roque   PHACT source
122
123
124
125
126
127
128
129
130
		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
4d26a735   Pedro Roque   Increased recogni...
131
 */
94b2b13d   Pedro Roque   PHACT source
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
4d26a735   Pedro Roque   Increased recogni...
149
 */
94b2b13d   Pedro Roque   PHACT source
150
151
152
153
154
155
156
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];
4d26a735   Pedro Roque   Increased recogni...
157
			variables[i].max = intervals[i].s[1];
94b2b13d   Pedro Roque   PHACT source
158
159
160
161
162
163
164
165
166
167
168
169
			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;
		}
	}
}