File: eval.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 (1856 lines) | stat: -rwxr-xr-x 43,610 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
/*
	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,&reg1);
		get_region(&p,&reg2);
		while (reg2.start!=-1)
		{
			assert(reg1.end<reg2.end);
			reg1=reg2;
			get_region(&p,&reg2);
		}
	}
#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,&reg1);

	/* 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,&reg2);
	while (reg2.start==reg1.start && reg2.end>reg1.end)
	{
		reg1=reg2;
		get_region(&p,&reg2);
	}
	
	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,&reg2);
		/* If regions start from same place, nesting is guaranteed */
		if (reg2.start==reg1.start)
		{
			reg1=reg2;
			get_region(&p,&reg2);
		}
	}
	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;

} /* END equal(RegionList *l,RegionList *r,int not) */

/*
 * Here we implement concat operation, which concats all overlapping regions
 * into one region and removes all nestings
 */
RegionList *concat(RegionList *l)
{
    SGREPDATA(l);
	ListIterator lp;
	RegionList *a;
	Region reg1,reg2;
#ifdef PROGRESS_REPORTS
	char *oper_name;
	int prog;int prog_start;
#endif

#ifdef DEBUG
	fprintf(stderr,"concat called\n");
#endif
	stats.concat++;
	a=new_region_list(sgrep);
	start_region_search(l,&lp);
	get_region(&lp,&reg1);
	
	/* We had empty list */
	if (reg1.start==-1) return a;

#ifdef PROGRESS_REPORTS
	oper_name="concat";
	prog=0;
	prog_start=LIST_SIZE(l);
#endif
	get_region(&lp,&reg2);
	
	while (reg2.start!=-1)
	{
		if (reg2.start>reg1.end+1)
		{
			/* separate regions, no concat */
			add_region(a,reg1.start,reg1.end);
			reg1=reg2;
		} else if ( reg2.end>reg1.end )
		{
			/* We found overlapping */
			reg1.end=reg2.end;
		}
		get_region(&lp,&reg2);
	}
	add_region(a,reg1.start,reg1.end);
	return a;
}

/*
 * Here we implement extracting operation
 */
RegionList *extracting(RegionList *l,RegionList *r)
{
    SGREPDATA(l);
	ListIterator lp,rp,tmpp;
	RegionList *a,*r2,*tmp,*new_tmp;
	Region l_reg,r_reg;
	int prev_s=-1;
	int prev_e=-1;
	int last_tmp;
#ifdef PROGRESS_REPORTS
	char *oper_name;
	int prog;int prog_start;
#endif

#ifdef DEBUG
	fprintf(stderr,"extracting called\n");
#endif
	stats.extracting++;
	/* to simplify things we do concat on right gc_list. Result stays
	   the same anyway */
	r2=concat(r);
	r=r2;
	
	a=new_region_list(sgrep);
#ifdef OPTIMIZE_SORTS
	a->nested=l->nested;
#endif
	tmp=new_region_list(sgrep);
	start_region_search(tmp,&tmpp);

#ifdef PROGRESS_REPORTS
	oper_name="extracting";
	prog=0;
	prog_start=LIST_SIZE(l);
#endif
	
	start_region_search(l,&lp);
	get_region(&lp,&l_reg);
	start_region_search(r,&rp);
	get_region(&rp,&r_reg);

	while ( l_reg.start!=-1 )
	{
		if ( l_reg.end<r_reg.start || r_reg.start==-1 )
		{
			/* Regions are separate, left starting first.
 			   no cutting */
 			if ( prev_s!=l_reg.start || prev_e!=l_reg.end )
 			{				
#ifdef DEBUG		
	fprintf(stderr,"extracting adding 1(%d,%d)\n",l_reg.start,l_reg.end);
#endif
				prev_s=l_reg.start;
				prev_e=l_reg.end;
				add_region(a,l_reg.start,l_reg.end);
			}
			l_reg=first_of(&lp,&tmpp);
		} else if ( r_reg.end<l_reg.start )
		{
			/* Regions are separate right starting first.
			   we skip right */
#ifdef DEBUG
			fprintf(stderr,"skipping right, left=(%d,%d) right=(%d,%d)\n",
				l_reg.start,l_reg.end,r_reg.start,r_reg.end);
#endif
			get_region(&rp,&r_reg);
		} else
		{
			/* We need to do clipping. */
			new_tmp=new_region_list(sgrep);
/* OBSOLETE			new_tmp->start_sorted_list=NULL;
   must_be_sorted=FALSE; */
			last_tmp=-1;
#ifdef DEBUG
			fprintf(stderr,"cutting loop, cutter (%d,%d)\n",r_reg.start,r_reg.end);
#endif
			while ( l_reg.start!=-1 && l_reg.start<=r_reg.end )
			{
			    if (l_reg.start<r_reg.start && 
				( prev_s!=l_reg.start ||
				  prev_e!=r_reg.start-1 ))
			    {
				prev_s=l_reg.start;
				prev_e=r_reg.start-1;
				
#ifdef DEBUG
				fprintf(stderr,"extracting adding 2(%d,%d)\n",l_reg.start,r_reg.start-1);
#endif		
				add_region(a,l_reg.start,r_reg.start-1);
			    }
			    if (r_reg.end<l_reg.end)
			    {
#ifdef DEBUG
				fprintf(stderr,"(%d,%d)<-new_tmp\n",r_reg.end+1,l_reg.end);
#endif
				if (l_reg.end<last_tmp) {
				    list_set_sorted(new_tmp,NOT_SORTED);
				}
				add_region(new_tmp,r_reg.end+1,l_reg.end);
				last_tmp=l_reg.end;
			    }
			    l_reg=first_of(&lp,&tmpp);
			}
			if (l_reg.start!=-1) prev_region(&lp,&l_reg);
				
			assert (tmpp.ind==tmp->length &&
				tmpp.node->next==NULL);

			free_gclist(tmp);
#ifdef PROGRESS_REPORTS
			prog--;
			prog_start+=LIST_SIZE(new_tmp);
#endif
			tmp=new_tmp;
			start_region_search(tmp,&tmpp);
			/* Left region is now handled -> skip to next */
			l_reg=first_of(&lp,&tmpp);
		}
	}
	free_gclist(r2);
	free_gclist(tmp);
	return a;
}

/*
 * Join operation
 */
RegionList *join(Evaluator *evaluator,RegionList *l,int number)
{
	RegionList *a;
	ListIterator p1,p2;
	Region r1,r2,prev_r1,prev_r2;
	int i;
	SGREPDATA(l);
#ifdef PROGRESS_REPORTS
	char *oper_name;
	int prog;int prog_start;
#endif
	
#ifdef DEBUG
	fprintf(stderr,"join called %d\n",number);
#endif
	assert(number>0);

	stats.join++;
	a=new_region_list(sgrep);
#ifdef OPTIMIZE_SORTS
	a->nested=l->nested;
#endif	
	if ( l->first==NULL )
	{
		/* This is an optimized chars node */
		to_chars(a,(l->chars+1)*number,
			 flist_total(evaluator->files)-1);
		return a;
	}
	
	/* List is smaller than join number, so return list is empty */
	if (LIST_SIZE(l)<number) return a;

#ifdef PROGRESS_REPORTS
	oper_name="join";
	prog=0;
	prog_start=LIST_SIZE(l);
#endif

	start_region_search(l,&p1);
	start_region_search(l,&p2);

	prev_r2.start=-1;	
	prev_r1.end=-1;	
	for (i=number;i>0;i--)
	{
		get_region(&p1,&r1);
		assert(r1.start!=-1);
	}
	while (r1.start!=-1)
	{
		get_region(&p2,&r2);
		if (r2.start==prev_r2.start)
		{
/*PK			if (prev.end<r2.end) */

			if (r1.end<=prev_r1.end)
			{
				list_set_sorted(a,NOT_SORTED);
			}
		}
		add_region(a,r2.start,r1.end);
		prev_r1=r1; 
		get_region(&p1,&r1);
		prev_r2=r2; 
	}
	if (list_get_sorted(a)!=START_SORTED)
	{ 
	  	remove_duplicates(a); /* There might be duplicates in a */
	};
	return a;	
}

