summaryrefslogtreecommitdiff
path: root/ragel/fsmgraph.cc
blob: 819bfa9641ee87c208c09b8f4160acb37a806159 (plain)
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
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
/*
 * Copyright 2001-2018 Adrian Thurston <thurston@colm.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <assert.h>
#include <iostream>

#include "fsmgraph.h"
#include "mergesort.h"
#include "action.h"

using std::endl;
	
Action::~Action()
{
	/* If we were created by substitution of another action then we don't own the inline list. */
	if ( substOf == 0 && inlineList != 0 ) {
		inlineList->empty();
		delete inlineList;
		inlineList = 0;
	}
}

InlineItem::~InlineItem()
{
	if ( children != 0 ) {
		children->empty();
		delete children;
	}
}

/* Make a new state. The new state will be put on the graph's
 * list of state. The new state can be created final or non final. */
StateAp *FsmAp::addState()
{
	/* Make the new state to return. */
	StateAp *state = new StateAp();

	if ( misfitAccounting ) {
		/* Create the new state on the misfit list. All states are created
		 * with no foreign in transitions. */
		misfitList.append( state );
	}
	else {
		/* Create the new state. */
		stateList.append( state );
	}

	return state;
}

/* Construct an FSM that is the concatenation of an array of characters. A new
 * machine will be made that has len+1 states with one transition between each
 * state for each integer in str. IsSigned determines if the integers are to
 * be considered as signed or unsigned ints. */
FsmAp *FsmAp::concatFsm( FsmCtx *ctx, Key *str, int len )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Make the first state and set it as the start state. */
	StateAp *last = fsm->addState();
	fsm->setStartState( last );

	/* Attach subsequent states. */
	for ( int i = 0; i < len; i++ ) {
		StateAp *newState = fsm->addState();
		fsm->attachNewTrans( last, newState, str[i], str[i] );
		last = newState;
	}

	/* Make the last state the final state. */
	fsm->setFinState( last );

	return fsm;
}

/* Case insensitive version of concatFsm. */
FsmAp *FsmAp::concatFsmCI( FsmCtx *ctx, Key *str, int len )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Make the first state and set it as the start state. */
	StateAp *last = fsm->addState();
	fsm->setStartState( last );

	/* Attach subsequent states. */
	for ( int i = 0; i < len; i++ ) {
		StateAp *newState = fsm->addState();

		KeySet keySet( ctx->keyOps );
		if ( str[i].isLower() )
			keySet.insert( str[i].toUpper() );
		if ( str[i].isUpper() )
			keySet.insert( str[i].toLower() );
		keySet.insert( str[i] );

		for ( int i = 0; i < keySet.length(); i++ )
			fsm->attachNewTrans( last, newState, keySet[i], keySet[i] );

		last = newState;
	}

	/* Make the last state the final state. */
	fsm->setFinState( last );

	return fsm;
}


/* Construct a machine that matches one character.  A new machine will be made
 * that has two states with a single transition between the states. */
FsmAp *FsmAp::concatFsm( FsmCtx *ctx, Key chr )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Two states first start, second final. */
	fsm->setStartState( fsm->addState() );

	StateAp *end = fsm->addState();
	fsm->setFinState( end );

	/* Attach on the character. */
	fsm->attachNewTrans( fsm->startState, end, chr, chr );

	return fsm;
}

/* Case insensitive version of single-char concat FSM. */
FsmAp *FsmAp::concatFsmCI( FsmCtx *ctx, Key chr )
{
	return concatFsmCI( ctx, &chr, 1 );
}


/* Construct a machine that matches any character in set.  A new machine will
 * be made that has two states and len transitions between the them. The set
 * should be ordered correctly accroding to KeyOps and should not contain
 * any duplicates. */
FsmAp *FsmAp::orFsm( FsmCtx *ctx, Key *set, int len )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Two states first start, second final. */
	fsm->setStartState( fsm->addState() );

	StateAp *end = fsm->addState();
	fsm->setFinState( end );

	for ( int i = 1; i < len; i++ )
		assert( ctx->keyOps->lt( set[i-1], set[i] ) );

	/* Attach on all the integers in the given string of ints. */
	for ( int i = 0; i < len; i++ )
		fsm->attachNewTrans( fsm->startState, end, set[i], set[i] );

	return fsm;
}

FsmAp *FsmAp::dotFsm( FsmCtx *ctx )
{
	FsmAp *retFsm = FsmAp::rangeFsm( ctx,
			ctx->keyOps->minKey, ctx->keyOps->maxKey );
	return retFsm;
}

FsmAp *FsmAp::dotStarFsm( FsmCtx *ctx )
{
	FsmAp *retFsm = FsmAp::rangeStarFsm( ctx,
			ctx->keyOps->minKey, ctx->keyOps->maxKey );
	return retFsm;
}

/* Construct a machine that matches a range of characters.  A new machine will
 * be made with two states and a range transition between them. The range will
 * match any characters from low to high inclusive. Low should be less than or
 * equal to high otherwise undefined behaviour results.  IsSigned determines
 * if the integers are to be considered as signed or unsigned ints. */
FsmAp *FsmAp::rangeFsm( FsmCtx *ctx, Key low, Key high )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Two states first start, second final. */
	fsm->setStartState( fsm->addState() );

	StateAp *end = fsm->addState();
	fsm->setFinState( end );

	/* Attach using the range of characters. */
	fsm->attachNewTrans( fsm->startState, end, low, high );

	return fsm;
}

FsmAp *FsmAp::notRangeFsm( FsmCtx *ctx, Key low, Key high )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Two states first start, second final. */
	fsm->setStartState( fsm->addState() );

	StateAp *end = fsm->addState();
	fsm->setFinState( end );

	/* Attach using the range of characters. */
	if ( ctx->keyOps->lt( ctx->keyOps->minKey, low ) ) {
		ctx->keyOps->decrement( low );
		fsm->attachNewTrans( fsm->startState, end, ctx->keyOps->minKey, low );
	}

	if ( ctx->keyOps->lt( high, ctx->keyOps->maxKey ) ) {
		ctx->keyOps->increment( high );
		fsm->attachNewTrans( fsm->startState, end, high, ctx->keyOps->maxKey );
	}

	return fsm;
}


FsmAp *FsmAp::rangeFsmCI( FsmCtx *ctx, Key lowKey, Key highKey )
{
	FsmAp *retFsm = rangeFsm( ctx, lowKey, highKey );

	/* Union the portion that covers alphas. */
	if ( lowKey.getVal() <= 'z' ) {
		int low, high;
		if ( lowKey.getVal() <= 'a' )
			low = 'a';
		else
			low = lowKey.getVal();

		if ( highKey.getVal() >= 'a' ) {
			if ( highKey.getVal() >= 'z' )
				high = 'z';
			else
				high = highKey.getVal();

			/* Add in upper(low) .. upper(high) */

			FsmAp *addFsm = FsmAp::rangeFsm( ctx, toupper(low), toupper(high) );
			FsmRes res = FsmAp::unionOp( retFsm, addFsm );
			retFsm = res.fsm;
		}
	}

	if ( lowKey.getVal() <= 'Z' ) {
		int low, high;
		if ( lowKey.getVal() <= 'A' )
			low = 'A';
		else
			low = lowKey.getVal();

		if ( highKey.getVal() >= 'A' ) {
			if ( highKey.getVal() >= 'Z' )
				high = 'Z';
			else
				high = highKey.getVal();

			/* Add in lower(low) .. lower(high) */
			FsmAp *addFsm = FsmAp::rangeFsm( ctx, tolower(low), tolower(high) );
			FsmRes res = FsmAp::unionOp( retFsm, addFsm );
			retFsm = res.fsm;
		}
	}

	return retFsm;
}

