<commit_before>﻿namespace Elementary.Hierarchy
{
    using System.Collections.Generic;
    using Elementary.Hierarchy.Generic;

    /// <summary>
    /// Provides extensions to classes implementing <see cref="IHasParentNode{TNode}"/>
    /// </summary>
    public static class HasParentNodeExtensions
    {
        /// <summary>
        /// Returns the parent node of the <paramref name="startNode"/>.
        /// If the node hasn't a parent an <see cref="InvalidOperationException"/> is thrown.
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <param name="startNode"></param>
        /// <returns>A parent node instance</returns>
        public static TNode Parent<TNode>(this TNode startNode)
            where TNode : class, IHasParentNode<TNode>
        {
            return startNode.Parent((TNode n) =>
            {
                if (!n.HasParentNode)
                    return (false, default(TNode));
                var parent = n.ParentNode;
                return (parent != null, parent);
                
            });
        }

        /// <summary>
        /// Traverses the tree upwards to the root node. The visited nodes are returned inside the <see cref="IEnumerable{TNode}"/>.
        /// The <paramref name="startNode"/> isn't returned.
        /// </summary>
        /// <typeparam name="TNode">Type of the node, implements <see cref="IHasParentNode{TNode}"/></typeparam>
        /// <param name="startNode">reference to the node to start from</param>
        /// <returns>
        /// An <see cref="IEnumerable{TNode}"/> containing all visited nodes but not <paramref name="startNode"/>.
        /// If the <paramref name="startNode"/> has no parent the collection is empty.
        /// </returns>
        public static IEnumerable<TNode> Ancestors<TNode>(this TNode startNode)
            where TNode : IHasParentNode<TNode>
        {
            return startNode.Ancestors((TNode n) =>
            {
                if (!n.HasParentNode)
                    return (false, default(TNode));

                return (true, n.ParentNode);
            });
        }

        /// <summary>
        /// Traverses the tree upwards to the root node. The visited nodes are returned inside the <see cref="IEnumerable{TNode}"/>.
        /// The <paramref name="startNode"/> is returned first.
        /// </summary>
        /// <typeparam name="TNode">Type of the node, implements <see cref="IHasParentNode{TNode}"/></typeparam>
        /// <param name="startNode">reference to the node to start from</param><commit_msg>Fixed: Parent() doesn't require a ref type anymore<commit_after>﻿namespace Elementary.Hierarchy
{
    using System.Collections.Generic;
    using Elementary.Hierarchy.Generic;

    /// <summary>
    /// Provides extensions to classes implementing <see cref="IHasParentNode{TNode}"/>
    /// </summary>
    public static class HasParentNodeExtensions
    {
        /// <summary>
        /// Returns the parent node of the <paramref name="startNode"/>.
        /// If the node hasn't a parent an <see cref="InvalidOperationException"/> is thrown.
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <param name="startNode"></param>
        /// <returns>A parent node instance</returns>
        public static TNode Parent<TNode>(this TNode startNode)
            where TNode : IHasParentNode<TNode>
        {
            return startNode.Parent((TNode n) =>
            {
                if (!n.HasParentNode)
                    return (false, default(TNode));
                return (true, n.ParentNode);
            });
        }

        /// <summary>
        /// Traverses the tree upwards to the root node. The visited nodes are returned inside the <see cref="IEnumerable{TNode}"/>.
        /// The <paramref name="startNode"/> isn't returned.
        /// </summary>
        /// <typeparam name="TNode">Type of the node, implements <see cref="IHasParentNode{TNode}"/></typeparam>
        /// <param name="startNode">reference to the node to start from</param>
        /// <returns>
        /// An <see cref="IEnumerable{TNode}"/> containing all visited nodes but not <paramref name="startNode"/>.
        /// If the <paramref name="startNode"/> has no parent the collection is empty.
        /// </returns>
        public static IEnumerable<TNode> Ancestors<TNode>(this TNode startNode)
            where TNode : IHasParentNode<TNode>
        {
            return startNode.Ancestors((TNode n) =>
            {
                if (!n.HasParentNode)
                    return (false, default(TNode));

                return (true, n.ParentNode);
            });
        }

        /// <summary>
        /// Traverses the tree upwards to the root node. The visited nodes are returned inside the <see cref="IEnumerable{TNode}"/>.
        /// The <paramref name="startNode"/> is returned first.
        /// </summary>
        /// <typeparam name="TNode">Type of the node, implements <see cref="IHasParentNode{TNode}"/></typeparam>
        /// <param name="startNode">reference to the node to start from</param>
{'fraction_non_alphanumeric': 0.1333984375, 'fraction_numerical': 0.0, 'mean_word_length': 2.0373665480427046, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1455957', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1326, 'n_words': 397}