| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package net.sourceforge.combean.samples.simplegraphs; |
| 23 | |
|
| 24 | |
import java.util.Collection; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.LinkedList; |
| 27 | |
|
| 28 | |
import net.sourceforge.combean.util.Pair; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class CompositeNumberGraph extends NumberGraph { |
| 35 | |
|
| 36 | |
private Collection<Pair> childGraphs; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public CompositeNumberGraph() { |
| 42 | 78 | super(); |
| 43 | |
|
| 44 | 78 | this.childGraphs = new LinkedList<Pair>(); |
| 45 | 78 | } |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
protected int calcNextNode(int sourceNodeNum, int minNodeNum) { |
| 51 | 585 | int nextNodeCand = NumberGraph.NOSUCHNODE; |
| 52 | |
|
| 53 | 585 | Iterator itChild = this.childGraphs.iterator(); |
| 54 | 1755 | while (itChild.hasNext()) { |
| 55 | 1170 | Pair offsetAndGraph = (Pair) itChild.next(); |
| 56 | 1170 | Integer offset = (Integer) offsetAndGraph.getFirst(); |
| 57 | 1170 | NumberGraph g = (NumberGraph) offsetAndGraph.getSecond(); |
| 58 | |
|
| 59 | 1170 | int startNodeInG = sourceNodeNum - offset.intValue(); |
| 60 | |
|
| 61 | 1170 | if (0 <= startNodeInG && startNodeInG < g.getNumNodes()) { |
| 62 | 771 | int currNodeInG = minNodeNum - offset.intValue(); |
| 63 | 771 | int nextNodeOfG = g.calcNextNode(startNodeInG, currNodeInG); |
| 64 | |
|
| 65 | 771 | if (nextNodeOfG != NumberGraph.NOSUCHNODE) { |
| 66 | 480 | nextNodeOfG += offset.longValue(); |
| 67 | |
|
| 68 | 480 | if (nextNodeCand == NumberGraph.NOSUCHNODE) { |
| 69 | 408 | nextNodeCand = nextNodeOfG; |
| 70 | |
} |
| 71 | |
else { |
| 72 | 72 | nextNodeCand = Math.min(nextNodeCand, nextNodeOfG); |
| 73 | |
} |
| 74 | |
} |
| 75 | |
} |
| 76 | 1170 | } |
| 77 | 585 | return nextNodeCand; |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public int getNumNodes() { |
| 84 | 1440 | int maxNodeOffset = 0; |
| 85 | 1440 | Iterator itChild = this.childGraphs.iterator(); |
| 86 | 4320 | while (itChild.hasNext()) { |
| 87 | 2880 | Pair offsetAndGraph = (Pair) itChild.next(); |
| 88 | 2880 | Integer offset = (Integer) offsetAndGraph.getFirst(); |
| 89 | 2880 | NumberGraph g = (NumberGraph) offsetAndGraph.getSecond(); |
| 90 | 2880 | maxNodeOffset = |
| 91 | |
Math.max(maxNodeOffset, |
| 92 | |
offset.intValue() + g.getNumNodes()); |
| 93 | 2880 | } |
| 94 | 1440 | return maxNodeOffset; |
| 95 | |
} |
| 96 | |
|
| 97 | |
public void add(NumberGraph g) { |
| 98 | 93 | add(g, 0); |
| 99 | 93 | } |
| 100 | |
|
| 101 | |
public void add(NumberGraph g, int offset) { |
| 102 | 156 | this.childGraphs.add(new Pair(new Integer(offset), g)); |
| 103 | 156 | } |
| 104 | |
|
| 105 | |
public int getNumGraphs() { |
| 106 | 6 | return this.childGraphs.size(); |
| 107 | |
} |
| 108 | |
} |