예제 #1
0
 private SplitPath CurrentSplitBlock(SplitPath splitPathNode)
 {
     return(currentSplitBlock = splitPathNode);
 }
예제 #2
0
        public static NODET FromPaths <PATHT, NODET>(
            IEnumerable <PATHT> paths,
            SplitPath <PATHT> funcsplit,
            CreateNewNodeFromPath <PATHT, NODET> createnewnode,
            AddChild <NODET> addchild)
        {
            var   root_dic         = new Dictionary <PATHT, TreePathItem <PATHT, NODET> >();
            NODET current_node     = default(NODET);
            bool  has_current_node = false;

            PATHT root_token  = default(PATHT);
            bool  found_root  = false;
            var   pathbuilder = new List <PATHT>();

            foreach (var path in paths)
            {
                pathbuilder.Clear();

                int depth = 0;

                var tokens = funcsplit(path);

                var current_dic = root_dic;

                foreach (var token in tokens)
                {
                    pathbuilder.Add(token);
                    if (current_dic.ContainsKey(token))
                    {
                        var next_dic = current_dic[token];

                        current_dic  = next_dic.dic;
                        current_node = next_dic.node;

                        has_current_node = true;
                    }
                    else
                    {
                        var new_node = createnewnode(pathbuilder.ToArray(), token, depth);
                        if (has_current_node)
                        {
                            addchild(current_node, new_node);
                        }

                        var new_dic = new Dictionary <PATHT, TreePathItem <PATHT, NODET> >();
                        current_dic[token] = new TreePathItem <PATHT, NODET>(new_node, new_dic);
                        current_dic        = new_dic;
                        current_node       = new_node;
                        has_current_node   = true;

                        if (depth == 0)
                        {
                            if (!found_root)
                            {
                                root_token = token;
                                found_root = true;
                            }
                            else
                            {
                                throw new System.ArgumentException("Input contains multiple roots");
                            }
                        }
                    }

                    depth++;
                }
            }

            if (root_dic.Count < 1)
            {
                return(default(NODET));
            }
            else if (root_dic.Count == 1)
            {
                var new_root_node = root_dic.ElementAt(0).Value.node;

                return(new_root_node);
            }
            else
            {
                throw new System.InvalidOperationException("Should never get to this state");
            }
        }