intervals.c 3.97 KB
/*
 * 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;
		}
	}
}