diff options
author | buraq <buraq@epfl.ch> | 2003-07-29 09:46:37 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2003-07-29 09:46:37 +0000 |
commit | 30e3b26eee42660fae2f732443b79ad8a182e308 (patch) | |
tree | 7579c6b84045c89919c4245764e1bb2486d9474d | |
parent | 4b03e0bc46b69145db66b8ced9d568a6271aa75c (diff) | |
download | scala-30e3b26eee42660fae2f732443b79ad8a182e308.tar.gz scala-30e3b26eee42660fae2f732443b79ad8a182e308.tar.bz2 scala-30e3b26eee42660fae2f732443b79ad8a182e308.zip |
refactoring: removed class FiniteAutom.
different types, so many casts were needed. In fact, FiniteAutom is not
used anywhere.
now fixed. fields are migrated to subclasses DetWordAutom and
NondetWordAutom, with more specific types
5 files changed, 267 insertions, 161 deletions
diff --git a/sources/scalac/transformer/matching/BerrySethi.java b/sources/scalac/transformer/matching/BerrySethi.java index 76a5c0aecd..79d67a8db3 100644 --- a/sources/scalac/transformer/matching/BerrySethi.java +++ b/sources/scalac/transformer/matching/BerrySethi.java @@ -592,8 +592,8 @@ class BerrySethi { labels, initials, finals, - (Object) deltaq, - (Object) defaultq, + deltaq, + defaultq, null/*(Object) qbinders*/); /* diff --git a/sources/scalac/transformer/matching/BindingBerrySethi.java b/sources/scalac/transformer/matching/BindingBerrySethi.java index 9763c389d0..ab1528bdb8 100644 --- a/sources/scalac/transformer/matching/BindingBerrySethi.java +++ b/sources/scalac/transformer/matching/BindingBerrySethi.java @@ -120,8 +120,8 @@ public class BindingBerrySethi extends BerrySethi { labels, initials, finals, - (Object) deltaq, - (Object) defaultq, + deltaq, + defaultq, (Object) qbinders); result.leftTrans = true; @@ -144,8 +144,8 @@ public class BindingBerrySethi extends BerrySethi { labels, revInitials, revFinals, - (Object) deltaqRev, - (Object) defaultqRev, + deltaqRev, + defaultqRev, (Object) qbinders); revnfa.rightTrans = true; diff --git a/sources/scalac/transformer/matching/DetWordAutom.java b/sources/scalac/transformer/matching/DetWordAutom.java index f1e487c7c6..52023487c2 100644 --- a/sources/scalac/transformer/matching/DetWordAutom.java +++ b/sources/scalac/transformer/matching/DetWordAutom.java @@ -7,12 +7,125 @@ import java.util.* ; import scalac.ApplicationError ; -public class DetWordAutom extends FiniteAutom { +public class DetWordAutom { + + // BEGIN stuff from FiniteAutom + + //final static Integer FINTAG = new Integer(0); + + /** number of states */ + protected int nstates; + + /** the 'alphabet' */ + protected HashSet labels; + + /** the set of final states, here as a TreeMap */ + protected TreeMap finals; + + /** dfa: HashMap trans: Object -> Integer + * nfa: HashMap trans: Object -> Vector [ Integer ] + * + * nfa: Integer ->(Object -> Vector [ Int ]) + * [q] |->( a |-> { q' | (q,a,q') in \deltaright } ) + * + * dfa: Integer ->(Object -> Int) + * [q] |->( a |-> q' | \deltaright(q,a) = q' } ) + */ + + public HashMap[] deltaq; + + public Integer[] defaultq; // this gives the default transitions + + //protected HashMap deltaq[]; + + // --- accessor methods + + /** returns number of states + */ + public int nstates() { + return nstates; + } + + /** returns the labels + */ + public HashSet labels() { + return labels; + } + + /** returns the transitions + */ + public HashMap deltaq( int state ) { + return deltaq[ state ]; + } + + /** returns the transitions + */ + public HashMap deltaq( Integer state ) { + return deltaq[ state.intValue() ]; + } + + + /** returns the transitions + */ + public Integer defaultq( int state ) { + return defaultq[ state ]; + } + + /** returns the transitions + */ + public Integer defaultq( Integer state ) { + return defaultq[ state.intValue() ]; + } + + + /** returns true if the state is final + */ + public boolean isFinal( int state ) { + return ((finals != null) + && (finals.get( new Integer( state )) != null)); + } + + /** returns true if the state is final + */ + public boolean isFinal( Integer state ) { + return ((finals != null) && finals.containsKey( state )); + } + + /** returns true if the state is final + */ + public Integer finalTag( Integer state ) { + return (Integer) finals.get( state ); + } + + + public Integer finalTag( int state ) { + return (Integer) finals.get( new Integer (state )); + } + + /** returns true if the set of states contains at least one final state + */ + boolean containsFinal( TreeSet Q ) { + for( Iterator it = Q.iterator(); it.hasNext(); ) { + if( isFinal( (Integer) it.next())) + return true; + } + return false; + } + + + /** returns true if there are no finite states + */ + public boolean isEmpty() { + return finals.isEmpty(); + } + + // END stuff from FiniteAutom static final int FIRST = 0; static final int LAST = FIRST + 1; - static final int WHICH_LONGEST_MATCH = FIRST ; + //static final int WHICH_LONGEST_MATCH = FIRST ; + static final int WHICH_LONGEST_MATCH = LAST ; // inherited from FiniteAutom: @@ -24,11 +137,11 @@ public class DetWordAutom extends FiniteAutom { //Integer defaultq[]; - // used only during determinization and debug printing + // TEMPORARY VAR used only during determinization and debug printing // Q -> (Label -> Q ) - HashMap delta; - // Q -> Integer; - HashMap indexMap; + HashMap delta; + // Q -> Integer; + HashMap indexMap; // Integer -> Q HashMap invIndexMap; @@ -39,13 +152,13 @@ public class DetWordAutom extends FiniteAutom { final static Integer NODEFAULT = new Integer( -1 ); public boolean isSink( int i ) { - return (((HashMap[])deltaq)[ i ].keySet().isEmpty() - && (defaultq != null ) - && (((Integer)defaultq( i )).intValue() == i)); + return ( deltaq[ i ].keySet().isEmpty() + && (defaultq != null ) + && (defaultq( i ).intValue() == i) ); } public boolean hasDefault( int i ) { - return (Integer) defaultq( i ) != NODEFAULT; + return defaultq( i ) != NODEFAULT; } void determinize( NondetWordAutom nfa ) { @@ -184,8 +297,8 @@ public class DetWordAutom extends FiniteAutom { } } - ((HashMap[])deltaq)[ state_x.intValue() ] = newTrans; - ((Integer[])defaultq)[ state_x.intValue() ] = defTarget_x; + deltaq[ state_x.intValue() ] = newTrans; + defaultq[ state_x.intValue() ] = defTarget_x; delta.remove( state ); deftrans.remove( state ); @@ -232,14 +345,14 @@ public class DetWordAutom extends FiniteAutom { /** for a set of nfa states (that must exist), returns its transitions */ HashMap deltaq( TreeSet nset ) { - return (HashMap) deltaq( (Integer) indexMap.get( nset ) ); + return deltaq( (Integer) indexMap.get( nset ) ); } /** for a set of nfa states (that must exist), returns its transitions */ Integer defaultq( TreeSet nset ) { - return (Integer) defaultq( (Integer) indexMap.get( nset ) ); + return defaultq( (Integer) indexMap.get( nset ) ); } /** returns target of the transition from state i with label label. @@ -254,7 +367,7 @@ public class DetWordAutom extends FiniteAutom { return (Integer) defaultq( i ) ; case SimpleLabel( _ ): case TreeLabel( _ ): - return (Integer) ((HashMap[])deltaq)[ i ].get( label ) ; + return (Integer) deltaq[ i ].get( label ) ; /*case Pair( Integer state, Label lab ): return state; */ @@ -315,7 +428,7 @@ public class DetWordAutom extends FiniteAutom { for( Iterator su = targets.iterator(); su.hasNext(); ) { TreeSet nset = (TreeSet) su.next(); - HashMap ddelta = (HashMap) dLeft.deltaq( nset ); + HashMap ddelta = dLeft.deltaq( nset ); // ... at THIS dstate if( (Integer) ddelta.get( theLabel ) == dstate ) { @@ -366,7 +479,7 @@ public class DetWordAutom extends FiniteAutom { for( Iterator su = targets.iterator(); su.hasNext(); ) { TreeSet nset = (TreeSet) su.next(); - Integer ddef = (Integer) dLeft.defaultq( nset ); + Integer ddef = dLeft.defaultq( nset ); //System.out.println( "ddef ="+ddef ); @@ -494,7 +607,7 @@ public class DetWordAutom extends FiniteAutom { ////System.out.print(" binders: "); ////System.out.print( right.qbinders[ npair.nstate.intValue() ] ); - HashMap delta = (HashMap) right.deltaq( npair.nstate ); + HashMap delta = right.deltaq( npair.nstate ); ////System.out.print(" we could have arrived : "); //search the delta for target invIndexMap @@ -564,7 +677,7 @@ public class DetWordAutom extends FiniteAutom { nsrc = new TreeMap( new StateSetComparator() ); // now for all default transitions that arrive at this nfa state - Vector defqs = (Vector) right.defaultq( npair.nstate ); + Vector defqs = right.defaultq( npair.nstate ); for( Iterator it = defqs.iterator(); it.hasNext(); ) { Integer nq = (Integer) it.next(); //System.out.println("checking nq="+nq); @@ -699,7 +812,7 @@ public class DetWordAutom extends FiniteAutom { hmap.put( dtarget, i ); } - ((HashMap[])deltaq)[ 0 ] = hmap; // careful, this maps Int to Int + deltaq[ 0 ] = hmap; // careful, this maps Int to Int qbinders[ 0 ] = new Vector(); //((Vector[])defaultq)[ 0 ] = new Vector(); is null diff --git a/sources/scalac/transformer/matching/FiniteAutom.java b/sources/scalac/transformer/matching/FiniteAutom.java deleted file mode 100644 index f075e4aa93..0000000000 --- a/sources/scalac/transformer/matching/FiniteAutom.java +++ /dev/null @@ -1,120 +0,0 @@ -package scalac.transformer.matching ; - -import java.util.Iterator ; -import java.util.HashSet ; -import java.util.HashMap ; -import java.util.TreeSet ; -import java.util.TreeMap ; - -public abstract class FiniteAutom { - - //final static Integer FINTAG = new Integer(0); - - /** number of states */ - protected int nstates; - - /** the 'alphabet' */ - protected HashSet labels; - - /** the set of final states, here as a TreeMap */ - protected TreeMap finals; - - /** dfa: HashMap trans: Object -> Integer - * nfa: HashMap trans: Object -> Vector [ Integer ] - * - * nfa: Integer ->(Object -> Vector [ Int ]) - * [q] |->( a |-> { q' | (q,a,q') in \deltaright } ) - * - * dfa: Integer ->(Object -> Int) - * [q] |->( a |-> q' | \deltaright(q,a) = q' } ) - */ - - public Object deltaq; - - public Object defaultq; // this gives the default transitions - - //protected HashMap deltaq[]; - - // --- accessor methods - - /** returns number of states - */ - public int nstates() { - return nstates; - } - - /** returns the labels - */ - public HashSet labels() { - return labels; - } - - /** returns the transitions - */ - public Object deltaq( int state ) { - return ((Object []) deltaq)[ state ]; - } - - /** returns the transitions - */ - public Object deltaq( Integer state ) { - return ((Object []) deltaq)[ state.intValue() ]; - } - - - /** returns the transitions - */ - public Object defaultq( int state ) { - return ((Object []) defaultq)[ state ]; - } - - /** returns the transitions - */ - public Object defaultq( Integer state ) { - return ((Object []) defaultq)[ state.intValue() ]; - } - - - /** returns true if the state is final - */ - public boolean isFinal( int state ) { - return ((finals != null) - && (finals.get( new Integer( state )) != null)); - } - - /** returns true if the state is final - */ - public boolean isFinal( Integer state ) { - return ((finals != null) && finals.containsKey( state )); - } - - /** returns true if the state is final - */ - public Integer finalTag( Integer state ) { - return (Integer) finals.get( state ); - } - - - public Integer finalTag( int state ) { - return (Integer) finals.get( new Integer (state )); - } - - /** returns true if the set of states contains at least one final state - */ - boolean containsFinal( TreeSet Q ) { - for( Iterator it = Q.iterator(); it.hasNext(); ) { - if( isFinal( (Integer) it.next())) - return true; - } - return false; - } - - - /** returns true if there are no finite states - */ - public boolean isEmpty() { - return finals.isEmpty(); - } - - -} diff --git a/sources/scalac/transformer/matching/NondetWordAutom.java b/sources/scalac/transformer/matching/NondetWordAutom.java index f4a7458f52..d0133955a8 100644 --- a/sources/scalac/transformer/matching/NondetWordAutom.java +++ b/sources/scalac/transformer/matching/NondetWordAutom.java @@ -13,7 +13,120 @@ import java.util.* ; * states are represented (implicitly) as Integer objects. */ -public class NondetWordAutom extends FiniteAutom { +public class NondetWordAutom { + + // BEGIN stuff from FiniteAutom + + //final static Integer FINTAG = new Integer(0); + + /** number of states */ + protected int nstates; + + /** the 'alphabet' */ + protected HashSet labels; + + /** the set of final states, here as a TreeMap */ + protected TreeMap finals; + + /** dfa: HashMap trans: Object -> Integer + * nfa: HashMap trans: Object -> Vector [ Integer ] + * + * nfa: Integer ->(Object -> Vector [ Int ]) + * [q] |->( a |-> { q' | (q,a,q') in \deltaright } ) + * + * dfa: Integer ->(Object -> Int) + * [q] |->( a |-> q' | \deltaright(q,a) = q' } ) + */ + + public HashMap[] deltaq; + + public Vector[] defaultq; // this gives the default transitions + + //protected HashMap deltaq[]; + + // --- accessor methods + + /** returns number of states + */ + public int nstates() { + return nstates; + } + + /** returns the labels + */ + public HashSet labels() { + return labels; + } + + /** returns the transitions + */ + public HashMap deltaq( int state ) { + return deltaq[ state ]; + } + + /** returns the transitions + */ + public HashMap deltaq( Integer state ) { + return deltaq[ state.intValue() ]; + } + + + /** returns the transitions + */ + public Vector defaultq( int state ) { + return defaultq[ state ]; + } + + /** returns the transitions + */ + public Vector defaultq( Integer state ) { + return defaultq[ state.intValue() ]; + } + + + /** returns true if the state is final + */ + public boolean isFinal( int state ) { + return ((finals != null) + && (finals.get( new Integer( state )) != null)); + } + + /** returns true if the state is final + */ + public boolean isFinal( Integer state ) { + return ((finals != null) && finals.containsKey( state )); + } + + /** returns true if the state is final + */ + public Integer finalTag( Integer state ) { + return (Integer) finals.get( state ); + } + + + public Integer finalTag( int state ) { + return (Integer) finals.get( new Integer (state )); + } + + /** returns true if the set of states contains at least one final state + */ + boolean containsFinal( TreeSet Q ) { + for( Iterator it = Q.iterator(); it.hasNext(); ) { + if( isFinal( (Integer) it.next())) + return true; + } + return false; + } + + + /** returns true if there are no finite states + */ + public boolean isEmpty() { + return finals.isEmpty(); + } + + // END stuff from FiniteAutom + // inherited from FiniteAutom @@ -58,13 +171,13 @@ public class NondetWordAutom extends FiniteAutom { for( Iterator it = Qsrc.iterator(); it.hasNext(); ) { // state int q1 = ((Integer) it.next()).intValue(); - Vector ps = (Vector) ((HashMap[])deltaq)[ q1 ].get( label ); + Vector ps = (Vector) deltaq[ q1 ].get( label ); //System.out.println( "q1 "+q1+" targ:"+ps.toString() ); if( ps!=null ) Qdest.addAll( ps ); - Qdest.addAll((Vector) defaultq( q1 )); + Qdest.addAll( defaultq( q1 ) ); } return Qdest; } @@ -76,7 +189,7 @@ public class NondetWordAutom extends FiniteAutom { for( Iterator p1 = P1.iterator(); p1.hasNext(); ) { int q1 = ((Integer) p1.next()).intValue(); //System.out.println(" q1:"+q1+ " defa: "+defaultq( q1 )); - defTarget.addAll( (Vector) defaultq( q1 )); + defTarget.addAll( defaultq( q1 ) ); } return defTarget; } @@ -175,7 +288,7 @@ public class NondetWordAutom extends FiniteAutom { _deltaq [ i + offset ] = (HashMap) mapmap( ((HashMap[])deltaq)[ i ], offset, new HashMap(), false, true); - _defaultq[ i + offset ] = vmap( offset, ((Vector[])defaultq)[ i ]); + _defaultq[ i + offset ] = vmap( offset, defaultq[ i ] ); } if ((_qbinders != null) &&( qbinders != null )) @@ -210,7 +323,7 @@ public class NondetWordAutom extends FiniteAutom { finals.put( q0, finTag ); - HashMap tmp = (HashMap) deltaq( ostate ); + HashMap tmp = deltaq( ostate ); if( reloc ) tmp = (HashMap) mapmap( tmp, 1, new HashMap(), false, true ); @@ -225,7 +338,7 @@ public class NondetWordAutom extends FiniteAutom { } //System.out.println( "normalize:defaultq[ "+ostate+" ] "+((Vector[]) defaultq) [ ostate.intValue() ]); if( defaultq( ostate ) != null ) - idefault.addAll( (Vector) defaultq( ostate ) ); + idefault.addAll( defaultq( ostate ) ); } if( reloc ) { @@ -247,9 +360,9 @@ public class NondetWordAutom extends FiniteAutom { this.qbinders = _qbinders; } - ((HashMap[])this.deltaq) [ 0 ] = idelta; + this.deltaq [ 0 ] = idelta; //System.out.println("normalize:deltaq[ 0 ]"+ idelta ); - ((Vector[])this.defaultq) [ 0 ] = new Vector( idefault ); + this.defaultq[ 0 ] = new Vector( idefault ); //System.out.println("normalize:defaultq[ 0 ]"+ idefault ); @@ -269,8 +382,8 @@ public class NondetWordAutom extends FiniteAutom { HashSet labels, TreeSet initials, TreeMap finals, - Object deltaq, - Object defaultq, + HashMap[] deltaq, + Vector[] defaultq, Object qbinders) { this.nstates = nstates; this.labels = labels; @@ -329,7 +442,7 @@ public class NondetWordAutom extends FiniteAutom { this.defaultq = new Vector [ nstates ]; //TreeSet defaultqSet = new TreeSet(); - ((HashMap [])deltaq)[ 0 ] = new HashMap(); // 0 = our new initial state + deltaq[ 0 ] = new HashMap(); // 0 = our new initial state TreeSet initials = new TreeSet(); @@ -345,8 +458,8 @@ public class NondetWordAutom extends FiniteAutom { n.relocate( offs, this.finals, - (HashMap[]) this.deltaq, - (Vector[]) this.defaultq, + this.deltaq, + this.defaultq, (Vector[]) this.qbinders ); } @@ -383,7 +496,7 @@ public class NondetWordAutom extends FiniteAutom { System.out.print("*"); //final } System.out.print(" transitions: {"); - HashMap arrows = ((HashMap[])deltaq)[ i ]; + HashMap arrows = deltaq[ i ]; for( Iterator it = arrows.keySet().iterator(); it.hasNext(); ) { |