public IEnumerable <SharpRow> InsertParentRows(IEnumerable <SharpRow> rows, bool skipFirstParentRow = false) { SharpNodeStack stack = new SharpNodeStack(); foreach (var row in rows) { var nodeRow = row as SharpNodeRow; if (nodeRow == null) { yield return(row); continue; } if (skipFirstParentRow) { stack.Enqueue(nodeRow.Node); skipFirstParentRow = false; yield return(row); continue; } if (stack.Top == nodeRow.Node.Parent) { if (!nodeRow.Node.IsValueNode) { stack.Enqueue(nodeRow.Node); } yield return(row); continue; } while (stack.Top != null && !stack.Top.HasDescendant(nodeRow.Node)) { stack.Dequeue(); } if (stack.Top == null || stack.Top.HasDescendant(nodeRow.Node)) { var nodes = nodeRow.Node.PathTo(stack.Top); foreach (var node in nodes) { stack.Enqueue(node); yield return(new SharpNodeRow(node)); } if (!nodeRow.Node.IsValueNode) { stack.Enqueue(nodeRow.Node); } yield return(row); } } }
public List <SharpNode> PathFrom(SharpNode ancestor) { if (Parent == null) { return(new List <SharpNode>()); } SharpNodeStack stack = new SharpNodeStack(); SharpNode start = Parent; if (IsValueNode) { var nodePath = Path; if (nodePath.EndsWith("/#")) { nodePath = nodePath.Substring(0, nodePath.Length - 2); } if (nodePath == Parent.Path) { start = Parent.Parent; } } if (start == null || ancestor == start || !ancestor.HasDescendant(start)) { return(new List <SharpNode>()); } stack.Enqueue(start); while (stack.Top != ancestor) { if (stack.Top.Parent == null || stack.Top == stack.Top.Parent) { break; } stack.Enqueue(stack.Top.Parent); } return(stack.ToList()); }