// Returns true if iteration was cancelled private bool InternalIterateTree(bool twoStagePass, int level, InheritanceNode fromNode, Action <InheritanceTreeIterator> iterateAction) { foreach (InheritanceNode child in fromNode.Childs) { var treeIterator = new InheritanceTreeIterator(level, fromNode, child); iterateAction(treeIterator); if (treeIterator.Cancel) { return(true); } if (child.Childs.Count() > 0) { bool cancel = InternalIterateTree(twoStagePass, level + 1, child, iterateAction); if (cancel) { return(true); } } if (twoStagePass) { treeIterator.CurrentStage = InheritanceTreeIterator.Stage.Leave; iterateAction(treeIterator); if (treeIterator.Cancel) { return(true); } } } return(false); }
bool IEqualityComparer.Equals(object x, object y) { InheritanceNode nodeX = (InheritanceNode)x; InheritanceNode nodeY = (InheritanceNode)y; return(nodeX.Interface == nodeY.Interface); }
internal void AddChild(InheritanceNode child) { if (!this.childList.Contains(child)) { this.childList.Add(child); } }
public InheritanceTreeIterator(int level, InheritanceNode parent, InheritanceNode current) { Level = level; Parent = parent; Current = current; this.CurrentStage = Stage.Enter; }
protected InheritanceNode(InheritanceNode parent, IInterface @interface, InheritanceNodeType nodeType, IEnumerable <InheritanceNode> childs) { this.Parent = parent; this.Interface = @interface; this.NodeType = nodeType; if (childs != null) { childList.AddRange(childs); } }
public static IEnumerable <IInterface> GetDependencyImplementationFlatList(IEnumerable <InheritancePath> paths) { List <IInterface> result = new List <IInterface>(); int maxPathLength = paths.Count() == 0 ? 0 : paths.Max(path => path.PathLength); for (int i = 0; i < maxPathLength; i++) { foreach (InheritancePath path in paths) { InheritanceNode inheritanceNode = path.GetNode(i); if (inheritanceNode != null && !result.Contains(inheritanceNode.Interface)) { result.Add(inheritanceNode.Interface); } } } return(result); }
public void RebuildTree(bool rebuildPaths) { if (!treeRebuilded) { treeRebuilded = true; } var interfacesIterator = new GenericTreeIterator <IInterface>(@interface => @interface.GetCurrentLevelInheritedInterfaces()); interfacesIterator.IterateTree(true, this.Interface, delegate(GenericTreeIterationArgs <IInterface, InheritanceNode> iterationArgs) { if (iterationArgs.CurrentStage == GenericTreeIterationStage.Leave) { iterationArgs.Data = null; } else { var childs = iterationArgs.Current.GetCurrentLevelInheritedInterfaces(); InheritanceNodeType nodeType = childs.Count() == 0 ? InheritanceNodeType.SuperRoot : InheritanceNodeType.Node; InheritanceNode parentNode = iterationArgs.Data ?? this; InheritanceNode newNode = CreateNode(nodeType, parentNode, iterationArgs.Current, null); parentNode.AddChild(newNode); iterationArgs.Data = newNode; } }); pathsRebuilded = false; if (rebuildPaths) { RebuildPaths(); } }
internal static InheritanceNode CreateNode(InheritanceNodeType nodeType, InheritanceNode parent, IInterface @interface, IEnumerable <InheritanceNode> childs) { return(new InheritanceNode(parent, @interface, nodeType, childs)); }
public int GetNodesDistance(InheritanceNode nodeFrom, InheritanceNode nodeTo) { return(GetNodesDistance(nodeFrom.Interface, nodeTo.Interface)); }
public int GetNodePosition(InheritanceNode node) { return(GetNodePosition(node.Interface)); }
public bool ContainsNode(InheritanceNode node) { return(ContainsNode(node.Interface)); }
public ReadOnlyCollection <InheritanceDetail> FoundDuplicatedDirectInheritances() { CheckTreeRebuilded(); CheckPathsRebuilded(); List <InheritanceDetail> result = new List <InheritanceDetail>(); var parentInterface = this.Interface; foreach (InheritanceNode childNode in this.Childs) { var childInterface = childNode.Interface; var hierarchyToSuperRootPath = FindInheritancePath(parentInterface, childInterface, 1).SingleOrDefault(); IEnumerable <InheritancePath> query; if (hierarchyToSuperRootPath != null) { query = from path in inheritancePaths /*let childInterfacePos = path.GetNodePosition(childInterface) * let parentInterfacePos = path.GetNodePosition(parentInterface)*/ where path.ContainsNode(childInterface) && path.SuperRootNode.Interface == childInterface && !path.Id.Equals(hierarchyToSuperRootPath.Id) select path; } else { query = from path in inheritancePaths let childInterfaceNode = path.GetNode(childInterface) let parentInterfaceNode = path.GetNode(parentInterface) //let childInterfacePos = path.GetNodePosition(childInterface) //let parentInterfacePos = path.GetNodePosition(parentInterface) where path.ContainsNode(childInterface) && childInterfaceNode.Parent != null && childInterfaceNode.Parent != parentInterfaceNode //path.SuperRootNode.Interface == childInterface && //!path.Id.Equals(hierarchyToSuperRootPath.Id) select path; } var paths = query.ToArray(); if (paths.Length > 0) { InheritanceDetail detail = new InheritanceDetail(childInterface); foreach (InheritancePath path in paths) { InheritanceNode node = path.GetNode(path.PathLength - 2); int distance = path.GetNodesDistance(node, path.SuperRootNode); if (!detail.ContainsItem(node.Interface)) { detail.Add(new InheritanceDetailItem(distance == 1 ? InheritanceType.Direct : InheritanceType.Indirect, node.Interface)); } } result.Add(detail); } } return(new ReadOnlyCollection <InheritanceDetail>(result)); }
int IEqualityComparer.GetHashCode(object obj) { InheritanceNode node = (InheritanceNode)obj; return(node.Interface.GetHashCode()); }