// This file is not part of your solution, it is part of the problem. Any // changes you make to this file will be lost when you turn in your solution. import java.util.regex.*; import java.util.Random; class NodePairing { static private class OptionValue { int value; } static private class Options { int nodeCount; int seed; } private static void badArg(String what, String s) { // Construct an error message about the given option having the given bad // value, print it, and die. overAndOut( "The " + what + " command-line argument should be a positive integer, not \"" + s + "\""); } private static boolean doOption(String arg, String pat, String what, OptionValue ov) { // Parse the given argument with the given patter, representing the given // command-line argument and store the argument value in the given // reference. final Pattern p = Pattern.compile("^-" + pat + "(-?\\d+)$"); final Matcher m = p.matcher(arg); if (!m.find()) return false; final String i = arg.substring(m.start(1), m.end(1)); try { ov.value = (new Integer(i)).intValue(); } catch (NumberFormatException e) { assert false : "bad number in option"; } if (ov.value < 1) badArg(what, arg); return true; } private static Options doOptions(String [] args) { // Process the given command line arguments, returning the option values. final Options options = new Options(); options.nodeCount = 10; options.seed = (int) System.currentTimeMillis(); OptionValue ov = new OptionValue(); int i; for (i = 0; i < args.length; i++) if (doOption(args[i], "n", "node count", ov)) options.nodeCount = ov.value; else if (doOption(args[i], "s", "random-number seed", ov)) options.seed = ov.value; else overAndOut("\"" + args[i] + "\" is an unrecognized command-line argument"); return options; } public static void main(String[] args) { final Options options = doOptions(args); final Random rint = new Random(options.seed); try { run(options.nodeCount, rint); } catch (Exception e) { } } private static void overAndOut(String emsg) { // Print an error message and die. System.err.println(emsg + "."); System.err.println("The command-line option is -nn"); System.exit(1); } private static void run(final int cnt, Random rint) { // Create and run an instance of the node-pairing problem. assert cnt > 0; int i; final NodeRunnable neighbors[] [] = new NodeRunnable [cnt][]; for (i = 0; i < cnt; i++) neighbors[i] = new NodeRunnable [cnt]; final NodeRunnable nodes[] = new NodeRunnable [cnt]; for (i = 0; i < cnt; i++) nodes[i] = new NodeRunnable(i, neighbors[i]); final Graph graph = new Graph(cnt, rint); for (i = 0; i < cnt; i++) graph.setNeighbors(i, neighbors[i], nodes); final Thread threads[] = new Thread [cnt]; for (i = 0; i < cnt; i++) { threads[i] = new Thread(nodes[i]); threads[i].start(); } for (i = 0; i < cnt; i++) try { threads[i].join(); } catch (InterruptedException e) { } graph.checkPairs(nodes); } } // $Log: NodePairing.java,v $ // Revision 1.2 2003/08/08 23:54:59 rclayton // Really generate random graphs; add the -s option to set the seed. // // Revision 1.1 2003/08/07 20:38:02 rclayton // Initial revision //