public static void Write(this IRecursiveParent root, TextWriter writer) { Requires.NotNull(root, "root"); Requires.NotNull(writer, "writer"); const string Indent = " "; writer.Write(root); writer.NewLine += Indent; foreach (var child in root.Children) { writer.WriteLine(); var childAsParent = child as IRecursiveParent; if (childAsParent != null) { Write(childAsParent, writer); } else { writer.Write(child); } } writer.NewLine = writer.NewLine.Substring(0, writer.NewLine.Length - Indent.Length); }
public void ProjectTreeChangesSinceLargeTreeTest() { int seed = Environment.TickCount; Console.WriteLine("Random seed: {0}", seed); var random = new Random(seed); var largeTree = ConstructVeryLargeTree(random, 4, 500, 10000); int actualSize = largeTree.ProjectTree.GetSelfAndDescendents().Count(); Console.WriteLine("Total tree size: {0} nodes", actualSize); IRecursiveParent lt = largeTree.ProjectTree; //lt.Write(Console.Out); // Pick one random node to change. var changedNodeIdentity = ((uint)random.Next(actualSize)) + largeTree.Identity; var originalNode = largeTree.Find(changedNodeIdentity); var changedNode = originalNode.WithCaption("Changed!"); // Now diff the two versions. var changes = largeTree.ChangesSince(changedNode.Root); Assert.Equal(1, changes.Count); Assert.Equal(ChangeKind.Replaced, changes[0].Kind); Assert.Equal(ProjectTreeChangedProperties.Caption, changes[0].Changes & ~ProjectTreeChangedProperties.PositionUnderParent); Assert.Equal(changedNodeIdentity, changes[0].Identity); }
/// <summary>Checks whether a given object is among this object's descendents.</summary> public static bool HasDescendent(this IRecursiveParent parent, IRecursiveType possibleDescendent) { Requires.NotNull(parent, "parent"); Requires.NotNull(possibleDescendent, "possibleDescendent"); return(HasDescendent(parent, possibleDescendent.Identity)); }
/// <summary>Checks whether an object with the specified identity is among this object's descendents.</summary> public static bool HasDescendent(this IRecursiveParent parent, IdentityFieldType identity) { Requires.NotNull(parent, "parent"); IRecursiveType result; return(parent.TryFind(identity, out result) && result != parent); }
private static ParentedRecursiveType <IRecursiveParent <TRecursiveType>, TRecursiveType> WithParent <TRecursiveType>(this TRecursiveType value, IRecursiveParent <TRecursiveType> parent = null) where TRecursiveType : IRecursiveType { return(new ParentedRecursiveType <IRecursiveParent <TRecursiveType>, TRecursiveType>(value, parent)); }
/// <summary> /// Walks a project tree starting at a given node, using a breadth-first search pattern. /// </summary> /// <param name="root">The node at which to begin the search.</param> /// <returns>A sequence of the given node, and all its descendents. The given node always comes first in the sequence.</returns> public static IEnumerable <TRecursiveType> GetSelfAndDescendentsBreadthFirst <TRecursiveType>(this IRecursiveParent <TRecursiveType> root) where TRecursiveType : IRecursiveType { var nodesToVisit = new Queue <TRecursiveType>(); nodesToVisit.Enqueue((TRecursiveType)root); while (nodesToVisit.Count > 0) { var visiting = nodesToVisit.Dequeue(); yield return(visiting); var visitingAsParent = visiting as IRecursiveParent <TRecursiveType>; if (visitingAsParent != null && visitingAsParent.Children != null) { foreach (var child in visitingAsParent.Children) { nodesToVisit.Enqueue(child); } } } }
/// <summary> /// Returns a sequence starting with the given <paramref name="root"/> /// followed by its descendents in a depth-first search. /// </summary> /// <param name="root">The node at which to start enumeration.</param> /// <returns>A sequence of nodes beginning with <paramref name="root"/> and including all descendents.</returns> public static IEnumerable <TRecursiveType> GetSelfAndDescendents <TRecursiveType>(this IRecursiveParent <TRecursiveType> root) where TRecursiveType : IRecursiveType { yield return((TRecursiveType)root); var rootAsParent = root as IRecursiveParent <TRecursiveType>; if (rootAsParent != null && rootAsParent.Children != null) { foreach (TRecursiveType child in rootAsParent.Children) { var childAsParent = child as IRecursiveParent <TRecursiveType>; if (childAsParent != null) { foreach (var descendent in childAsParent.GetSelfAndDescendents()) { yield return(descendent); } } else { yield return(child); } } } }