⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graph.java

📁 用applet实现很多应用小程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     */
    public Node getNodeFromKey(long key) {
        int n = getNodeIndex(key);
        return (n<0 ? null : getNode(n) );
    }
    
    /**
     * Get the in-degree of a node, the number of edges for which the node
     * is the target.
     * @param node the node id (node table row number)
     * @return the in-degree of the node
     */
    public int getInDegree(int node) {
        return m_links.getInt(node, INDEGREE);
    }
    
    /**
     * Get the in-degree of a node, the number of edges for which the node
     * is the target.
     * @param n the Node instance
     * @return the in-degree of the node
     */
    public int getInDegree(Node n) {
        nodeCheck(n, true);
        return getInDegree(n.getRow());
    }
    
    /**
     * Get the out-degree of a node, the number of edges for which the node
     * is the source.
     * @param node the node id (node table row number)
     * @return the out-degree of the node
     */
    public int getOutDegree(int node) {
        return m_links.getInt(node, OUTDEGREE);
    }
    
    /**
     * Get the out-degree of a node, the number of edges for which the node
     * is the source.
     * @param n the Node instance
     * @return the out-degree of the node
     */
    public int getOutDegree(Node n) {
        nodeCheck(n, true);
        return getOutDegree(n.getRow());
    }
    
    /**
     * Get the degree of a node, the number of edges for which a node
     * is either the source or the target.
     * @param node the node id (node table row number)
     * @return the total degree of the node
     */
    public int getDegree(int node) {
        return getInDegree(node) + getOutDegree(node);
    }
    
    /**
     * Get the degree of a node, the number of edges for which a node
     * is either the source or the target.
     * @param n the Node instance
     * @return the total degree of the node
     */
    public int getDegree(Node n) {
        nodeCheck(n, true);
        return getDegree(n.getRow());
    }
    
    // ------------------------------------------------------------------------
    // Edge Accessor Methods
    
    /**
     * Indicates if the edges of this graph are directed or undirected.
     * @return true if directed edges, false if undirected edges
     */
    public boolean isDirected() {
        return m_directed;
    }
    
    /**
     * Internal method for checking the validity of an edge.
     * @param e the Edge to check for validity
     * @param throwException true if this method should throw an Exception
     * when an invalid node is encountered
     * @return true is the edge is valid, false if invalid
     */
    protected boolean edgeCheck(Edge e, boolean throwException) {
        if ( !e.isValid() ) {
            if ( throwException ) {
                throw new IllegalArgumentException(
                    "Edge must be valid.");
            }
            return false;
        }
        if ( e.getGraph() != this ) {
            if ( throwException ) {
                throw new IllegalArgumentException(
                    "Edge must be part of this Graph.");
            }
            return false;
        }
        return true;
    }
    
    /**
     * Get the collection of edges as a TupleSet. Returns the same result as
     * {@link CompositeTupleSet#getSet(String)} using
     * {@link #EDGES} as the parameter.
     * @return the edges of this graph as a TupleSet instance
     */
    public TupleSet getEdges() {
        return getSet(EDGES);
    }

    /**
     * Get the backing edge table.
     * @return the table of edge values
     */
    public Table getEdgeTable() {
        return (Table)getSet(EDGES);
    }
    /**
     * Get the number of edges in this graph.
     * @return the number of edges
     */
    public int getEdgeCount() {
        return getEdgeTable().getRowCount();
    }

    /**
     * Get the Edge tuple instance corresponding to an edge id.
     * @param e an edge id (edge table row number)
     * @return the Node instance corresponding to the node id
     */
    public Edge getEdge(int e) {
        return ( e < 0 ? null : (Edge)m_edgeTuples.getTuple(e) );
    }
    
    /**
     * Returns an edge from the source node to the target node. This
     * method returns the first such edge found; in the case of multiple
     * edges there may be more.
     */
    public int getEdge(int source, int target) {
        int outd = getOutDegree(source); 
        if ( outd > 0 ) {
            int[] edges = (int[])m_links.get(source, OUTLINKS);
            for ( int i=0; i<outd; ++i ) {
                if ( getTargetNode(edges[i]) == target )
                    return edges[i];
            }
        }
        return -1;
    }
    
    /**
     * Get an Edge with given source and target Nodes. There may be times
     * where there are multiple edges between two nodes; in those cases
     * this method returns the first such edge found.
     * @param source the source Node
     * @param target the target Node
     * @return an Edge with given source and target nodes, or null if no
     * such edge is found.
     */
    public Edge getEdge(Node source, Node target) {
        nodeCheck(source, true);
        nodeCheck(target, true);
        return getEdge(getEdge(source.getRow(), target.getRow()));
    }
    
    /**
     * Get the source node id (node table row number) for the given edge
     * id (edge table row number).
     * @param edge an edge id (edge table row number)
     * @return the source node id (node table row number)
     */
    public int getSourceNode(int edge) {
        return getNodeIndex(getEdgeTable().getLong(edge, m_skey));
    }
    
    /**
     * Get the source Node for the given Edge instance.
     * @param e an Edge instance
     * @return the source Node of the edge
     */
    public Node getSourceNode(Edge e) {
        edgeCheck(e, true);
        return getNode(getSourceNode(e.getRow()));
    }
    
    /**
     * Get the target node id (node table row number) for the given edge
     * id (edge table row number).
     * @param edge an edge id (edge table row number)
     * @return the target node id (node table row number)
     */
    public int getTargetNode(int edge) {
        return getNodeIndex(getEdgeTable().getLong(edge, m_tkey));
    }
    
    /**
     * Get the target Node for the given Edge instance.
     * @param e an Edge instance
     * @return the target Node of the edge
     */
    public Node getTargetNode(Edge e) {
        edgeCheck(e, true);
        return getNode(getTargetNode(e.getRow()));
    }
    
    /**
     * Given an edge id and an incident node id, return the node id for
     * the other node connected to the edge.
     * @param edge an edge id (edge table row number)
     * @param node a node id (node table row number). This node id must
     * be connected to the edge
     * @return the adjacent node id
     */
    public int getAdjacentNode(int edge, int node) {
        int s = getSourceNode(edge);
        int d = getTargetNode(edge);
        
        if ( s == node ) {
            return d;
        } else if ( d == node ) {
            return s;
        } else {
            throw new IllegalArgumentException(
                "Edge is not incident on the input node.");
        }
    }
    
    /**
     * Given an Edge and an incident Node, return the other Node
     * connected to the edge.
     * @param e an Edge instance
     * @param n a Node instance. This node must
     * be connected to the edge
     * @return the adjacent Node
     */
    public Node getAdjacentNode(Edge e, Node n) {
        edgeCheck(e, true);
        nodeCheck(n, true);
        return getNode(getAdjacentNode(e.getRow(), n.getRow()));
    }

    // ------------------------------------------------------------------------
    // Iterators
    
    // -- table row iterators ----
    
    /**
     * Get an iterator over all node ids (node table row numbers).
     * @return an iterator over all node ids (node table row numbers)
     */
    public IntIterator nodeRows() {
        return getNodeTable().rows();
    }

    /**
     * Get an iterator over all edge ids (edge table row numbers).
     * @return an iterator over all edge ids (edge table row numbers)
     */
    public IntIterator edgeRows() {
        return getEdgeTable().rows();
    }
    
    /**
     * Get an iterator over all edge ids for edges incident on the given node.
     * @param node a node id (node table row number)
     * @return an iterator over all edge ids for edges incident on the given
     * node
     */
    public IntIterator edgeRows(int node) {
        return edgeRows(node, UNDIRECTED);
    }
    
    /**
     * Get an iterator edge ids for edges incident on the given node.
     * @param node a node id (node table row number)
     * @param direction the directionality of the edges to include. One of
     * {@link #INEDGES} (for in-linking edges),
     * {@link #OUTEDGES} (for out-linking edges), or
     * {@link #UNDIRECTED} (for all edges).
     * @return an iterator over all edge ids for edges incident on the given
     * node
     */
    public IntIterator edgeRows(int node, int direction) {
        if ( direction==OUTEDGES ) {
            int[] outedges = (int[])m_links.get(node, OUTLINKS);
            return new IntArrayIterator(outedges, 0, getOutDegree(node));
        } else if ( direction==INEDGES ) {
            int[] inedges = (int[])m_links.get(node, INLINKS);
            return new IntArrayIterator(inedges, 0, getInDegree(node));
        } else if ( direction==UNDIRECTED ) {
            return new CompositeIntIterator(
                edgeRows(node, OUTEDGES), edgeRows(node, INEDGES));
        } else {
            throw new IllegalArgumentException("Unrecognized edge type: " 
                + direction + ". Type should be one of Graph.OUTEDGES, "
                + "Graoh.INEDGES, or Graph.ALL");
        }
    }
    
    /**
     * Get an iterator over all edges that have the given node as a target.
     * That is, edges that link into the given target node.
     * @param node a node id (node table row number)
     * @return an iterator over all edges that have the given node as a target
     */
    public IntIterator inEdgeRows(int node) {
        return edgeRows(node, INEDGES);
    }

    /**
     * Get an iterator over all edges that have the given node as a source.
     * That is, edges that link out from the given source node.
     * @param node a node id (node table row number)
     * @return an iterator over all edges that have the given node as a source
     */
    public IntIterator outEdgeRows(int node) {
        return edgeRows(node, OUTEDGES);
    }
    
    // -- tuple iterators --
    
    /**
     * Get an iterator over all nodes in the graph.
     * @return an iterator over Node instances
     */
    public Iterator nodes() {
        return m_nodeTuples.iterator(nodeRows());
    }

    /**
     * Get an iterator over all neighbor nodes for the given Node in the graph.
     * @param n a Node in the graph
     * @return an iterator over all Nodes connected to the input node
     */
    public Iterator neighbors(Node n) {
        return new NeighborIterator(n, edges(n));
    }

    /**
     * Get an iterator over all in-linking neighbor nodes for the given Node.
     * @param n a Node in the graph
     * @return an iterator over all Nodes that point to the input target node
     */
    public Iterator inNeighbors(Node n) {
        return new NeighborIterator(n, inEdges(n));
    }

    /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -