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 */
013package org.jikesrvm.compilers.opt.dfsolver;
014
015import java.util.Enumeration;
016import java.util.HashSet;
017import java.util.Iterator;
018
019import org.jikesrvm.compilers.opt.util.GraphNode;
020
021/**
022 * Represents a single lattice cell in a dataflow system.
023 */
024public abstract class DF_AbstractCell implements DF_LatticeCell {
025  /**
026   * Set of DF_Equations which use this lattice cell.
027   */
028  private final HashSet<DF_Equation> uses;
029  /**
030   * Set of DF_Equations which define this lattice cell.
031   */
032  private final HashSet<DF_Equation> defs;
033
034  /**
035   * Default Constructor
036   */
037  public DF_AbstractCell() {
038    uses = new HashSet<DF_Equation>(1);
039    defs = new HashSet<DF_Equation>(1);
040  }
041
042  /**
043   * This constructor bounds the initial capacity to save space.
044   * @param capacity the initial capacity of the "uses" set
045   */
046  public DF_AbstractCell(int capacity) {
047    uses = new HashSet<DF_Equation>(capacity);
048    defs = new HashSet<DF_Equation>(capacity);
049  }
050
051  @Override
052  public Iterator<DF_Equation> getUses() {
053    return uses.iterator();
054  }
055
056  @Override
057  public Iterator<DF_Equation> getDefs() {
058    return defs.iterator();
059  }
060
061  @Override
062  public void addUse(DF_Equation eq) {
063    uses.add(eq);
064  }
065
066  @Override
067  public void addDef(DF_Equation eq) {
068    defs.add(eq);
069  }
070
071  @Override
072  public Enumeration<GraphNode> inNodes() {
073    return new Enumeration<GraphNode>() {
074      private final Iterator<DF_Equation> i = defs.iterator();
075
076      @Override
077      public boolean hasMoreElements() {
078        return i.hasNext();
079      }
080
081      @Override
082      public GraphNode nextElement() {
083        return i.next();
084      }
085    };
086  }
087
088  @Override
089  public Enumeration<GraphNode> outNodes() {
090    return new Enumeration<GraphNode>() {
091      private final Iterator<DF_Equation> i = uses.iterator();
092
093      @Override
094      public boolean hasMoreElements() {
095        return i.hasNext();
096      }
097
098      @Override
099      public GraphNode nextElement() {
100        return i.next();
101      }
102    };
103  }
104
105  /**
106   * Field used for GraphNode interface.  TODO: is this needed?
107   */
108  private int index;
109
110  /**
111   * Implementation of GraphNode interface.
112   */
113  @Override
114  public void setIndex(int i) {
115    index = i;
116  }
117
118  @Override
119  public int getIndex() {
120    return index;
121  }
122
123}