Esempio n. 1
0
        public IEnumerable <int> Knuth_Morris_Pratt_TextReader_Search(string haystack, string needle, int bufferSize)
        {
            var pattern = new KMPSearch(needle);
            var reader  = new StringReader(haystack);

            return(pattern.SearchIn(reader, bufferSize).ToArray());
        }
 public void Search()
 {
     Assert.IsTrue(KMPSearch.Search("abd", "abdfgh").SequenceEqual(new List <int> {
     }));                                                                               /* Testing the case where substring is longer than string. */
     Assert.AreEqual(1, KMPSearch.Search("abcd", "bc")[0]);
     Assert.AreEqual(2, KMPSearch.Search("abcd", "cd")[0]);
     Assert.AreEqual(12, KMPSearch.Search("aaaaaakcdkaaaabcd", "aab")[0]);
     Assert.IsTrue(KMPSearch.Search("abcaab", "a").SequenceEqual(new List <int> {
         0, 3, 4
     }));
     Assert.IsTrue(KMPSearch.Search("abcaab", "abc").SequenceEqual(new List <int> {
         0
     }));
     Assert.AreEqual(0, KMPSearch.Search("aaabbbdaacbb", "kjh").Count);
 }
Esempio n. 3
0
        static void Main(string[] args)
        {
            string[] a = { "abv", "abd", "abc", "afr", "abt", "ayv", "bvc", "ayc" };
            var      b = a;

            //LSD.Sort(a, 3);
            MSD.Sort(b);

            int i = ForceSearch.Search1("ll", "hello");

            KMPSearch kmp = new KMPSearch("llo");

            i = kmp.Search("hello");

            Console.ReadKey();
        }
Esempio n. 4
0
        public void FindAllTest()
        {
            data.BaseStream.Seek(0, SeekOrigin.Begin);
            kMPSearch = new KMPSearch(Token);
            data.BaseStream.Position = 0;
            var positionsWithTokens = kMPSearch.FindAll(data, false);

            data.BaseStream.Position = 0;
            var positionsWithout = kMPSearch.FindAll(data, true);

            data.BaseStream.Position = 0;

            Assert.AreEqual(positionsWithTokens.Count, positionsWithout.Count);
            Assert.AreEqual(positionsWithTokens.Count, Chunks.Length);

            // every chunk should be a string and the token
            long offset = 0;

            for (int i = 0; i < positionsWithTokens.Count; i++)
            {
                var    buffer = data.ReadBytes((int)(positionsWithTokens[i] - offset));
                string chunk  = System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Length);
                Assert.IsTrue(Chunks[i] + Token == chunk);
                offset = positionsWithTokens[i];
            }
            data.BaseStream.Position = 0;

            // every chunk should be just the string
            offset = 0;
            for (int i = 0; i < positionsWithout.Count; i++)
            {
                var    buffer = data.ReadBytes((int)(positionsWithout[i] - offset));
                string chunk  = System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Length);
                Assert.IsTrue(Chunks[i] == chunk);
                offset = positionsWithout[i] + Token.Length;
                // skip the token
                data.BaseStream.Seek(Token.Length, SeekOrigin.Current);
            }
        }
Esempio n. 5
0
        public void TestSearch(string txt, string pat, int index)
        {
            var res = new KMPSearch().Search(pat, txt);

            Assert.AreEqual(index, res);
        }
Esempio n. 6
0
        public IEnumerable <int> Knuth_Morris_Pratt_String_Search(string haystack, string needle)
        {
            var pattern = new KMPSearch(needle);

            return(pattern.SearchIn(haystack).ToArray());
        }