32bf470223d68b039b033fa73e5feb034d9618f2
[repair.git] / Repair / RepairCompiler / MCC / IR / ExactSize.java
1 package MCC.IR;
2 import java.util.*;
3 import MCC.State;
4
5
6 class ExactSize {
7     State state;
8     private Hashtable sizemap;
9     private Hashtable constraintmap;
10
11     public ExactSize(State state) {
12         this.state=state;
13         this.sizemap=new Hashtable();
14         this.constraintmap=new Hashtable();
15         computesizes();
16     }
17     
18     public int getsize(SetDescriptor sd) {
19         if (sizemap.containsKey(sd))
20             return ((Integer)sizemap.get(sd)).intValue();
21         else
22             return -1;
23     }
24     public Constraint getConstraint(SetDescriptor sd) {
25         return (Constraint)constraintmap.get(sd);
26     }
27
28     private void computesizes() {
29         for(Iterator it=state.stSets.descriptors();it.hasNext();) {
30             SetDescriptor sd=(SetDescriptor)it.next();
31             for(int i=0;i<state.vConstraints.size();i++) {
32                 Constraint c=(Constraint)state.vConstraints.get(i);
33                 if (c.numQuantifiers()!=0)
34                     continue;
35                 DNFConstraint dconst=c.dnfconstraint;
36                 int oldsize=-1;
37                 boolean matches=true;
38                 for(int j=0;j<dconst.size();j++) {
39                     Conjunction conj=dconst.get(j);
40                     boolean goodmatch=false;
41                     for(int k=0;k<conj.size();k++) {
42                         DNFPredicate dpred=conj.get(k);
43                         if (!dpred.isNegated()) {
44                             Predicate p=dpred.getPredicate();
45                             if (p instanceof ExprPredicate) {
46                                 ExprPredicate ep=(ExprPredicate)p;
47                                 if (ep.getType()==ExprPredicate.SIZE&&
48                                     ep.getOp()==Opcode.EQ&&
49                                     ep.getDescriptor()==sd&&
50                                     ep.isRightInt()) {
51                                     if (j==0) {
52                                         oldsize=ep.rightSize();
53                                         goodmatch=true;
54                                         break;
55                                     } else {
56                                         if (oldsize==ep.rightSize()) {
57                                             goodmatch=true;
58                                             break;
59                                         }
60                                     }
61                                 }
62                             }
63                         }
64                     }
65                     if (!goodmatch) {
66                         matches=false;
67                         break; //this constraint won't work
68                     }
69                 }
70                 if (matches) {
71                     System.out.println("Set "+sd.toString()+" has size "+oldsize);
72                     sizemap.put(sd,new Integer(oldsize));
73                     constraintmap.put(sd,c);
74                 }
75             }
76         }
77     }
78 }