?? network.java
字號:
// /** Same as <code>getMultigraphAdjacencyMatrix</code>, but it makes two passes // * through the graph:// * 1. it counts the edges between every node, so it can allocate the right size of arrays// * 2. it fills the arrays.// * // * This replaces the "array copy"s with a bunch of hash-table look-ups// * // * @see getMultigraphAdjacencyMatrix// */ // public Edge[][][] getMultigraphAdjacencyMatrix_II()// {// final int n = allNodes.numObjs;// final Edge[][][] matrix = new Edge[n][n][]; //I assume it filled with nulls?// int[][] counts = new int[n][n];//// //phase 1: counting// for(int x=0;x<n;x++)// {// int[] counts_x = counts[x];// Object sourceNode = allNodes.objs[x]; // final Bag edges = getEdgesOut(sourceNode);// // for(int i=0;i<edges.numObjs;i++)// counts_x[((IndexOutIn)indexOutInHash.get(((Edge)edges.objs[i]).getOtherNode(sourceNode))).index]++;// }// // //phase 2: memory allocation// for(int x=0;x<n;x++)// {// int[] counts_x = counts[x];// Edge[][] edges_x = matrix[x];// for(int y=0;y<n;y++)// {// int count_xy= counts_x[y];// edges_x[y]=(count_xy==0)? emptyEdgeArray: new Edge[count_xy];// }// }// // //phase 3: copy the data// for(int x=0;x<n;x++)// {// int[] counts_x = counts[x];// Edge[][] edges_x = matrix[x];// Object fromNode = allNodes.objs[x];// final Bag edges = getEdgesOut(fromNode);// // for(int i=0;i<edges.numObjs;i++)// {// Edge e = (Edge)edges.objs[i];// int y = ((IndexOutIn)indexOutInHash.get(e.getOtherNode(fromNode))).index;// edges_x[y][--counts_x[y]]=e;// }// }// // return matrix;// } /** Get all edges that leave a node. Do NOT modify this Bag -- it is used internally. */ // this bizarre construction puts us just at 32 bytes so we can be inlined public Bag getEdgesOut( final Object node ) { IndexOutIn ioi = (IndexOutIn)(indexOutInHash.get(node)); Bag b; if (ioi==null || (b=ioi.out)==null) return emptyBag; return b; } /** Get all edges that enter a node. Do NOT modify this Bag -- it is used internally. */ // this bizarre construction puts us just at 32 bytes so we can be inlined public Bag getEdgesIn( final Object node ) { IndexOutIn ioi = (IndexOutIn)(indexOutInHash.get(node)); Bag b; if (ioi==null || (b=ioi.in)==null) return emptyBag; return b; } /** Get all the edges that enter or leave a node. If a Bag is provided, it will be cleared, then filled and returned. Else a Bag will be constructed and returned. If the graph is undirected, then edgesIn and edgesOut should be the same thing, and so this is roughly equivalent to bag.addAll(getEdgesIn(node)); If the graph is directed, then both the edgesIn AND the edgesOut are added to the Bag. Generally speaking you should try to use the more efficient getEdgesIn(...) and getEdgesOut(...) methods instead if you can. */ public Bag getEdges( final Object node, Bag bag ) { if( !directed ) { if ( bag == null) bag = new Bag(); else bag.clear(); IndexOutIn ioi = (IndexOutIn)(indexOutInHash.get(node)); if (ioi==null) return bag; if (ioi.in!=null && ioi.in.numObjs>0) bag.addAll(ioi.in); //if (ioi.out!=null && ioi.in!=ioi.out && ioi.out.numObjs>0) bag.addAll(ioi.out); return bag; } else { if ( bag == null) bag = new Bag(); else bag.clear(); IndexOutIn ioi = (IndexOutIn)(indexOutInHash.get(node)); if (ioi==null) return bag; if (ioi.in!=null && ioi.in.numObjs>0) bag.addAll(ioi.in); if (ioi.out!=null && ioi.out.numObjs>0) bag.addAll(ioi.out); return bag; } } /** Add a node */ public void addNode( final Object node ) { if( indexOutInHash.get( node ) != null ) // if the object already exists return; allNodes.add( node ); IndexOutIn ioih = new IndexOutIn( allNodes.numObjs-1, null, null ); indexOutInHash.put( node, ioih ); } /** Add an edge, storing info as the edge's associated information object. If you add an edge, and its nodes have not been added yet, they will automatically be added as well. */ public void addEdge( final Object from, final Object to, final Object info ) { addEdge( new Edge( from, to, info ) ); } /** Add an edge. If you add an edge, and its nodes have not been added yet, they will automatically be added as well. Throws an exception if the edge is null or if it's already added to a Field (including this one). */ public void addEdge( final Edge edge ) { if (edge==null) throw new RuntimeException("Attempted to add a null Edge."); // assert ownership if (edge.owner!=null) throw new RuntimeException("Attempted to add an Edge already added elsewhere"); edge.owner = this; edge.indexFrom = 0; edge.indexTo = 0; IndexOutIn outNode = (IndexOutIn)(indexOutInHash.get(edge.from)); if( outNode==null) { addNode( edge.from ); outNode = (IndexOutIn)(indexOutInHash.get(edge.from)); } if( outNode.out == null ) { if(directed) outNode.out = new Bag(); else { if(outNode.in!=null) outNode.out = outNode.in; else outNode.out = outNode.in = new Bag(); } } outNode.out.add( edge ); edge.indexFrom = outNode.out.numObjs-1; IndexOutIn inNode = (IndexOutIn)(indexOutInHash.get(edge.to)); if( inNode==null) { addNode( edge.to ); inNode = (IndexOutIn)(indexOutInHash.get(edge.to)); } if( inNode.in == null ) { if(directed) inNode.in = new Bag(); else { if(inNode.out!=null) inNode.in = inNode.out; else inNode.in = inNode.out = new Bag(); } } inNode.in.add( edge ); edge.indexTo = inNode.in.numObjs-1; } /** Removes an edge and returns it. The edge will still retain its info, to, and from fields, so you can add it again with addEdge. Returns null if the edge is null or if there is no such edge added to the field. */ public Edge removeEdge( final Edge edge ) { if( edge == null ) return null; // remove ownership if (edge.owner != this) return null; edge.owner = null; // remove the edge out the "out" node's "out" bag final Bag outNodeBag = ((IndexOutIn)(indexOutInHash.get(edge.from))).out; outNodeBag.remove( edge.indexFrom ); if( outNodeBag.numObjs > edge.indexFrom ) ((Edge)(outNodeBag.objs[edge.indexFrom])).indexFrom = edge.indexFrom; // remove the edge out the "in" node's "in" bag final Bag inNodeBag = ((IndexOutIn)(indexOutInHash.get(edge.to))).in; inNodeBag.remove( edge.indexTo ); if( inNodeBag.numObjs > edge.indexTo ) ((Edge)(inNodeBag.objs[edge.indexTo])).indexTo = edge.indexTo; // return the edge return edge; } /** Removes a node, deleting all incoming and outgoing edges from the Field as well. Returns the node, or null if there is no such node in the field. */ public Object removeNode( final Object node ) { IndexOutIn ioi = (IndexOutIn)(indexOutInHash.get(node)); if (ioi == null) return null; // remove all edges coming in the node while( ioi.out != null && ioi.out.numObjs > 0 ) { removeEdge( (Edge)(ioi.out.objs[0]) ); } // remove all edges leaving the node while( ioi.in != null && ioi.in.numObjs > 0 ) { removeEdge( (Edge)(ioi.in.objs[0]) ); } // remove the node allNodes.remove(ioi.index); if (allNodes.numObjs > ioi.index) // update the index of the guy who just got moved ((IndexOutIn)(indexOutInHash.get(allNodes.objs[ioi.index]))).index = ioi.index; // return the edge return node; } /** Removes all nodes, deleting all edges from the Field as well. Returns the nodes as a Bag, which you are free to modify as it's no longer used internally by the Network. */ public Bag clear() { indexOutInHash = new HashMap(); Bag retval = allNodes; allNodes = new Bag(); return retval; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -