From: <ste...@us...> - 2009-05-08 12:01:53
|
Revision: 4267 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4267&view=rev Author: steverstrong Date: 2009-05-08 12:01:51 +0000 (Fri, 08 May 2009) Log Message: ----------- Fix for test Legacy.MasterDetailTest.MasterDetail Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Tree/HqlSqlWalkerTreeNodeStream.cs Modified: trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Tree/HqlSqlWalkerTreeNodeStream.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Tree/HqlSqlWalkerTreeNodeStream.cs 2009-05-08 06:07:00 UTC (rev 4266) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Tree/HqlSqlWalkerTreeNodeStream.cs 2009-05-08 12:01:51 UTC (rev 4267) @@ -12,27 +12,82 @@ 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) { - // Adding a child to the current node. If currently no children, then also need to insert Down & Up nodes - bool needUp = false; - int insertPoint = nodes.IndexOf(parent) + parent.ChildCount + 1; + if (child.ChildCount > 0) + { + throw new InvalidOperationException("Currently do not support adding nodes with children"); + } - if (parent.ChildCount == 0) + 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); - needUp = true; + insertPoint++; // We've just added a new node } - insertPoint++; // We either just inserted a Down node, or one existed already which we need to count + else + { + insertPoint = parentIndex + numberOfChildNodes; + } parent.AddChild(child); nodes.Insert(insertPoint, child); insertPoint++; - if (needUp) + 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; + } + else if (nodes[parentIndex + 1] != down) + { + // Next node is not a DOWN node, so we have no children + return 0; + } + else + { + // 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; + } + } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |