Exemplo n.º 1
0
 public Graph copy() {
   WeightedArcSet list = new WeightedArcSet();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   ArcLabelledNodeIterator it = graph.nodeIterator();
   while (it.hasNext()) {
     Integer aux1 = it.nextInt();
     Integer aux2 = null;
     ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors();
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux2, aux1, suc.label().getFloat());
         list.add(arc);
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   Graph result = new Graph(list.toArray(new WeightedArc[0]));
   result.nodes.clear();
   result.nodesReverse.clear();
   for (Integer n : this.nodes.keySet()) {
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
     result.nodesReverse.put(this.nodes.get(n), n);
     result.nodes.put(n, this.nodes.get(n));
   }
   return result;
 }
Exemplo n.º 2
0
 public static Graph merge(Graph g1, Graph g2) {
   int commit = 0;
   WeightedArcSet list = new WeightedArcSet();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   ArcLabelledNodeIterator it1 = g1.graph.nodeIterator();
   while (it1.hasNext()) {
     Integer aux1 = it1.nextInt();
     ArcLabelledNodeIterator.LabelledArcIterator suc = it1.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < g1.graph.numNodes()))
       try {
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux1, aux2, suc.label().getFloat());
         list.add(arc);
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   ArcLabelledNodeIterator it2 = g2.graph.nodeIterator();
   while (it2.hasNext()) {
     Integer aux1 = it2.nextInt();
     ArcLabelledNodeIterator.LabelledArcIterator suc = it2.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < g2.graph.numNodes()))
       try {
         int aaux1 = aux1 + g1.numNodes();
         int aaux2 = aux2 + g1.numNodes();
         if (g1.nodes.get(aux1) != null && g1.nodes.get(aux1).equals(g2.nodes.get(aux1)))
           aaux1 = g1.nodesReverse.get(g2.nodes.get(aux1));
         if (g1.nodes.get(aux2) != null && g1.nodes.get(aux2).equals(g2.nodes.get(aux2)))
           aaux2 = g1.nodesReverse.get(g2.nodes.get(aux2));
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux1, aux2, suc.label().getFloat());
         list.add(arc);
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   Graph result = new Graph(list.toArray(new WeightedArc[0]));
   result.nodes.clear();
   result.nodesReverse.clear();
   for (Integer n : g1.nodes.keySet()) {
     result.nodesReverse.put(g1.nodes.get(n), n);
     result.nodes.put(n, g1.nodes.get(n));
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
   }
   for (Integer n : g2.nodes.keySet()) {
     int nn = n + g1.numNodes();
     if (g1.nodes.get(n) != null && g1.nodes.get(n).equals(g2.nodes.get(n)))
       nn = g1.nodesReverse.get(g2.nodes.get(n));
     result.nodesReverse.put(g2.nodes.get(n), nn);
     result.nodes.put(nn, g2.nodes.get(n));
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
   }
   result.iterator = result.nodeIterator();
   return result;
 }