/* Construct a machine that a repeated range of characters.  */
FsmAp *FsmAp::rangeStarFsm( FsmCtx *ctx, Key low, Key high )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* One state which is final and is the start state. */
	fsm->setStartState( fsm->addState() );
	fsm->setFinState( fsm->startState );

	/* Attach start to start using range of characters. */
	fsm->attachNewTrans( fsm->startState, fsm->startState, low, high );

	return fsm;
}

/* Construct a machine that matches the empty string.  A new machine will be
 * made with only one state. The new state will be both a start and final
 * state. IsSigned determines if the machine has a signed or unsigned
 * alphabet. Fsm operations must be done on machines with the same alphabet
 * signedness. */
FsmAp *FsmAp::lambdaFsm( FsmCtx *ctx )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Give it one state with no transitions making it
	 * the start state and final state. */
	fsm->setStartState( fsm->addState() );
	fsm->setFinState( fsm->startState );

	return fsm;
}

/* Construct a machine that matches nothing at all. A new machine will be
 * made with only one state. It will not be final. */
FsmAp *FsmAp::emptyFsm( FsmCtx *ctx )
{
	FsmAp *fsm = new FsmAp( ctx );

	/* Give it one state with no transitions making it
	 * the start state and final state. */
	fsm->setStartState( fsm->addState() );

	return fsm;
}

void FsmAp::transferOutData( StateAp *destState, StateAp *srcState )
{
	for ( TransList::Iter trans = destState->outList; trans.lte(); trans++ ) {
		if ( trans->plain() ) {
			if ( trans->tdap()->toState != 0 ) {
				/* Get the actions data from the outActionTable. */
				trans->tdap()->actionTable.setActions( srcState->outActionTable );

				/* Get the priorities from the outPriorTable. */
				trans->tdap()->priorTable.setPriors( srcState->outPriorTable );
			}
		}
		else {
			for ( CondList::Iter cond = trans->tcap()->condList; cond.lte(); cond++ ) {
				if ( cond->toState != 0 ) {
					/* Get the actions data from the outActionTable. */
					cond->actionTable.setActions( srcState->outActionTable );

					/* Get the priorities from the outPriorTable. */
					cond->priorTable.setPriors( srcState->outPriorTable );
				}
			}
		}
	}

	if ( destState->nfaOut != 0 ) {
		for ( NfaTransList::Iter na = *destState->nfaOut; na.lte(); na++ )
			transferOutToNfaTrans( na, srcState );
	}
}

/* Union worker used by union, set diff (subtract) and intersection. */
FsmRes FsmAp::doUnion( FsmAp *fsm, FsmAp *other )
{
	/* Build a state set consisting of both start states */
	StateSet startStateSet;
	startStateSet.insert( fsm->startState );
	startStateSet.insert( other->startState );

	/* Both of the original start states loose their start state status. */
	fsm->unsetStartState();
	other->unsetStartState();

	/* Bring in the rest of other's entry points. */
	fsm->copyInEntryPoints( other );
	other->entryPoints.empty();

	/* Merge the lists. This will move all the states from other
	 * into this. No states will be deleted. */
	fsm->stateList.append( other->stateList );
	fsm->misfitList.append( other->misfitList );

	/* Move the final set data from other into this. */
	fsm->finStateSet.insert(other->finStateSet);
	other->finStateSet.empty();

	/* Since other's list is empty, we can delete the fsm without
	 * affecting any states. */
	delete other;

	/* Create a new start state. */
	fsm->setStartState( fsm->addState() );

	/* Merge the start states. */
	fsm->mergeStateList( fsm->startState, startStateSet.data, startStateSet.length() );

	/* Fill in any new states made from merging. */
	return fillInStates( fsm );
}

bool FsmAp::inEptVect( EptVect *eptVect, StateAp *state )
{
	if ( eptVect != 0 ) {
		/* Vect is there, walk it looking for state. */
		for ( int i = 0; i < eptVect->length(); i++ ) {
			if ( eptVect->data[i].targ == state )
				return true;
		}
	}
	return false;
}

/* Fill epsilon vectors in a root state from a given starting point. Epmploys
 * a depth first search through the graph of epsilon transitions. */
void FsmAp::epsilonFillEptVectFrom( StateAp *root, StateAp *from, bool parentLeaving )
{
	/* Walk the epsilon transitions out of the state. */
	for ( EpsilonTrans::Iter ep = from->epsilonTrans; ep.lte(); ep++ ) {
		/* Find the entry point, if the it does not resove, ignore it. */
		EntryMapEl *enLow, *enHigh;
		if ( entryPoints.findMulti( *ep, enLow, enHigh ) ) {
			/* Loop the targets. */
			for ( EntryMapEl *en = enLow; en <= enHigh; en++ ) {
				/* Do not add the root or states already in eptVect. */
				StateAp *targ = en->value;
				if ( targ != from && !inEptVect(root->eptVect, targ) ) {
					/* Maybe need to create the eptVect. */
					if ( root->eptVect == 0 )
						root->eptVect = new EptVect();

					/* If moving to a different graph or if any parent is
					 * leaving then we are leaving. */
					bool leaving = parentLeaving || 
							root->owningGraph != targ->owningGraph;

					/* All ok, add the target epsilon and recurse. */
					root->eptVect->append( EptVectEl(targ, leaving) );
					epsilonFillEptVectFrom( root, targ, leaving );
				}
			}
		}
	}
}

void FsmAp::shadowReadWriteStates()
{
	/* Init isolatedShadow algorithm data. */
	for ( StateList::Iter st = stateList; st.lte(); st++ )
		st->isolatedShadow = 0;

	/* Any states that may be both read from and written to must 
	 * be shadowed. */
	for ( StateList::Iter st = stateList; st.lte(); st++ ) {
		/* Find such states by looping through stateVect lists, which give us
		 * the states that will be read from. May cause us to visit the states
		 * that we are interested in more than once. */
		if ( st->eptVect != 0 ) {
			/* For all states that will be read from. */
			for ( EptVect::Iter ept = *st->eptVect; ept.lte(); ept++ ) {
				/* Check for read and write to the same state. */
				StateAp *targ = ept->targ;
				if ( targ->eptVect != 0 ) {
					/* State is to be written to, if the shadow is not already
					 * there, create it. */
					if ( targ->isolatedShadow == 0 ) {
						StateAp *shadow = addState();
						mergeStates( shadow, targ );
						targ->isolatedShadow = shadow;
					}

					/* Write shadow into the state vector so that it is the
					 * state that the epsilon transition will read from. */
					ept->targ = targ->isolatedShadow;
				}
			}
		}
	}
}

void FsmAp::resolveEpsilonTrans()
{
	/* Walk the state list and invoke recursive worker on each state. */
	for ( StateList::Iter st = stateList; st.lte(); st++ )
		epsilonFillEptVectFrom( st, st, false );

	/* Prevent reading from and writing to of the same state. */
	shadowReadWriteStates();

	/* For all states that have epsilon transitions out, draw the transitions,
	 * clear the epsilon transitions. */
	for ( StateList::Iter st = stateList; st.lte(); st++ ) {
		/* If there is a state vector, then create the pre-merge state. */
		if ( st->eptVect != 0 ) {
			/* Merge all the epsilon targets into the state. */
			for ( EptVect::Iter ept = *st->eptVect; ept.lte(); ept++ ) {
				if ( ept->leaving )
					mergeStatesLeaving( st, ept->targ );
				else
					mergeStates( st, ept->targ );
			}

			/* Clean up the target list. */
			delete st->eptVect;
			st->eptVect = 0;
		}

		/* Clear the epsilon transitions vector. */
		st->epsilonTrans.empty();
	}
}

