Add copyOf method to Net class for creating deep copies of nets

This commit is contained in:
2025-02-14 16:01:40 +01:00
parent 1cab669fd9
commit c2b9d350aa

View File

@@ -244,6 +244,29 @@ public final class Net implements Iterable<ServerNode> {
}
}
/**
* Create a copy of the net passed as input.
* The new net will have the same nodes and connections.
*
* @param net the net to copy
* @return a new Net object
*/
public static Net copyOf(Net net) {
var newNet = new Net();
for (var index = 0; index < net.size(); index++) {
var node = net.servers.get(index);
var conn = net.connections.get(index);
conn = new ArrayList<>(conn);
newNet.indices.put(node, index);
newNet.servers.add(node);
newNet.connections.add(conn);
}
return newNet;
}
@Override
public String toString() {
var builder = new StringBuilder();