eval.c 9.32 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * eval.c:  Cell recomputation routines.
 * (C) 1998 The Free Software Foundation
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 */

#include <config.h>
#include <gnome.h>
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "utils.h"
14 15
#include "eval.h"

Arturo Espinosa's avatar
Arturo Espinosa committed
16
static GHashTable *dependency_hash;
Arturo Espinosa's avatar
Arturo Espinosa committed
17 18

void
Arturo Espinosa's avatar
Arturo Espinosa committed
19
cell_eval (Cell *cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
20 21 22 23
{
	char *error_msg;
	Value *v;

24
	g_return_if_fail (cell != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
25 26

	v = eval_expr (cell->sheet, cell->parsed_node,
Arturo Espinosa's avatar
Arturo Espinosa committed
27 28 29 30 31 32 33 34
		       cell->col->pos,
		       cell->row->pos,
		       &error_msg);

	if (cell->value)
		value_release (cell->value);
	
	if (v == NULL){
Miguel de Icaza's avatar
Miguel de Icaza committed
35
		cell_set_rendered_text (cell, error_msg);
Arturo Espinosa's avatar
Arturo Espinosa committed
36 37 38
		cell->value = NULL;
	} else {
		cell->value = v;
Miguel de Icaza's avatar
Miguel de Icaza committed
39
		cell_render_value (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
40 41
	}

42 43
	cell_calc_dimensions (cell);
	
Arturo Espinosa's avatar
Arturo Espinosa committed
44
	sheet_redraw_cell_region (cell->sheet,
Arturo Espinosa's avatar
Arturo Espinosa committed
45 46 47 48
				  cell->col->pos, cell->row->pos,
				  cell->col->pos, cell->row->pos);
}

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
/*
 * Comparission function for the dependency hash table
 */
static gint
dependency_equal (gconstpointer v, gconstpointer v2)
{
	DependencyRange *r1 = (DependencyRange *) v;
	DependencyRange *r2 = (DependencyRange *) v2;

	if (r1->sheet != r2->sheet)
		return 0;
	if (r1->start_col != r2->start_col)
		return 0;
	if (r1->start_row != r2->start_row)
		return 0;
	if (r1->end_col != r2->end_col)
		return 0;
	if (r1->end_row != r2->end_row)
		return 0;

	return 1;
}

/*
 * Hash function for DependencyRange structures
 */
static guint
Arturo Espinosa's avatar
Arturo Espinosa committed
76
dependency_hash_func (gconstpointer v)
77
{
Arturo Espinosa's avatar
Arturo Espinosa committed
78
	const DependencyRange *r = v;
79

Arturo Espinosa's avatar
Arturo Espinosa committed
80
	return ((((r->start_row << 8) + r->end_row) << 8) +
81 82 83 84 85 86 87 88 89
		(r->start_col << 8) + (r->end_col));
}

/*
 * Initializes the hash table for the dependency ranges
 */
static void
dependency_hash_init (void)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
90
	dependency_hash = g_hash_table_new (dependency_hash_func, dependency_equal);
91 92 93 94 95 96 97 98 99
}

/*
 * We add the dependency of Cell a in the ranges
 * enclose by CellRef a and CellRef b
 *
 * We compute the location from cell->row->pos and cell->col->pos
 */
static void
Arturo Espinosa's avatar
Arturo Espinosa committed
100
add_cell_range_deps (Cell *cell, CellRef *a, CellRef *b)
101 102 103 104 105 106 107 108
{
	DependencyRange range, *result;
	int col = cell->col->pos;
	int row = cell->row->pos;

	/* Convert to absolute cordinates */
	cell_get_abs_col_row (a, col, row, &range.start_col, &range.start_row);
	cell_get_abs_col_row (b, col, row, &range.end_col,   &range.end_row);
Arturo Espinosa's avatar
Arturo Espinosa committed
109 110 111

	range.ref_count = 0;
	range.sheet = cell->sheet;
112 113 114 115

	/* Look it up */
	result = g_hash_table_lookup (dependency_hash, &range);
	if (result){
Arturo Espinosa's avatar
Arturo Espinosa committed
116 117
		GList *cl;
		
118 119 120
		result->ref_count++;

		/* Is the cell already listed? */
Arturo Espinosa's avatar
Arturo Espinosa committed
121 122
		cl = g_list_find (result->cell_list, cell);
		if (cl)
123 124 125 126 127 128 129 130 131 132
			return;

		/* It was not: add it */
		result->cell_list = g_list_prepend (result->cell_list, cell);
		return;
	}

	/* Create a new DependencyRange structure */ 
	result = g_new (DependencyRange, 1);
	*result = range;
Arturo Espinosa's avatar
Arturo Espinosa committed
133 134
	result->ref_count = 1;
	result->cell_list = g_list_prepend (NULL, cell);
135

136 137 138 139 140 141 142
	g_hash_table_insert (dependency_hash, result, result);
}

/*
 * Adds the dependencies for a Value
 */
static void
Arturo Espinosa's avatar
Arturo Espinosa committed
143
add_value_deps (Cell *cell, Value *value)
144 145 146
{
	GList *l;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
147
	switch (value->type){
148 149 150 151 152 153 154 155 156
	case VALUE_STRING:
	case VALUE_INTEGER:
	case VALUE_FLOAT:
		/* Constants are no dependencies */
		break;
		
		/* Check every element of the array */
	case VALUE_ARRAY:
		for (l = value->v.array; l; l = l->next)
Arturo Espinosa's avatar
Arturo Espinosa committed
157
			add_value_deps (cell, l->data);
158 159 160 161
		break;
		
	case VALUE_CELLRANGE:
		add_cell_range_deps (
Arturo Espinosa's avatar
Arturo Espinosa committed
162 163 164
			cell,
			&value->v.cell_range.cell_a,
			&value->v.cell_range.cell_b);
165 166 167 168 169 170 171 172 173
		break;
	}
}

/*
 * This routine walks the expression tree looking for cell references
 * and cell range references.
 */
static void
Arturo Espinosa's avatar
Arturo Espinosa committed
174
add_tree_deps (Cell *cell, ExprTree *tree)
175
{
Arturo Espinosa's avatar
Arturo Espinosa committed
176 177
	GList *l;
	
178 179 180 181 182 183 184 185 186 187 188 189 190
	switch (tree->oper){
	case OP_EQUAL:
	case OP_NOT_EQUAL:
	case OP_GT:
	case OP_GTE:
	case OP_LT:
	case OP_LTE:
	case OP_ADD:
	case OP_SUB:
	case OP_MULT:
	case OP_DIV:
	case OP_EXP:
	case OP_CONCAT:
Arturo Espinosa's avatar
Arturo Espinosa committed
191 192
		add_tree_deps (cell, tree->u.binary.value_a);
		add_tree_deps (cell, tree->u.binary.value_b);
193 194 195
		return;

	case OP_CONSTANT:
Arturo Espinosa's avatar
Arturo Espinosa committed
196
		add_value_deps (cell, tree->u.constant);
197 198 199 200
		return;

	case OP_FUNCALL: 
		for (l = tree->u.function.arg_list; l; l = l->next)
Arturo Espinosa's avatar
Arturo Espinosa committed
201
			add_tree_deps (cell, l->data);
202 203 204 205
		return;

	case OP_VAR: 
		add_cell_range_deps (
Arturo Espinosa's avatar
Arturo Espinosa committed
206 207 208
			cell,
			&tree->u.constant->v.cell,
			&tree->u.constant->v.cell);
209 210 211
		return;

	case OP_NEG:
Arturo Espinosa's avatar
Arturo Espinosa committed
212
		add_tree_deps (cell, tree->u.value);
213 214 215 216 217 218 219 220 221 222 223
		return;
	
	} /* switch */
}

/*
 * This registers the dependencies for this cell
 * by scanning all of the references made in the
 * parsed expression.
 */
void
Arturo Espinosa's avatar
Arturo Espinosa committed
224
cell_add_dependencies (Cell *cell)
225 226 227 228 229 230 231
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->parsed_node != NULL);
	
	if (!dependency_hash)
		dependency_hash_init ();

Arturo Espinosa's avatar
Arturo Espinosa committed
232
	add_tree_deps (cell, cell->parsed_node);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
}

/*
 * List used by cell_drop_dependencies and dependency_remove_cell
 * to accumulate all of the "dead" DependencyRange structures.
 *
 * Once the table_foreach process has finished, these are released
 */
static GList *remove_list;

static void
dependency_remove_cell (gpointer key, gpointer value, gpointer the_cell)
{
	DependencyRange *range = value;
	GList *list = range->cell_list;

	list = g_list_find (range->cell_list, the_cell);
	if (!list)
		return;
Arturo Espinosa's avatar
Arturo Espinosa committed
252

253
	range->cell_list = g_list_remove_link (range->cell_list, list);
Arturo Espinosa's avatar
Arturo Espinosa committed
254
	
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	range->ref_count--;

	if (range->ref_count == 0)
		remove_list = g_list_prepend (remove_list, range);
}

/*
 * Remove the Cell from the DependecyRange hash tables
 */
void
cell_drop_dependencies (Cell *cell)
{
	g_return_if_fail (cell != NULL);

	if (!dependency_hash)
		return;
	
	g_hash_table_foreach (dependency_hash, dependency_remove_cell, cell);

Arturo Espinosa's avatar
Arturo Espinosa committed
274
	/* Drop any unused DependencyRanges (because their ref_count reached zero) */
275 276 277
	if (remove_list){
		GList *l = remove_list;
		
Arturo Espinosa's avatar
Arturo Espinosa committed
278 279
		for (; l ; l = l->next){
			g_hash_table_remove (dependency_hash, l->data);
280
			g_free (l->data);
Arturo Espinosa's avatar
Arturo Espinosa committed
281
		}
282 283 284 285 286 287 288
		
		g_list_free (remove_list);
		remove_list = NULL;
	}
}

typedef struct {
Arturo Espinosa's avatar
Arturo Espinosa committed
289 290
	int   start_col, start_row;
	int   end_col, end_row;
291 292 293 294
	Sheet *sheet;
	GList *list;
} get_dep_closure_t;

Arturo Espinosa's avatar
Arturo Espinosa committed
295 296 297 298 299 300 301 302 303 304 305 306 307
static gboolean
intersects (Sheet *sheet, int col, int row, DependencyRange *range)
{
	if ((col >= range->start_col) &&
	    (col <= range->end_col)   &&
	    (row >= range->start_row) &&
	    (row <= range->end_row)   &&
	    (sheet = range->sheet))
		return TRUE;

	return FALSE;
}

308 309 310
static void
search_cell_deps (gpointer key, gpointer value, gpointer closure)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
311
	DependencyRange *range = key;
312
	get_dep_closure_t *c = closure;
Arturo Espinosa's avatar
Arturo Espinosa committed
313
	Sheet *sheet = c->sheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
314
	GList *l;
315

Arturo Espinosa's avatar
Arturo Espinosa committed
316 317 318 319 320 321
	if (!(intersects (sheet, c->start_col, c->start_row, range) ||
	      intersects (sheet, c->end_col,   c->end_row,   range) ||
	      intersects (sheet, c->start_col, c->end_row,   range) ||
	      intersects (sheet, c->end_col,   c->start_row, range)))
		return;
			
Arturo Espinosa's avatar
Arturo Espinosa committed
322 323 324
	for (l = range->cell_list; l; l = l->next){
		Cell *cell = l->data;

325
		c->list = g_list_prepend (c->list, cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
326
	}
327 328
}