RegionList *first(RegionList *input, int num) {
    /* It would be cool, if i just could truncate the input list.
     * Too bad that checking for if i just could is too complicated */
    RegionList *result;
    ListIterator i;
    Region r;
    SGREPDATA(input);

    result=new_region_list(sgrep);
    start_region_search(input,&i);
    get_region(&i,&r);
    while(num>0 && r.start!=-1) {
	add_region(result,r.start,r.end);
	get_region(&i,&r);
	num--;
    }
    return result;
}

RegionList *last(RegionList *input, int num) {
    RegionList *result;
    ListIterator i;
    Region r;
    SGREPDATA(input);

    num=LIST_SIZE(input)-num;
    if (num<0) num=0;
    result=new_region_list(sgrep);
    start_region_search_from(input,num,&i);
    get_region(&i,&r);
    while(r.start!=-1) {
	add_region(result,r.start,r.end);
	get_region(&i,&r);	
    }
    return result;    
}

RegionList *first_bytes(RegionList *input, int num) {
    ListIterator i;
    Region r1,r2;
    RegionList *result;


    assert(num>=0);
    result=new_region_list(input->sgrep);
    if (num==0) return result;

    result->nested=input->nested;
    start_region_search(input,&i);
    r2.start=-1;
    get_region(&i,&r1);

    /* I too used to think that first_bytes() is a really simple operation..
     * Too bad it didn't last. */
    while(r1.start!=-1) {
	if (r1.end-r1.start+1>=num && 
	    (r1.start!=r2.start || r1.start+num-1!=r2.end)) {
	    r1.end=r1.start+num-1;
	    add_region(result,r1.start,r1.end);
	}
	get_region(&i,&r2);
	if (r2.start==-1) {
	    r1.start=-1;
	} else if (r2.end-r2.start+1>=num && 
		   (r1.start!=r2.start || r2.start+num-1!=r1.end)) {
	    r2.end=r2.start+num-1;
	    add_region(result,r2.start,r2.end);
	    get_region(&i,&r1);
	} else {
	    get_region(&i,&r1);
	}
    }
    return result;
}


RegionList *last_bytes(RegionList *input, int num) {
    ListIterator i;
    Region r1,r2;
    RegionList *result;

    /* This could probably be done faster, without not requiring to
     * set result as NOT_SORTED and remove_duplicates() */
    assert(num>=0);
    result=new_region_list(input->sgrep);    
    if (num==0) return result;

    if (input->nested) {
	list_set_sorted(result,NOT_SORTED); /* Sad but true */
	result->nested=1;
    }
    start_region_search(input,&i);
    r2.start=-1;
    get_region(&i,&r1);
    while(r1.start!=-1) {
	if (r1.end-r1.start+1>=num && 
	    (r1.end!=r2.end || r1.end+1-num!=r2.start)) {
	    r1.start=r1.end+1-num;
	    add_region(result,r1.start,r1.end);
	}
	get_region(&i,&r2);
	if (r2.start==-1) {
	    r1.start=-1;
	} else if (r2.end-r2.start+1>=num && 
		   (r2.end!=r1.end || r2.end+1-num!=r1.start)) {
	    r2.start=r2.end+1-num;
	    add_region(result,r2.start,r2.end);
	    get_region(&i,&r1);
	} else {
	    get_region(&i,&r1);
	}
    }
    if (result->nested) {
	remove_duplicates(result);
    }
	
    return result;
}

/* 
 * Handles ordering which does _not_ produce nesting gc-lists. 
 * For example '"--" quote "--"' to catch SGML comments.
 */
RegionList *quote(RegionList *l,RegionList *r,int type)
{
	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,"quote called\n");
#endif	
	stats.quote++;
	a=new_region_list(sgrep);

#ifdef PROGRESS_REPORTS
	prog_start=LIST_SIZE(r);
	prog=0;
	switch (type) {
	case L_QUOTE:
		oper_name="_quote";
		break;
	case R_QUOTE:
		oper_name="quote_";
		break;
	case LR_QUOTE:
		oper_name="_quote_";
		break;
	default:
		oper_name="quote";
	}
