eval.c 37.3 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1
/* vim: set sw=8: */
Arturo Espinosa's avatar
Arturo Espinosa committed
2
/*
Jody Goldberg's avatar
Jody Goldberg committed
3
 * eval.c:  Manage calculation dependencies between objects
Arturo Espinosa's avatar
Arturo Espinosa committed
4
 *
Jody Goldberg's avatar
Jody Goldberg committed
5
 * Copyright (C) 2000,2001
Jody Goldberg's avatar
Jody Goldberg committed
6
 *  Jody Goldberg   (jody@gnome.org)
Jody Goldberg's avatar
Jody Goldberg committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
Arturo Espinosa's avatar
Arturo Espinosa committed
22
 */
23 24
#include <gnumeric-config.h>
#include "gnumeric.h"
25
#include "eval.h"
26

27
#include "parse-util.h"
28
#include "ranges.h"
29
#include "value.h"
Michael Meeks's avatar
Michael Meeks committed
30
#include "main.h"
31 32
#include "workbook-control.h"
#include "workbook-view.h"
33
#include "workbook-private.h"
Jody Goldberg's avatar
Jody Goldberg committed
34
#include "expr.h"
35
#include "expr-name.h"
Jody Goldberg's avatar
Jody Goldberg committed
36
#include "cell.h"
Jody Goldberg's avatar
Jody Goldberg committed
37
#include "sheet.h"
Michael Meeks's avatar
Michael Meeks committed
38

39
#define BUCKET_SIZE	128
40

41
#define UNLINK_DEP(dep)							\
42
  do {									\
43 44
	if (dep->sheet->deps->dependent_list == dep)			\
		dep->sheet->deps->dependent_list = dep->next_dep;	\
45 46 47 48 49
	if (dep->next_dep)						\
		dep->next_dep->prev_dep = dep->prev_dep;		\
	if (dep->prev_dep)						\
		dep->prev_dep->next_dep = dep->next_dep;		\
	/* Note, that ->prev_dep and ->next_dep are still valid.  */	\
50 51
  } while (0)

Jody Goldberg's avatar
Jody Goldberg committed
52
static GPtrArray *dep_classes = NULL;
Michael Meeks's avatar
Michael Meeks committed
53

Jody Goldberg's avatar
Jody Goldberg committed
54 55
void
dependent_types_init (void)
56
{
Jody Goldberg's avatar
Jody Goldberg committed
57
	g_return_if_fail (dep_classes == NULL);
58

Jody Goldberg's avatar
Jody Goldberg committed
59 60 61 62
	/* Init with a pair of NULL classes so we can access directly */
	dep_classes = g_ptr_array_new ();
	g_ptr_array_add	(dep_classes, NULL);
	g_ptr_array_add	(dep_classes, NULL);
63 64
}

Jody Goldberg's avatar
Jody Goldberg committed
65 66
void
dependent_types_shutdown (void)
67
{
Jody Goldberg's avatar
Jody Goldberg committed
68 69
	g_return_if_fail (dep_classes != NULL);
	g_ptr_array_free (dep_classes, TRUE);
70 71
}

Jody Goldberg's avatar
Jody Goldberg committed
72 73 74 75 76 77 78 79 80
/**
 * dependent_register_type :
 * @klass : A vtable
 *
 * Store the vtable and allocate an ID for a new class
 * of dependents.
 */
guint32
dependent_type_register (DependentClass const *klass)
81
{
Jody Goldberg's avatar
Jody Goldberg committed
82
	guint32 res;
83

Jody Goldberg's avatar
Jody Goldberg committed
84
	g_return_val_if_fail (dep_classes != NULL, 0);
85

Jody Goldberg's avatar
Jody Goldberg committed
86 87
	g_ptr_array_add	(dep_classes, (gpointer)klass);
	res = dep_classes->len-1;
88

Jody Goldberg's avatar
Jody Goldberg committed
89
	g_return_val_if_fail (res <= DEPENDENT_TYPE_MASK, res);
90

Jody Goldberg's avatar
Jody Goldberg committed
91
	return res;
92 93 94
}


Jody Goldberg's avatar
Jody Goldberg committed
95 96 97
/**
 * dependent_set_expr :
 * @dep : The dependent we are interested in.
98
 * @new_expr : new expression.
Jody Goldberg's avatar
Jody Goldberg committed
99 100 101
 *
 * When the expression associated with a dependent needs to change
 * this routine dispatches to the virtual handler.
Michael Meeks's avatar
Michael Meeks committed
102
 */
Jody Goldberg's avatar
Jody Goldberg committed
103
void
104
dependent_set_expr (Dependent *dep, ExprTree *new_expr)
Michael Meeks's avatar
Michael Meeks committed
105
{
106
	int const t = dependent_type (dep);
Jody Goldberg's avatar
Jody Goldberg committed
107 108 109 110 111 112

	if (t == DEPENDENT_CELL) {
		/*
		 * Explicitly do not check for array subdivision, we may be
		 * replacing the corner of an array.
		 */
113
		cell_set_expr_unsafe (DEP_TO_CELL (dep), new_expr);
Jody Goldberg's avatar
Jody Goldberg committed
114 115 116 117
	} else {
		DependentClass *klass = g_ptr_array_index (dep_classes, t);

		g_return_if_fail (klass);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		if (klass->set_expr != NULL)
			(*klass->set_expr) (dep, new_expr);
#if 0
		{
			ParsePos pos;
			char *str;

			parse_pos_init_dep (&pos, dep);
			dependent_debug_name (dep, stdout);

			str = expr_tree_as_string (new_expr, &pos);
			printf(" new = %s\n", str);
			g_free (str);

			str = expr_tree_as_string (dep->expression, &pos);
			printf("\told = %s\n", str);
			g_free (str);
		}
#endif

		if (new_expr != NULL)
			expr_tree_ref (new_expr);
140
		if (dependent_is_linked (dep))
141 142 143 144 145 146 147
			dependent_unlink (dep, NULL);
		if (dep->expression != NULL)
			expr_tree_unref (dep->expression);

		dep->expression = new_expr;
		if (new_expr != NULL)
			dependent_changed (dep, TRUE);
Jody Goldberg's avatar
Jody Goldberg committed
148
	}
Michael Meeks's avatar
Michael Meeks committed
149 150
}

