आपको यहां Boyer-Moore algorithm उपयोगी मिल सकता है। अपनी सूची को एक सरणी में खोजें और खोजें। एल्गोरिदम कोड this post से लिया गया है।
static int SimpleBoyerMooreSearch(byte[] haystack, byte[] needle)
{
int[] lookup = new int[256];
for (int i = 0; i < lookup.Length; i++) { lookup[i] = needle.Length; }
for (int i = 0; i < needle.Length; i++)
{
lookup[needle[i]] = needle.Length - i - 1;
}
int index = needle.Length - 1;
var lastByte = needle.Last();
while (index < haystack.Length)
{
var checkByte = haystack[index];
if (haystack[index] == lastByte)
{
bool found = true;
for (int j = needle.Length - 2; j >= 0; j--)
{
if (haystack[index - needle.Length + j + 1] != needle[j])
{
found = false;
break;
}
}
if (found)
return index - needle.Length + 1;
else
index++;
}
else
{
index += lookup[checkByte];
}
}
return -1;
}
फिर आप इस तरह खोज सकते हैं। यदि lbyte
एक निश्चित समय के बाद स्थिर रहेगा, तो आप इसे एक बार सरणी में परिवर्तित कर सकते हैं और उसे पास कर सकते हैं।
//index is returned, or -1 if 'searchBytes' is not found
int startIndex = SimpleBoyerMooreSearch(lbyte.ToArray(), searchBytes);
टिप्पणी के आधार पर अद्यतन। यहाँ IList
कार्यान्वयन जिसका अर्थ है कि सरणियों और सूची (और बाकी है कि IList
लागू करता है कुछ भी पारित किया जा सकता)
static int SimpleBoyerMooreSearch(IList<byte> haystack, IList<byte> needle)
{
int[] lookup = new int[256];
for (int i = 0; i < lookup.Length; i++) { lookup[i] = needle.Count; }
for (int i = 0; i < needle.Count; i++)
{
lookup[needle[i]] = needle.Count - i - 1;
}
int index = needle.Count - 1;
var lastByte = needle[index];
while (index < haystack.Count)
{
var checkByte = haystack[index];
if (haystack[index] == lastByte)
{
bool found = true;
for (int j = needle.Count - 2; j >= 0; j--)
{
if (haystack[index - needle.Count + j + 1] != needle[j])
{
found = false;
break;
}
}
if (found)
return index - needle.Count + 1;
else
index++;
}
else
{
index += lookup[checkByte];
}
}
return -1;
}
के बाद से सरणियों और सूचियों IList लागू है, वहाँ कोई रूपांतरण आवश्यक है जब वह अपने मामले में बुला रहे हैं।
int startIndex = SimpleBoyerMooreSearch(lbyte, searchBytes);
"lbyte" क्या है के साथ कर सकता है? –
बाइट – Paparazzi
@YairNevet की एक सूची बाइट्स की एक और सूची @YairNevet, इस अनिवार्य है यहाँ orderedlist.containssequence वहाँ होना चाहिए एक अच्छा समाधान के रूप में अपनी अनिवार्य रूप से string.contains के साथ हल किया लेकिन यह एक अधिक सामान्य मामले [यहां] देखने का प्रयास करें –