#endif
	start_region_search(r,&rp);
	start_region_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 {
		/* Skip until we find ending quote after start quote */
		while (l_reg.end>=r_reg.start && r_reg.start!=-1) {
			get_region(&rp,&r_reg);
		}
		if (r_reg.start>=0) {
			/* Add region using operation type */
			switch (type) {
			case QUOTE:
				add_region(a,l_reg.start, r_reg.end);
				break;
			case L_QUOTE:
				add_region(a,l_reg.end+1, r_reg.end);
				break;
			case R_QUOTE:
				add_region(a,l_reg.start, r_reg.start-1);
				break;
			case LR_QUOTE:
				/* No empty regions */
				if (l_reg.end+1<r_reg.start) {
					add_region(a,l_reg.end+1,r_reg.start-1);
				} 
				break;
			default:
			    abort();
			/* Skip until starting quote is after last ending
			   quote */
			}
			while (l_reg.start<=r_reg.end && l_reg.start!=-1) {
				get_region(&lp,&l_reg);
			}
		}
	} while ( l_reg.start!=-1 && r_reg.start!=-1);
	return a;
}

/*
 * NOTE: assumes that require_start_sorted_array has been called for
 * list
 */
int list_find_first_start(RegionList *list, int start, int index) {
    int end;
    int middle;
    Region region;

    assert(list->start_sorted_array);
    end=LIST_SIZE(list);
    assert(start<=end);

    middle=1;
    while(start+middle<end) {
	region_at(list,start+middle,&region);
	/* printf("lffs1(%d): %d %d %d (%d,%d)\n",index,start,middle,end,
	   region.start,region.end); */
	if (region.start<index) {
	    start+=middle+1;
	    middle+=middle;
	} else {
	    end=start+middle;
	    break;
	}
    }

    while(start!=end) {
	middle=(end+start)/2;
	region_at(list,middle,&region);
	/* printf("lffs2(%d): %d %d %d (%d,%d)\n",index,start,middle,end,
	   region.start,region.end);  */
	if (region.start<index) {
	    start=middle+1;
	} else {
	    end=middle;
	}
    }
    return start;
}

RegionList *parenting(Evaluator *evaluator,RegionList *l, RegionList *r) {
    RegionList *result;
    Region parent,child;
    ListIterator child_i;
    int parent_i,parent_size;
    Region *stack=evaluator->tmp_stack;
    int sp=0; /* Stack pointer */
    SGREPDATA(l);

    stats.parenting++;

    /* Initialization */
    result=new_region_list(sgrep);
    if (LIST_SIZE(r)>1) result->nested=1;

    list_require_start_sorted_array(l);
    parent_size=LIST_SIZE(l);
    if (parent_size==0) return result;
    parent_i=0;
    region_at(l,parent_i,&parent);
 
    start_region_search(r,&child_i);
    get_region(&child_i,&child);

    list_set_sorted(result,NOT_SORTED);
    /* The parenting loop */
    while(child.start!=-1 && (sp>0 || parent_i<parent_size)) {

	/* This should be the most frequent case: parent candidate 
	 * ends before child has even started. Just simply skip to next
	 * parent */
	while(parent.end<child.start && parent_i<parent_size) {
	    parent_i=list_find_first_start(l,parent_i,parent.end+1);
	    if (parent_i<parent_size) {
		region_at(l,parent_i,&parent);	    
	    }
	}
	
	/* Skip child candidates as long as
	 * 0. stack is empty
	 * 1. there is childs
	 * 2. child is not contained in parent
	 * 3. parent ends after child starts
	 */
	if (sp==0) {
	    while(child.start!=-1 && 
		  (!contains(parent,child)) &&
		  parent.end>child.start) {
		get_region(&child_i,&child);
	    }
	}

	/* No more childs --> nothing to do anymore */
	if (child.start==-1) break;
	
	if (parent_i<parent_size && contains(parent,child)) {
	    /* Now we know, that the child is contained in parent */

	    /* Now add the known good candidate to stack */
	    if (evaluator->tmp_stack_size==sp) {
		/* Allocate more space (previous_size*1.5) */
		evaluator->tmp_stack_size+=evaluator->tmp_stack_size/2;
		stack=(Region *)sgrep_realloc(stack,
						     evaluator->tmp_stack_size*
						     sizeof(Region));
		evaluator->tmp_stack=stack;
	    }
	    stack[sp++]=parent;
	    parent_i++;
	    if (parent_i<parent_size) {
		region_at(l,parent_i,&parent);
	    }
	} else {
	    /* Now the innermost containing element is the topmost element
	     * in the stack. If stack is empty there is no containing element
	     */
	    if (sp>0) {
		assert(contains(stack[sp-1],child));
		add_region(result,stack[sp-1].start,stack[sp-1].end);
		/* This child is now handled. Next child */
		get_region(&child_i,&child);

		/* Now remove the regions which do not contain the new child
		 * from stack */ 
		if (child.start!=-1) {
		    while(sp>0 && stack[sp-1].end<child.end) {
			assert(stack[sp-1].start<=child.start);
			sp--;		    
		    }
		}
	    }	    
	}
    }
    /* The result list is not sorted and might contain duplicates */
    remove_duplicates(result);
    return result;
}


