<commit_before>﻿using System;
using Antlr.Runtime.Tree;

namespace NHibernate.Hql.Ast.ANTLR.Tree
{
	[CLSCompliant(false)]
	public class HqlSqlWalkerTreeNodeStream : CommonTreeNodeStream
	{
		public HqlSqlWalkerTreeNodeStream(object tree) : base(tree) {}
		public HqlSqlWalkerTreeNodeStream(ITreeAdaptor adaptor, object tree) : base(adaptor, tree) {}

		public HqlSqlWalkerTreeNodeStream(ITreeAdaptor adaptor, object tree, int initialBufferSize)
			: base(adaptor, tree, initialBufferSize) {}

		/// <summary>
		/// Insert a new node into both the Tree and the Node Array.  Add DOWN & UP nodes if needed
		/// </summary>
		/// <param name="parent">The parent node</param>
		/// <param name="child">The child node</param>
		public void InsertChild(IASTNode parent, IASTNode child)
		{
			if (child.ChildCount > 0)
			{
				throw new InvalidOperationException("Currently do not support adding nodes with children");
			}

			int parentIndex = nodes.IndexOf(parent);
			int numberOfChildNodes = NumberOfChildNodes(parentIndex);
			int insertPoint;
			
			if (numberOfChildNodes == 0)
			{
				insertPoint = parentIndex + 1;  // We want to insert immediately after the parent
				nodes.Insert(insertPoint, down);
				insertPoint++;  // We've just added a new node
			}
			else
			{
				insertPoint = parentIndex + numberOfChildNodes;
			}

			parent.AddChild(child);
			nodes.Insert(insertPoint, child);
			insertPoint++;

			if (numberOfChildNodes == 0)
			{
				nodes.Insert(insertPoint, up);
			}
		}

		/// <summary>
		/// Count the number of child nodes (including DOWNs & UPs) of a parent node
		/// </summary>
		/// <param name="parentIndex">The index of the parent in the node array</param>
		/// <returns>The number of child nodes</returns>
		int NumberOfChildNodes(int parentIndex)
		{
			if (nodes.Count -1 == parentIndex)
			{
				// We are at the end
				return 0;
			}
			
			if (nodes[parentIndex + 1] != down)
			{
				// Next node is not a DOWN node, so we have no children
				return 0;
			}

			// Count the DOWNs & UPs
			int downCount = 0;
			int index = 1;
			do
			{
				if (nodes[parentIndex + index] == down)
				{
					downCount++;
				}
				else if (nodes[parentIndex + index] == up)
				{
					downCount--;
				}

				index++;
				
			} while (downCount > 0);

			return index - 1;
		}
	}
}<commit_msg>Minor (removed warn on XML comment)<commit_after>﻿using System;
using Antlr.Runtime.Tree;

namespace NHibernate.Hql.Ast.ANTLR.Tree
{
	[CLSCompliant(false)]
	public class HqlSqlWalkerTreeNodeStream : CommonTreeNodeStream
	{
		public HqlSqlWalkerTreeNodeStream(object tree) : base(tree) {}
		public HqlSqlWalkerTreeNodeStream(ITreeAdaptor adaptor, object tree) : base(adaptor, tree) {}

		public HqlSqlWalkerTreeNodeStream(ITreeAdaptor adaptor, object tree, int initialBufferSize)
			: base(adaptor, tree, initialBufferSize) {}

		/// <summary>
		/// Insert a new node into both the Tree and the Node Array. Add DOWN and UP nodes if needed.
		/// </summary>
		/// <param name="parent">The parent node</param>
		/// <param name="child">The child node</param>
		public void InsertChild(IASTNode parent, IASTNode child)
		{
			if (child.ChildCount > 0)
			{
				throw new InvalidOperationException("Currently do not support adding nodes with children");
			}

			int parentIndex = nodes.IndexOf(parent);
			int numberOfChildNodes = NumberOfChildNodes(parentIndex);
			int insertPoint;
			
			if (numberOfChildNodes == 0)
			{
				insertPoint = parentIndex + 1;  // We want to insert immediately after the parent
				nodes.Insert(insertPoint, down);
				insertPoint++;  // We've just added a new node
			}
			else
			{
				insertPoint = parentIndex + numberOfChildNodes;
			}

			parent.AddChild(child);
			nodes.Insert(insertPoint, child);
			insertPoint++;

			if (numberOfChildNodes == 0)
			{
				nodes.Insert(insertPoint, up);
			}
		}

		/// <summary>
		/// Count the number of child nodes (including DOWNs & UPs) of a parent node
		/// </summary>
		/// <param name="parentIndex">The index of the parent in the node array</param>
		/// <returns>The number of child nodes</returns>
		int NumberOfChildNodes(int parentIndex)
		{
			if (nodes.Count -1 == parentIndex)
			{
				// We are at the end
				return 0;
			}
			
			if (nodes[parentIndex + 1] != down)
			{
				// Next node is not a DOWN node, so we have no children
				return 0;
			}

			// Count the DOWNs & UPs
			int downCount = 0;
			int index = 1;
			do
			{
				if (nodes[parentIndex + index] == down)
				{
					downCount++;
				}
				else if (nodes[parentIndex + index] == up)
				{
					downCount--;
				}

				index++;
				
			} while (downCount > 0);

			return index - 1;
		}
	}
}
{'fraction_non_alphanumeric': 0.11079077429983525, 'fraction_numerical': 0.004942339373970346, 'mean_word_length': 2.550438596491228, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 31, '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': '936810', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1477, 'n_words': 434}