Arturo Espinosa's avatar
Arturo Espinosa committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
GList *
region_get_dependencies (Sheet *sheet, int start_col, int start_row, int end_col, int end_row)
{
	get_dep_closure_t closure;

	if (!dependency_hash)
		dependency_hash_init ();
	
	closure.start_col = start_col;
	closure.start_row = start_row;
	closure.end_col = end_col;
	closure.end_row = end_row;
	closure.sheet = sheet;
	closure.list = NULL;

	g_hash_table_foreach (dependency_hash, &search_cell_deps, &closure);

	return closure.list;
}

349 350 351 352 353
GList *
cell_get_dependencies (Sheet *sheet, int col, int row)
{
	get_dep_closure_t closure;

Arturo Espinosa's avatar
Arturo Espinosa committed
354 355 356
	if (!dependency_hash)
		dependency_hash_init ();
	
Arturo Espinosa's avatar
Arturo Espinosa committed
357 358 359 360
	closure.start_col = col;
	closure.start_row = row;
	closure.end_col = col;
	closure.end_row = row;
361 362
	closure.sheet = sheet;
	closure.list = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
363 364

	g_hash_table_foreach (dependency_hash, &search_cell_deps, &closure);
365 366 367 368

	return closure.list;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
void
cell_queue_recalc (Cell *cell)
{
	Workbook *wb;

	g_return_if_fail (cell != NULL);
	
	wb = ((Sheet *)cell->sheet)->workbook;
	wb->eval_queue = g_list_prepend (wb->eval_queue, cell);
}

void
cell_queue_recalc_list (GList *list)
{
	Workbook *wb;
	Cell *first_cell;
	
	if (!list)
		return;

	first_cell = list->data;
	wb = ((Sheet *)(first_cell->sheet))->workbook;

	wb->eval_queue = g_list_concat (wb->eval_queue, list);
}

static Cell *
pick_next_cell_from_queue (Workbook *wb)
397
{
Arturo Espinosa's avatar
Arturo Espinosa committed
398 399 400 401 402 403 404 405 406 407
	Cell *cell;

	if (!wb->eval_queue)
		return NULL;
	
	cell = wb->eval_queue->data;
	wb->eval_queue = g_list_remove (wb->eval_queue, cell);
	return cell;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/*
 * Increments the generation.  Every time the generation is
 * about to wrap around, we reset all of the cell counters to zero
 */
void
workbook_next_generation (Workbook *wb)
{
	if (wb->generation == 255){
		GList *cell_list = wb->formula_cell_list;
		
		for (; cell_list; cell_list = cell_list->next){
			Cell *cell = cell_list->data;

			cell->generation = 0;
		}
		wb->generation = 1;
	} else
		wb->generation++;
}

428 429 430 431
/*
 * Computes all of the cells pending computation and
 * any dependency.
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
432 433 434
void
workbook_recalc (Workbook *wb)
{
435
	int generation;
Arturo Espinosa's avatar
Arturo Espinosa committed
436
	Cell *cell;
437 438 439 440
	GList *deps, *l;

	workbook_next_generation (wb);
	generation = wb->generation;
Arturo Espinosa's avatar
Arturo Espinosa committed
441 442
	
	while ((cell = pick_next_cell_from_queue (wb))){
443
		cell->generation = generation;
Arturo Espinosa's avatar
Arturo Espinosa committed
444
		cell_eval (cell);
445
		deps = cell_get_dependencies (cell->sheet, cell->col->pos, cell->row->pos);
446 447 448 449 450 451 452 453 454

		for (l = deps ; l; l = l->next){
			Cell *one_cell;

			one_cell = l->data;
			if (one_cell->generation != generation)
				cell_queue_recalc (one_cell);
		}
		g_list_free (deps);
Arturo Espinosa's avatar
Arturo Espinosa committed
455
	}
456
}
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

/*
 * Recomputes all of the formulas.
 */
void
workbook_recalc_all (Workbook *workbook)
{
	GList *l;
	
	for (l = workbook->formula_cell_list; l; l = l->next){
		Cell *cell = l->data;

		cell_queue_recalc (cell);
	}
	workbook_recalc (workbook);
}