// Methods: /// <summary> /// Adds a dependent to the object /// </summary> /// <param name="n"></param> public void AddDependent(DependancyNode n) { if (!Dependents.ContainsKey(n.ToString())) { Dependents.Add(n.ToString(), n); } }
/// <summary> /// Removes all existing dependencies of the form (s,r). Then, for each /// t in newDependents, adds the dependency (s,t). /// Throws a ArgumentNullException if s or t is null. /// </summary> public void ReplaceDependents(string s, IEnumerable <string> newDependents) { if (s == null || newDependents == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } if (allNodes.ContainsKey(s)) { DependancyNode dependee = allNodes[s]; dependee.RemoveDependents(); foreach (string n in newDependents) { if (n != null) { DependancyNode newDependent; if (allNodes.ContainsKey(n)) { newDependent = allNodes[n]; } else { newDependent = new DependancyNode(n); allNodes.Add(n, newDependent); } dependee.AddDependent(newDependent); newDependent.AddDependee(dependee); } else { throw new ArgumentNullException("Can't replace dependents with null"); } } } }
/// <summary> /// Removes all existing dependencies of the form (r,t). Then, for each /// s in newDependees, adds the dependency (s,t). /// Throws a ArgumentNullException if s or t is null. /// </summary> public void ReplaceDependees(string t, IEnumerable <string> newDependees) { if (t == null || newDependees == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } if (allNodes.ContainsKey(t)) { DependancyNode dependent = allNodes[t]; Dictionary <string, DependancyNode> dependees = dependent.GetDependents(); dependent.RemoveDependees(); foreach (string n in newDependees) { if (n != null) { DependancyNode newDependee; if (allNodes.ContainsKey(n)) { newDependee = allNodes[n]; } else { newDependee = new DependancyNode(n); allNodes.Add(n, newDependee); } dependent.AddDependee(newDependee); newDependee.AddDependent(dependent); } else { throw new ArgumentNullException("Can't replace dependees with null"); } } } }
/// <summary> /// Adds the dependency (s,t) to this DependencyGraph. /// This has no effect if (s,t) already belongs to this DependencyGraph. /// Throws a ArgumentNullException if s or t is null. /// </summary> public void AddDependency(string s, string t) { if (s == null || t == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } DependancyNode dependeeNode; DependancyNode dependentNode; if (allNodes.ContainsKey(s)) { dependeeNode = allNodes[s]; } else { dependeeNode = new DependancyNode(s); allNodes.Add(s, dependeeNode); } if (allNodes.ContainsKey(t)) { dependentNode = allNodes[t]; } else { dependentNode = new DependancyNode(t); allNodes.Add(t, dependentNode); } dependeeNode.AddDependent(dependentNode); dependentNode.AddDependee(dependeeNode); }
/// <summary> /// Creates a DependencyGraph containing a copy of all the dependencies in the other graph /// Throws a ArgumentNullException if OtherGraph is null. /// </summary> /// <param name="OtherGraph">The other dependency graph from which the dependencies are copied</param> public DependencyGraph(DependencyGraph OtherGraph) { if (OtherGraph == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } allNodes = new Dictionary <string, DependancyNode>(); foreach (DependancyNode n in OtherGraph.allNodes.Values) { DependancyNode NewNode; if (allNodes.ContainsKey(n.ToString())) { NewNode = allNodes[n.ToString()]; } else { NewNode = new DependancyNode(n.ToString()); allNodes.Add(NewNode.ToString(), NewNode); } foreach (DependancyNode n2 in n.GetDependees().Values) { DependancyNode NewDependee; if (allNodes.ContainsKey(n2.ToString())) { NewDependee = allNodes[n2.ToString()]; } else { NewDependee = new DependancyNode(n2.ToString()); allNodes.Add(NewDependee.ToString(), NewDependee); } NewNode.AddDependee(NewDependee); } foreach (DependancyNode n2 in n.GetDependents().Values) { DependancyNode NewDependent; if (allNodes.ContainsKey(n2.ToString())) { NewDependent = allNodes[n2.ToString()]; } else { NewDependent = new DependancyNode(n2.ToString()); allNodes.Add(NewDependent.ToString(), NewDependent); } NewNode.AddDependent(NewDependent); } } }
/// <summary> /// Reports whether dependees(s) is non-empty. /// Throws a ArgumentNullException if s is null /// </summary> public bool HasDependees(string s) { if (s == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } if (allNodes.ContainsKey(s)) { DependancyNode dependent = allNodes[s]; return(dependent.GetDependees().Count > 0); } return(false); }
/// <summary> /// Removes the dependency (s,t) from this DependencyGraph. /// Does nothing if (s,t) doesn't belong to this DependencyGraph. /// Throws a ArgumentNullException if s or t is null. /// </summary> public void RemoveDependency(string s, string t) { if (s == null || t == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } if (allNodes.ContainsKey(s) && allNodes.ContainsKey(t)) { DependancyNode dependee = allNodes[s]; DependancyNode dependent = allNodes[t]; dependee.RemoveDependent(t); dependent.RemoveDependee(s); } }
/// <summary> /// Enumerates dependees(s). /// Throws a ArgumentNullException if s is null. /// </summary> public IEnumerable <string> GetDependees(string s) { if (s == null) { throw new ArgumentNullException("Argument(s) must be non-null"); } if (!allNodes.ContainsKey(s)) { return(new DependancyNode(s).GetDependees().Keys); } DependancyNode dependentNode = allNodes[s]; return(dependentNode.GetDependees().Keys); }