/// <summary> /// Returns all nodes at a depth relative to the specified node. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child nodes. /// </param> /// <param name="node"> /// The node relative to which the level is returned. /// </param> /// <param name="depth"> /// The depth of the level to be returned relative to <paramref name="node"/>. This must /// be a nonnegative number. /// </param> /// <returns> /// An <see cref="System.Collections.Generic.IEnumerable<T>"/> that contains all the nodes /// nodes at a depth relative to the specified node. A depth of 0 will return the node /// itself, 1 all children of the node, etc... /// </returns> public static IEnumerable <T> GetLevel <T>(this ITreeWalker <T> walker, T node, int depth) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } if (depth < 0) { throw new ArgumentException("Depth must be nonnegative.", "depth"); } // If 'depth' is zero then return the node. // Otherwise, use GetDescendants to return all nodes at 'depth'. if (depth == 0) { return(Enumerable.Repeat(node, 1)); } else { return (walker .GetDescendants(node, (x, i) => i == depth)); } }
/// <summary> /// Gets the nearest descendants that match the <paramref name="key"/>. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child /// nodes. /// </param> /// <param name="node"> /// The root node to be queried. /// </param> /// <param name="key"> /// The key each node will be compared to. /// </param> /// <param name="comparer"> /// The <see cref="IEqualityComparer<T>"/> used to compare the key and the node. If /// this is null then the default <see cref="EqualityComparer<T>.Default"/> will be /// used. /// </param> /// <returns> /// An <see cref="System.Collections.Generic.IEnumerable<T>"/> that contains all the /// matching nodes in the tree ordered based on a pre-order traversal. /// </returns> public static IEnumerable <T> GetDescendants <T>( this ITreeWalker <T> walker, T node, T key, IEqualityComparer <T> comparer) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } if (key == null) { throw new ArgumentNullException("key"); } // If the comparer is null then use the defautl comparer for that type. comparer = comparer ?? EqualityComparer <T> .Default; return(walker.GetDescendants(node, n => comparer.Equals(n, key))); }
/// <summary> /// Gets the nearest descendants based on a predicate. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child /// nodes. /// </param> /// <param name="node"> /// The root node to be queried. /// </param> /// <param name="predicate"> /// A predicate to test each node for selection. The first argument is the current node /// being evaluated and the second argument is the depth of the current node relative to /// the original node that the query began on. /// </param> /// <returns> /// An <see cref="System.Collections.Generic.IEnumerable<T>"/> that contains all the /// matching nodes in the tree ordered based on a pre-order traversal. /// </returns> public static IEnumerable <T> GetDescendants <T>( this ITreeWalker <T> walker, T node, Func <T, int, bool> predicate) { // Validate parameters. if (node == null) { throw new ArgumentNullException("node"); } return (walker .GetDescendants( new T[] { node }, predicate)); }
/// <summary> /// Gets the nearest descendants based on a predicate. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child /// nodes. /// </param> /// <param name="nodes"> /// The root nodes to be queried. /// </param> /// <param name="predicate"> /// A predicate to test each node for selection. The argument is the current node being /// evaluated. /// </param> /// <returns> /// An <see cref="System.Collections.Generic.IEnumerable<T>"/> that contains all the /// matching nodes in the tree ordered based on a pre-order traversal. /// </returns> public static IEnumerable <T> GetDescendants <T>( this ITreeWalker <T> walker, IEnumerable <T> nodes, Func <T, bool> predicate) { // Validate parameters. if (predicate == null) { throw new ArgumentNullException("predicate"); } return (walker .GetDescendants( nodes, (n, i) => predicate.Invoke(n))); }
public static IEnumerable <TResult> GetDescendantsOfType <TSource, TResult>( this ITreeWalker <TSource> walker, IEnumerable <TSource> nodes) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (nodes == null) { throw new ArgumentNullException("nodes"); } foreach (object descendant in walker.GetDescendants(nodes, n => n is TResult)) { yield return((TResult)descendant); } }
/// <summary> /// Gets the nearest descendants that match the <paramref name="key"/>. /// </summary> /// <typeparam name="T">The type of elements in the tree.</typeparam> /// <param name="walker"> /// The <see cref="ITreeWalker<T>"/> that knows how to find the parent and child /// nodes. /// </param> /// <param name="node"> /// The root node to be queried. /// </param> /// <param name="key"> /// The key each node will be compared to. /// </param> /// <returns> /// An <see cref="System.Collections.Generic.IEnumerable<T>"/> that contains all the /// matching nodes in the tree ordered based on a pre-order traversal. /// </returns> public static IEnumerable <T> GetDescendants <T>( this ITreeWalker <T> walker, T node, T key) { // Validate parameters. if (walker == null) { throw new ArgumentNullException("walker"); } if (node == null) { throw new ArgumentNullException("node"); } if (key == null) { throw new ArgumentNullException("key"); } return(walker.GetDescendants(node, key, null)); }