File: optimize.c

package info (click to toggle)
sgrep 1.94a-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 880 kB
  • ctags: 1,074
  • sloc: ansic: 10,566; sh: 3,433; makefile: 134
file content (513 lines) | stat: -rw-r--r-- 11,762 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
	System: Structured text retrieval tool sgrep.
	Module: optimize.c 
	Author: Pekka Kilpel�inen & Jani Jaakkola
	Description: Optimizes the operator tree by removing identical
		     subtrees. ( optimize_tree() )
	Version history: Original version February 1995 by JJ & PK
	Copyright: University of Helsinki, Dept. of Computer Science
*/

#include <string.h>

#include "sgrep.h"

/*
 * If you wan't to see how operator tree is shrinked, define this
 */
/* #define DEBUG_OPTTREE */

typedef struct {
    SgrepData *sgrep;
    int label_c;
    ParseTreeNode **root;
    ParseTreeLeaf **phrase_list;
    int tree_size;
    int optimized_nodes;
    int optimized_phrases;
} Optimizer;


/*
 * sorts phrase list, so that same phrases can easily be detected
 */
struct PHRASE_NODE *qsort_phrases(struct PHRASE_NODE **phrase_list)
{
	struct PHRASE_NODE *list1,*list2,*comp,*next,*p_list;
	p_list=*phrase_list;
#ifdef DEBUG
	fprintf(stderr,"\nqsort called ");
#endif
	if (p_list==NULL) 
	{
		return NULL; /* Empty list. Return from recursion */
	}
	comp=p_list;
#ifdef DEBUG
	fprintf(stderr,"comp=%s\n",comp->phrase->s);
#endif
	p_list=p_list->next;
	if (p_list==NULL)
	{
		/* Only one phrase in list. Return from recursion */
		return *phrase_list;
	}
	
	list1=NULL;
	list2=comp;
	comp->next=NULL;
	while(p_list!=NULL)
	{
		next=p_list->next;
		if ( strcmp((char *)comp->phrase->s,
		     (char *)p_list->phrase->s)<0 )
		{
#ifdef DEBUG			
				fprintf(stderr,"list2+%s\n",p_list->phrase->s);
#endif
			p_list->next=list2;
			list2=p_list;
		} else
		{
#ifdef DEBUG
			fprintf(stderr,"list1+%s\n",p_list->phrase->s);
#endif
			p_list->next=list1;
			list1=p_list;
		}
		p_list=next;
	}
	/* order should be now list1 .. comp .. list2 */
	comp=qsort_phrases(&list2);
#ifdef DEBUG
	printf("vika on %s\n",comp->phrase->s);
#endif
	if (list1==NULL) 
	{
		*phrase_list=list2;
		return comp;
	}
	qsort_phrases(&list1)->next=list2;
	*phrase_list=list1;
	return comp;
}

/*
 * Merges duplicate phrases in phrase list
 */
void remove_duplicate_phrases(Optimizer *o)
{
	struct PHRASE_NODE *pn;
	struct PHRASE_NODE *lpn=NULL;
	struct PHRASE_NODE *tmp;
	SgrepData *sgrep=o->sgrep;
	char *last;
	
	/* we need to sort phrase list first */
	qsort_phrases(o->phrase_list);
	pn=*o->phrase_list;
	
	last=""; /* It's not possible to have empty phrase in the list,
		    so this can never be matched */
	
	while (pn!=NULL)
	{
		if (strcmp(last,(char *)pn->phrase->s)==0)
		{
#ifdef DEBUG
			fprintf(stderr," skipping duplicate\n");
#endif
			/* Phrase was already in the list */
			
			/* We give parent same label the first alike phrase had */
			pn->parent->label_left=o->label_c;
			/* Removing pn from phrase list */
			lpn->next=pn->next;
			pn->parent->leaf=lpn;
			/* Freeing memory allocated to pn */
			tmp=pn;
			pn=pn->next;
			assert(pn==NULL || (
			    pn->parent!=NULL && 
			    pn->parent->label_left==LABEL_PHRASE
			    ));
			delete_string(tmp->phrase);
			tmp->phrase=NULL;
			sgrep_free(tmp);
			
			/* Statistics... */
			o->optimized_phrases++;
		} 
		else
		{
			last=(char *)pn->phrase->s;
			o->label_c++;
			pn->parent->label_left=o->label_c;
			lpn=pn;
			pn=pn->next;

			assert(pn==NULL || pn->parent!=NULL);
			assert(pn==NULL || pn->parent->label_left==LABEL_PHRASE);

#ifdef DEBUG
			fprintf(stderr,"Checking duplicates for \"%s\" having label %d\n"
				,last,label_c);
#endif
		}
	}		
}

