Differential D6953 Diff 25202 java/src/main/java/org/softwareheritage/graph/experiments/topology/InOutDegree.java
Changeset View
Changeset View
Standalone View
Standalone View
java/src/main/java/org/softwareheritage/graph/experiments/topology/InOutDegree.java
Show All 9 Lines | |||||
import com.martiansoftware.jsap.JSAP; | import com.martiansoftware.jsap.JSAP; | ||||
import com.martiansoftware.jsap.JSAPException; | import com.martiansoftware.jsap.JSAPException; | ||||
import com.martiansoftware.jsap.JSAPResult; | import com.martiansoftware.jsap.JSAPResult; | ||||
import com.martiansoftware.jsap.Parameter; | import com.martiansoftware.jsap.Parameter; | ||||
import com.martiansoftware.jsap.SimpleJSAP; | import com.martiansoftware.jsap.SimpleJSAP; | ||||
import com.martiansoftware.jsap.UnflaggedOption; | import com.martiansoftware.jsap.UnflaggedOption; | ||||
import it.unimi.dsi.logging.ProgressLogger; | import it.unimi.dsi.logging.ProgressLogger; | ||||
import org.softwareheritage.graph.Graph; | import org.softwareheritage.graph.SwhBidirectionalGraph; | ||||
import org.softwareheritage.graph.Node; | import org.softwareheritage.graph.Node; | ||||
public class InOutDegree { | public class InOutDegree { | ||||
private InOutDegree() { | private InOutDegree() { | ||||
} | } | ||||
private static final int NODE_ARRAY_SIZE = Node.Type.values().length + 1; | private static final int NODE_ARRAY_SIZE = Node.Type.values().length + 1; | ||||
private static final int TYPE_ALL = Node.Type.values().length; | private static final int TYPE_ALL = Node.Type.values().length; | ||||
private static final int TYPE_CNT = Node.Type.toInt(Node.Type.CNT); | private static final int TYPE_CNT = Node.Type.toInt(Node.Type.CNT); | ||||
private static final int TYPE_DIR = Node.Type.toInt(Node.Type.DIR); | private static final int TYPE_DIR = Node.Type.toInt(Node.Type.DIR); | ||||
private static final int TYPE_REV = Node.Type.toInt(Node.Type.REV); | private static final int TYPE_REV = Node.Type.toInt(Node.Type.REV); | ||||
private static final int TYPE_REL = Node.Type.toInt(Node.Type.REL); | private static final int TYPE_REL = Node.Type.toInt(Node.Type.REL); | ||||
private static final int TYPE_SNP = Node.Type.toInt(Node.Type.SNP); | private static final int TYPE_SNP = Node.Type.toInt(Node.Type.SNP); | ||||
private static final int TYPE_ORI = Node.Type.toInt(Node.Type.ORI); | private static final int TYPE_ORI = Node.Type.toInt(Node.Type.ORI); | ||||
public static long[] outdegreeTypes(final Graph graph, long node) { | public static long[] outdegreeTypes(final SwhBidirectionalGraph graph, long node) { | ||||
long[] out = new long[NODE_ARRAY_SIZE]; | long[] out = new long[NODE_ARRAY_SIZE]; | ||||
var successors = graph.successors(node); | var successors = graph.successors(node); | ||||
long neighbor; | long neighbor; | ||||
while ((neighbor = successors.nextLong()) != -1) { | while ((neighbor = successors.nextLong()) != -1) { | ||||
out[Node.Type.toInt(graph.getNodeType(neighbor))]++; | out[Node.Type.toInt(graph.getNodeType(neighbor))]++; | ||||
out[TYPE_ALL]++; | out[TYPE_ALL]++; | ||||
} | } | ||||
return out; | return out; | ||||
} | } | ||||
public static long[] indegreeTypes(final Graph graph, long node) { | public static long[] indegreeTypes(final SwhBidirectionalGraph graph, long node) { | ||||
return outdegreeTypes(graph.transpose(), node); | return outdegreeTypes(graph.transpose(), node); | ||||
} | } | ||||
public static void writeDistribution(HashMap<Long, Long> distribution, String filename) throws IOException { | public static void writeDistribution(HashMap<Long, Long> distribution, String filename) throws IOException { | ||||
PrintWriter f = new PrintWriter(new FileWriter(filename)); | PrintWriter f = new PrintWriter(new FileWriter(filename)); | ||||
TreeMap<Long, Long> sortedDistribution = new TreeMap<>(distribution); | TreeMap<Long, Long> sortedDistribution = new TreeMap<>(distribution); | ||||
for (Map.Entry<Long, Long> entry : sortedDistribution.entrySet()) { | for (Map.Entry<Long, Long> entry : sortedDistribution.entrySet()) { | ||||
f.println(entry.getKey() + " " + entry.getValue()); | f.println(entry.getKey() + " " + entry.getValue()); | ||||
} | } | ||||
f.close(); | f.close(); | ||||
} | } | ||||
public static void run(final Graph graph, String resultsDir) throws IOException { | public static void run(final SwhBidirectionalGraph graph, String resultsDir) throws IOException { | ||||
// Per-type | // Per-type | ||||
var cnt_in_dir = new HashMap<Long, Long>(); | var cnt_in_dir = new HashMap<Long, Long>(); | ||||
var dir_in_dir = new HashMap<Long, Long>(); | var dir_in_dir = new HashMap<Long, Long>(); | ||||
var dir_in_rev = new HashMap<Long, Long>(); | var dir_in_rev = new HashMap<Long, Long>(); | ||||
var dir_in_all = new HashMap<Long, Long>(); | var dir_in_all = new HashMap<Long, Long>(); | ||||
var dir_out_all = new HashMap<Long, Long>(); | var dir_out_all = new HashMap<Long, Long>(); | ||||
var dir_out_dir = new HashMap<Long, Long>(); | var dir_out_dir = new HashMap<Long, Long>(); | ||||
var dir_out_cnt = new HashMap<Long, Long>(); | var dir_out_cnt = new HashMap<Long, Long>(); | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | static public void main(final String[] arg) | ||||
final String basename = jsapResult.getString("basename"); | final String basename = jsapResult.getString("basename"); | ||||
final String resultsDir = jsapResult.userSpecified("resultsDir") | final String resultsDir = jsapResult.userSpecified("resultsDir") | ||||
? jsapResult.getString("resultsDir") | ? jsapResult.getString("resultsDir") | ||||
: basename; | : basename; | ||||
final ProgressLogger pl = new ProgressLogger(); | final ProgressLogger pl = new ProgressLogger(); | ||||
Graph graph = Graph.loadMapped(basename); | SwhBidirectionalGraph graph = SwhBidirectionalGraph.loadMapped(basename); | ||||
run(graph, resultsDir); | run(graph, resultsDir); | ||||
} | } | ||||
} | } |