本文整理汇总了C#中GraphNode.AddChild方法的典型用法代码示例。如果您正苦于以下问题:C# GraphNode.AddChild方法的具体用法?C# GraphNode.AddChild怎么用?C# GraphNode.AddChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GraphNode
的用法示例。
在下文中一共展示了GraphNode.AddChild方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConstructDependencyGraph
/// <summary>
/// Construct a depdenency graph of the <see cref="Character"/>'s <see cref="ModifierSource"/>s.
/// </summary>
/// <param name="character">
/// The <see cref="Character"/> to generate the dependency graph for. This cannot be null.
/// </param>
/// <param name="modifierSources">
/// The already extracted list of <see cref="ModifierSource"/>s to use. This cannot be null.
/// </param>
/// <returns>
/// A <see cref="GraphNode{ModifierSource}"/> that is the root node of a dependency graph. The root
/// node can be ignored and is not part of the graph itself.
/// </returns>
/// <exception cref="ArgumentNullException">
/// No argument can be null.
/// </exception>
internal static GraphNode<ModifierSource> ConstructDependencyGraph(Character character, IList<ModifierSource> modifierSources)
{
if (character == null)
{
throw new ArgumentNullException("character");
}
if (modifierSources == null)
{
throw new ArgumentNullException("modifierSources");
}
GraphNode<ModifierSource> root;
IEqualityComparer<ModifierSource> comparer;
Dictionary<ModifierSource, GraphNode<ModifierSource>> graphNodeLookup;
// Rather than use "EqualityComparer<ModifierSource>.Default", use identity equality.
// This allows two instances of the same modifier source with the same name to be used
// within the graph. This can occur frequently with powers.
comparer = new IdentityEqualityComparer<ModifierSource>();
// Optimization for GraphNode.Find().
graphNodeLookup = new Dictionary<ModifierSource, GraphNode<ModifierSource>>();
// Construct a dependency graph.
root = new GraphNode<ModifierSource>(new NullModifierSource(), comparer);
foreach (ModifierSource modifierSource in modifierSources)
{
List<KeyValuePair<ModifierSource, ModifierSource>> dependencies;
GraphNode<ModifierSource> parentGraphNode;
GraphNode<ModifierSource> childGraphNode;
// Get the dependencies
dependencies = modifierSource.GetDependencies(character);
// For each dependency, find the source ModifierSource and add
// the destination under it.
foreach (KeyValuePair<ModifierSource, ModifierSource> dependency in dependencies)
{
// Find the parent of the dependency
if (!graphNodeLookup.TryGetValue(dependency.Key, out parentGraphNode))
{
parentGraphNode = new GraphNode<ModifierSource>(dependency.Key, comparer);
root.AddChild(parentGraphNode);
graphNodeLookup[dependency.Key] = parentGraphNode;
}
// Find the child of the dependency
if (!graphNodeLookup.TryGetValue(dependency.Value, out childGraphNode))
{
childGraphNode = new GraphNode<ModifierSource>(dependency.Value, comparer);
graphNodeLookup[dependency.Value] = childGraphNode;
}
// Add the child to the parent
parentGraphNode.AddChild(childGraphNode);
}
}
return root;
}
示例2: Test_AddChild_CircularReferenceOneLevel
public void Test_AddChild_CircularReferenceOneLevel()
{
GraphNode<string> parent = new GraphNode<string>("parent");
GraphNode<string> child = new GraphNode<string>("child");
parent.AddChild(child);
Assert.That(() => child.AddChild(parent),
Throws.ArgumentException.And.Property("ParamName").EqualTo("graphNode"));
}
示例3: Test_GetDepthFirstEnumerator
public void Test_GetDepthFirstEnumerator()
{
GraphNode<string> rootGraphNode = new GraphNode<string>("root");
GraphNode<string> leftParentGraphNode = new GraphNode<string>("leftParent");
GraphNode<string> leftFirstChildGraphNode = new GraphNode<string>("leftFirstChild");
GraphNode<string> leftSecondChildGraphNode = new GraphNode<string>("leftSecondChild");
GraphNode<string> rightParentGraphNode = new GraphNode<string>("rightParent");
GraphNode<string> rightChildGraphNode = new GraphNode<string>("rightChild");
GraphNode<string> rightGrandChildGraphNode = new GraphNode<string>("rightGrandChild");
rootGraphNode.AddChild(leftParentGraphNode);
leftParentGraphNode.AddChild(leftFirstChildGraphNode);
leftParentGraphNode.AddChild(leftSecondChildGraphNode);
rootGraphNode.AddChild(rightParentGraphNode);
rightParentGraphNode.AddChild(rightChildGraphNode);
rightChildGraphNode.AddChild(rightGrandChildGraphNode);
GraphNode<string>[] expected = new[]
{
rootGraphNode,
leftParentGraphNode,
leftFirstChildGraphNode,
leftSecondChildGraphNode,
rightParentGraphNode,
rightChildGraphNode,
rightGrandChildGraphNode
};
Assert.That(expected.Zip(rootGraphNode.GetDepthFirstEnumerator(), (x, y) => x.Equals(y)).All(x => x), Is.True);
}
示例4: Test_AddChild_Self
public void Test_AddChild_Self()
{
GraphNode<string> graphNode = new GraphNode<string>(string.Empty);
Assert.That(() => graphNode.AddChild(graphNode),
Throws.ArgumentException.And.Property("ParamName").EqualTo("graphNode"));
}
示例5: Test_IsParent_ChildsParent
public void Test_IsParent_ChildsParent()
{
GraphNode<string> parentGraphNode = new GraphNode<string>("parent");
GraphNode<string> childGraphNode = new GraphNode<string>("child");
parentGraphNode.AddChild(childGraphNode);
Assert.That(childGraphNode.IsParent(parentGraphNode));
}