FsmRes FsmAp::applyNfaTrans( FsmAp *fsm, StateAp *fromState, StateAp *toState, NfaTrans *nfaTrans )
{
	fsm->setMisfitAccounting( true );

	fsm->mergeStates( fromState, toState, false );

	/* Epsilons can caused merges which leave behind unreachable states. */
	FsmRes res = FsmAp::fillInStates( fsm );
	if ( !res.success() )
		return res;

	/* Can nuke the epsilon transition that we will never
	 * follow. */
	fsm->detachFromNfa( fromState, toState, nfaTrans );
	fromState->nfaOut->detach( nfaTrans );
	delete nfaTrans;

	if ( fromState->nfaOut->length() == 0 ) {
		delete fromState->nfaOut;
		fromState->nfaOut = 0;
	}

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	return FsmRes( FsmRes::Fsm(), fsm );
}

void FsmAp::globOp( FsmAp **others, int numOthers )
{
	for ( int m = 0; m < numOthers; m++ ) {
		assert( ctx == others[m]->ctx );
	}

	/* All other machines loose start states status. */
	for ( int m = 0; m < numOthers; m++ )
		others[m]->unsetStartState();
	
	/* Bring the other machines into this. */
	for ( int m = 0; m < numOthers; m++ ) {
		/* Bring in the rest of other's entry points. */
		copyInEntryPoints( others[m] );
		others[m]->entryPoints.empty();

		/* Merge the lists. This will move all the states from other into
		 * this. No states will be deleted. */
		stateList.append( others[m]->stateList );
		assert( others[m]->misfitList.length() == 0 );

		/* Move the final set data from other into this. */
		finStateSet.insert( others[m]->finStateSet );
		others[m]->finStateSet.empty();

		/* Since other's list is empty, we can delete the fsm without
		 * affecting any states. */
		delete others[m];
	}
}

/* Used near the end of an fsm construction. Any labels that are still around
 * are referenced only by gotos and calls and they need to be made into
 * deterministic entry points. */
void FsmAp::deterministicEntry()
{
	/* States may loose their entry points, turn on misfit accounting. */
	setMisfitAccounting( true );

	/* Get a copy of the entry map then clear all the entry points. As we
	 * iterate the old entry map finding duplicates we will add the entry
	 * points for the new states that we create. */
	EntryMap prevEntry = entryPoints;
	unsetAllEntryPoints();

	for ( int enId = 0; enId < prevEntry.length(); ) {
		/* Count the number of states on this entry key. */
		int highId = enId;
		while ( highId < prevEntry.length() && prevEntry[enId].key == prevEntry[highId].key )
			highId += 1;

		int numIds = highId - enId;
		if ( numIds == 1 ) {
			/* Only a single entry point, just set the entry. */
			setEntry( prevEntry[enId].key, prevEntry[enId].value );
		}
		else {
			/* Multiple entry points, need to create a new state and merge in
			 * all the targets of entry points. */
			StateAp *newEntry = addState();
			for ( int en = enId; en < highId; en++ )
				mergeStates( newEntry, prevEntry[en].value );

			/* Add the new state as the single entry point. */
			setEntry( prevEntry[enId].key, newEntry );
		}

		enId += numIds;
	}

	/* The old start state may be unreachable. Remove the misfits and turn off
	 * misfit accounting. */
	removeMisfits();
	setMisfitAccounting( false );
}

/* Unset any final states that are no longer to be final due to final bits. */
void FsmAp::unsetKilledFinals()
{
	/* Duplicate the final state set before we begin modifying it. */
	StateSet fin( finStateSet );

	for ( int s = 0; s < fin.length(); s++ ) {
		/* Check for killing bit. */
		StateAp *state = fin.data[s];
		if ( state->stateBits & STB_GRAPH1 ) {
			/* One final state is a killer, set to non-final. */
			unsetFinState( state );
		}

		/* Clear all killing bits. Non final states should never have had those
		 * state bits set in the first place. */
		state->stateBits &= ~STB_GRAPH1;
	}
}

/* Unset any final states that are no longer to be final due to final bits. */
void FsmAp::unsetIncompleteFinals()
{
	/* Duplicate the final state set before we begin modifying it. */
	StateSet fin( finStateSet );

	for ( int s = 0; s < fin.length(); s++ ) {
		/* Check for one set but not the other. */
		StateAp *state = fin.data[s];
		if ( state->stateBits & STB_BOTH && 
				(state->stateBits & STB_BOTH) != STB_BOTH )
		{
			/* One state wants the other but it is not there. */
			unsetFinState( state );
		}

		/* Clear wanting bits. Non final states should never have had those
		 * state bits set in the first place. */
		state->stateBits &= ~STB_BOTH;
	}
}

/* Kleene star operator. Makes this machine the kleene star of itself. Any
 * transitions made going out of the machine and back into itself will be
 * notified that they are leaving transitions by having the leavingFromState
 * callback invoked. */
FsmRes FsmAp::starOp( FsmAp *fsm )
{
	/* The start func orders need to be shifted before doing the star. */
	fsm->ctx->curActionOrd += fsm->shiftStartActionOrder( fsm->ctx->curActionOrd );

	/* Turn on misfit accounting to possibly catch the old start state. */
	fsm->setMisfitAccounting( true );

	/* Create the new new start state. It will be set final after the merging
	 * of the final states with the start state is complete. */
	StateAp *prevStartState = fsm->startState;
	fsm->unsetStartState();
	fsm->setStartState( fsm->addState() );

	/* Merge the new start state with the old one to isolate it. */
	fsm->mergeStates( fsm->startState, prevStartState );

	if ( !fsm->startState->isFinState() ) {
		/* Common case, safe to merge. */
		for ( StateSet::Iter st = fsm->finStateSet; st.lte(); st++ )
			fsm->mergeStatesLeaving( *st, fsm->startState );
	}
	else {
		/* Merge the start state into all final states. Except the start state on
		 * the first pass. If the start state is set final we will be doubling up
		 * its transitions, which will get transfered to any final states that
		 * follow it in the final state set. This will be determined by the order
		 * of items in the final state set. To prevent this we just merge with the
		 * start on a second pass. */
		StateSet origFin = fsm->finStateSet;
		for ( StateSet::Iter st = origFin; st.lte(); st++ ) {
			if ( *st != fsm->startState )
				fsm->mergeStatesLeaving( *st, fsm->startState );
		}

		/* Now it is safe to merge the start state with itself (provided it
		 * is set final). */
		if ( fsm->startState->isFinState() )
			fsm->mergeStatesLeaving( fsm->startState, fsm->startState );
	}

	/* Now ensure the new start state is a final state. */
	fsm->setFinState( fsm->startState );

	/* Fill in any states that were newed up as combinations of others. */
	FsmRes res = FsmAp::fillInStates( fsm );
	if ( !res.success() )
		return res;

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	fsm->afterOpMinimize();

	return res;
}

FsmRes FsmAp::plusOp( FsmAp *fsm )
{
	/* Need a duplicate for the star end. */
	FsmAp *factorDup = new FsmAp( *fsm );

	/* Star the duplicate. */
	FsmRes res1 = FsmAp::starOp( factorDup );
	if ( !res1.success() )
		return res1;

	FsmRes res2 = FsmAp::concatOp( fsm, res1.fsm );
	if ( !res2.success() )
		return res2;

	return res2;
}

FsmRes FsmAp::questionOp( FsmAp *fsm )
{
	/* Make the null fsm. */
	FsmAp *nu = FsmAp::lambdaFsm( fsm->ctx );

	/* Perform the question operator. */
	FsmRes res = FsmAp::unionOp( fsm, nu );
	if ( !res.success() )
		return res;

	return res;
}

FsmRes FsmAp::exactRepeatOp( FsmAp *fsm, int times )
{
	/* Zero repetitions produces lambda machine. */
	if ( times == 0 ) {
		FsmCtx *fsmCtx = fsm->ctx;
		delete fsm;
		return FsmRes( FsmRes::Fsm(), FsmAp::lambdaFsm( fsmCtx ) );
	}

	/* The start func orders need to be shifted before doing the
	 * repetition. */
	fsm->ctx->curActionOrd += fsm->shiftStartActionOrder( fsm->ctx->curActionOrd );

	/* A repeat of one does absolutely nothing. */
	if ( times == 1 )
		return FsmRes( FsmRes::Fsm(), fsm );

	/* Make a machine to make copies from. */
	FsmAp *copyFrom = new FsmAp( *fsm );

	/* Concatentate duplicates onto the end up until before the last. */
	for ( int i = 1; i < times-1; i++ ) {
		FsmAp *dup = new FsmAp( *copyFrom );
		FsmRes res = concatOp( fsm, dup );
		if ( !res.success() ) {
			delete copyFrom;
			return res;
		}
	}

	/* Now use the copyFrom on the end. */
	FsmRes res = concatOp( fsm, copyFrom );
	if ( !res.success())
		return res;

	res.fsm->afterOpMinimize();

	return res;
}

FsmRes FsmAp::maxRepeatOp( FsmAp *fsm, int times )
{
	/* Zero repetitions produces lambda machine. */
	if ( times == 0 ) {
		FsmCtx *fsmCtx = fsm->ctx;
		delete fsm;
		return FsmRes( FsmRes::Fsm(), FsmAp::lambdaFsm( fsmCtx ) );
	}

	fsm->ctx->curActionOrd += fsm->shiftStartActionOrder( fsm->ctx->curActionOrd );

	/* A repeat of one optional merely allows zero string. */
	if ( times == 1 ) {
		isolateStartState( fsm );
		fsm->setFinState( fsm->startState );
		return FsmRes( FsmRes::Fsm(), fsm );
	}

	/* Make a machine to make copies from. */
	FsmAp *copyFrom = new FsmAp( *fsm );

	/* The state set used in the from end of the concatentation. Starts with
	 * the initial final state set, then after each concatenation, gets set to
	 * the the final states that come from the the duplicate. */
	StateSet lastFinSet( fsm->finStateSet );

	/* Set the initial state to zero to allow zero copies. */
	isolateStartState( fsm );
	fsm->setFinState( fsm->startState );

	/* Concatentate duplicates onto the end up until before the last. */
	for ( int i = 1; i < times-1; i++ ) {
		/* Make a duplicate for concating and set the fin bits to graph 2 so we
		 * can pick out it's final states after the optional style concat. */
		FsmAp *dup = new FsmAp( *copyFrom );
		dup->setFinBits( STB_GRAPH2 );
		FsmRes res = concatOp( fsm, dup, false, &lastFinSet, true );
		if ( !res.success() ) {
			delete copyFrom;
			return res;
		}

		/* Clear the last final state set and make the new one by taking only
		 * the final states that come from graph 2.*/
		lastFinSet.empty();
		for ( int i = 0; i < fsm->finStateSet.length(); i++ ) {
			/* If the state came from graph 2, add it to the last set and clear
			 * the bits. */
			StateAp *fs = fsm->finStateSet[i];
			if ( fs->stateBits & STB_GRAPH2 ) {
				lastFinSet.insert( fs );
				fs->stateBits &= ~STB_GRAPH2;
			}
		}
	}

	/* Now use the copyFrom on the end, no bits set, no bits to clear. */
	FsmRes res = concatOp( fsm, copyFrom, false, &lastFinSet, true );
	if ( !res.success() )
		return res;

	res.fsm->afterOpMinimize();

	return res;
}

FsmRes FsmAp::minRepeatOp( FsmAp *fsm, int times )
{
	if ( times == 0 ) {
		/* Acts just like a star op on the machine to return. */
		return FsmAp::starOp( fsm );
	}
	else {
		/* Take a duplicate for the star below. */
		FsmAp *dup = new FsmAp( *fsm );

		/* Do repetition on the first half. */
		FsmRes exact = FsmAp::exactRepeatOp( fsm, times );
		if ( !exact.success() ) {
			delete dup;
			return exact;
		}

		/* Star the duplicate. */
		FsmRes star = FsmAp::starOp( dup );
		if ( !star.success() ) {
			delete exact.fsm;
			return star;
		}

		/* Tack on the kleene star. */
		return FsmAp::concatOp( exact.fsm, star.fsm );
	}
}

FsmRes FsmAp::rangeRepeatOp( FsmAp *fsm, int lowerRep, int upperRep )
{
	if ( lowerRep == 0 && upperRep == 0 ) {
		FsmCtx *fsmCtx = fsm->ctx;
		delete fsm;
		return FsmRes( FsmRes::Fsm(), FsmAp::lambdaFsm( fsmCtx ) );
	}
	else if ( lowerRep == 0 ) {
		/* Just doing max repetition. Already guarded against n == 0. */
		return FsmAp::maxRepeatOp( fsm, upperRep );
	}
	else if ( lowerRep == upperRep ) {
		/* Just doing exact repetition. Already guarded against n == 0. */
		return FsmAp::exactRepeatOp( fsm, lowerRep );
	}
	else {
		/* This is the case that 0 < lowerRep < upperRep. Take a
		 * duplicate for the optional repeat. */
		FsmAp *dup = new FsmAp( *fsm );

		/* Do repetition on the first half. */
		FsmRes exact = FsmAp::exactRepeatOp( fsm, lowerRep );
		if ( !exact.success() ) {
			delete dup;
			return exact;
		}

		/* Do optional repetition on the second half. */
		FsmRes optional = FsmAp::maxRepeatOp( dup, upperRep - lowerRep );
		if ( !optional.success() ) {
			delete exact.fsm;
			return optional;
		}

		/* Concat two halves. */
		return FsmAp::concatOp( exact.fsm, optional.fsm );
	}
}

/* Concatenates other to the end of this machine. Other is deleted.  Any
 * transitions made leaving this machine and entering into other are notified
 * that they are leaving transitions by having the leavingFromState callback
 * invoked. Supports specifying the fromStates (istead of first final state
 * set). This is useful for a max-repeat schenario, where from states are not
 * all of first's final states. Also supports treating the concatentation as
 * optional, which leaves the final states of the first machine as final. */
FsmRes FsmAp::concatOp( FsmAp *fsm, FsmAp *other, bool lastInSeq, StateSet *fromStates, bool optional )
{
	for ( PriorTable::Iter g = other->startState->guardedInTable; g.lte(); g++ ) {
		fsm->allTransPrior( 0, g->desc );
		other->allTransPrior( 0, g->desc->other );
	}

	/* Assert same signedness and return graph concatenation op. */
	assert( fsm->ctx == other->ctx );

	/* For the merging process. */
	StateSet finStateSetCopy, startStateSet;

	/* Turn on misfit accounting for both graphs. */
	fsm->setMisfitAccounting( true );
	other->setMisfitAccounting( true );

	/* Get the other's start state. */
	StateAp *otherStartState = other->startState;

	/* Unset other's start state before bringing in the entry points. */
	other->unsetStartState();

	/* Bring in the rest of other's entry points. */
	fsm->copyInEntryPoints( other );
	other->entryPoints.empty();

	/* Bring in other's states into our state lists. */
	fsm->stateList.append( other->stateList );
	fsm->misfitList.append( other->misfitList );

	/* If from states is not set, then get a copy of our final state set before
	 * we clobber it and use it instead. */
	if ( fromStates == 0 ) {
		finStateSetCopy = fsm->finStateSet;
		fromStates = &finStateSetCopy;
	}

	/* Unset all of our final states and get the final states from other. */
	if ( !optional )
		fsm->unsetAllFinStates();
	fsm->finStateSet.insert( other->finStateSet );

	/* Since other's lists are empty, we can delete the fsm without
	 * affecting any states. */
	delete other;

	/* Merge our former final states with the start state of other. */
	for ( int i = 0; i < fromStates->length(); i++ ) {
		StateAp *state = fromStates->data[i];

		/* Merge the former final state with other's start state. */
		fsm->mergeStatesLeaving( state, otherStartState );

		/* If the former final state was not reset final then we must clear
		 * the state's out trans data. If it got reset final then it gets to
		 * keep its out trans data. This must be done before fillInStates gets
		 * called to prevent the data from being sourced. */
		if ( ! state->isFinState() )
			fsm->clearOutData( state );
	}

	/* Fill in any new states made from merging. */
	FsmRes res = fillInStates( fsm );
	if ( !res.success() )
		return res;

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	res.fsm->afterOpMinimize( lastInSeq );

	return res;
}

