Exemple #1
0
        public TrieNode(char value, int depth, TrieNode parent, string parentString)
        {
            Value    = value;
            Children = new List <TrieNode>();
            Depth    = depth;
            Parent   = parent;

            TrieNode      currentParent = Parent;
            StringBuilder wordBuilder   = new StringBuilder(value);

            if (parentString == null)
            {
                while (currentParent != null)
                {
                    if (currentParent.Parent != null)
                    {
                        wordBuilder.Insert(0, currentParent.Value);
                    }

                    currentParent = currentParent.Parent;
                }

                ZupaNode = new ZupaNode(wordBuilder.ToString());
            }
            else
            {
                ZupaNode = new ZupaNode(parentString + value);
            }
        }
        public ZupaNode Submit(string input)
        {
            ZupaNode found = Query(input);

            if (found != null)
            {
                return(found);
            }

            List <string> tokens = input.Split(' ').Select(e => e.Trim()).ToList();

            var children = InsertRange(tokens);

            return(new ZupaNode(children));
        }
        public ZupaNode Query(string input)
        {
            ZupaNode resultNode = null;

            List <string> tokens = input.Split(' ').Select(e => e.Trim()).ToList();

            List <ZupaNode> childNodes = tokens.Select(token => Prefix(token).ZupaNode).ToList();

            string pingGuid       = Guid.NewGuid().ToString();
            bool   shouldContinue = true;

            //Parallel.ForEach(childNodes, childNode => childNode.Ping(
            //    pingGuid,
            //    childNodes.Count,
            //    new Func<ZupaNode, ZupaNode>(foundNode =>
            //    {
            //        resultNode = foundNode;

            //        return resultNode;
            //    })
            //));

            //childNodes.ForEach(childNode => {
            //    if (shouldContinue)
            //    {
            //        childNode.Ping(
            //            pingGuid,
            //            childNodes.Count,
            //            new Func<ZupaNode, ZupaNode>(foundNode =>
            //            {
            //                resultNode = foundNode;
            //                shouldContinue = false;

            //                return resultNode;
            //            }),
            //            ref shouldContinue
            //        );
            //    }
            //});

            foreach (var childNode in childNodes)
            {
                if (shouldContinue)
                {
                    childNode.Ping(
                        pingGuid,
                        childNodes.Count,
                        new Func <ZupaNode, ZupaNode>(foundNode =>
                    {
                        resultNode     = foundNode;
                        shouldContinue = false;

                        return(resultNode);
                    }),
                        ref shouldContinue
                        );
                }
            }

            return(resultNode);
        }