Jody Goldberg's avatar
fix.  
Jody Goldberg committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/**
 * dependent_set_sheet
 * @dep :
 * @sheet :
 */
void
dependent_set_sheet (Dependent *dep, Sheet *sheet)
{
	g_return_if_fail (dep != NULL);
	g_return_if_fail (dep->sheet == NULL);
	g_return_if_fail (!dependent_is_linked (dep));

	dep->sheet = sheet;
	if (dep->expression != NULL)
		dependent_changed (dep, TRUE);
}

Michael Meeks's avatar
Michael Meeks committed
168
/*
Jody Goldberg's avatar
Jody Goldberg committed
169 170 171
 * dependent_queue_recalc:
 * @dep: the dependent that contains the expression needing recomputation.
 *
172
 * Marks @dep as needing recalculation
Jody Goldberg's avatar
Jody Goldberg committed
173
 * NOTE : it does NOT recursively dirty dependencies.
Michael Meeks's avatar
Michael Meeks committed
174
 */
175 176
#define dependent_queue_recalc(dep) \
  do { (dep)->flags |= DEPENDENT_NEEDS_RECALC; } while (0)
177

178 179
static void
cb_cell_list_deps (Dependent *dep, gpointer user)
180
{
181 182 183
	GSList **list = (GSList **)user;
	*list = g_slist_prepend (*list, dep);
}
184

185 186 187 188 189 190
static GSList *
cell_list_deps (const Cell *cell)
{
	GSList *deps = NULL;
	cell_foreach_dep (cell, cb_cell_list_deps, &deps);
	return deps;
191 192 193
}


Jody Goldberg's avatar
Jody Goldberg committed
194 195 196 197 198
/**
 * dependent_queue_recalc_list :
 * @list :
 *
 * Queues any elements of @list for recalc that are not already queued,
199
 * and marks all elements as needing a recalc.
Jody Goldberg's avatar
Jody Goldberg committed
200 201
 */
static void
202
dependent_queue_recalc_list (GSList *list)
203
{
204 205
	GSList *work = NULL;

Jody Goldberg's avatar
Jody Goldberg committed
206 207
	for (; list != NULL ; list = list->next) {
		Dependent *dep = list->data;
208 209
		if (!(dep->flags & DEPENDENT_NEEDS_RECALC)) {
			dependent_queue_recalc (dep);
210
			work = g_slist_prepend (work, dep);
211 212
		}
	}
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	/*
	 * Work is now a list of marked cells whose dependencies need
	 * to be marked.  Marking early guarentees that we will not
	 * get duplicates.  (And it thus limits the length of the list.)
	 * We treat work as a stack.
	 */

	while (work) {
		Dependent *dep = work->data;

		/* Pop the top element.  */
		list = work;
		work = work->next;
		g_slist_free_1 (list);

229
		if (dependent_is_cell (dep)) {
230
			GSList *deps = cell_list_deps (DEP_TO_CELL (dep));
231 232 233
			GSList *waste = NULL;
			GSList *next;
			for (list = deps; list != NULL ; list = next) {
234
				Dependent *dep = list->data;
235 236 237 238 239
				next = list->next;
				if (dep->flags & DEPENDENT_NEEDS_RECALC) {
					list->next = waste;
					waste = list;
				} else {
240
					dependent_queue_recalc (dep);
241 242
					list->next = work;
					work = list;
243 244
				}
			}
245
			g_slist_free (waste);
246
		}
Jody Goldberg's avatar
Jody Goldberg committed
247
	}
248 249
}

250 251 252 253 254 255 256

void
cb_dependent_queue_recalc (Dependent *dep, gpointer ignore)
{
	g_return_if_fail (dep != NULL);

	if (!(dep->flags & DEPENDENT_NEEDS_RECALC)) {
257 258
		GSList listrec;
		listrec.next = NULL;
259
		listrec.data = dep;
260
		dependent_queue_recalc_list (&listrec);
261 262 263
	}
}

264
/**************************************************************************/
265
#define ENABLE_MICRO_HASH
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
#ifdef ENABLE_MICRO_HASH
typedef struct {
	gint     num_buckets;
	gint     num_elements;
	union {
		GSList **buckets;
		GSList *singleton;
	} u;
} MicroHash;

#define MICRO_HASH_MIN_SIZE 11
#define MICRO_HASH_MAX_SIZE 13845163
#define MICRO_HASH_RESIZE(hash_table)				\
G_STMT_START {							\
	if ((hash_table->num_buckets > MICRO_HASH_MIN_SIZE &&		\
	     hash_table->num_buckets >= 3 * hash_table->num_elements) || 	\
	    (hash_table->num_buckets < MICRO_HASH_MAX_SIZE &&		\
	     3 * hash_table->num_buckets <= hash_table->num_elements))	\
		micro_hash_resize (hash_table);			\
} G_STMT_END

/* The records are aligned so the bottom few bits don't hold much
 * entropy
 */
#define MICRO_HASH_hash(key)	((guint)((int) (key) >> 9))