/* 
 * Recursively adds pointers to parents to every tree and phrase node 
 * counts also operator tree size
 */
int add_parents(ParseTreeNode *node,ParseTreeNode *parent)
{
    int nodes=1; /* This node */
    node->parent=parent;

    assert(node->label_right==LABEL_NOTKNOWN);
	
    node->refcount=0;
    if (node->oper==PHRASE)
    {
	node->leaf->parent=node;
    } else
    {
	assert(node->left!=NULL);
	nodes+=add_parents(node->left,node);
	if (node->right!=NULL)
	{
	    nodes+=add_parents(node->right,node);
	}
    }
    return nodes;
}

/*
 * Recursively creates a list of leaf nodes from parse tree
 */
int create_leaf_list(ParseTreeNode *root, ParseTreeNode **list, int ind)
{
	if (root->oper==PHRASE)
	{
		list[ind]=root;
		return ind+1;
	}
	ind=create_leaf_list(root->left,list,ind);
	if (root->right!=NULL)
	{
		ind=create_leaf_list(root->right,list,ind);
	}
	return ind;
}
	
#ifdef DEBUG
void dump_phrase_list(struct PHRASE_NODE *pn)
{
	while (pn!=NULL)
	{
		fprintf(stderr,"string %s parent label %d having %d references\n",
			pn->phrase->s,pn->parent->label,pn->parent->refcount);
		pn=pn->next;
	}
}
#endif

/*
 * Compares two tree nodes. returns 0 if they are alike
 * alike means: same oper, and same subtrees
 */
int comp_tree_nodes(ParseTreeNode **n1, ParseTreeNode **n2)
{
    int x;
    if ( ((*n1)->oper==JOIN || (*n1)->oper==FIRST || (*n1)->oper==LAST) &&
	 (*n2)->oper==(*n1)->oper ) {
	/* Join operation takes int parameter, which much be checked */
	x=(*n1)->number - (*n2)->number;
    } else {
	x=(*n1)->oper - (*n2)->oper;
    }
    if (x!=0) return x;

    /* if label_left==LABEL_CONS right subtree must be NULL ! */
    assert( (*n1)->label_left!=LABEL_CONS || (*n1)->right==NULL );
    assert( (*n2)->label_left!=LABEL_CONS || (*n2)->right==NULL );

    if ( (*n1)->label_left==LABEL_CONS && (*n2)->label_left==LABEL_CONS ) 
	return (*n1)!=(*n2); /* FIXME: this might be wrong */
    x=(*n1)->label_left - (*n2)->label_left;
    if (x!=0) return x;
    x=(*n1)->label_right - (*n2)->label_right;
    return x;
}

/*
 * sorts a leaf list using stdlib qsort and comp_tree_nodes 
 */
void sort_leaf_list(ParseTreeNode **leaf_list,int nmemb)
{
#ifdef DEBUG
	fprintf(stderr,"Sorting leaf list of size %d\n",nmemb);
#endif
	qsort(leaf_list,nmemb,sizeof(ParseTreeNode **),
		(int (*)(const void*,const void*))comp_tree_nodes);
}

