Coverage Report - net.sourceforge.combean.graph.alg.traversal.DFSNodeNumberingVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
DFSNodeNumberingVisitor
86%
18/21
25%
1/4
1,111
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 20.02.2005
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.graph.alg.traversal;
 23  
 
 24  
 import net.sourceforge.combean.graph.containers.NodeNumberingAdvisor;
 25  
 import net.sourceforge.combean.interfaces.graph.Graph;
 26  
 import net.sourceforge.combean.interfaces.graph.Node;
 27  
 import net.sourceforge.combean.interfaces.graph.containers.NodeNumbering;
 28  
 
 29  
 /**
 30  
  * @author schickin
 31  
  *
 32  
  */
 33  
 public class DFSNodeNumberingVisitor extends IdleDFSVisitor {
 34  
     
 35  
     /**
 36  
      * store dfs number, starting with 1
 37  
      * (will be set to the negative value of the actual dfs number
 38  
      * while the node is still in the dfs stack.
 39  
      */
 40  18
     private NodeNumbering dfsNum = null;
 41  
 
 42  18
     private int currentDFSNum = 0;
 43  
     
 44  
     /**
 45  
      * constructor 
 46  
      */
 47  
     public DFSNodeNumberingVisitor() {
 48  18
         super();
 49  18
     }
 50  
 
 51  
     /**
 52  
      * @return Returns the dfs numbering.
 53  
      */
 54  
     public final NodeNumbering getDfsNumbering() {
 55  12
         return this.dfsNum;
 56  
     }
 57  
     
 58  
     /**
 59  
      * Set the dfs numbering. If left unset, the NodeNumberingAdvisor will
 60  
      * be consulted.
 61  
      * 
 62  
      * @param dfsNum The dfs numbering to set.
 63  
      */
 64  
     public final void setDfsNumbering(NodeNumbering dfsNum) {
 65  0
         this.dfsNum = dfsNum;
 66  0
     }
 67  
     
 68  
     /**
 69  
      * @param v
 70  
      * @return the dfs number of v (positive value,
 71  
      * regardless whether v is on the stack of not.
 72  
      */
 73  
     public final int getDfsNum(Node v) {
 74  171
         return Math.abs(this.dfsNum.getNumber(v));
 75  
     }
 76  
     
 77  
     /**
 78  
      * @param v
 79  
      * @return true if v is on the dfs stack while the dfs is executed.
 80  
      */
 81  
     protected final boolean isOnStack(Node v) {
 82  0
         return this.dfsNum.getNumber(v) < 0;
 83  
     }
 84  
 
 85  
     /* (non-Javadoc)
 86  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.DFSVisitor#leaveNode(net.sourceforge.combean.interfaces.graph.Node)
 87  
      */
 88  
     public void leaveNode(Node v) {
 89  78
         int dfsV = -this.dfsNum.getNumber(v);
 90  78
         this.dfsNum.setNumber(v, dfsV);
 91  78
     }
 92  
 
 93  
     /* (non-Javadoc)
 94  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor#visitNode(net.sourceforge.combean.interfaces.graph.Node)
 95  
      */
 96  
     public void visitNode(Node v) {
 97  108
         this.dfsNum.setNumber(v, -(++this.currentDFSNum));
 98  108
     }
 99  
 
 100  
   /* (non-Javadoc)
 101  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor#init(net.sourceforge.combean.interfaces.graph.Graph)
 102  
      */
 103  
     public void init(Graph g) {
 104  18
         if (this.dfsNum == null) {
 105  18
             this.dfsNum = NodeNumberingAdvisor.getFastestNodeNumbering(g);
 106  
         }
 107  18
         this.dfsNum.init(g);
 108  18
         this.currentDFSNum = 0;
 109  18
     }
 110  
 
 111  
     /* (non-Javadoc)
 112  
      * @see net.sourceforge.combean.interfaces.graph.alg.traverse.TraversalVisitor#initLocal(net.sourceforge.combean.interfaces.graph.Graph, net.sourceforge.combean.interfaces.graph.Node)
 113  
      */
 114  
     public void initLocal(Graph g, Node startNode) {
 115  6
         init(g);
 116  6
     }
 117  
 
 118  
 }