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

📄 edgelist.java

📁 ALGAE是一个快速创建算法演示的框架。目前支持的算法实现语言包括java和c
💻 JAVA
字号:
package edu.odu.cs.zeil.AlgAE.Client.DataViewer.DataGraph;import edu.odu.cs.zeil.AlgAE.Direction;import edu.odu.cs.zeil.AlgAE.Client.DataViewer.DataGraph.Edge;import java.util.Enumeration;import java.util.Vector;/** * A collection of edges. */public class EdgeList{  private Vector list;  /**   *  Create an empty list.   */  public EdgeList ()  {    list = new Vector();  }    /**   *  Create a copy of an existing list.   *   *  @param e the EdgeList to be copied   */  public EdgeList (EdgeList e)  {    list = (Vector)(e.list.clone());  }  /**   * How many elements in this list?   */  public int size()  {    return list.size();  }  /**   *  Allow enumeration of all edges in this list.   */  public Enumeration edges()  {    return list.elements();  }  /**   *  Add an edge to the list.   *   *  @param e the edge to add   */  public void add (Edge e)  {    list.addElement (e);  }  /**   * Finds an existing edge in this EdgeList   *  @param source       Node that the edge points from. May not be null.   *  @param destination  Node that the edge points to. May be null.   *  @param dir    Direction from which the edge leaves this node.   *   *  @return Position within <code>list</code> of an existing edge   *     matching the above parameters, or -1 if no such edge exists.   */  private int findPosition (Node source, Node destination, int dir)  {    int i;    for (i = 0; i < list.size(); ++i)      {	Edge e = (Edge)(list.elementAt(i));	if ((e.source == source) &&	    (e.destination == destination) &&	    (e.dir == dir))	  {	    return i;	  }      }    return -1;  }  /**   * Finds an existing edge in this EdgeList   *  @param source       Node that the edge points from. May not be null.   *  @param destination  Node that the edge points to. May be null.   *   *  @return Position within <code>list</code> of an existing edge   *     matching the above parameters, or -1 if no such edge exists.   */  private int findPosition (Node source, Node destination)  {    int i;    for (i = 0; i < list.size(); ++i)      {	Edge e = (Edge)(list.elementAt(i));	if ((e.source == source) &&	    (e.destination == destination))	  {	    return i;	  }      }    return -1;  }  /**   * Finds an existing edge in this EdgeList.   *  @param source       Node that the edge points from. May not be null.   *  @param destination  Node that the edge points to. May be null.   *  @param dir    Direction from which the edge leaves this node.   *   *  @return An edge matching the above parameters, or null if no such   *          edge exists.   */  public Edge find (Node source, Node destination, int dir)  {    int i = findPosition (source, destination, dir);    if (i >= 0)      return (Edge)(list.elementAt(i));    else      return null;  }  /**   * Finds an existing edge in this EdgeList.   *  @param source       Node that the edge points from. May not be null.   *  @param destination  Node that the edge points to. May be null.   *   *  @return An edge matching the above parameters, or null if no such   *          edge exists.   */  public Edge find (Node source, Node destination)  {    int i = findPosition (source, destination);    if (i >= 0)      return (Edge)(list.elementAt(i));    else      return null;  }  /**   * Finds an existing edge in this EdgeList.   *  @param dir    Direction from which the edge leaves this node.   *   *  @return An edge matching the above parameters, or null if no such   *          edge exists.   */  public Edge find (int dir)  {    int i = findPosition (dir);    if (i >= 0)      return (Edge)(list.elementAt(i));    else      return null;  }  /**   * Is an edge in this list?   *  @param edge    Edge to search for   *   *  @return True if the edge  is in this list   */  public boolean contains (Edge e)  {    return findPosition(e) > 0;  }  /**   * Is an edge in this list?   *  @param source       Node that the edge points from. May not be null.   *  @param destination  Node that the edge points to. May be null.   *  @param dir    Direction from which the edge leaves this node.   *   *  @return True if the edge  is in this list   */  public boolean contains (Node source, Node destination, int dir)  {    return findPosition(source, destination, dir) > 0;  }  /**   * Is an edge in this list?   *  @param dir    Direction from which the edge leaves this node.   *   *  @return True if such an edge  is in this list   */  public boolean contains (int dir)  {    return findPosition(dir) > 0;  }  /**   * Finds an existing edge from this node.   *  @param dir    Direction from which the edge leaves this node.   *   *  @return Position within <code>list</code> of an existing edge   *     matching the above parameters, or -1 if no such edge exists.   */  private int findPosition (int dir)  {    int i;    for (i = 0; i < list.size(); ++i)      {	Edge e = (Edge)(list.elementAt(i));	if (e.dir == dir)	  {	    return i;	  }      }    return -1;  }  /**   * Finds an existing edge from this node.   *  @param edge    An edge to search for   *   *  @return Position within <code>list</code> of an existing edge   *     matching the above parameters, or -1 if no such edge exists.   */  private int findPosition (Edge edge)  {    return list.indexOf(edge);  }  /**   *  Remove an edge from this list.   *   *  @param e The edge to remove.   *  @return true if the edge was in the list   */  public boolean remove (Edge e)  {    return list.removeElement (e);  }    /**   *  Remove an edge from this list.   *   *  @param source       Node that the edge points from. May not be null.   *  @param destination  Node that the edge points to. May be null.   *  @param dir    Direction from which the edge leaves this node.   *   *  @return true if the edge was in the list   */  public boolean remove (Node source, Node destination, int dir)  {    int i = findPosition (source, destination, dir);    if (i >= 0)      list.removeElementAt (i);    return (i >= 0);  }    /**   *  Remove an edge from this list.   *   *  @param dir    Direction from which the edge leaves this node.   *   *  @return true if the edge was in the list   */  public boolean remove (int dir)  {    int i = findPosition (dir);    if (i >= 0)      list.removeElementAt (i);    return (i >= 0);  }    /**   *  Remove all edges from this list.   */  public void clear ()  {    list.removeAllElements ();  }  }

⌨️ 快捷键说明

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