FsmRes FsmAp::rightStartConcatOp( FsmAp *fsm, FsmAp *other, bool lastInSeq )
{
	PriorDesc *priorDesc0 = fsm->ctx->allocPriorDesc();
	PriorDesc *priorDesc1 = fsm->ctx->allocPriorDesc();

	/* Set up the priority descriptors. The left machine gets the
	 * lower priority where as the right get the higher start priority. */
	priorDesc0->key = fsm->ctx->nextPriorKey++;
	priorDesc0->priority = 0;
	fsm->allTransPrior( fsm->ctx->curPriorOrd++, priorDesc0 );

	/* The start transitions of the right machine gets the higher
	 * priority. Use the same unique key. */
	priorDesc1->key = priorDesc0->key;
	priorDesc1->priority = 1;
	other->startFsmPrior( fsm->ctx->curPriorOrd++, priorDesc1 );

	return concatOp( fsm, other, lastInSeq );
}

/* Returns union of fsm and other. Other is deleted. */
FsmRes FsmAp::unionOp( FsmAp *fsm, FsmAp *other, bool lastInSeq )
{
	assert( fsm->ctx == other->ctx );

	fsm->ctx->unionOp = true;

	fsm->setFinBits( STB_GRAPH1 );
	other->setFinBits( STB_GRAPH2 );

	/* Turn on misfit accounting for both graphs. */
	fsm->setMisfitAccounting( true );
	other->setMisfitAccounting( true );

	/* Call Worker routine. */
	FsmRes res = doUnion( fsm, other );
	if ( !res.success() )
		return res;

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	fsm->ctx->unionOp = false;
	fsm->unsetFinBits( STB_BOTH );

	fsm->afterOpMinimize( lastInSeq );

	return res;
}

/* Intersects other with this machine. Other is deleted. */
FsmRes FsmAp::intersectOp( FsmAp *fsm, FsmAp *other, bool lastInSeq )
{
	assert( fsm->ctx == other->ctx );

	/* Turn on misfit accounting for both graphs. */
	fsm->setMisfitAccounting( true );
	other->setMisfitAccounting( true );

	/* Set the fin bits on this and other to want each other. */
	fsm->setFinBits( STB_GRAPH1 );
	other->setFinBits( STB_GRAPH2 );

	/* Call worker Or routine. */
	FsmRes res = doUnion( fsm, other );
	if ( !res.success() )
		return res;

	/* Unset any final states that are no longer to 
	 * be final due to final bits. */
	fsm->unsetIncompleteFinals();

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	/* Remove states that have no path to a final state. */
	fsm->removeDeadEndStates();

	fsm->afterOpMinimize( lastInSeq );

	return res;
}

/* Set subtracts other machine from this machine. Other is deleted. */
FsmRes FsmAp::subtractOp( FsmAp *fsm, FsmAp *other, bool lastInSeq )
{
	assert( fsm->ctx == other->ctx );

	/* Turn on misfit accounting for both graphs. */
	fsm->setMisfitAccounting( true );
	other->setMisfitAccounting( true );

	/* Set the fin bits of other to be killers. */
	other->setFinBits( STB_GRAPH1 );

	/* Call worker Or routine. */
	FsmRes res = doUnion( fsm, other );
	if ( !res.success() )
		return res;

	/* Unset any final states that are no longer to 
	 * be final due to final bits. */
	fsm->unsetKilledFinals();

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	/* Remove states that have no path to a final state. */
	fsm->removeDeadEndStates();

	fsm->afterOpMinimize( lastInSeq );

	return res;
}

FsmRes FsmAp::epsilonOp( FsmAp *fsm )
{
	fsm->setMisfitAccounting( true );

	for ( StateList::Iter st = fsm->stateList; st.lte(); st++ )
		st->owningGraph = 0;

	/* Perform merges. */
	fsm->resolveEpsilonTrans();

	/* Epsilons can caused merges which leave behind unreachable states. */
	FsmRes res = FsmAp::fillInStates( fsm );
	if ( !res.success() )
		return res;

	/* Remove the misfits and turn off misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	return res;
}

/* Make a new maching by joining together a bunch of machines without making
 * any transitions between them. A negative finalId results in there being no
 * final id. */
FsmRes FsmAp::joinOp( FsmAp *fsm, int startId, int finalId, FsmAp **others, int numOthers )
{
	for ( int m = 0; m < numOthers; m++ ) {
		assert( fsm->ctx == others[m]->ctx );
	}

	/* Set the owning machines. Start at one. Zero is reserved for the start
	 * and final states. */
	for ( StateList::Iter st = fsm->stateList; st.lte(); st++ )
		st->owningGraph = 1;
	for ( int m = 0; m < numOthers; m++ ) {
		for ( StateList::Iter st = others[m]->stateList; st.lte(); st++ )
			st->owningGraph = 2+m;
	}

	/* All machines loose start state status. */
	fsm->unsetStartState();
	for ( int m = 0; m < numOthers; m++ )
		others[m]->unsetStartState();
	
	/* Bring the other machines into this. */
	for ( int m = 0; m < numOthers; m++ ) {
		/* Bring in the rest of other's entry points. */
		fsm->copyInEntryPoints( others[m] );
		others[m]->entryPoints.empty();

		/* Merge the lists. This will move all the states from other into
		 * this. No states will be deleted. */
		fsm->stateList.append( others[m]->stateList );
		assert( others[m]->misfitList.length() == 0 );

		/* Move the final set data from other into this. */
		fsm->finStateSet.insert( others[m]->finStateSet );
		others[m]->finStateSet.empty();

		/* Since other's list is empty, we can delete the fsm without
		 * affecting any states. */
		delete others[m];
	}

	/* Look up the start entry point. */
	EntryMapEl *enLow = 0, *enHigh = 0;
	bool findRes = fsm->entryPoints.findMulti( startId, enLow, enHigh );
	if ( ! findRes ) {
		/* No start state. Set a default one and proceed with the join. Note
		 * that the result of the join will be a very uninteresting machine. */
		fsm->setStartState( fsm->addState() );
	}
	else {
		/* There is at least one start state, create a state that will become
		 * the new start state. */
		StateAp *newStart = fsm->addState();
		fsm->setStartState( newStart );

		/* The start state is in an owning machine class all it's own. */
		newStart->owningGraph = 0;

		/* Create the set of states to merge from. */
		StateSet stateSet;
		for ( EntryMapEl *en = enLow; en <= enHigh; en++ )
			stateSet.insert( en->value );

		/* Merge in the set of start states into the new start state. */
		fsm->mergeStateList( newStart, stateSet.data, stateSet.length() );
	}

	/* Take a copy of the final state set, before unsetting them all. This
	 * will allow us to call clearOutData on the states that don't get
	 * final state status back back. */
	StateSet finStateSetCopy = fsm->finStateSet;

	/* Now all final states are unset. */
	fsm->unsetAllFinStates();

	if ( finalId >= 0 ) {
		/* Create the implicit final state. */
		StateAp *finState = fsm->addState();
		fsm->setFinState( finState );

		/* Assign an entry into the final state on the final state entry id. Note
		 * that there may already be an entry on this id. That's ok. Also set the
		 * final state owning machine id. It's in a class all it's own. */
		fsm->setEntry( finalId, finState );
		finState->owningGraph = 0;
	}

	/* Hand over to workers for resolving epsilon trans. This will merge states
	 * with the targets of their epsilon transitions. */
	fsm->resolveEpsilonTrans();

	/* Invoke the relinquish final callback on any states that did not get
	 * final state status back. */
	for ( StateSet::Iter st = finStateSetCopy; st.lte(); st++ ) {
		if ( !((*st)->stateBits & STB_ISFINAL) )
			fsm->clearOutData( *st );
	}

	/* Fill in any new states made from merging. */
	FsmRes res = FsmAp::fillInStates( fsm );
	if ( !res.success() )
		return res;

	/* Joining can be messy. Instead of having misfit accounting on (which is
	 * tricky here) do a full cleaning. */
	fsm->removeUnreachableStates();

	return res;
}

/* Ensure that the start state is free of entry points (aside from the fact
 * that it is the start state). If the start state has entry points then Make a
 * new start state by merging with the old one. Useful before modifying start
 * transitions. If the existing start state has any entry points other than the
 * start state entry then modifying its transitions changes more than the start
 * transitions. So isolate the start state by separating it out such that it
 * only has start stateness as it's entry point. */
FsmRes FsmAp::isolateStartState( FsmAp *fsm )
{
	/* Do nothing if the start state is already isolated. */
	if ( fsm->isStartStateIsolated() )
		return FsmRes( FsmRes::Fsm(), fsm );

	/* Turn on misfit accounting to possibly catch the old start state. */
	fsm->setMisfitAccounting( true );

	/* This will be the new start state. The existing start
	 * state is merged with it. */
	StateAp *prevStartState = fsm->startState;
	fsm->unsetStartState();
	fsm->setStartState( fsm->addState() );

	/* Merge the new start state with the old one to isolate it. */
	fsm->mergeStates( fsm->startState, prevStartState );

	/* Stfil and stateDict will be empty because the merging of the old start
	 * state into the new one will not have any conflicting transitions. */
	assert( fsm->stateDict.treeSize == 0 );
	assert( fsm->nfaList.length() == 0 );

	/* The old start state may be unreachable. Remove the misfits and turn off
	 * misfit accounting. */
	fsm->removeMisfits();
	fsm->setMisfitAccounting( false );

	return FsmRes( FsmRes::Fsm(), fsm );
}

StateAp *FsmAp::dupStartState()
{
	StateAp *dup = addState();
	mergeStates( dup, startState );
	return dup;
}

/* A state merge which represents the drawing in of leaving transitions.  If
 * there is any out data then we duplicate the source state, transfer the out
 * data, then merge in the state. The new state will be reaped because it will
 * not be given any in transitions. */
void FsmAp::mergeStatesLeaving( StateAp *destState, StateAp *srcState )
{
	if ( !hasOutData( destState ) ) {
		/* Perform the merge, indicating we are leaving, which will affect how
		 * out conds are merged. */
		mergeStates( destState, srcState, true );
	}
	else {
		/* Dup the source state. */
		StateAp *ssMutable = addState();
		mergeStates( ssMutable, srcState );

		/* Do out data transfer (and out condition embedding). */
		transferOutData( ssMutable, destState );

		if ( destState->outCondSpace != 0 ) {

			doEmbedCondition( ssMutable, destState->outCondSpace->condSet,
					destState->outCondKeys );
		}

		/* Now we merge with dest, setting leaving = true. This dictates how
		 * out conditions should be merged. */
		mergeStates( destState, ssMutable, true );
	}
}

void FsmAp::checkEpsilonRegularInteraction( const PriorTable &t1, const PriorTable &t2 )
{
	for ( PriorTable::Iter pd1 = t1; pd1.lte(); pd1++ ) {
		for ( PriorTable::Iter pd2 = t2; pd2.lte(); pd2++ ) {
			/* Looking for unequal guarded priorities with the same key. */
			if ( pd1->desc->key == pd2->desc->key ) {
				if ( pd1->desc->priority < pd2->desc->priority || 
						pd1->desc->priority > pd2->desc->priority )
				{
					if ( ctx->checkPriorInteraction && pd1->desc->guarded ) {
						if ( ! priorInteraction ) {
							priorInteraction = true;
							guardId = pd1->desc->guardId;
						}
					}
				}
			}
		}
	}
}

void FsmAp::mergeStateProperties( StateAp *destState, StateAp *srcState )
{
	/* Draw in any properties of srcState into destState. */
	if ( srcState == destState ) {
		/* Duplicate the list to protect against write to source. The
		 * priorities sets are not copied in because that would have no
		 * effect. */
		destState->epsilonTrans.append( EpsilonTrans( srcState->epsilonTrans ) );

		/* Get all actions, duplicating to protect against write to source. */
		destState->toStateActionTable.setActions( 
				ActionTable( srcState->toStateActionTable ) );
		destState->fromStateActionTable.setActions( 
				ActionTable( srcState->fromStateActionTable ) );
		destState->outActionTable.setActions( ActionTable( srcState->outActionTable ) );
		destState->errActionTable.setActions( ErrActionTable( srcState->errActionTable ) );
		destState->eofActionTable.setActions( ActionTable( srcState->eofActionTable ) );

		/* Not touching guarded-in table or out conditions. Probably should
		 * leave some of the above alone as well. */
	}
	else {
		/* Get the epsilons, out priorities. */
		destState->epsilonTrans.append( srcState->epsilonTrans );
		destState->outPriorTable.setPriors( srcState->outPriorTable );

		/* Get all actions. */
		destState->toStateActionTable.setActions( srcState->toStateActionTable );
		destState->fromStateActionTable.setActions( srcState->fromStateActionTable );
		destState->outActionTable.setActions( srcState->outActionTable );
		destState->errActionTable.setActions( srcState->errActionTable );
		destState->eofActionTable.setActions( srcState->eofActionTable );
		destState->lmNfaParts.insert( srcState->lmNfaParts );
		destState->guardedInTable.setPriors( srcState->guardedInTable );
	}
}

void FsmAp::mergeStateBits( StateAp *destState, StateAp *srcState )
{
	/* Get bits and final state status. Note in the above code we depend on the
	 * original final state status being present. */
	destState->stateBits |= ( srcState->stateBits & ~STB_ISFINAL );
	if ( srcState->isFinState() )
		setFinState( destState );
}

void FsmAp::mergeNfaTransitions( StateAp *destState, StateAp *srcState )
{
	/* Copy in any NFA transitions. */
	if ( srcState->nfaOut != 0 ) {
		if ( destState->nfaOut == 0 )
			destState->nfaOut = new NfaTransList;

		for ( NfaTransList::Iter nt = *srcState->nfaOut; nt.lte(); nt++ ) {
			NfaTrans *trans = new NfaTrans(
					nt->pushTable, nt->restoreTable,
					nt->popFrom, nt->popCondSpace, nt->popCondKeys,
					nt->popAction, nt->popTest, nt->order );

			destState->nfaOut->append( trans );
			attachToNfa( destState, nt->toState, trans );
		}
	}
}

void FsmAp::checkPriorInteractions( StateAp *destState, StateAp *srcState )
{
	/* Run a check on priority interactions between epsilon transitions and
	 * regular transitions. This can't be used to affect machine construction,
	 * only to check for priority guards. */
	if ( destState->nfaOut != 0 ) {
		for ( NfaTransList::Iter nt = *destState->nfaOut; nt.lte(); nt++ ) {
			for ( TransList::Iter trans = destState->outList; trans.lte(); trans++ ) {
				if ( trans->plain() ) {
					checkEpsilonRegularInteraction(
							trans->tdap()->priorTable, nt->priorTable );
				}
				else {
					for ( CondList::Iter cond = trans->tcap()->condList;
							cond.lte(); cond++ )
					{
						checkEpsilonRegularInteraction(
								cond->priorTable, nt->priorTable );

					}
				}
			}
		}
	}
}

void FsmAp::mergeStates( StateAp *destState, StateAp *srcState, bool leaving )
{
	/* Transitions. */
	outTransCopy( destState, srcState->outList.head );

	/* Properties such as out data, to/from actions. */
	mergeStateProperties( destState, srcState );

	/* Merge out conditions, depends on the operation (leaving or not). */
	mergeOutConds( destState, srcState, leaving );

	/* State bits, including final state stats. Out conds depnds on this
	 * happening after. */
	mergeStateBits( destState, srcState );

	/* Draw in the NFA transitions. */
	mergeNfaTransitions( destState, srcState );

	/* Hacked in check for priority interactions, allowing detection of some
	 * bad situations. */
	checkPriorInteractions( destState, srcState );
}

void FsmAp::mergeStateList( StateAp *destState, 
		StateAp **srcStates, int numSrc )
{
	for ( int s = 0; s < numSrc; s++ )
		mergeStates( destState, srcStates[s] );
}

void FsmAp::cleanAbortedFill( StateAp *state )
{
	/* Iterate the out transitions, deleting them. */
	for ( TransList::Iter n, t = state->outList; t.lte(); ) {
		n = t.next();
		if ( t->plain() )
			delete t->tdap();
		else
			delete t->tcap();
		t = n;
	}

	state->outList.abandon();

	if ( state->nfaIn != 0 ) {
		delete state->nfaIn;
		state->nfaIn = 0;
	}

	if ( state->nfaOut != 0 ) {
		state->nfaOut->empty();
		delete state->nfaOut;
		state->nfaOut = 0;
	}
}
			
void FsmAp::cleanAbortedFill()
{
	while ( nfaList.length() > 0 ) {
		StateAp *state = nfaList.head;

		StateSet *stateSet = &state->stateDictEl->stateSet;
		//mergeStateList( state, stateSet->data, stateSet->length() );

		for ( StateSet::Iter s = *stateSet; s.lte(); s++ )
			detachStateDict( state, *s );

		nfaList.detach( state );
	}

	/* Disassociated state dict elements from states. */
	for ( StateDict::Iter sdi = stateDict; sdi.lte(); sdi++ )
		sdi->targState->stateDictEl = 0;

	/* Delete all the state dict elements. */
	stateDict.empty();

	/* Delete all the transitions. */
	for ( StateList::Iter state = stateList; state.lte(); state++ )
		cleanAbortedFill( state );

	/* Delete all the states. */
	stateList.empty();

	/* Delete all the transitions. */
	for ( StateList::Iter state = misfitList; state.lte(); state++ )
		cleanAbortedFill( state );

	/* Delete all the states. */
	misfitList.empty();
}

bool FsmAp::overStateLimit()
{
	if ( ctx->stateLimit > FsmCtx::STATE_UNLIMITED ) {
		long states = misfitList.length() + stateList.length();
		if ( states > ctx->stateLimit )
			return true;
	}
	return false;
}

bool FsmAp::fillAbort( FsmRes &res, FsmAp *fsm )
{
	if ( fsm->priorInteraction ) {
		fsm->cleanAbortedFill();
		int guardId = fsm->guardId;
		delete fsm;
		res = FsmRes( FsmRes::PriorInteraction(), guardId );
		return true;
	}

	if ( fsm->overStateLimit() ) {
		fsm->cleanAbortedFill();
		delete fsm;
		res = FsmRes( FsmRes::TooManyStates() );
		return true;
	}

	return false;
}

FsmRes FsmAp::fillInStates( FsmAp *fsm )
{
	/* Used as return value on success. Filled in with error on abort. */
	FsmRes res( FsmRes::Fsm(), fsm );

	/* Merge any states that are awaiting merging. This will likey cause other
	 * states to be added to the NFA list. */
	while ( true ) {
		if ( fillAbort( res, fsm ) )
			return res;

		if ( fsm->nfaList.length() == 0 )
			break;

		StateAp *state = fsm->nfaList.head;

		StateSet *stateSet = &state->stateDictEl->stateSet;
		fsm->mergeStateList( state, stateSet->data, stateSet->length() );

		for ( StateSet::Iter s = *stateSet; s.lte(); s++ )
			fsm->detachStateDict( state, *s );

		fsm->nfaList.detach( state );
	}

	/* The NFA list is empty at this point. There are no state sets we need to
	 * preserve. */

	/* Disassociated state dict elements from states. */
	for ( StateDict::Iter sdi = fsm->stateDict; sdi.lte(); sdi++ )
		sdi->targState->stateDictEl = 0;

	/* Delete all the state dict elements. */
	fsm->stateDict.empty();

	return res;
}

/* Check if a machine defines a single character. This is useful in validating
 * ranges and machines to export. */
bool FsmAp::checkSingleCharMachine()
{
	/* Must have two states. */
	if ( stateList.length() != 2 )
		return false;
	/* The start state cannot be final. */
	if ( startState->isFinState() )
		return false;
	/* There should be only one final state. */
	if ( finStateSet.length() != 1 )
		return false;
	/* The final state cannot have any transitions out. */
	if ( finStateSet[0]->outList.length() != 0 )
		return false;
	/* The start state should have only one transition out. */
	if ( startState->outList.length() != 1 )
		return false;
	/* The singe transition out of the start state should not be a range. */
	TransAp *startTrans = startState->outList.head;
	if ( ctx->keyOps->ne( startTrans->lowKey, startTrans->highKey ) )
		return false;
	return true;
}

FsmRes FsmAp::condCostFromState( FsmAp *fsm, StateAp *state, long depth )
{
	/* Nothing to do if the state is already on the list. */
	if ( state->stateBits & STB_ONLIST )
		return FsmRes( FsmRes::Fsm(), fsm );

	if ( depth > fsm->ctx->condsCheckDepth )
		return FsmRes( FsmRes::Fsm(), fsm );

	/* Doing depth first, put state on the list. */
	state->stateBits |= STB_ONLIST;

	/* Recurse on everything ranges. */
	for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) {
		if ( trans->plain() ) {
			if ( trans->tdap()->toState != 0 ) {
				FsmRes res = condCostFromState( fsm, trans->tdap()->toState, depth + 1 );
				if ( !res.success() )
					return res;
			}
		}
		else {
			for ( CondSet::Iter csi = trans->condSpace->condSet; csi.lte(); csi++ ) {
				if ( (*csi)->costMark )
					return FsmRes( FsmRes::CondCostTooHigh(), (*csi)->costId );
			}
			
			for ( CondList::Iter cond = trans->tcap()->condList; cond.lte(); cond++ ) {
				if ( cond->toState != 0 ) {
					FsmRes res = condCostFromState( fsm, cond->toState, depth + 1 );
					if ( !res.success() )
						return res;
				}
			}
		}
	}

	if ( state->nfaOut != 0 ) {
		for ( NfaTransList::Iter n = *state->nfaOut; n.lte(); n++ ) {
			/* We do not increment depth here since this is an epsilon transition. */
			FsmRes res = condCostFromState( fsm, n->toState, depth );
			if ( !res.success() )
				return res;
		}
	}

	for ( ActionTable::Iter a = state->fromStateActionTable; a.lte(); a++ ) {
		if ( a->value->costMark )
			return FsmRes( FsmRes::CondCostTooHigh(), a->value->costId );
	}

	return FsmRes( FsmRes::Fsm(), fsm );
}


/* Returns either success (using supplied fsm), or some error condition. */
FsmRes FsmAp::condCostSearch( FsmAp *fsm )
{
	/* Init on state list flags. */
	for ( StateList::Iter st = fsm->stateList; st.lte(); st++ )
		st->stateBits &= ~STB_ONLIST;

	FsmRes res = condCostFromState( fsm, fsm->startState, 1 );
	if ( !res.success() )
		delete fsm;
	return res;
}

void FsmAp::condCost( Action *action, long repId )
{
	action->costMark = true;
	action->costId = repId;
}

/*
 * This algorithm assigns a price to each state visit, then adds that to a
 * running total. Note that we do not guard against multiple visits to a state,
 * since we are estimating runtime cost.
 *
 * We rely on a character histogram and are looking for a probability of being
 * in any given state, given that histogram, simple and very effective.
 */
void FsmAp::breadthFromState( double &total, int &minDepth, double *histogram,
		FsmAp *fsm, StateAp *state, long depth, int maxDepth, double stateScore )
{
	if ( depth > maxDepth )
		return;
	
	/* Recurse on everything ranges. */
	for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) {

		/* Compute target state score. */
		double span = 0;
		for ( int i = trans->lowKey.getVal(); i <= trans->highKey.getVal(); i++ )
			span += histogram[i];

		double targetStateScore = stateScore * ( span );

		/* Add to the level. */
		total += targetStateScore;

		if ( trans->plain() ) {
			if ( trans->tdap()->toState != 0 ) {
				if ( trans->tdap()->toState->isFinState() && ( minDepth < 0 || depth < minDepth ) )
					minDepth = depth;

				breadthFromState( total, minDepth, histogram, fsm, trans->tdap()->toState,
						depth + 1, maxDepth, targetStateScore );
			}
		}
		else {
			for ( CondList::Iter cond = trans->tcap()->condList; cond.lte(); cond++ ) {
				if ( cond->toState != 0 ) {
					if ( cond->toState->isFinState() && ( minDepth < 0 || depth < minDepth ) )
						minDepth = depth;

					breadthFromState( total, minDepth, histogram, fsm, cond->toState,
							depth + 1, maxDepth, targetStateScore );
				}
			}
		}
	}

	if ( state->nfaOut != 0 ) {
		for ( NfaTransList::Iter n = *state->nfaOut; n.lte(); n++ ) {
			if ( n->toState->isFinState() && ( minDepth < 0 || depth < minDepth ) )
				minDepth = depth;

			/* We do not increment depth here since this is an epsilon transition. */
			breadthFromState( total, minDepth, histogram, fsm, n->toState, depth, maxDepth, stateScore );
		}
	}
}

