Exemple #1
0
        /// <summary>
        /// an attempt to do the forwardonlycursor alg using a stack instead of a queue. is slightly slower. not
        /// the tightest implementation either.
        /// </summary>
        /// <param name="trie"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static List <StringSearchMatch> FindMatchesUsingForwardOnlyCursor2(ITrieStructure trie, string text)
        {
            List <StringSearchMatch> rv = new List <StringSearchMatch>();

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

            var maxIndex = text.Length - 1;

            Stack <MatchUoW> bucket = new Stack <MatchUoW>();
            int bucketSize          = 0;

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

                //1.  set up new items
                Stack <MatchUoW> newItems = new Stack <MatchUoW>();
                var node = trie.Root[currentChar];
                if (node != null)
                {
                    MatchUoW uow = new MatchUoW(i, currentChar, node);
                    newItems.Push(uow);
                }

                //2.  handle old items
                for (int j = 0; j < bucketSize; j++)
                {
                    var eachOld = bucket.Pop();

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

                    //can we carry the item over?
                    if (eachOld.MoveNext(currentChar))
                    {
                        newItems.Push(eachOld);
                    }
                }

                bucketSize = newItems.Count;
                for (int j = 0; j < bucketSize; j++)
                {
                    bucket.Push(newItems.Pop());
                }
            }
            return(rv);
        }
Exemple #2
0
        /// <summary>
        /// alg that only reads each character of the text to search, once.  at each index it creates possible
        /// match item that is queued.  Iterating thru the index these queued items are dequeued and processed for
        /// matching.  Sort of a snowplow approach that accumulates potential matches and simultaneously filters out bad
        /// ones, and when a word appears, it accumulates and returns it.
        /// </summary>
        /// <param name="trie"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static List<StringSearchMatch> FindMatchesUsingForwardOnlyCursor(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
                List<MatchUoW> reQueuedItems = new List<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.Add(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;
        }
Exemple #3
0
        /// <summary>
        /// an attempt to do the forwardonlycursor alg using a stack instead of a queue. is slightly slower. not
        /// the tightest implementation either.
        /// </summary>
        /// <param name="trie"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static List<StringSearchMatch> FindMatchesUsingForwardOnlyCursor2(ITrieStructure trie, string text)
        {
            List<StringSearchMatch> rv = new List<StringSearchMatch>();

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

            var maxIndex = text.Length - 1;

            Stack<MatchUoW> bucket = new Stack<MatchUoW>();
            int bucketSize = 0;

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

                //1.  set up new items
                Stack<MatchUoW> newItems = new Stack<MatchUoW>();
                var node = trie.Root[currentChar];
                if (node != null)
                {
                    MatchUoW uow = new MatchUoW(i, currentChar, node);
                    newItems.Push(uow);
                }

                //2.  handle old items
                for (int j = 0; j < bucketSize; j++)
                {
                    var eachOld = bucket.Pop();

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

                    //can we carry the item over?
                    if (eachOld.MoveNext(currentChar))
                    {
                        newItems.Push(eachOld);
                    }
                }

                bucketSize = newItems.Count;
                for (int j = 0; j < bucketSize; j++)
                {
                    bucket.Push(newItems.Pop());
                }
            }
            return rv;
        }
Exemple #4
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);
        }