/*
 * Removes duplicate subtrees from operator tree
 */
void shrink_tree(Optimizer *o)
{
	int leaf_list_size;
	int i;
	ParseTreeNode *dad;
	ParseTreeNode *me;
	ParseTreeNode *big_brother;
	int imleft;
	ParseTreeNode **list0;
	int list0_size;
	ParseTreeNode **list1;
	int list1_size;
	ParseTreeNode **tmp;
	ParseTreeNode *root=*o->root;
	SgrepData *sgrep=o->sgrep;

	leaf_list_size=o->tree_size*sizeof(ParseTreeNode *);
	list0=(ParseTreeNode **)sgrep_malloc(leaf_list_size);
	list1=(ParseTreeNode **)sgrep_malloc(leaf_list_size);
	list0_size=create_leaf_list(root,list0,0);
	list1_size=0;
	
	while (list0_size>1) {
		/* or and equal operators parameters can be swapped */
		for (i=0;i<list0_size;i++)
		{
		        if ((list0[i]->oper==OR ||
			     list0[i]->oper==EQUAL ||
			     list0[i]->oper==NEAR)
			    && list0[i]->label_left<list0[i]->label_right)
			{
				int tmp;
				ParseTreeNode *tree_tmp;
#ifdef DEBUG_OPTTREE
				fprintf(stderr,"swapping subtrees\n");
#endif
				tmp=list0[i]->label_left;
				list0[i]->label_left=list0[i]->label_right;
				list0[i]->label_right=tmp;
				tree_tmp=list0[i]->left;
				list0[i]->left=list0[i]->right;
				list0[i]->right=tree_tmp;
			}
		}
		
		sort_leaf_list(list0,list0_size);
#ifdef DEBUG_OPTTREE
		fprintf(stderr,"shrinking tree node list of size %d:\n",list0_size);
#endif
		big_brother=NULL;
		for (i=0;i<list0_size;i++)
		{
			me=list0[i];
			dad=me->parent;
			imleft= (dad->left==me);

			if (big_brother==NULL || comp_tree_nodes(&big_brother,&me)!=0 )
			{
				o->label_c++;
				big_brother=me;
			} else
			{	
			    o->optimized_nodes++;
				/* These don't really need to be changed,
				   It just might help catch some bugs */
				me->left=NULL;
				me->right=NULL;
				me->oper=INVALID;
				sgrep_free(me);
			}
			
			if (imleft)
			{
				dad->label_left=o->label_c;
				dad->left=big_brother;
			} else
			{
				dad->label_right=o->label_c;
				dad->right=big_brother;
			}
			assert(dad->left!=NULL);

			if (dad->label_left!=LABEL_NOTKNOWN &&
			     (dad->label_right!=LABEL_NOTKNOWN ||
			      dad->right==NULL) )
			{
				if (dad->right==NULL) dad->label_right=LABEL_NOTKNOWN;
				list1[list1_size++]=dad;
			}				
#ifdef DEBUG_OPTTREE
			fprintf(stderr," label=%-3d oper=%-15s left_label=%-3d right_label=%-3d\n",
				label_c,
				give_oper_name(big_brother->oper),
				big_brother->label_left,
				big_brother->label_right);
#endif
		}
		tmp=list0;
		list0=list1;
		list1=tmp;
		list0_size=list1_size;
		list1_size=0;
	}
	sgrep_free(list0);
	sgrep_free(list1);
}

/*
 * Creates the reference counters
 */
void create_reference_counters(ParseTreeNode *root) {
    if (root==NULL) return;	

    if (root->label_left==LABEL_CONS || root->label_left==LABEL_CHARS) {
	/* Lists with these labels should never be freed, because
	 * they are still valid when reusing parse tree
	 */
	root->refcount=-1;
    } else {
	if (root->refcount==0) {
	    /* This node is visited first time. So we need to go down too */
	    create_reference_counters(root->left);
	    create_reference_counters(root->right);
	}
	root->refcount++;
    }    
}