void FsmAp::breadthFromEntry( double &total, int &minDepth, double *histogram, FsmAp *fsm, StateAp *state )
{
	long depth = 1;
	int maxDepth = 5;
	double stateScore = 1.0;

	FsmAp::breadthFromState( total, minDepth, histogram, fsm, state, depth, maxDepth, stateScore );
}


void FsmAp::applyEntryPriorGuard( FsmAp *fsm, long repId )
{
	PriorDesc *priorDesc0 = fsm->ctx->allocPriorDesc();
	PriorDesc *priorDesc1 = fsm->ctx->allocPriorDesc();

	priorDesc0->key = fsm->ctx->nextPriorKey;
	priorDesc0->priority = 0;
	priorDesc0->guarded = true;
	priorDesc0->guardId = repId;
	priorDesc0->other = priorDesc1;

	priorDesc1->key = fsm->ctx->nextPriorKey;
	priorDesc1->priority = 1;
	priorDesc1->guarded = true;
	priorDesc1->guardId = repId;
	priorDesc1->other = priorDesc0;

	/* Roll over for next allocation. */
	fsm->ctx->nextPriorKey += 1;

	/* Only need to set the first. Second is referenced using 'other' field. */
	fsm->startState->guardedInTable.setPrior( 0, priorDesc0 );
}

void FsmAp::applyRepeatPriorGuard( FsmAp *fsm, long repId )
{
	PriorDesc *priorDesc2 = fsm->ctx->allocPriorDesc();
	PriorDesc *priorDesc3 = fsm->ctx->allocPriorDesc();

	priorDesc2->key = fsm->ctx->nextPriorKey;
	priorDesc2->priority = 0;
	priorDesc2->guarded = true;
	priorDesc2->guardId = repId;
	priorDesc2->other = priorDesc3;

	priorDesc3->key = fsm->ctx->nextPriorKey;
	priorDesc3->guarded = true;
	priorDesc3->priority = 1;
	priorDesc3->guardId = repId;
	priorDesc3->other = priorDesc2;

	/* Roll over for next allocation. */
	fsm->ctx->nextPriorKey += 1;

	/* Only need to set the first. Second is referenced using 'other' field. */
	fsm->startState->guardedInTable.setPrior( 0, priorDesc2 );
	
	fsm->allTransPrior( fsm->ctx->curPriorOrd++, priorDesc3 );
	fsm->leaveFsmPrior( fsm->ctx->curPriorOrd++, priorDesc2 );
}

FsmRes FsmAp::condPlus( FsmAp *fsm, long repId, Action *ini, Action *inc, Action *min, Action *max )
{
	condCost( ini, repId );
	condCost( inc, repId );
	condCost( min, repId );
	if ( max != 0 )
		condCost( max, repId );

	fsm->startFsmAction( 0, inc );

	if ( max != 0 ) {
		FsmRes res = fsm->startFsmCondition( max, true );
		if ( !res.success() )
			return res;
	}

	/* Need a duplicated for the star end. */
	FsmAp *dup = new FsmAp( *fsm );

	applyRepeatPriorGuard( dup, repId );

	/* Star the duplicate. */
	FsmRes dupStar = FsmAp::starOp( dup );
	if ( !dupStar.success() ) {
		delete fsm;
		return dupStar;
	}

	FsmRes res = FsmAp::concatOp( fsm, dupStar.fsm );
	if ( !res.success() )
		return res;

	/* End plus operation. */

	res.fsm->leaveFsmCondition( min, true );

	/* Init action. */
	res.fsm->startFromStateAction( 0,  ini );

	/* Leading priority guard. */
	applyEntryPriorGuard( res.fsm, repId );

	return res;
}

FsmRes FsmAp::condStar( FsmAp *fsm, long repId, Action *ini, Action *inc, Action *min, Action *max )
{
	condCost( ini, repId );
	condCost( inc, repId );
	condCost( min, repId );
	if ( max != 0 )
		condCost( max, repId );

	/* Increment. */
	fsm->startFsmAction( 0, inc );

	/* Max (optional). */
	if ( max != 0 ) {
		FsmRes res = fsm->startFsmCondition( max, true );
		if ( !res.success() )
			return res;
	}

	applyRepeatPriorGuard( fsm, repId );

	/* Star. */
	FsmRes res = FsmAp::starOp( fsm );
	if ( !res.success() )
		return res;

	/* Restrict leaving. */
	res.fsm->leaveFsmCondition( min, true );

	/* Init action. */
	res.fsm->startFromStateAction( 0,  ini );

	/* Leading priority guard. */
	applyEntryPriorGuard( res.fsm, repId );

	return res;
}

/* Remove duplicates of unique actions from an action table. */
void FsmAp::removeDups( ActionTable &table )
{
	/* Scan through the table looking for unique actions to 
	 * remove duplicates of. */
	for ( int i = 0; i < table.length(); i++ ) {
		/* Remove any duplicates ahead of i. */
		for ( int r = i+1; r < table.length(); ) {
			if ( table[r].value == table[i].value )
				table.vremove(r);
			else
				r += 1;
		}
	}
}

/* Remove duplicates from action lists. This operates only on transition and
 * eof action lists and so should be called once all actions have been
 * transfered to their final resting place. */
void FsmAp::removeActionDups()
{
	/* Loop all states. */
	for ( StateList::Iter state = stateList; state.lte(); state++ ) {
		/* Loop all transitions. */
		for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) {
			if ( trans->plain() )
				removeDups( trans->tdap()->actionTable );
			else {
				for ( CondList::Iter cond = trans->tcap()->condList; cond.lte(); cond++ )
					removeDups( cond->actionTable );
			}
		}
		removeDups( state->toStateActionTable );
		removeDups( state->fromStateActionTable );
		removeDups( state->eofActionTable );
	}
}