static void
micro_hash_resize (MicroHash *hash_table)
{
	GSList **new_buckets, *node, *next;
	guint bucket;
	gint old_num_buckets = hash_table->num_buckets;
	gint new_num_buckets = CLAMP (g_spaced_primes_closest (hash_table->num_elements),
		MICRO_HASH_MIN_SIZE, MICRO_HASH_MAX_SIZE);

	if (old_num_buckets <= 1) {
		if (new_num_buckets == 1)
			return;
		new_buckets = g_new0 (GSList *, new_num_buckets);
		for (node = hash_table->u.singleton; node; node = next) {
			next = node->next;
			bucket =  MICRO_HASH_hash (node->data) % new_num_buckets;
			node->next = new_buckets [bucket];
			new_buckets [bucket] = node;
		}
		hash_table->u.buckets = new_buckets;
	} else if (new_num_buckets > 1) {
		new_buckets = g_new0 (GSList *, new_num_buckets);
		for (old_num_buckets = hash_table->num_buckets; old_num_buckets-- > 0 ; )
			for (node = hash_table->u.buckets [old_num_buckets]; node; node = next) {
				next = node->next;
				bucket =  MICRO_HASH_hash (node->data) % new_num_buckets;
				node->next = new_buckets [bucket];
				new_buckets [bucket] = node;
			}
		g_free (hash_table->u.buckets);
		hash_table->u.buckets = new_buckets;
	} else {
		GSList *singleton = NULL;
		while (old_num_buckets-- > 0)
			singleton = g_slist_concat (singleton, hash_table->u.buckets [old_num_buckets]);
		g_free (hash_table->u.buckets);
		hash_table->u.singleton = singleton;
	}

	hash_table->num_buckets = new_num_buckets;
}

static void
micro_hash_insert (MicroHash *hash_table, gpointer key)
{
	GSList **head;
	int const hash_size = hash_table->num_buckets;

	if (hash_size > 1) {
		guint const bucket = MICRO_HASH_hash (key) % hash_size;
		head = hash_table->u.buckets + bucket;
	} else
		head = & (hash_table->u.singleton);

	if (g_slist_find (*head, key) == NULL) {
		*head = g_slist_prepend (*head, key);
		hash_table->num_elements++;
		MICRO_HASH_RESIZE (hash_table);
	}
}

static void
micro_hash_remove (MicroHash *hash_table, gpointer key)
{
	GSList **head;
	int const hash_size = hash_table->num_buckets;

	if (hash_size > 1) {
		guint const bucket = MICRO_HASH_hash (key) % hash_size;
		head = hash_table->u.buckets + bucket;
	} else
		head = & (hash_table->u.singleton);

	for (; *head != NULL ; head = &((*head)->next))
		if ((*head)->data == key) {
			*head = (*head)->next;
			hash_table->num_elements--;
			MICRO_HASH_RESIZE (hash_table);
			return;
		}
}

static void
micro_hash_release (MicroHash *hash_table)
{
	guint i = hash_table->num_buckets;

	if (i > 1) {
		while (i-- > 0)
			g_slist_free (hash_table->u.buckets[i]);
		g_free (hash_table->u.buckets);
		hash_table->u.buckets = NULL;
	} else {
		g_slist_free (hash_table->u.singleton);
		hash_table->u.singleton = NULL;
	}
	hash_table->num_elements = 0;
	hash_table->num_buckets = 1;
}

static void
micro_hash_init (MicroHash *hash_table, gpointer key)
{
	hash_table->num_elements = 0;
	hash_table->num_buckets = 1;
	hash_table->u.singleton = g_slist_prepend (NULL, key);
}

/*************************************************************************/

typedef MicroHash	DepCollection;
#define dep_collection_init(dc, dep)	\
	micro_hash_init (&(dc), dep)
#define dep_collection_insert(dc, dep)	\
	micro_hash_insert (&(dc), dep)
#define dep_collection_remove(dc, dep)	\
	micro_hash_remove (&(dc), dep)
#define dep_collection_is_empty(dc)				\
	(dc.num_elements == 0)
#define dep_collection_foreach_dep(dc, dep, code) do {		\
	GSList *l;						\
	int i = dc.num_buckets;					\
	if (i <= 1) { 						\
		for (l = dc.u.singleton; l ; l = l->next) {	\
			Dependent *dep = l->data;		\
			code					\
		}						\
	} else while (i-- > 0) {				\
		for (l = dc.u.buckets [i]; l ; l = l->next) {	\
			Dependent *dep = l->data;		\
			code					\
		}						\
	}							\
} while (0)
#define dep_collection_foreach_list(dc, list, code) do {	\
	GSList *list;						\
	int i = dc.num_buckets;					\
	if (i <= 1) { 						\
		list = dc.u.singleton;				\
		code						\
	} else while (i-- > 0) {				\
		list = dc.u.buckets[i];				\
		code						\
	}							\
} while (0)
#else
typedef GSList *	DepCollection;
#define dep_collection_init(dc, dep)	\
	dc = g_slist_prepend (NULL, dep)
#define dep_collection_insert(dc, dep) \
	if (!g_slist_find (dc, dep)) dc = g_slist_prepend (dc, dep)
#define dep_collection_remove(dc, dep)	\
	dc = g_slist_remove (dc, dep);
#define dep_collection_is_empty(dc)	\
	(dc == NULL)
#define dep_collection_foreach_dep(dc, dep, code) do {		\
	GSList *l;						\
	for (l = dc; l != NULL ; l = l->next) {			\
		Dependent *dep = l->data;			\
		code						\
	}							\
} while (0)
#define dep_collection_foreach_list(dc, list, code) do {	\
	GSList *list = dc;					\
	code							\
} while (0)
#endif

Jody Goldberg's avatar
Jody Goldberg committed
460 461 462 463 464 465
/**************************************************************************
 * Data structures for managing dependencies between objects.
 *
 * The DependencyRange hash needs to be improved.  It is a huge
 * performance hit when there are large numbers of range depends.
 */
466

Jody Goldberg's avatar
Jody Goldberg committed
467 468 469 470 471
/*
 * A DependencyRange defines a range of cells whose values
 * are used by another objects in the spreadsheet.
 *
 * A change in those cells will trigger a recomputation on the
472
 * cells listed in deps.
Jody Goldberg's avatar
Jody Goldberg committed
473 474
 */
typedef struct {
475
	DepCollection	deps;	/* Must be first */
Jody Goldberg's avatar
Jody Goldberg committed
476 477
	Range  range;
} DependencyRange;
478

479
/*
480 481
 *  A DependencySingle stores a list of dependents that rely
 * on the cell at @pos.
482
 *
483
 * A change in this cell will trigger a recomputation on the
484
 * cells listed in deps.
485
 */
Jody Goldberg's avatar
Jody Goldberg committed
486
typedef struct {
487
	DepCollection	deps;	/* Must be first */
Jody Goldberg's avatar
Jody Goldberg committed
488 489 490
	CellPos pos;
} DependencySingle;

