cleanup and merge common parents for array merge; fixes unit test

This commit is contained in:
Terence Parr 2012-07-22 16:39:53 -07:00
parent f1e4d85d0a
commit e18b9132d9
2 changed files with 44 additions and 8 deletions

View File

@ -380,13 +380,15 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
}
// trim merged
// trim merged if we combined a few that had same stack tops
if ( k < mergedParents.length ) { // write index < last position; trim
int p = mergedParents.length-1;
int lastSlot = mergedParents.length - 1;
int p = lastSlot; // walk backwards from last index until we find non-null parent
while ( p>=0 && mergedParents[p]==null ) { p--; }
// p is now last non-null index
if ( p < mergedParents.length-1 ) {
int n = p+1;
assert p>0; // could only happen to be <0 if two arrays with $
if ( p < lastSlot ) {
int n = p+1; // how many slots we really used in merge
if ( n == 1 ) { // for just one merged element, return singleton top
return new SingletonPredictionContext(mergedParents[0],
mergedInvokingStates[0]);
@ -401,12 +403,23 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
// TODO: if we created same array as a or b, return that instead
// TODO: make pass over all M parents; merge any equal() ones
// combineCommonParents(mergedParents);
combineCommonParents(mergedParents);
return M;
}
/** make pass over all M parents; merge any equals() ones */
protected static void combineCommonParents(PredictionContext[] parents) {
// compare pairwise
for (int i = 0; i < parents.length; i++) {
for (int j = i+1; j < parents.length; j++) {
if ( parents[i].equals(parents[j]) ) {
parents[j] = parents[i]; // use left one if same
}
}
}
}
public static String toDotString(PredictionContext context) {
if ( context==null ) return "";
StringBuilder buf = new StringBuilder();

View File

@ -393,6 +393,19 @@ public class TestGraphNodes extends TestCase {
// Array merges
@Test public void test_A$_A$_fullctx() {
ArrayPredictionContext A1 = array(PredictionContext.EMPTY);
ArrayPredictionContext A2 = array(PredictionContext.EMPTY);
PredictionContext r = PredictionContext.merge(A1, A2, fullCtx());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s1 [shape=box, label=\"[-2]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_Aab_Ac() { // a,b + c
SingletonPredictionContext a = a();
SingletonPredictionContext b = b();
@ -660,9 +673,19 @@ public class TestGraphNodes extends TestCase {
ArrayPredictionContext A2 = array(b2, d);
PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
// TODO: doesn't merge parents
String expecting =
"\n";
"digraph G {\n" +
"rankdir=LR;\n" +
" s11 [shape=box, label=\"[1, 2, 4]\"];\n" +
" s3 [label=\"7\"];\n" +
" s0 [label=\"$\"];\n" +
" s1 [label=\"6\"];\n" +
" s11->s1 [label=\"parent[0]\"];\n" +
" s11->s3 [label=\"parent[1]\"];\n" +
" s11->s1 [label=\"parent[2]\"];\n" +
" s3->s0;\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}