#ifdef DEBUG_OPTTREE
/*
 * Prints the optimized tree to stderr
 */
void print_opt_tree(ParseTreeNode *root, int depth, int label)
{
	int i;
	char line[80];
	static char *visited=NULL;
	
	if (visited==NULL)
	{
		visited=e_malloc(stats.tree_size*2); /* Should be enough */
		for (i=0;i<stats.tree_size*2;i++) visited[i]=FALSE;
	}
	if (depth>50)
	{
		fprintf(stderr,"oops, oper tree depth > 50\n");
		exit(3);
	}
	if (root->refcount>1 && !visited[label])
	{
		sprintf(line," %2d-%3d:",root->refcount,label);
	} else
	{
		if (label==0)
			line[0]=0;
		else
			sprintf(line,"         ");
	}
	for(i=0;i<depth;i++) strcat(line," ");
	if (label!=LABEL_NOTKNOWN)
	{
		if (visited[label])
		{
			fprintf(stderr,"%s^%d\n",line,label);
			return;
		}
		visited[label]=TRUE;
	}
	i=strlen(line);
	if ( root==NULL )
	{
		fprintf(stderr,"\nprint_opt_tree: got NULL node\n");
		exit(3);
	}
	if ( root->oper==PHRASE )
	{
		switch (root->label_left) {
		case LABEL_START:
			sprintf(line+i,"start");
			break;
		case LABEL_END:
			sprintf(line+i,"end");
			break;
		case LABEL_CONS:
			sprintf(line+i,"constant list");
			break;
		case LABEL_CHARS:
			sprintf(line+i,"chars");
			break;
		case LABEL_NOTKNOWN:
			sprintf(line+i,"unknown phrase type");
			break;
		default:
			sprintf(line+i,"\"%s\"",root->leaf->phrase->s);
			break;
		}
		fprintf(stderr,"%s\n",line);
		return;
	}
	if (root->oper<0 || root->oper>R_WORDS)
	{
		printf("\nprint tree: got invalid oper (%d)\n",root->oper);
		exit(3);
	}
	if (root->right!=NULL)
	{
		print_opt_tree(root->left,depth+1,root->label_left);
		sprintf(line+i,"%s",give_oper_name(root->oper));
		fprintf(stderr,"%s\n",line);
		print_opt_tree(root->right,depth+1,root->label_right);
	} else
	{
		sprintf(line+i,"%s(",give_oper_name(root->oper));
		fprintf(stderr,"%s\n",line);
		print_opt_tree(root->left,depth+1,root->label_left);
	}
}
#endif

/*
 * Performs operator tree optimizations
 */
void optimize_tree(struct SgrepStruct *sgrep,
		   ParseTreeNode **root, struct PHRASE_NODE **phrase_list)
{
    Optimizer optimizer;
    optimizer.sgrep=sgrep;
    optimizer.label_c=LABEL_FIRST;
    optimizer.root=root;
    optimizer.phrase_list=phrase_list;
    optimizer.tree_size=0;
    optimizer.optimized_nodes=0;
    optimizer.optimized_phrases=0;

    /* We need nodes parent information for optimization */
    optimizer.tree_size=add_parents(*root,NULL);
	
#ifdef DEBUG
    fprintf(stderr,"parse tree size is %d\n",stats.tree_size);
#endif
    /* Duplicate phrases are removed and their parents labeled */
    remove_duplicate_phrases(&optimizer);
	
    /* Duplicate subtrees are removed */
    shrink_tree(&optimizer);
	
    create_reference_counters(*root);
#ifdef DEBUG_OPTTREE
    print_opt_tree(*root,0,0);
#endif
    stats.parse_tree_size+=optimizer.tree_size;
    stats.optimized_phrases+=optimizer.optimized_phrases;
    stats.optimized_nodes+=optimizer.optimized_nodes;
}