/* --------------------------------------------------------------------

Copyright (c) 1996 by Christopher R. Waterson. All rights reserved.

Permission to use, copy, modify, and distribute this software
and its documentation for NON-COMMERCIAL purposes and without
fee is hereby granted provided that this copyright notice
appears in all copies.

$Id: SparseGraph.java 1.1 1996/06/19 01:43:14 waterson Exp $

File:		SparseGraph.java
Synopsis:	A sparsely connected graph
Created:	1996/06/18 waterson
Modified:	$Date: 1996/06/19 01:43:14 $ $Author: waterson $

-------------------------------------------------------------------- */


import java.util.*;


/**
 * A sparsely connected graph.
 */

public class SparseGraph {
	/**
	 * The nodes in the graph
	 */

	Vector m_nodes;


	/**
	 * The arcs in the graph
	 */

	Vector m_arcs;


	/**
	 * Craete a new, empty sparse graph
	 */

	public
	SparseGraph() {
		m_nodes = new Vector();
		m_arcs = new Vector();
	}



	/**
	 * Add a node to the graph.
	 */

	public void
	addNode(Object node)
	throws GraphException {
		if (m_nodes.contains(node)) {
			throw new GraphException("Attempt to add duplicate node.");
		}

		m_nodes.addElement(node);
	}



	/**
	 * Add a directed arc to the graph. Note that the arc must be between two nodes
	 * that already exist in the graph.
	 */

	public void
	addArc(GraphArc arc)
	throws GraphException {
		if (! m_nodes.contains(arc.from())
		||  ! m_nodes.contains(arc.to()) ) {
			throw new GraphException("Attempt to create a arc between non-existant nodes.");
		}

		if (m_arcs.contains(arc)) {
			throw new GraphException("Attempt to duplicate an existing arc.");
		}

		m_arcs.addElement(arc);
	}



	/**
	 * Determine if the graph contains the specified directed arc.
	 */

	public boolean
	containsArc(GraphArc arc) {
		if ( m_arcs.contains(arc) ) {
			return true;
		} else {
			return false;
		}
	}



	/**
	 * Determine if the graph contains the specified node
	 */

	public boolean
	containsNode(Object node) {
		if (m_nodes.contains(node)) {
			return true;
		} else {
			return false;
		}
	}



	/**
	 * Return all of the nodes in the graph
	 */

	public Enumeration
	nodes() {
		return m_nodes.elements();
	}



	/**
	 * Return all of the arcs in the graph
	 */

	public Enumeration
	arcs() {
		return m_arcs.elements();
	}
}