491 492
/* A utility type */
typedef struct {
493
	DepCollection	deps;	/* Must be first */
494 495
} DependencyAny;

Jody Goldberg's avatar
Jody Goldberg committed
496 497 498 499 500
typedef enum {
	REMOVE_DEPS = 0,
	ADD_DEPS = 1
} DepOperation;

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
static guint
deprange_hash_func (gconstpointer v)
{
	DependencyRange const *r = v;

	return ((((r->range.start.row << 8) + r->range.end.row) << 8) +
		(r->range.start.col << 8) + (r->range.end.col));
}
static gint
deprange_equal_func (gconstpointer v, gconstpointer v2)
{
	DependencyRange const *r1 = (DependencyRange const *) v;
	DependencyRange const *r2 = (DependencyRange const *) v2;

	if (r1->range.start.col != r2->range.start.col)
		return 0;
	if (r1->range.start.row != r2->range.start.row)
		return 0;
	if (r1->range.end.col != r2->range.end.col)
		return 0;
	if (r1->range.end.row != r2->range.end.row)
		return 0;

	return 1;
}


static guint
depsingle_hash (gconstpointer key)
{
	DependencySingle const *d = (DependencySingle const *) key;

	return (d->pos.row << 8) ^ d->pos.col;
}

static gint
depsingle_equal (gconstpointer ai, gconstpointer bi)
{
	DependencySingle const *a = (DependencySingle const *)ai;
	DependencySingle const *b = (DependencySingle const *)bi;

	return (a->pos.row == b->pos.row &&
		a->pos.col == b->pos.col);
}

546
static void
Jody Goldberg's avatar
Jody Goldberg committed
547 548
handle_cell_single_dep (Dependent *dep, CellPos const *pos,
			CellRef const *a, DepOperation operation)
Michael Meeks's avatar
Michael Meeks committed
549
{
550
	DependencyContainer *deps;
Jody Goldberg's avatar
Jody Goldberg committed
551
	DependencySingle *single;
552
	DependencySingle lookup;
553

Jody Goldberg's avatar
Jody Goldberg committed
554 555 556 557
	if (a->sheet == NULL)
		deps = dep->sheet->deps;
	else
		deps = a->sheet->deps;
558

Jody Goldberg's avatar
Jody Goldberg committed
559
	if (!deps)
560 561
		return;

Jody Goldberg's avatar
Jody Goldberg committed
562
	/* Convert to absolute cordinates */
563
	cellref_get_abs_pos (a, pos, &lookup.pos);
Michael Meeks's avatar
Michael Meeks committed
564

Jody Goldberg's avatar
Jody Goldberg committed
565
	single = g_hash_table_lookup (deps->single_hash, &lookup);
Jody Goldberg's avatar
Jody Goldberg committed
566

Jody Goldberg's avatar
Jody Goldberg committed
567 568
	if (operation == ADD_DEPS) {
		if (single) {
569 570
			/* Inserts if it is not already there */
			dep_collection_insert (single->deps, dep);
Jody Goldberg's avatar
Jody Goldberg committed
571 572 573
		} else {
			single  = g_new (DependencySingle, 1);
			*single = lookup;
574
			dep_collection_init (single->deps, dep);
Jody Goldberg's avatar
Jody Goldberg committed
575
			g_hash_table_insert (deps->single_hash, single, single);
576
		}
Jody Goldberg's avatar
Jody Goldberg committed
577 578
	} else { /* Remove */
		if (single) {
579 580
			dep_collection_remove (single->deps, dep);
			if (dep_collection_is_empty (single->deps)) {
Jody Goldberg's avatar
Jody Goldberg committed
581 582 583
				g_hash_table_remove (deps->single_hash, single);
				g_free (single);
			}
Jody Goldberg's avatar
Jody Goldberg committed
584
		} else
Jody Goldberg's avatar
Jody Goldberg committed
585
			;/* Referenced twice and list killed already */
586 587 588
	}
}

589
static void
590
add_range_dep (DependencyContainer *deps, Dependent *dep,
591
	       DependencyRange const *r)
592
{
593 594 595 596 597 598
	int i = r->range.start.row / BUCKET_SIZE;
	int const end = r->range.end.row / BUCKET_SIZE;

	for ( ; i <= end; i++) {
		/* Look it up */
		DependencyRange *result;
599

600 601 602 603 604 605 606
		if (deps->range_hash [i] == NULL) {
			deps->range_hash [i] =
				g_hash_table_new (deprange_hash_func,
						  deprange_equal_func);
			result = NULL;
		} else {
			result = g_hash_table_lookup (deps->range_hash[i], r);
Michael Meeks's avatar
Michael Meeks committed
607

608
			if (result) {
609 610
				/* Inserts if it is not already there */
				dep_collection_insert (result->deps, dep);
Morten Welinder's avatar
Morten Welinder committed
611
				continue;
612 613
			}
		}
614

615 616 617
		/* Create a new DependencyRange structure */
		result = g_new (DependencyRange, 1);
		*result = *r;
618
		dep_collection_init (result->deps, dep);
619 620
		g_hash_table_insert (deps->range_hash[i], result, result);
	}
621 622
}

Michael Meeks's avatar
Michael Meeks committed
623
static void
624
drop_range_dep (DependencyContainer *deps, Dependent *dep,
625
		DependencyRange const *r)
Michael Meeks's avatar
Michael Meeks committed
626
{
627 628
	int i = r->range.start.row / BUCKET_SIZE;
	int const end = r->range.end.row / BUCKET_SIZE;
629
	DependencyRange *result;
630

Morten Welinder's avatar
Morten Welinder committed
631 632 633
	if (!deps)
		return;

634 635 636
	for ( ; i <= end; i++) {
		result = g_hash_table_lookup (deps->range_hash[i], r);
		if (result) {
637 638
			dep_collection_remove (result->deps, dep);
			if (dep_collection_is_empty (result->deps)) {
639 640 641
				g_hash_table_remove (deps->range_hash[i], result);
				g_free (result);
			}
Michael Meeks's avatar
Michael Meeks committed
642
		}
Jody Goldberg's avatar
Jody Goldberg committed
643 644 645
	}
}

646
/*
647
 * Add the dependency of Dependent dep on the range
648 649
 * enclose by CellRef a and CellRef b
 *
650
 * We compute the location from @pos
651 652
 */
static void
Jody Goldberg's avatar
Jody Goldberg committed
653 654
handle_cell_range_deps (Dependent *dep, CellPos const *pos,
			CellRef const *a, CellRef const *b, DepOperation operation)
655 656
{
	DependencyRange range;
657
	DependencyContainer *depsa, *depsb;
Michael Meeks's avatar
Michael Meeks committed
658

659 660 661
	cellref_get_abs_pos (a, pos, &range.range.start);
	cellref_get_abs_pos (b, pos, &range.range.end);
	range_normalize (&range.range);
Michael Meeks's avatar
Michael Meeks committed
662

663 664 665 666 667
	depsa = eval_sheet (a->sheet, dep->sheet)->deps;
	if (operation)
		add_range_dep  (depsa, dep, &range);
	else
		drop_range_dep (depsa, dep, &range);
668

669 670 671
	depsb = eval_sheet (b->sheet, dep->sheet)->deps;
	if (depsa != depsb) {
		/* FIXME: we need to iterate sheets between to be correct */
Michael Meeks's avatar
Michael Meeks committed
672
		if (operation)
673
			add_range_dep  (depsb, dep, &range);
Michael Meeks's avatar
Michael Meeks committed
674
		else
675
			drop_range_dep (depsb, dep, &range);
Michael Meeks's avatar
Michael Meeks committed
676
	}
677 678
}

679 680 681 682
/*
 * Adds the dependencies for a Value
 */
static void
Jody Goldberg's avatar
Jody Goldberg committed
683 684
handle_value_deps (Dependent *dep, CellPos const *pos,
		   Value const *value, DepOperation operation)
685
{
Michael Meeks's avatar
Michael Meeks committed
686
	switch (value->type) {
687
	case VALUE_EMPTY:
688 689 690
	case VALUE_STRING:
	case VALUE_INTEGER:
	case VALUE_FLOAT:
691 692
	case VALUE_BOOLEAN:
	case VALUE_ERROR:
Jody Goldberg's avatar
Jody Goldberg committed
693
		/* Constants have no dependencies */
694
		break;
695

696 697
		/* Check every element of the array */
	case VALUE_ARRAY:
698
	{
699
		int x, y;
700

701 702
		for (x = 0; x < value->v_array.x; x++)
			for (y = 0; y < value->v_array.y; y++)
703
				handle_value_deps (dep, pos,
704
						   value->v_array.vals [x] [y],
Michael Meeks's avatar
Michael Meeks committed
705
						   operation);
706
		break;
707
	}
708
	case VALUE_CELLRANGE:
709
		handle_cell_range_deps (dep, pos,
710 711
			&value->v_range.cell.a,
			&value->v_range.cell.b,
Michael Meeks's avatar
Michael Meeks committed
712
			operation);
713
		break;
714 715 716
	default:
		g_warning ("Unknown Value type, dependencies lost");
		break;
717 718 719 720 721 722 723 724
	}
}

/*
 * This routine walks the expression tree looking for cell references
 * and cell range references.
 */
static void
Jody Goldberg's avatar
Jody Goldberg committed
725
handle_tree_deps (Dependent *dep, CellPos const *pos,
726
		  ExprTree *tree, DepOperation operation)
727
{
728
	switch (tree->any.oper) {
Morten Welinder's avatar
Morten Welinder committed
729
	case OPER_ANY_BINARY:
730 731
		handle_tree_deps (dep, pos, tree->binary.value_a, operation);
		handle_tree_deps (dep, pos, tree->binary.value_b, operation);
732 733
		return;

Morten Welinder's avatar
Morten Welinder committed
734
	case OPER_ANY_UNARY:
735
		handle_tree_deps (dep, pos, tree->unary.value, operation);
Morten Welinder's avatar
Morten Welinder committed
736
		return;
737 738

	case OPER_VAR:
739
		handle_cell_single_dep (dep, pos, &tree->var.ref, operation);
740 741
		return;

742
	case OPER_CONSTANT:
743
		handle_value_deps (dep, pos, tree->constant.value, operation);
744
		return;
Jody Goldberg's avatar
Jody Goldberg committed
745

Michael Meeks's avatar
Michael Meeks committed
746 747 748 749
	/*
	 * FIXME: needs to be taught implicit intersection +
	 * more cunning handling of argument type matching.
	 */
Jody Goldberg's avatar
Jody Goldberg committed
750
	case OPER_FUNCALL: {
751
		ExprList *l;
Jody Goldberg's avatar
Jody Goldberg committed
752

753
		for (l = tree->func.arg_list; l; l = l->next)
754
			handle_tree_deps (dep, pos, l->data, operation);
755
		return;
Jody Goldberg's avatar
Jody Goldberg committed
756
	}
757

758
	case OPER_NAME:
759 760 761 762 763
		if (operation == ADD_DEPS)
			expr_name_add_dep (tree->name.name, dep);
		else
			expr_name_remove_dep (tree->name.name, dep);
		if (!tree->name.name->builtin)
764
			handle_tree_deps (dep, pos, tree->name.name->t.expr_tree, operation);
765 766
		return;

767
	case OPER_ARRAY:
768
		if (tree->array.x != 0 || tree->array.y != 0) {
769
			/* Non-corner cells depend on the corner */
Michael Meeks's avatar
Michael Meeks committed
770 771
			CellRef a;

Morten Welinder's avatar
Morten Welinder committed
772
			/* We cannot support array expressions unless
773 774 775 776
			 * we have a position.
			 */
			g_return_if_fail (pos != NULL);

777
			a.col_relative = a.row_relative = FALSE;
778 779 780
			a.sheet = dep->sheet;
			a.col   = pos->col - tree->array.x;
			a.row   = pos->row - tree->array.y;
Michael Meeks's avatar
Michael Meeks committed
781

782
			handle_cell_single_dep (dep, pos, &a, operation);
783 784
		} else
			/* Corner cell depends on the contents of the expr */
785
			handle_tree_deps (dep, pos, tree->array.corner.expr, operation);
786
		return;
787 788 789 790 791 792 793 794 795

	case OPER_SET: {
		ExprList *l;

		for (l = tree->set.set; l; l = l->next)
			handle_tree_deps (dep, pos, l->data, operation);
		return;
	}

796 797 798
	default:
		g_warning ("Unknown Operation type, dependencies lost");
		break;
799
	}
800 801
}

Jody Goldberg's avatar
Jody Goldberg committed
802 803 804 805 806 807 808 809
/**
 * dependent_link:
 * @dep : the dependent that changed
 * @pos: The optionally NULL position of the dependent.
 *
 * Adds the dependent to the workbook wide list of dependents.
 */
void
810
dependent_link (Dependent *dep, CellPos const *pos)
Jody Goldberg's avatar
Jody Goldberg committed
811
{
812
	Sheet *sheet;
Jody Goldberg's avatar
Jody Goldberg committed
813 814 815

	g_return_if_fail (dep != NULL);
	g_return_if_fail (dep->expression != NULL);
Jody Goldberg's avatar
Jody Goldberg committed
816
	g_return_if_fail (!(dep->flags & DEPENDENT_IS_LINKED));
Jody Goldberg's avatar
Jody Goldberg committed
817 818 819
	g_return_if_fail (IS_SHEET (dep->sheet));
	g_return_if_fail (dep->sheet->deps != NULL);

820
	sheet = dep->sheet;
Jody Goldberg's avatar
Jody Goldberg committed
821

822
	/* Make this the new head of the dependent list.  */
823
	dep->prev_dep = NULL;
824
	dep->next_dep = sheet->deps->dependent_list;
825 826
	if (dep->next_dep)
		dep->next_dep->prev_dep = dep;
827
	sheet->deps->dependent_list = dep;
Jody Goldberg's avatar
Jody Goldberg committed
828
	dep->flags |= DEPENDENT_IS_LINKED;
Jody Goldberg's avatar
Jody Goldberg committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842

	handle_tree_deps (dep, pos, dep->expression, ADD_DEPS);
}

/**
 * dependent_unlink:
 * @dep : the dependent that changed
 * @pos: The optionally NULL position of the dependent.
 *
 * Removes the dependent from the workbook wide list of dependents.
 */
void
dependent_unlink (Dependent *dep, CellPos const *pos)
{
843
	static CellPos const dummy = { 0, 0 };
Jody Goldberg's avatar
Jody Goldberg committed
844 845
	g_return_if_fail (dep != NULL);

846
	if (pos == NULL)
Jody Goldberg's avatar
typo  
Jody Goldberg committed
847
		pos = &dummy;
848

Jody Goldberg's avatar
Jody Goldberg committed
849 850
	if (dep->sheet != NULL) {
		g_return_if_fail (dep->expression != NULL);
Jody Goldberg's avatar
Jody Goldberg committed
851
		g_return_if_fail (dep->flags & DEPENDENT_IS_LINKED);
Jody Goldberg's avatar
Jody Goldberg committed
852 853
		g_return_if_fail (IS_SHEET (dep->sheet));

854
		/* see note in do_deps_destroy */
855 856 857 858 859 860 861 862
		/* NOTE That notion is wrong it is only viable for removing
		 * dependents within the sheet or book being destroyed.
		 * If we are deleting a sheet with references outside that sheet
		 * we need to drop those dependencies.  A good idea would be to
		 * flag dependents with links outside the current sheet and
		 * book (both) and use that to save time later.
		 */

863
		if (dep->sheet->deps != NULL) {
864
			handle_tree_deps (dep, pos, dep->expression, REMOVE_DEPS);
865
			UNLINK_DEP (dep);
866 867 868
/* FIXME FIXME FIXME : Massive hack !  rework dependent linkage cycle after release */
		} if (!dependent_is_cell (dep))
			handle_tree_deps (dep, pos, dep->expression, REMOVE_DEPS);
Jody Goldberg's avatar
Jody Goldberg committed
869

Jody Goldberg's avatar
Jody Goldberg committed
870
		dep->flags &= ~(DEPENDENT_IS_LINKED | DEPENDENT_NEEDS_RECALC);
Jody Goldberg's avatar
Jody Goldberg committed
871 872 873 874 875 876 877 878 879
	}
}

/**
 * dependent_unlink_sheet :
 * @sheet :
 *
 * An internal routine to remove all expressions associated with a given sheet
 * from the workbook wide expression list.  WARNING : This is a dangerous
880 881
 * internal function.  it leaves the dependents in an invalid state.  It is
 * intended for use by sheet_destroy_contents.
Jody Goldberg's avatar
Jody Goldberg committed
882 883
 */
void
884
dependent_unlink_sheet (Sheet *sheet)
Jody Goldberg's avatar
Jody Goldberg committed
885 886 887
{
	g_return_if_fail (IS_SHEET (sheet));

888
	SHEET_FOREACH_DEPENDENT (sheet, dep, {
Jody Goldberg's avatar
Jody Goldberg committed
889
		 dep->flags &= ~DEPENDENT_IS_LINKED;
890 891
		 UNLINK_DEP (dep);
	 });
Jody Goldberg's avatar
Jody Goldberg committed
892 893 894 895 896 897 898
}

/**
 * dependent_changed:
 * @cell : the dependent that changed
 * @queue_recalc: also queue a recalc for the dependent.
 *
899 900
 * Registers the expression with the sheet and optionally queues a recalc
 * of the dependent.
Jody Goldberg's avatar
Jody Goldberg committed
901 902
 */
