public static TreeWithDependencies <TNode> Decode <TNode>(string text, Delegates.CreateNodeFromNameAndPath <TNode> createNodeWithName, Delegates.AddChildrenToNode <TNode> addChildrenToNode) { var nodesByPath = new Dictionary <string, TNode>(); var flats = FlatListSerializer.DecodeFlatlist(text); var nodes = GetTreeFromFlatlist(flats, nodesByPath, 0, createNodeWithName, addChildrenToNode).ToList(); var deps = GetDependenciesFromFlatList(flats, nodesByPath); return(new TreeWithDependencies <TNode>(nodes.ToSet(), deps)); }
private static IEnumerable <TNode> GetTreeFromFlatlist <TNode>(IReadOnlyCollection <FlatEntry> descendants, IDictionary <string, TNode> nodesByPath, int currentDepth, Delegates.CreateNodeFromNameAndPath <TNode> createNodeWithName, Delegates.AddChildrenToNode <TNode> addChildrenToNode) { var isDirectChildren = descendants.ToLookup(x => x.Depth() == currentDepth); var notDirectChildren = isDirectChildren[false].ToMutableSet(); var directChildren = isDirectChildren[true]; foreach (var directChild in directChildren) { var path = directChild.Path; var name = GetNameFromPath(path); var topNode = createNodeWithName(name, path); var subDescendants = notDirectChildren.Where(x => x.IsDescendantTo(directChild)).ToList(); notDirectChildren.ExceptWith(subDescendants); var withChildren = addChildrenToNode(topNode, GetTreeFromFlatlist(subDescendants, nodesByPath, currentDepth + 1, createNodeWithName, addChildrenToNode)); nodesByPath.Add(path, withChildren); yield return(withChildren); } Debug.Assert(notDirectChildren.Count == 0); }
public static TreeWithDependencies <T> HierarchicalGraph <T>(string text, Delegates.CreateNodeFromNameAndPath <T> createNodeWithName, Delegates.AddChildrenToNode <T> addChildrenToNode) => HierarchicalGraphDecoder.Decode(text, createNodeWithName, addChildrenToNode);