RegionList *childrening(RegionList *children, 
			RegionList *parents) {
    RegionList *result;
    ListIterator parent_i;
    int child_number;
    Region parent,child,next_child;
    int childrens;
    RegionList *saved_parents;
    int last_parent_end;
    int loops=0;
    int first;
    SGREPDATA(children);
    
    stats.childrening++;

    /* Initialization */
    start_region_search(parents,&parent_i);    
    list_require_start_sorted_array(children);
    get_region(&parent_i,&parent);
    saved_parents=new_region_list(sgrep);
    saved_parents->nested=1;
    child_number=0;
    childrens=LIST_SIZE(children);
    result=new_region_list(sgrep);
    first=parent.start;

    /* While there is parents and child candidates left */
    while(first!=-1) { 

	/* Find first possible child candidate */
	child_number=list_find_first_start(children,child_number,first);
	
	if (child_number<childrens) {
	    region_at(children,child_number,&child);

	    /*
	     * Now the ugly part. 
	     * Deal with parent region being also a child candidate
	     */	    
	    if (child.start==parent.start && child.end==parent.end) {
		first++;
		continue;
	    }

	    /* Deal with multiple child candidate regions having same start 
	     * point. 
	     */	    
	    while (contains(parent,child) &&
		   child_number+1<childrens) {
		/* UGH! */
		region_at(children,child_number+1,&next_child);
		if (next_child.start==child.start && 
		    contains(parent,next_child)) {
		    /* We found a better child candidate */
		    assert(next_child.end>child.end);
		    child_number++;
		    child=next_child;
		} else {
		    break;
		}		    
	    }
	    
	    assert(child_number<childrens && parent.start<=child.start);

	    /* Now check the final candidate */
	    if (contains(parent,child)) {
		/* Add found child candidate to result list*/
		add_region(result,child.start,child.end);
		first=child.end+1;
	    } else {
		/* This parent is handled; find next */
		last_parent_end=parent.end;
		get_region(&parent_i,&parent);
		while(parent.start!=-1 && parent.start<=last_parent_end) {
		    /* Overlapping or nested parents. 
		     * Handle it in next iteration */
		    add_region(saved_parents,parent.start,parent.end);
		    get_region(&parent_i,&parent);
		}
		first=parent.start;
	    }
	} else {
	    first=-1;
	}
	
	/* Check, if we need to restart search with saved parents */
	if (first==-1 && LIST_SIZE(saved_parents)>0) {
	    /* We need to restart */
	    if (loops>0) {
		/* Free region lists created here */
		delete_region_list(parents);
	    }
	    loops++;
	    /* fprintf(stderr,"Childrening: loop #%d\n",loops); */
	    parents=saved_parents;
	    start_region_search(parents,&parent_i);
	    get_region(&parent_i,&parent);
	    saved_parents=new_region_list(sgrep);	    
	    saved_parents->nested=1;
	    child_number=0;
	    /* After second loop it is possible that result contains
	     * unordered nested regions */
	    list_set_sorted(result,NOT_SORTED);
	    result->nested=1;
	    first=parent.start;
	}
    }

    /* Clean up */
    delete_region_list(saved_parents);
    if (loops>0) delete_region_list(parents);

    if (loops>0) {
	/* Result list might be nested or contain duplicates only if
	 * we needed multiple passes */
	remove_duplicates(result);
    }

    return result;
}

