Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1. import java.lang.IllegalArgumentException;
  2. import java.lang.ref.WeakReference;
  3. import java.lang.StringBuilder;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.Set;
  9. import java.util.Vector;
  10. import java.util.WeakHashMap;
  11.  
  12. public final class Gr {
  13.     static {
  14.         rng = new Random();
  15.         rngChars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f',
  16.                     'g', 'h', 'i', 'j', 'k', 'l',
  17.                     'm', 'n', 'o', 'p', 'q', 'r',
  18.                     's', 't', 'u', 'v', 'w', 'x',
  19.                     'y', 'z'};
  20.     }
  21.  
  22.     private Gr() {
  23.     }
  24.  
  25.     public static interface Graph {
  26.         public void marker(GraphImpl impl);
  27.     }
  28.  
  29.     private final static class GraphImpl implements Graph {
  30.         public void marker(GraphImpl impl) {
  31.         }
  32.  
  33.         public final HashSet<Node> nodes = new HashSet<>();
  34.     }
  35.  
  36.     public static interface Node {
  37.         public void marker(NodeImpl impl);
  38.     }
  39.  
  40.     private final static class NodeImpl implements Node {
  41.         {
  42.             to = new WeakHashMap<>();
  43.             from = new WeakHashMap<>();
  44.         }
  45.  
  46.         public void marker(NodeImpl impl) {
  47.         }
  48.  
  49.         public NodeImpl(String label) {
  50.             this.label = label;
  51.         }
  52.  
  53.         public final WeakHashMap<Node, Integer> to;
  54.         public final WeakHashMap<Node, Void> from;
  55.         public WeakReference<GraphImpl> owner;
  56.         public String label;
  57.     }
  58.  
  59.     public static Graph newGraph() {
  60.         return new GraphImpl();
  61.     }
  62.  
  63.     public static Node newNode(String label) {
  64.         return new NodeImpl(label);
  65.     }
  66.  
  67.     public static void add(Graph graph, Node node) {
  68.         final GraphImpl graphImpl = (GraphImpl)graph;
  69.         final NodeImpl nodeImpl = (NodeImpl)node;
  70.  
  71.         if (nodeImpl.owner != null) {
  72.             throw new IllegalArgumentException("Already owned node");
  73.         }
  74.         nodeImpl.owner = new WeakReference<>(graphImpl);
  75.         graphImpl.nodes.add(nodeImpl);
  76.     }
  77.  
  78.     public static void link(Graph graph, Node a, Node b, int weight) {
  79.         final GraphImpl graphImpl = (GraphImpl)graph;
  80.         NodeImpl aImpl = (NodeImpl)a;
  81.         NodeImpl bImpl = (NodeImpl)b;
  82.  
  83.         if (aImpl == bImpl) {
  84.             throw new IllegalArgumentException("Two same nodes");
  85.         }
  86.         if (aImpl.owner.get() != graph) {
  87.             throw new IllegalArgumentException("Wrong owner");
  88.         }
  89.         if (bImpl.owner.get() != graph) {
  90.             throw new IllegalArgumentException("Wrong owner");
  91.         }
  92.  
  93.         if (aImpl.hashCode() < bImpl.hashCode()) {
  94.             final NodeImpl tmp = aImpl;
  95.             aImpl = bImpl;
  96.             bImpl = tmp;
  97.         }
  98.  
  99.         if (null == aImpl.to.put(bImpl, weight)) {
  100.             bImpl.from.put(aImpl, null);
  101.         }
  102.     }
  103.  
  104.     public static boolean connectsTo(Graph graph, Node a, Node b) {
  105.         final GraphImpl graphImpl = (GraphImpl)graph;
  106.         NodeImpl aImpl = (NodeImpl)a;
  107.         NodeImpl bImpl = (NodeImpl)b;
  108.  
  109.         if (aImpl == bImpl) {
  110.             return true;
  111.         }
  112.         if (aImpl.owner.get() != graph) {
  113.             throw new IllegalArgumentException("Wrong owner");
  114.         }
  115.         if (bImpl.owner.get() != graph) {
  116.             throw new IllegalArgumentException("Wrong owner");
  117.         }
  118.  
  119.         if (aImpl.hashCode() < bImpl.hashCode()) {
  120.             final NodeImpl tmp = aImpl;
  121.             aImpl = bImpl;
  122.             bImpl = tmp;
  123.         }
  124.  
  125.         return aImpl.to.containsKey(bImpl);
  126.     }
  127.  
  128.     public static int weight(Graph graph, Node a, Node b) {
  129.         final GraphImpl graphImpl = (GraphImpl)graph;
  130.         NodeImpl aImpl = (NodeImpl)a;
  131.         NodeImpl bImpl = (NodeImpl)b;
  132.  
  133.         if (aImpl == bImpl) {
  134.             return 0;
  135.         }
  136.         if (aImpl.owner.get() != graph) {
  137.             throw new IllegalArgumentException("Wrong owner");
  138.         }
  139.         if (bImpl.owner.get() != graph) {
  140.             throw new IllegalArgumentException("Wrong owner");
  141.         }
  142.  
  143.         if (aImpl.hashCode() < bImpl.hashCode()) {
  144.             final NodeImpl tmp = aImpl;
  145.             aImpl = bImpl;
  146.             bImpl = tmp;
  147.         }
  148.  
  149.         Integer result = aImpl.to.get(bImpl);
  150.         if (null == result) {
  151.             throw new IllegalArgumentException("Node not not an edge");
  152.         }
  153.         return result;
  154.     }
  155.  
  156.     public static void unlink(Graph graph, Node a, Node b) {
  157.         final GraphImpl graphImpl = (GraphImpl)graph;
  158.         NodeImpl aImpl = (NodeImpl)a;
  159.         NodeImpl bImpl = (NodeImpl)b;
  160.  
  161.         if (aImpl == bImpl) {
  162.             throw new IllegalArgumentException("Two same nodes");
  163.         }
  164.         if (aImpl.owner.get() != graph) {
  165.             throw new IllegalArgumentException("Wrong owner");
  166.         }
  167.         if (bImpl.owner.get() != graph) {
  168.             throw new IllegalArgumentException("Wrong owner");
  169.         }
  170.  
  171.         if (aImpl.hashCode() < bImpl.hashCode()) {
  172.             final NodeImpl tmp = aImpl;
  173.             aImpl = bImpl;
  174.             bImpl = tmp;
  175.         }
  176.  
  177.         aImpl.to.remove(bImpl);
  178.         bImpl.from.remove(aImpl);
  179.     }
  180.  
  181.     public static Iterable<Node> nodeIterator(Graph graph) {
  182.         return new IterWrapper(graph);
  183.     }
  184.  
  185.     private final static class IterWrapper implements Iterable<Node> {
  186.         public Iterator<Node> iterator() {
  187.             final GraphImpl graphImpl = (GraphImpl)graph;
  188.             return graphImpl.nodes.iterator();
  189.         }
  190.  
  191.         public IterWrapper(Graph graph) {
  192.             this.graph = graph;
  193.         }
  194.  
  195.         Graph graph;
  196.     }
  197.  
  198.     public static Set<Map.Entry<Node, Integer>> nodeEdges(Graph graph, Node node) {
  199.         final GraphImpl graphImpl = (GraphImpl)graph;
  200.         final NodeImpl nodeImpl = (NodeImpl)node;
  201.  
  202.         if (nodeImpl.owner.get() != graph) {
  203.             throw new IllegalArgumentException("Wrong owner");
  204.         }
  205.  
  206.         return nodeImpl.to.entrySet();
  207.     }
  208.  
  209.     public static String label(Graph graph, Node node) {
  210.         final GraphImpl graphImpl = (GraphImpl)graph;
  211.         final NodeImpl nodeImpl = (NodeImpl)node;
  212.  
  213.         if (nodeImpl.owner.get() != graph) {
  214.             throw new IllegalArgumentException("Wrong owner");
  215.         }
  216.  
  217.         return nodeImpl.label;
  218.     }
  219.  
  220.     public static void remove(Graph graph, Node node) {
  221.         final GraphImpl graphImpl = (GraphImpl)graph;
  222.         final NodeImpl nodeImpl = (NodeImpl)node;
  223.  
  224.         if (nodeImpl.owner.get() != graph) {
  225.             throw new IllegalArgumentException("Wrong owner");
  226.         }
  227.  
  228.         for (final Node from: nodeImpl.from.keySet()) {
  229.             ((NodeImpl)from).to.remove(nodeImpl);
  230.         }
  231.     }
  232.  
  233.     private static String randLabel() {
  234.         final int len = 6;
  235.         final StringBuilder builder = new StringBuilder(len);
  236.         for (int ii = 0; ii < len; ++ii) {
  237.             builder.append(rngChars[rng.nextInt(rngChars.length)]);
  238.         }
  239.         return builder.toString();
  240.     }
  241.  
  242.     private static Graph randGraph() {
  243.         final Graph graph = Gr.newGraph();
  244.  
  245.         final int nodeCount = 10;
  246.         final Node[] nodes = new Node[nodeCount];
  247.  
  248.         for (int ii = 0; ii < nodes.length; ++ii) {
  249.             nodes[ii] = Gr.newNode(Gr.randLabel());
  250.         }
  251.  
  252.         for (int ii = 0; ii < nodes.length; ++ii) {
  253.             Gr.add(graph, nodes[ii]);
  254.         }
  255.  
  256.         for (int ii = 0; ii < nodes.length * nodes.length; ++ii) {
  257.             Node a;
  258.             Node b;
  259.             for (;;) {
  260.                 a = nodes[rng.nextInt(nodes.length)];
  261.                 b = nodes[rng.nextInt(nodes.length)];
  262.                 if (a != b) {
  263.                     break;
  264.                 }
  265.             }
  266.             Gr.link(graph, a, b, rng.nextInt(34) + 1);
  267.         }
  268.  
  269.         return graph;
  270.     }
  271.  
  272.     public static void main(String[] args) {
  273.         final Graph graph = randGraph();
  274.  
  275.         System.out.print("*");
  276.         for (final Node xx : Gr.nodeIterator(graph)) {
  277.             System.out.print("\t");
  278.             System.out.print(Gr.label(graph, xx));
  279.         }
  280.         System.out.println();
  281.  
  282.         for (final Node yy : Gr.nodeIterator(graph)) {
  283.             final String yyLabel = Gr.label(graph, yy);
  284.             System.out.print(yyLabel);
  285.             for (final Node xx : Gr.nodeIterator(graph)) {
  286.                 final int weight;
  287.                 if (Gr.connectsTo(graph, xx, yy)) {
  288.                     weight = Gr.weight(graph, xx, yy);
  289.                 } else {
  290.                     weight = -1;
  291.                 }
  292.                 final String weightStr = Integer.toString(weight);
  293.                 System.out.print("\t");
  294.                 System.out.print(weightStr);
  295.             }
  296.             System.out.println();
  297.         }
  298.     }
  299.  
  300.     private final static char[] rngChars;
  301.     private final static Random rng;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement