private int GetDepth(InstructionTreeNode node) { int biggestDepth = 0; if (node.Parents != null) { foreach (var parent in node.Parents) { var depth = GetDepth(parent) + 1; if (depth > biggestDepth) { biggestDepth = depth; } } } if (node.PreviousSibling != null) { if (node.PreviousSibling.Depth > biggestDepth) { biggestDepth = node.PreviousSibling.Depth; } } return(biggestDepth); }
private InstructionTreeNode GetNode(Instruction instruction) { var node = _nodes.Where(n => n.Instruction == instruction).FirstOrDefault(); if (node == null) { node = new InstructionTreeNode { Instruction = instruction, Id = _nextId++ }; _nodes.Add(node); } return(node); }
private void LinkInstruction(InstructionTreeNode parent, Instruction instruction) { if (instruction != null) { var child = GetNode(instruction); if (parent.Children == null) { parent.Children = new List <InstructionTreeNode>(); } parent.Children.Add(child); if (child.Parents == null) { child.Parents = new List <InstructionTreeNode>(); } child.Parents.Add(parent); } }
private void LinkSibling(InstructionTreeNode from, InstructionTreeNode to) { from.NextSibling = to; to.PreviousSibling = from; }
private void BuildTree() { _roots.Clear(); _nodes.Clear(); for (var i = 0; i < _set.Instructions.Count; i++) { var instruction = _set.Instructions[i]; var node = GetNode(instruction); var fields = instruction.GetType().GetFields().Where(field => typeof(Instruction).IsAssignableFrom(field.FieldType)); var lists = instruction.GetType().GetFields().Where(field => field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(List <>) && typeof(Instruction).IsAssignableFrom(field.FieldType.GetGenericArguments()[0])); foreach (var field in fields) { var child = field.GetValue(instruction) as Instruction; LinkInstruction(node, child); } foreach (var listField in lists) { InstructionTreeNode from = null; var list = listField.GetValue(instruction) as IList; for (var s = 0; s < list.Count; s++) { var sibling = list[s] as Instruction; if (s == 0) { LinkInstruction(node, sibling); } else { LinkSibling(from, GetNode(sibling)); } from = GetNode(sibling); } } } for (var i = 0; i < _nodes.Count; i++) { var node = _nodes[i]; if (node.Parents == null) { _roots.Add(node); } } var heights = new List <int>(); foreach (var node in _nodes) { node.Depth = GetDepth(node); while (heights.Count <= node.Depth) { heights.Add(0); } var y = heights[node.Depth]++; node.Rect = GetRect(node.Depth, y); } }