List <ABGraphNode> CreateGraph(Dictionary <string, ABGraphNode> nodeMap) { List <ABGraphNode> ret = new List <ABGraphNode>(); while (nodeMap.Count > 0) { var kv = nodeMap.First(); ABGraphNode node = kv.Value; ABGraphNode outNode = null; if (IsOnlyOneOrZeroParentNode(node, out outNode)) { if (outNode.children.Count > 0) { foreach (var child in outNode.GetAllChildren(null)) { nodeMap.Remove(child.path); } } } else { while (outNode.parents.Count > 0) { outNode.parents[0].RemoveChild(outNode); } var children = outNode.GetAllChildren(); foreach (var child in children) { ABGraphNode childoutnode = null; if (IsOnlyOneOrZeroParentNode(child, out childoutnode)) { nodeMap.Remove(child.path); } } } nodeMap.Remove(outNode.path); Debug.Assert(!ret.Contains(outNode)); ret.Add(outNode); } int nodecount = 0; foreach (var node in ret) { Debug.Assert(node.parents.Count <= 1); var children = node.GetAllChildren(); nodecount += children.Count + 1; foreach (var child in children) { Debug.Assert(child.parents.Count == 1); } } Debug.Assert(nodecount == mRawResourceMap.Count); return(ret); }
void AddChildNode(ABGraphNode parent, List <string> childPaths, Dictionary <string, OneResource> map, Dictionary <string, ABGraphNode> rawNodeMap) { List <OneResource> childResources = new List <OneResource>(); foreach (var path in childPaths) { childResources.Add(map[path]); } var minResource = childResources.Min(); List <OneResource> minResources = new List <OneResource>(); foreach (var res in childResources) { if (res.reference == minResource.reference) { minResources.Add(res); } } foreach (var item in minResources) { ABGraphNode graphNode = null; if (!rawNodeMap.TryGetValue(item.path, out graphNode)) { graphNode = new ABGraphNode(); graphNode.path = item.path; graphNode.reference = item.reference; rawNodeMap.Add(item.path, graphNode); } if (graphNode.parents.Find((n) => n.path == parent.path) != null) { return; } graphNode.parents.Add(parent); if (parent.children.Find((n) => n.path == graphNode.path) != null) { return; } parent.children.Add(graphNode); if (item.dependices.Count > 0) { AddChildNode(graphNode, item.dependices, map, rawNodeMap); } } }
bool IsOnlyOneOrZeroParentNode(ABGraphNode node, out ABGraphNode ret, List <ABGraphNode> closedList = null) { if (node.parents.Count > 1) { ret = node; return(false); } if (closedList == null) { closedList = new List <ABGraphNode>(); } if (node.children.Count > 0) { var children = node.GetAllChildren(closedList); foreach (var child in children) { if (!IsOnlyOneOrZeroParentNode(child, out ret, closedList)) { return(false); } } } if (node.parents.Count == 0) { ret = node; closedList.Add(node); return(true); } else { closedList.Add(node); ABGraphNode parent = node.parents[0]; var children = parent.GetAllChildren(closedList); foreach (var child in children) { if (!IsOnlyOneOrZeroParentNode(child, out ret, closedList)) { return(false); } } closedList.Add(parent); return(IsOnlyOneOrZeroParentNode(parent, out ret, closedList)); } }
public Dictionary <string, ABGraphNode> CreateHierarchy(Dictionary <string, OneResource> map) { Dictionary <string, ABGraphNode> rawNodeMap = new Dictionary <string, ABGraphNode>(); foreach (var kv in map) { var resource = kv.Value; ABGraphNode graphNode = null; if (!rawNodeMap.TryGetValue(resource.path, out graphNode)) { graphNode = new ABGraphNode(); graphNode.path = resource.path; graphNode.reference = resource.reference; rawNodeMap.Add(resource.path, graphNode); } if (resource.dependices.Count > 0) { AddChildNode(graphNode, resource.dependices, map, rawNodeMap); } } Debug.Assert(map.Count == rawNodeMap.Count); return(rawNodeMap); }
public void RemoveChild(ABGraphNode child) { child.parents.Remove(this); this.children.Remove(child); }