Coverage details for edu.uci.ics.jung.random.generators.Lattice1DGenerator

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University
3  * of California
4  * All rights reserved.
5  *
6  * This software is open-source under the BSD license; see either
7  * "license.txt" or
8  * http://jung.sourceforge.net/license.txt for a description.
9  */
10 /*
11  * Created on Jan 8, 2004
12  *
13  */
14 package edu.uci.ics.jung.random.generators;
15  
16 import edu.uci.ics.jung.graph.ArchetypeGraph;
17 import edu.uci.ics.jung.graph.Graph;
18 import edu.uci.ics.jung.graph.Vertex;
19 import edu.uci.ics.jung.graph.decorators.Indexer;
20 import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
21 import edu.uci.ics.jung.utils.GraphUtils;
22  
23 /**
24  * Simple generator of an n x 1 lattice where each vertex
25  * is incident with each of its 2 neighbors (except possibly
26  * for the vertices on the edge depending upon whether the lattice
27  * is specified to be toroidal or not).
28  * @author Scott White
29  *
30  */
31 public class Lattice1DGenerator implements GraphGenerator
32 {
33     private int mNumNodes;
34     private boolean mIsToroidal;
35  
36     /**
37      * Constructs an instance of the lattice generator
38      * @param numNodes # of nodes in the generated graph
39      * @param isToroidal whether the lattice wraps around or not
40      */
41     public Lattice1DGenerator(int numNodes, boolean isToroidal)
4262    {
4362        if (numNodes < 1)
44         {
450            throw new IllegalArgumentException("Lattice size must be at least 1.");
46         }
47  
4862        mNumNodes = numNodes;
4962        mIsToroidal = isToroidal;
5062    }
51  
52     /* (non-Javadoc)
53      * @see edu.uci.ics.jung.random.generators.GraphGenerator#generateGraph()
54      */
55     public ArchetypeGraph generateGraph()
56     {
571        Graph g = new UndirectedSparseGraph();
581        GraphUtils.addVertices(g, mNumNodes);
591        int upI = 0;//, downI = 0;
601        Indexer id = Indexer.getIndexer(g);
61  
62         //create lattice structure
636        for (int i = 0; i < mNumNodes; i++)
64         {
6510            for (int s = 1; s <= 1; s++)
66             {
675                Vertex ithVertex = (Vertex) id.getVertex(i);
685                upI = upIndex(i, s);
695                if (i != mNumNodes - 1 || (i == mNumNodes - 1 && mIsToroidal))
70                 {
715                    GraphUtils.addEdge(
72                         g,
73                         ithVertex,
74                         (Vertex) id.getVertex(upI));
75                 }
76             }
77         }
78  
791        return g;
80     }
81  
82     /**
83          * Determines the vertices with a smaller index that are in the neighborhood of currentIndex.
84          * @param numSteps indicates the number of steps away from the current index that are being considered.
85          * @param currentIndex the index of the selected vertex.
86          */
87     protected int downIndex(int currentIndex, int numSteps)
88     {
890        if (currentIndex - numSteps < 0)
90         {
910            return (mNumNodes - 1) + (currentIndex - numSteps);
92         }
930        return currentIndex + numSteps;
94     }
95  
96     /**
97      * Determines the index of the neighbor ksteps above
98      * @param numSteps is the number of steps away from the current index that is being considered.
99      * @param currentIndex the index of the selected vertex.
100      */
101     protected int upIndex(int currentIndex, int numSteps)
102     {
10311195        if (currentIndex + numSteps > mNumNodes - 1)
104         {
105199            return numSteps - (mNumNodes - currentIndex);
106         }
10710996        return currentIndex + numSteps;
108     }
109  
110 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.