RegionList *eval_near(RegionList *l, 
		 RegionList *r, int how_near) {
    RegionList *first_list, *second_list;
    ListIterator first_i, second_i;
    Region result,first,second;
    RegionList *result_list;
    SGREPDATA(l);

    /* To simplify things, we outer() on first and second */
    first_list= (l->nested) ? outer(l) : l;
    second_list= (r->nested) ? outer(r) : r;
    
    /* Initialize */
    start_region_search(first_list,&first_i);
    get_region(&first_i,&first);
    start_region_search(second_list,&second_i);
    get_region(&second_i,&second);
    result_list=new_region_list(sgrep); 
    result.start=result.end=-1;

    /* Do the job */
    while(first.start!=-1 && second.start!=-1) {

	if (first.start<second.start ||
	    (first.start==second.start && first.end<second.end)) {	    
	    /* first starts first */
	    
	    if (second.start-1-first.end <= how_near) {		
		/* Found a match */

		/* Check for nesting result */
		assert(first.start>=result.start);
		if (second.end>result.end) {
		    /* No nesting. So we have a proper match
		     * Now we can add the region from previous iteration */
		    if (result.start>0) {
			add_region(result_list,result.start,result.end);    
		    }
		    /* Save the match to be added later */
		    result.start=first.start;
		    result.end=second.end;
		}
	    }
	    get_region(&first_i,&first);

	} else {

	    /* second starts first */
	    
	    if (first.start-1-second.end <= how_near) {		
		/* Found a match */

		/* Check for nesting result */
		assert(second.start>=result.start);
		if (first.end>result.end) {
		    /* No nesting. So we have a proper match
		     * Now we can add the region from previous iteration */
		    if (result.start>=0) {
			add_region(result_list,result.start,result.end);    
		    }
		    /* Save the match to be added later */
		    result.start=second.start;
		    result.end=first.end;
		}
	    }
	    get_region(&second_i,&second);
	}
    }
    
    /* Free the outer() lists, if they we're created */
    if (first_list!=l) {
	delete_region_list(first_list);
    }
    if (second_list!=r) {
	delete_region_list(second_list);
    }

    /* Add the last result */
    if (result.start>0) {
	add_region(result_list,result.start,result.end);
    }

    return result_list;
}

RegionList *near_before(RegionList *l, 
		 RegionList *r, int how_near) {
    RegionList *first_list, *second_list;
    ListIterator first_i;
    int second_size;
    int second_i;
    Region first;
    RegionList *result_list;
    SGREPDATA(l);

    /* To simplify things, we outer() on first and second */
    first_list= (l->nested) ? outer(l) : l;
    second_list= (r->nested) ? outer(r) : r;
    
    /* Initialize */
    start_region_search(first_list,&first_i);
    get_region(&first_i,&first);
    if (first.start==-1) {
	return new_region_list(sgrep);
    }
    list_require_start_sorted_array(second_list);
    second_size=LIST_SIZE(second_list);
    second_i=list_find_first_start(second_list,0,first.end+1);
    result_list=new_region_list(sgrep); 

    /* Do the job */
    while(first.start!=-1 && second_i<second_size) {
	    
	    if (region_lvalue_at(second_list,second_i).start-1-
		first.end <= how_near) {		
		/* Found a match */
		add_region(result_list,
			   first.start,
			   region_lvalue_at(second_list,second_i).end);
	    }
	    /* Next */
	    get_region(&first_i,&first);
	    if (first.start!=-1) {
		second_i=list_find_first_start(second_list,
					       second_i,first.end+1);
	    }
    }
    
    /* Free the outer() lists, if they we're created */
    if (first_list!=l) {
	delete_region_list(first_list);
    }
    if (second_list!=r) {
	delete_region_list(second_list);
    }

    return result_list;
}