/* * Graph.cs * Authors: * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections.Generic; using System.Text; using ColonySim.Structures; namespace ColonySim.Graphs { /// /// Represents the graph that makes up the connections for mapping the /// flow and gathering of resources through the Colony. /// internal abstract class Graph { #region Fields // The list of all nodes in the graph protected List mNodes = new List(); #endregion #region Collection /// /// Add the node to the graph. /// /// The node for the graph. public virtual void AddNode(Node pNode) { // Add the node if we don't have it if (!mNodes.Contains(pNode)) { // Add the node mNodes.Add(pNode); } } /// /// Remove the given node from the graph. /// /// The node to remove. /// Whether the node was removed successfully or not. public virtual bool RemoveNode(Node pNode) { // Remove it from the list of nodes bool retVal = mNodes.Remove(pNode); // Remove this from each of the adjacent nodes adjacencies foreach (Node node in pNode.Adjacencies) { // Remove the adjacency node.RemoveAdjacency(pNode); } // Return return retVal; } #endregion #region Utility /// /// Creates and edge between the two nodes by adding them to the /// correct adjacency lists. /// /// The first node. /// The second node. public abstract void CreateEdge(Node pA, Node pB); /// /// Removes an edge from the two nodes by removing the adjacencies that /// link them together. /// /// The first node. /// The second node. public abstract void DestroyEdge(Node pA, Node pB); #endregion } }