コード例 #1
0
        public static List <StringSearchMatch> FindMatchesUsingForwardOnlyCursor2a(ITrieStructure trie, string text)
        {
            List <StringSearchMatch> rv = new List <StringSearchMatch>();

            if (string.IsNullOrEmpty(text))
            {
                return(rv);
            }

            var maxIndex = text.Length - 1;

            Queue <MatchUoW> queue = new Queue <MatchUoW>();

            for (int i = 0; i <= maxIndex; i++)
            {
                //get current char
                char currentChar = text[i];

                //dequeue all carryover items, and identify which ones can continue
                Queue <MatchUoW> reQueuedItems = new Queue <MatchUoW>();
                int queueCount = queue.Count;
                if (queueCount > 0)
                {
                    for (int j = 0; j < queueCount; j++)
                    {
                        MatchUoW dequeueItem = queue.Dequeue();

                        //if this matches, update the rv
                        var match = dequeueItem.GetWordMatch();
                        if (match != null)
                        {
                            rv.Add(match);
                        }

                        //can we carry the item over?
                        if (dequeueItem.MoveNext(currentChar))
                        {
                            reQueuedItems.Enqueue(dequeueItem);
                        }
                    }

                    //queue up the ones that continue
                    foreach (var each in reQueuedItems)
                    {
                        queue.Enqueue(each);
                    }
                }


                //Possibly create a unit of work for this particular character (starting from root)
                var node = trie.Root[currentChar];
                if (node == null)
                {
                    continue;
                }

                MatchUoW uow = new MatchUoW(i, currentChar, node);
                queue.Enqueue(uow);
            }
            return(rv);
        }