void
903
dependent_changed (Dependent *dep, gboolean queue_recalc)
Jody Goldberg's avatar
Jody Goldberg committed
904
{
905 906
	static CellPos const pos = { 0, 0 };

907 908
	g_return_if_fail (dep != NULL);

909 910 911 912
	/* A pos should not be necessary, but supply one just in case.  If a
	 * user specifies a relative reference this is probably what they want.
	 */
	dependent_link (dep, &pos);
913

914 915
	if (queue_recalc) {
		if (dep->sheet->workbook->priv->recursive_dirty_enabled)
916
			cb_dependent_queue_recalc (dep,  NULL);
917 918 919
		else
			dependent_queue_recalc (dep);
	}
Jody Goldberg's avatar
Jody Goldberg committed
920
}
921

922 923
/**
 * cell_add_dependencies:
Jody Goldberg's avatar
Jody Goldberg committed
924 925
 * @cell:
 *
926 927 928
 * This registers the dependencies for this cell
 * by scanning all of the references made in the
 * parsed expression.
929
 **/
930
void
Arturo Espinosa's avatar
Arturo Espinosa committed
931
cell_add_dependencies (Cell *cell)
932 933
{
	g_return_if_fail (cell != NULL);
934 935
	g_return_if_fail (cell->base.sheet != NULL);
	g_return_if_fail (cell->base.sheet->deps != NULL);
Morten Welinder's avatar
Morten Welinder committed
936

937
	if (cell_has_expr (cell))
938 939
		handle_tree_deps (CELL_TO_DEP (cell), &cell->pos,
				  cell->base.expression, ADD_DEPS);
940 941
}

942 943
/**
 * cell_drop_dependencies:
Jody Goldberg's avatar
Jody Goldberg committed
944 945
 * @cell:
 *
946
 *
947
 **/
948 949 950 951
void
cell_drop_dependencies (Cell *cell)
{
	g_return_if_fail (cell != NULL);
952
	g_return_if_fail (cell->base.sheet != NULL);
953

954
	if (cell_has_expr (cell))
955 956 957 958
		handle_tree_deps (CELL_TO_DEP (cell), &cell->pos,
				  cell->base.expression, REMOVE_DEPS);
}

959
void
Jody Goldberg's avatar
Jody Goldberg committed
960
cell_eval (Cell *cell)
961
{
962 963 964 965 966 967 968 969 970
	g_return_if_fail (cell != NULL);

	if (cell_needs_recalc (cell)) {
		gboolean finished = cell_eval_content (cell);

		/* This should always be the top of the stack */
		g_return_if_fail (finished);

		cell->base.flags &= ~DEPENDENT_NEEDS_RECALC;
Jody Goldberg's avatar
Jody Goldberg committed
971 972 973 974 975 976 977 978 979 980 981 982 983
	}
}

/**
 * cell_queue_recalc :
 * @cell :
 *
 * Queue the cell and everything that depends on it for recalculation.
 * If a dependency is already queued ignore it.
 */
void
cell_queue_recalc (Cell const *cell)
{
984 985
	g_return_if_fail (cell != NULL);

Jody Goldberg's avatar
Jody Goldberg committed
986
	if (!cell_needs_recalc (cell)) {
987 988
		GSList *deps;

989 990
		if (cell_has_expr (cell))
			dependent_queue_recalc (CELL_TO_DEP (cell));
991 992

		deps = cell_list_deps (cell);
993
		dependent_queue_recalc_list (deps);
994
		g_slist_free (deps);
Jody Goldberg's avatar
Jody Goldberg committed
995 996 997 998 999 1000 1001
	}
}

typedef struct {
	int      col, row;
	DepFunc	 func;
	gpointer user;
1002
} search_rangedeps_closure_t;
Jody Goldberg's avatar
Jody Goldberg committed
1003 1004

static void
1005
cb_search_rangedeps (gpointer key, gpointer value, gpointer closure)
Jody Goldberg's avatar
Jody Goldberg committed
1006
{
1007
	search_rangedeps_closure_t const *c = closure;
Jody Goldberg's avatar
Jody Goldberg committed
1008 1009 1010
	DependencyRange const *deprange = key;
	Range const *range = &(deprange->range);

Jody Goldberg's avatar
Jody Goldberg committed
1011 1012 1013
#if 0
	/* When things get slow this is a good test to enable */
	static int counter = 0;
Jody Goldberg's avatar
Jody Goldberg committed
1014 1015
	if ((++counter % 100000) == 0)
	    printf ("%d\n", counter / 100000);
Jody Goldberg's avatar
Jody Goldberg committed
1016
#endif
Jody Goldberg's avatar
Jody Goldberg committed
1017 1018 1019

	/* No intersection is the common case */
	if (range_contains (range, c->col, c->row)) {
1020
		DepFunc	 func = c->func;
1021
		dep_collection_foreach_dep (deprange->deps, dep,
1022
			(func) (dep, c->user););
Jody Goldberg's avatar
Jody Goldberg committed
1023 1024 1025 1026 1027 1028
	}
}

static void
cell_foreach_range_dep (Cell const *cell, DepFunc func, gpointer user)
{
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	search_rangedeps_closure_t closure;
	GHashTable *bucket =
		cell->base.sheet->deps->range_hash [cell->pos.row /BUCKET_SIZE];

	if (bucket != NULL) {
		closure.col   = cell->pos.col;
		closure.row   = cell->pos.row;
		closure.func  = func;
		closure.user  = user;

		/* FIXME FIXME FIXME :
		 * This call decimates performance
		 * If this list contains lots of ranges we are toast.  Consider
		 * subdividing the master list.  A simple fixed bucket scheme is
		 * probably sufficient (say 64x64) but we could go to something
		 * adaptive or a simple quad tree.
		 */
		g_hash_table_foreach (bucket,
			&cb_search_rangedeps, &closure);
	}
Jody Goldberg's avatar
Jody Goldberg committed
1049 1050 1051 1052 1053 1054
}

static void
cell_foreach_single_dep (Sheet const *sheet, int col, int row,
			 DepFunc func, gpointer user)
{
1055 1056
	DependencySingle lookup, *single;
	DependencyContainer *deps = sheet->deps;
Jody Goldberg's avatar
Jody Goldberg committed
1057 1058 1059 1060 1061

	lookup.pos.col = col;
	lookup.pos.row = row;

	single = g_hash_table_lookup (deps->single_hash, &lookup);
1062 1063 1064
	if (single != NULL)
		dep_collection_foreach_dep (single->deps, dep,
			(*func) (dep, user););
Jody Goldberg's avatar
Jody Goldberg committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
}

