1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| private static Graph<Integer> load(Path path) throws IOException {
return loadGraph(null, path);
}
private static <T> Graph<T> load(PropertyType ptype, Path...paths) throws IOException {
return loadGraph(ptype.getFactory(), paths);
}
private static <T> Graph<T> loadGraph(Function<String,T> parser, PropertyType ptype, Path...paths) throws IOException {
Graph<T> graph = ptype==null?new Graph<>():new Graph<>(ptype);
for(Path path : paths) {
try(BufferedReader reader = Files.newBufferedReader(path)) {
for(String line = reader.readLine(); line!=null; line = reader.readLine()) {
String[] vertices = line.split(" ");
if (vertices.length >= 2) {
final int x = Integer.parseInt(vertices[0]);
final int y = Integer.parseInt(vertices[1]);
if( parser!=null ) {
if ( vertices.length>2 ) {
graph.addEdge(x, y, parser.apply(vertices[2]));
}
}
else {
graph.addEdge(x, y);
}
}
}
}
}
return graph;
} |
Partager