-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
executable file
·1856 lines (1688 loc) · 42.6 KB
/
eval.c
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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
System: Structured text retrieval tool sgrep.
Module: eval.c
Author: Pekka Kilpeläinen & Jani Jaakkola
Description: Handles the evaluation of sgrep expressions, thus
implementing the actual semantics of sgrep language.
used through eval() function
Version history: Original version February 1995 by JJ & PK
Copyright: University of Helsinki, Dept. of Compter Science
*/
#include <string.h>
#define SGREP_LIBRARY
#include "sgrep.h"
#ifdef OPTIMIZE_SORTS
/* Define this if you want always test that nest optimization works */
#define ASSERT_NESTS
#endif
/* Check for proper containment */
#define contains(r1,r2) \
(((r1).start<(r2).start && (r1).end>=(r2).end) || \
((r1).start<=(r2).start && (r1).end>(r2).end))
/*
* Sometimes regions need to be stacked or queued
*/
#define DEFAULT_STACK_SIZE 1024
typedef struct {
struct SgrepStruct *sgrep;
const FileList *files;
Region *tmp_stack;
int tmp_stack_size;
} Evaluator;
RegionList *recursive_eval(Evaluator *,ParseTreeNode *root);
RegionList *eval_operator(Evaluator *,ParseTreeNode *root);
RegionList *or(RegionList *,RegionList *);
RegionList *nest_order(Evaluator *, RegionList *,RegionList *,int);
RegionList *quote(RegionList *,RegionList *,int);
RegionList *in(RegionList *,RegionList *,int);
RegionList *containing(Evaluator *,RegionList *,RegionList *,int);
RegionList *extracting(RegionList *,RegionList *);
RegionList *outer(RegionList *);
RegionList *inner(Evaluator*, RegionList *);
RegionList *concat(RegionList *);
RegionList *join(Evaluator *evaluator,RegionList *,int number);
RegionList *first(RegionList *, int number);
RegionList *last(RegionList *, int number);
RegionList *first_bytes(RegionList *,int number);
RegionList *last_bytes(RegionList *,int number);
RegionList *equal(RegionList *,RegionList *,int);
RegionList *parenting(Evaluator *,RegionList *l, RegionList *r);
RegionList *childrening(RegionList *l, RegionList *r);
RegionList *eval_near(RegionList *l, RegionList *r,int num);
RegionList *near_before(RegionList *l, RegionList *r,int num);
int free_tree_node(ParseTreeNode *node);
RegionList *eval(struct SgrepStruct *sgrep,
const FileList *file_list,
ParseTreeNode *root) {
RegionList *r;
Evaluator evaluator;
evaluator.sgrep=sgrep;
evaluator.files=file_list;
evaluator.tmp_stack_size=DEFAULT_STACK_SIZE;
evaluator.tmp_stack=(Region *)sgrep_malloc(DEFAULT_STACK_SIZE*
sizeof(Region));
r=recursive_eval(&evaluator,root);
if (evaluator.tmp_stack) sgrep_free(evaluator.tmp_stack);
return r;
}
/*
* Recursively evaluates parse tree using operation functions
* root points the root node of parse tree
*/
RegionList *recursive_eval(Evaluator *evaluator,ParseTreeNode *root)
{
#ifdef DEBUG
static int depth=0;
int i;
#endif
RegionList *a;
SGREPDATA(evaluator);
a=root->result;
#ifdef DEBUG
depth++;
for(i=0;i<depth;i++) fputc(' ',stderr);
fprintf(stderr,"Evaluating oper %s l_label=%d r_label=%d\n",
give_oper_name(root->oper),root->label_left,root->label_right);
#endif
assert(root->oper!=INVALID);
/* If this is a leaf node, we just use leafs gc list */
if ( a==NULL && root->oper==PHRASE )
{
/* Check for lazy evaluation mode */
if (sgrep->index_file && root->leaf->regions==NULL) {
assert(root->leaf->phrase!=NULL);
assert(sgrep->index_reader);
if (root->leaf->phrase->s[0]=='#') {
/* Builtin, can't be found from index */
const char *s=string_to_char(root->leaf->phrase);
RegionList *list=new_region_list(sgrep);
root->leaf->regions=list;
if (strcmp(s,"#start")==0) {
int start=flist_start(evaluator->files,0);
add_region(list,start,start);
} else if (strcmp(s,"#end")==0) {
int end=flist_total(evaluator->files)-1;
add_region(list,end,end);
} else {
sgrep_error(sgrep,
"Don't know how to handle phrase %s\n",
s);
}
} else {
root->leaf->regions=index_lookup(
sgrep->index_reader,
root->leaf->phrase->s);
}
}
assert(root->leaf->regions!=NULL);
a=root->leaf->regions;
root->leaf->regions=NULL;
a->refcount=root->refcount;
#ifdef DEBUG
for(i=0;i<depth;i++) fputc(' ',stderr);
fprintf(stderr,"Using phrase list %s\n",root->leaf->phrase->s);
#endif
}
/* If gc_list is still NULL, it means that it hasn't been
* evaluated yet */
if ( a==NULL )
{
/* Eval it now */
a=eval_operator(evaluator,root);
a->refcount=root->refcount;
/* We free subtrees unneeded gclists */
if (free_tree_node(root->left))
{
#ifdef DEBUG
for(i=0;i<depth;i++) putc(' ',stderr);
fprintf(stderr,"label %d freed (left)\n",root->label_left);
#endif
}
if (free_tree_node(root->right))
{
#ifdef DEBUG
for(i=0;i<depth;i++) putc(' ',stderr);
fprintf(stderr,"label %d freed (right)\n",root->label_right);
#endif
}
}
#ifdef DEBUG
else
{
for(i=0;i<depth;i++) fputc(' ',stderr);
fprintf(stderr,"Using already known list\n");
}
#endif
/* Keeps track of longest used gc list */
if (LIST_SIZE(a)>stats.longest_list)
stats.longest_list=LIST_SIZE(a);
#ifdef ASSERT_NESTS
/* We check that if list isn't marked as nested, it really isn't */
if (!a->nested)
{
Region reg1,reg2;
ListIterator p;
start_region_search(a,&p);
get_region(&p,®1);
get_region(&p,®2);
while (reg2.start!=-1)
{
assert(reg1.end<reg2.end);
reg1=reg2;
get_region(&p,®2);
}
}
#endif
root->result=a;
#ifdef DEBUG
for(i=0;i<depth;i++) fputc(' ',stderr);
fprintf(stderr,"eval done\n");
depth--;
#endif
return a;
}
/*
* Handles the actual evaluation of some operation
*/
RegionList *eval_operator(Evaluator *evaluator,ParseTreeNode *root)
{
RegionList *a,*l,*r;
a=NULL;
assert(root->left!=NULL);
/* Evaluate left and right subtrees first */
l=recursive_eval(evaluator,root->left);
/* Functions don't have right subtree. */
if (root->right==NULL) r=NULL;
else r=recursive_eval(evaluator,root->right);
/* Statistics */
evaluator->sgrep->statistics.operators_evaluated++;
/* Find the correct evaluation function */
switch (root->oper) {
case OR:
a=or(l,r);
break;
case ORDERED:
case L_ORDERED:
case R_ORDERED:
case LR_ORDERED:
a=nest_order(evaluator,l,r,root->oper);
break;
case QUOTE:
case L_QUOTE:
case R_QUOTE:
case LR_QUOTE:
a=quote(l,r,root->oper);
break;
case IN:
a=in(l,r,0);
break;
case NOT_IN:
a=in(l,r,1);
break;
case CONTAINING:
a=containing(evaluator,l,r,0);
break;
case NOT_CONTAINING:
a=containing(evaluator,l,r,1);
break;
/* Start PK Febr 95 */
case EQUAL:
a=equal(l,r,0);
break;
case NOT_EQUAL:
a=equal(l,r,1);
break;
/* End PK Febr 95 */
case PARENTING:
a=parenting(evaluator,l,r);
break;
case CHILDRENING:
a=childrening(l,r);
break;
case OUTER:
a=outer(l);
break;
case INNER:
a=inner(evaluator,l);
break;
case EXTRACTING:
a=extracting(l,r);
break;
case CONCAT:
a=concat(l);
break;
case JOIN:
a=join(evaluator,l,root->number);
break;
case FIRST:
a=first(l,root->number);
break;
case LAST:
a=last(l,root->number);
break;
case FIRST_BYTES:
a=first_bytes(l,root->number);
break;
case LAST_BYTES:
a=last_bytes(l,root->number);
break;
case NEAR:
a=eval_near(l,r,root->number);
break;
case NEAR_BEFORE:
a=near_before(l,r,root->number);
break;
default:
sgrep_error(evaluator->sgrep,
"Unknown operator in parse tree (%d)\n",
root->oper);
assert(0 && "Unknown operator in parse tree");
break;
}
return a;
}
/*
* Decrements tree nodes reference counter, and frees nodes gc list if
* counter comes down to 0. Returns 1 if something was freed, 0
* otherwise
*/
int free_tree_node(ParseTreeNode *node)
{
if (node==NULL) return 0; /* This was a leaf or function node */
if (node->result!=NULL && node->result->refcount!=-1) {
node->result->refcount--;
assert(node->result->refcount>=0);
if (node->result->refcount==0) {
free_gclist(node->result);
node->result=NULL;
return 1;
}
}
return 0;
}
#ifdef PROGRESS_REPORTS
/*
* Shows a progress report on stderr
*/
void report_progress(char *line,int size, int now)
{
fprintf(stderr,"%s %d%% done%s\r",
line,(100*now)/size," ");
fflush(stderr);
}
#endif
/*
* Gives first region from two gc_lists, eliminating same regions.
*/
Region first_of(ListIterator *lp,ListIterator *rp)
{
Region l_reg,r_reg;
/* quite straightforward limiting of two gc lists.
same regions are concatanated */
get_region(lp,&l_reg);
get_region(rp,&r_reg);
if (r_reg.start!=-1 && l_reg.start!=-1)
{
if (l_reg.start<r_reg.start)
{
prev_region(rp,&r_reg);
return l_reg;
} else if (l_reg.start>r_reg.start)
{
prev_region(lp,&l_reg);
return r_reg;
} else if (l_reg.end<r_reg.end)
{
prev_region(rp,&r_reg);
return l_reg;
} else if (l_reg.end>r_reg.end)
{
prev_region(lp,&l_reg);
return r_reg;
} else
{
return r_reg;
}
}
if (r_reg.start!=-1) return r_reg;
if (l_reg.start!=-1) return l_reg;
/* Both lists were empty, we return (-1,-1) */
return r_reg;
}
/*
* Handles or operation
*/
RegionList *or(RegionList *l,RegionList *r)
{
ListIterator lp,rp;
RegionList *a;
Region tmp;
#ifdef OPTIMIZE_SORTS
Region prev;
#endif
#ifdef PROGRESS_REPORTS
char *oper_name;
int prog;int prog_start;
#endif
SGREPDATA(l);
#ifdef DEBUG
fprintf(stderr,"or called\n");
#endif
stats.or_oper++;
a=new_region_list(sgrep);
#ifdef OPTIMIZE_SORTS
prev.start=-1;
prev.end=-1;
#endif
start_region_search(l,&lp);
start_region_search(r,&rp);
#ifdef PROGRESS_REPORTS
prog_start=LIST_SIZE(r)+LIST_SIZE(l);
prog=0;
oper_name="or";
#endif
for(tmp=first_of(&lp,&rp);tmp.start!=-1;tmp=first_of(&lp,&rp))
{
#ifdef OPTIMIZE_SORTS
if ( tmp.end<=prev.end )
{
/* We had nesting */
a->nested=1;
}
#endif
add_region(a,tmp.start,tmp.end);
#ifdef OPTIMIZE_SORTS
prev=tmp;
#endif
}
return a;
}
/*
* Handles ordering which produces possibly nesting gc-lists.
*/
RegionList *nest_order(Evaluator *evaluator,
RegionList *l,RegionList *r,int type)
{
ListIterator lp,rp;
RegionList *a;
Region r_reg,l_reg;
int nest_depth=0;
int nestings;
int s,e;
SGREPDATA(evaluator);
#ifdef PROGRESS_REPORTS
char *oper_name;
int prog;int prog_start;
#endif
#ifdef DEBUG
fprintf(stderr,"nest_order called\n");
#endif
start_region_search(r,&rp);
stats.order++;
a=new_region_list(sgrep);
a->nested=l->nested || r->nested;
#ifdef DEBUG
if (a->nested) fprintf(stderr,"inherited nesting\n");
#endif
#ifdef PROGRESS_REPORTS
prog_start=LIST_SIZE(r);
prog=0;
switch (type) {
case L_ORDERED:
oper_name="_.";
break;
case R_ORDERED:
oper_name="._";
break;
case LR_ORDERED:
oper_name="__";
break;
default:
oper_name="..";
}
#endif
nestings=0;
start_end_sorted_search(l,&lp);
get_region(&lp,&l_reg);
get_region(&rp,&r_reg);
/* If left or right region list was empty, we can return empty list */
if (l_reg.start==-1 || r_reg.start==-1) return a;
do
{
if (l_reg.end<r_reg.start && l_reg.start!=-1 )
{
/* left region is first. Add to nest_stack
and nest queue */
if (nest_depth==evaluator->tmp_stack_size)
{
evaluator->tmp_stack_size+=evaluator->tmp_stack_size/2;
evaluator->tmp_stack=(Region *)sgrep_realloc(
evaluator->tmp_stack,
evaluator->tmp_stack_size*sizeof(Region));
}
evaluator->tmp_stack[nest_depth++]=l_reg;
nestings=0;
#ifdef DEBUG
if (nest_depth==1)
fprintf(stderr," New q");
else fprintf(stderr," +");
fprintf(stderr,"(%d:%d)",l_reg.start,l_reg.end);
#endif
get_region(&lp,&l_reg);
}
else if (nest_depth>0)
{
#ifdef DEBUG
fprintf(stderr," %d",r_reg.end);
#endif
if (type==L_ORDERED || type==LR_ORDERED)
s=evaluator->tmp_stack[--nest_depth].end+1;
else s=evaluator->tmp_stack[--nest_depth].start;
if (type==R_ORDERED || type==LR_ORDERED)
e=r_reg.start-1;
else e=r_reg.end;
if (e>=s)
{
/* If we have taken region from nest stack
* twice in row, it probably means, that
* we have a nested result list */
nestings++;
if (nestings==2)
{
#ifdef DEBUG
if (!a->nested)
fprintf(stderr,"nesting order detecded\n");
#endif
a->nested=1;
list_set_sorted(a,NOT_SORTED);
}
add_region(a,s,e);
}
get_region(&rp,&r_reg);
} else
{
get_region(&rp,&r_reg);
}
} while ( r_reg.start!=-1 );
return a;
}
/*
* Handles in operation
*/
RegionList *in(RegionList *l,RegionList *r, int not)
/* Changed by PK in Febr 95 to capture the semantics of _proper_
containment */
{
ListIterator lp,rp;
RegionList *a,*r2;
Region r_reg,l_reg,r_reg2;
char *oper_name;
#ifdef PROGRESS_REPORTS
int prog;int prog_start;
#endif
SGREPDATA(l);
#ifdef DEBUG
fprintf(stderr,"in called\n");
#endif
if (not)
{
stats.not_in++;
oper_name="not in";
} else
{
stats.in++;
oper_name="in";
}
a=new_region_list(sgrep);
#ifdef OPTIMIZE_SORTS
a->nested=l->nested;
#endif
start_region_search(l,&lp);
get_region(&lp,&l_reg);
/*
* To simplify things we do an outer function on right gc_list
*/
#ifdef OPTIMIZE_SORTS
if (r->nested)
{
#endif
r2=outer(r);
r=r2;
#ifdef OPTIMIZE_SORTS
} else r2=NULL;
#endif
start_region_search(r,&rp);
#ifdef PROGRESS_REPORTS
prog_start=LIST_SIZE(l)+LIST_SIZE(r);
prog=0;
#endif
get_region(&rp,&r_reg);
while (r_reg.start!=-1 && l_reg.start!=-1)
{
#ifdef DEBUG
fprintf(stderr,"in: left=(%d,%d) right=(%d,%d)\n",
l_reg.start,l_reg.end,
r_reg.start,r_reg.end);
#endif
if (l_reg.start<r_reg.start)
{
/* Left region starts before right -> can't be
in right region or any right region that follows
current one */
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
} else /* l_reg.start>=r_reg.start */
{
if (l_reg.end<=r_reg.end)
{
/* left region is in right region */
/* Start PK Febr 95 */
if (l_reg.start>r_reg.start ||
l_reg.end<r_reg.end)
{ /* inclusion is proper */
if (!not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
} else { /* l_reg == r_reg */
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
}
/* End PK Febr 95 */
} else if (l_reg.start==r_reg.start)
{
/* Regions start from same place. Because
no right region after current one can start
from same place we can skip left region */
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
} else
{
/* left and right region are overlapping */
#ifdef DEBUG
fprintf(stderr,"in overlap\n");
#endif
get_region(&rp,&r_reg2);
if (r_reg2.start==-1)
{
/* All right regions have been scanned */
if ( l_reg.start > r_reg.end )
{
/* Left region end after last right region.
We can fall out of loop */
r_reg=r_reg2;
} else
{
/* Next left region might still be in right
region */
if (not) add_region(a,l_reg.start, l_reg.end);
get_region(&lp,&l_reg);
}
} else
{
/* There are still right regions */
if ( l_reg.start >= r_reg2.start )
{
/* Since left region starts after new right region,
* We can safely skip previous right */
r_reg=r_reg2;
} else
{
/* Left region is not in previous or next
right region */
prev_region(&rp,&r_reg2);
if (not) add_region(a,l_reg.start, l_reg.end);
get_region(&lp,&l_reg);
}
}
}
}
}
#ifdef DEBUG
fprintf(stderr,"in fall out\n");
#endif
/* If we have "not in" and right gc_list is empty, we need to copy
rest of left list */
if (not)
{
while (l_reg.start!=-1)
{
add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
}
}
/* because we created list r2 here, we free it here */
if (r2!=NULL) free_gclist(r2);
return a;
}
/*
* Handles outer function
*/
RegionList *outer(RegionList *gcl)
{
ListIterator p;
Region reg1,reg2;
RegionList *a;
#ifdef PROGRESS_REPORTS
char *oper_name;
int prog;int prog_start;
#endif
SGREPDATA(gcl);
#ifdef PROGRESS_REPORTS
oper_name="outer";
prog_start=LIST_SIZE(gcl);
prog=0;
#endif
stats.outer++;
reg2.start=0;
a=new_region_list(sgrep);
start_region_search(gcl,&p);
get_region(&p,®1);
/* if we had empty gc list */
if (reg1.start==-1) return a;
/* If there are many regions starting from same place, we choose the
longest */
get_region(&p,®2);
while (reg2.start==reg1.start && reg2.end>reg1.end)
{
reg1=reg2;
get_region(&p,®2);
}
while(reg1.start!=-1 && reg2.start!=-1)
{
if (reg2.end>reg1.end && reg2.start!=reg1.start)
{
/* reg2 ends after reg1 -> no nesting */
add_region(a,reg1.start,reg1.end);
reg1=reg2;
}
get_region(&p,®2);
/* If regions start from same place, nesting is guaranteed */
if (reg2.start==reg1.start)
{
reg1=reg2;
get_region(&p,®2);
}
}
add_region(a,reg1.start,reg1.end);
return a;
}
/*
* Handles inner function
*/
RegionList *inner(Evaluator *evaluator,RegionList *gcl)
{
ListIterator p;
int inq_ind=0;
RegionList *a=NULL;
Region n_reg,c_reg;
int i;
Region *inner_stack;
SGREPDATA(evaluator);
#ifdef PROGRESS_REPORTS
char *oper_name;
int prog;int prog_start;
#endif
#ifdef DEBUG
fprintf(stderr,"inner called\n");
#endif
stats.inner++;
a=new_region_list(sgrep);
inner_stack=evaluator->tmp_stack;
#ifdef PROGRESS_REPORTS
prog_start=LIST_SIZE(gcl);
prog=0;
oper_name="inner";
#endif
start_region_search(gcl,&p);
get_region(&p,&c_reg);
while (c_reg.start!=-1) {
get_region(&p,&n_reg);
assert(n_reg.start>=c_reg.start || n_reg.start==-1 );
if ( n_reg.start>c_reg.end || n_reg.start==-1 )
{
/* n_reg and c_reg are separate. Therefore c_reg must
be innermost */
/* Now we can empty inner_stack */
#ifdef DEBUG
fprintf(stderr,"empty inner stack (%d regions)\n",inq_ind);
#endif
for (i=0;i<inq_ind;i++)
{
assert(inner_stack[i].start<=c_reg.start);
if (inner_stack[i].end<c_reg.end)
/* Region in inner_stack was innermost */
add_region(a,inner_stack[i].start,
inner_stack[i].end);
}
inq_ind=0;
add_region(a,c_reg.start,c_reg.end);
} else if ( n_reg.end>c_reg.end )
{
/* n_reg and c_reg are overlapping. Let's add c_reg
to inner_stack */
if (evaluator->tmp_stack_size==inq_ind)
{
evaluator->tmp_stack_size+=evaluator->tmp_stack_size/2;
inner_stack=(Region *)
sgrep_realloc(inner_stack,
evaluator->tmp_stack_size*
sizeof(Region));
evaluator->tmp_stack=inner_stack;
}
inner_stack[inq_ind++]=c_reg;
} else {
/* if neither of the previous if's was taken,
c_reg contains n_reg. We remove regions containing n_reg from
inner_stack */
while(inq_ind &&
n_reg.start>=inner_stack[inq_ind-1].start &&
n_reg.end<=inner_stack[inq_ind-1].end )
{
inq_ind--;
}
}
c_reg=n_reg;
if (inq_ind)
assert(c_reg.start<inner_stack[inq_ind-1].start ||
c_reg.end>inner_stack[inq_ind-1].end);
}
return a;
}
RegionList *containing(Evaluator *evaluator,RegionList *l,
RegionList *r,int not)
/* Changed by PK in Febr 95 to capture the semantics of _proper_
containment */
{
ListIterator lp,rp;
RegionList *a,*r2;
Region r_reg,l_reg;
SGREPDATA(evaluator);
#ifdef PROGRESS_REPORTS
char *oper_name;
int prog;int prog_start;
#endif
#ifdef DEBUG
fprintf(stderr,"containing called\n");
#endif
if (not) stats.not_containing++; else stats.containing++;
a=new_region_list(sgrep);
#ifdef OPTIMIZE_SORTS
a->nested=l->nested;
#endif
start_region_search(l,&lp);
get_region(&lp,&l_reg);
/* To simplify things we do an inner function on right gc_list */
#ifdef OPTIMIZE_SORTS
if (r->nested)
{
#endif
r2=inner(evaluator,r);
r=r2;
#ifdef OPTIMIZE_SORTS
} else r2=NULL;
#endif
#ifdef PROGRESS_REPORTS
oper_name= (not) ? "not containing" : "containing";
prog=0;
prog_start=LIST_SIZE(l)+LIST_SIZE(r);
#endif
start_region_search(r,&rp);
get_region(&rp,&r_reg);
while (r_reg.start!=-1 && l_reg.start!=-1)
{
if ( l_reg.start>r_reg.start )
{
/* right starts before left */
get_region(&rp,&r_reg);
} else if ( l_reg.end>=r_reg.end )
{
/* left contains right */
/* Start PK Febr 95 */
if (l_reg.start<r_reg.start ||
l_reg.end>r_reg.end)
{ /* Containment is proper */
if (!not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
} else { /* l_reg == r_reg */
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
}
/* End PK Febr 95 */
} else {
/* left comes after right */
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
}
}
/* When right list ended, there still might be something in left list */
while (not && l_reg.start!=-1)
{
add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
}
/* because we created list r2 here, we free it here */
if (r2!=NULL) free_gclist(r2);
return a;
}
RegionList *equal(RegionList *l,RegionList *r,int not)
/* Intersection of GC_LISTs *l and *r */
/* PK Febr '95 */
{
ListIterator lp,rp;
RegionList *a;
Region r_reg,l_reg;
SGREPDATA(l);
#ifdef PROGRESS_REPORTS
char *oper_name;
int prog;int prog_start;
#endif
#ifdef DEBUG
fprintf(stderr,"equal called\n");
#endif
if (not) stats.not_equal++; else stats.equal++;
a=new_region_list(sgrep);
#ifdef OPTIMIZE_SORTS
a->nested=l->nested;
#endif
start_region_search(l,&lp);
get_region(&lp,&l_reg);
#ifdef PROGRESS_REPORTS
oper_name= (not) ? "not equal" : "equal";
prog=0;
prog_start=LIST_SIZE(l)+LIST_SIZE(r);
#endif
start_region_search(r,&rp);
get_region(&rp,&r_reg);
while (r_reg.start!=-1 && l_reg.start!=-1)
{
if ( l_reg.start<r_reg.start )
{
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
} else if ( r_reg.start<l_reg.start )
{
get_region(&rp,&r_reg);
} else /* r_reg.start=l_reg.start */
if ( l_reg.end<r_reg.end )
{
if (not) add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
} else if ( r_reg.end<l_reg.end )
{
get_region(&rp,&r_reg);
} else /* l_reg = r_reg */
{
if (!not) add_region(a,l_reg.start,l_reg.end);
get_region(&rp,&r_reg);
get_region(&lp,&l_reg);
}
}
/* When right list ended, there still might be something in left list */
while (not && l_reg.start!=-1)
{
add_region(a,l_reg.start,l_reg.end);
get_region(&lp,&l_reg);
}
return a;