void
cell_foreach_dep (Cell const *cell, DepFunc func, gpointer user)
{
	g_return_if_fail (cell != NULL);

	/* accelerate exit */
	if (!cell->base.sheet->deps)
		return;

	cell_foreach_range_dep (cell, func, user);
	cell_foreach_single_dep (cell->base.sheet, cell->pos.col, cell->pos.row,
				 func, user);
}

static void
1082 1083 1084
cb_recalc_all_depends (gpointer key, gpointer value, gpointer ignore)
{
	DependencyAny const *depany = key;
1085 1086
	dep_collection_foreach_list (depany->deps, list,
		dependent_queue_recalc_list (list););
1087 1088 1089 1090
}

static void
cb_range_contained_depend (gpointer key, gpointer value, gpointer user)
Jody Goldberg's avatar
Jody Goldberg committed
1091 1092 1093 1094 1095 1096
{
	DependencyRange const *deprange  = key;
	Range const *range = &deprange->range;
	Range const *target = user;

	if (range_overlap (target, range))
1097 1098
		dep_collection_foreach_list (deprange->deps, list,
			dependent_queue_recalc_list (list););
Jody Goldberg's avatar
Jody Goldberg committed
1099 1100 1101
}

static void
1102
cb_single_contained_depend (gpointer key, gpointer value, gpointer user)
Jody Goldberg's avatar
Jody Goldberg committed
1103
{
1104 1105
	DependencySingle const *depsingle  = key;
	Range const *target = user;
Jody Goldberg's avatar
Jody Goldberg committed
1106

1107
	if (range_contains (target, depsingle->pos.col, depsingle->pos.row))
1108 1109
		dep_collection_foreach_list (depsingle->deps, list,
			dependent_queue_recalc_list (list););
Jody Goldberg's avatar
Jody Goldberg committed
1110
}
1111

Jody Goldberg's avatar
Jody Goldberg committed
1112 1113 1114 1115 1116 1117
/**
 * sheet_region_queue_recalc :
 *
 * @sheet : The sheet.
 * @range : Optionally NULL range.
 *
1118 1119
 * Queues things that depend on @sheet!@range for recalc.
 *
Jody Goldberg's avatar
Jody Goldberg committed
1120 1121 1122
 * If @range is NULL the entire sheet is used.
 */
void
1123
sheet_region_queue_recalc (Sheet const *sheet, Range const *r)
Jody Goldberg's avatar
Jody Goldberg committed
1124
{
1125 1126
	int i;

Jody Goldberg's avatar
Jody Goldberg committed
1127 1128 1129
	g_return_if_fail (IS_SHEET (sheet));
	g_return_if_fail (sheet->deps != NULL);

1130
	if (r == NULL) {
1131
		/* mark the contained depends dirty non recursively */
1132 1133
		SHEET_FOREACH_DEPENDENT (sheet, dep, {
			dependent_queue_recalc (dep);
1134
		});
1135

1136 1137 1138 1139 1140 1141 1142
		/* look for things that depend on the sheet */
		for (i = (SHEET_MAX_ROWS-1)/BUCKET_SIZE; i >= 0 ; i--) {
			GHashTable *hash = sheet->deps->range_hash[i];
			if (hash != NULL)
				g_hash_table_foreach (hash,
					&cb_recalc_all_depends, NULL);
		}
Jody Goldberg's avatar
Jody Goldberg committed
1143
		g_hash_table_foreach (sheet->deps->single_hash,
1144
			&cb_recalc_all_depends, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
1145
	} else {
1146
		int const first = r->start.row / BUCKET_SIZE;
1147

1148
		/* mark the contained depends dirty non recursively */
1149 1150
		SHEET_FOREACH_DEPENDENT (sheet, dep, {
			Cell *cell = DEP_TO_CELL (dep);
1151
			if (dependent_is_cell (dep) &&
1152 1153 1154
			    range_contains (r, cell->pos.col, cell->pos.row))
				dependent_queue_recalc (dep);
		});
Jody Goldberg's avatar
Jody Goldberg committed
1155

1156 1157 1158 1159 1160 1161 1162 1163 1164
		/* look for things that depend on target region */
		for (i = (r->end.row)/BUCKET_SIZE; i >= first ; i--) {
			GHashTable *hash = sheet->deps->range_hash[i];
			if (hash != NULL)
			g_hash_table_foreach (hash,
				&cb_range_contained_depend, (gpointer)r);
		}
		g_hash_table_foreach (sheet->deps->single_hash,
			&cb_single_contained_depend, (gpointer)r);
Jody Goldberg's avatar
Jody Goldberg committed
1165
	}
1166 1167
}

Jody Goldberg's avatar
Jody Goldberg committed
1168 1169
/*******************************************************************/

1170
inline static void
1171
invalidate_refs (Dependent *dep, ExprRewriteInfo const *rwinfo)
Jody Goldberg's avatar
Jody Goldberg committed
1172
{
1173
	ExprTree *newtree = expr_rewrite (dep->expression, rwinfo);
Jody Goldberg's avatar
Jody Goldberg committed
1174

1175
	/* We are told this dependent depends on this region, hence if newtree
1176
	 * is null then either
1177 1178 1179
	 * 1) we did not depend on it ( ie. serious breakage )
	 * 2j we had a duplicate reference and we have already removed it.
	 * 3) We depended on things via a name which will be invalidated elsewhere
1180
	 */
1181 1182
	if (newtree != NULL)
		dependent_set_expr (dep, newtree);
1183 1184
}

1185 1186 1187 1188
/*
 * WARNING : Hash is pointing to freed memory once this is complete
 * This is tightly coupled with do_deps_destroy.
 */
1189
static void
1190
cb_dep_hash_invalidate (gpointer key, gpointer value, gpointer <