001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.dfsolver;
014    
015    import java.util.HashSet;
016    import java.util.Iterator;
017    
018    import org.jikesrvm.compilers.opt.util.GraphNode;
019    import org.jikesrvm.compilers.opt.util.GraphNodeEnumeration;
020    
021    /**
022     * DF_LatticeCell.java
023     *
024     * Represents a single lattice cell in a dataflow system.
025     */
026    public abstract class DF_AbstractCell implements DF_LatticeCell {
027      /**
028       * Set of DF_Equations which use this lattice cell.
029       */
030      private final HashSet<DF_Equation> uses;
031      /**
032       * Set of DF_Equations which define this lattice cell.
033       */
034      private final HashSet<DF_Equation> defs;
035    
036      /**
037       * Default Constructor
038       */
039      public DF_AbstractCell() {
040        uses = new HashSet<DF_Equation>(1);
041        defs = new HashSet<DF_Equation>(1);
042      }
043    
044      /**
045       * This constructor bounds the initial capacity to save space.
046       * @param capacity the initial capacity of the "uses" set
047       */
048      public DF_AbstractCell(int capacity) {
049        uses = new HashSet<DF_Equation>(capacity);
050        defs = new HashSet<DF_Equation>(capacity);
051      }
052    
053      /**
054       * Returns an enumeration of the equations in which this
055       * lattice cell is used.
056       * @return an enumeration of the equations in which this
057       * lattice cell is used
058       */
059      public Iterator<DF_Equation> getUses() {
060        return uses.iterator();
061      }
062    
063      /**
064       * Return an enumeration of the equations in which this
065       * lattice cell is defined.
066       * @return an enumeration of the equations in which this
067       * lattice cell is defined
068       */
069      public Iterator<DF_Equation> getDefs() {
070        return defs.iterator();
071      }
072    
073      /**
074       * Return a string representation of the cell
075       * @return a string representation of the cell
076       */
077      public abstract String toString();
078    
079      /**
080       * Note that this variable appears on the RHS of an equation.
081       *
082       * @param eq the equation
083       */
084      public void addUse(DF_Equation eq) {
085        uses.add(eq);
086      }
087    
088      /**
089       * Note that this variable appears on the LHS of an equation.
090       *
091       * @param eq the equation
092       */
093      public void addDef(DF_Equation eq) {
094        defs.add(eq);
095      }
096    
097      public GraphNodeEnumeration inNodes() {
098        return new GraphNodeEnumeration() {
099          private final Iterator<DF_Equation> i = defs.iterator();
100    
101          public boolean hasMoreElements() { return i.hasNext(); }
102    
103          public GraphNode next() { return i.next(); }
104    
105          public GraphNode nextElement() { return next(); }
106        };
107      }
108    
109      public GraphNodeEnumeration outNodes() {
110        return new GraphNodeEnumeration() {
111          private final Iterator<DF_Equation> i = uses.iterator();
112    
113          public boolean hasMoreElements() { return i.hasNext(); }
114    
115          public GraphNode next() { return i.next(); }
116    
117          public GraphNode nextElement() { return next(); }
118        };
119      }
120    
121      /**
122       * Field used for GraphNode interface.  TODO: is this needed?
123       */
124      private int index;
125    
126      /**
127       * Implementation of GraphNode interface.
128       */
129      public void setIndex(int i) {
130        index = i;
131      }
132    
133      /**
134       * Implementation of GraphNode interface.
135       */
136      public int getIndex() {
137        return index;
138      }
139    
140      private int scratch;
141    
142      public int getScratch() {
143        return scratch;
144      }
145    
146      public int setScratch(int o) {
147        return (scratch = o);
148      }
149    }
150    
151    
152