c# - Search for an Array or List in a List -
have
list<byte> lbyte    have
byte[] searchbytes   how can search lbyte not single byte index of searchbytes?
 e.g.
int32 index = lbyte.firstindexof(searchbytes);   here brute force came with.
 not performance looking for.     
public static int32 listindexofarray(list<byte> lb, byte[] sbs) {     if (sbs == null) return -1;     if (sbs.length == 0) return -1;     if (sbs.length > 8) return -1;     if (sbs.length == 1) return lb.firstordefault(x => x == sbs[0]);     int32 sbslen = sbs.length;     int32 sbscurmatch = 0;     (int = 0; < lb.count; i++)     {         if (lb[i] == sbs[sbscurmatch])         {             sbscurmatch++;             if (sbscurmatch == sbslen)             {                 //int index = lb.findindex(e => sbs.all(f => f.equals(e)));  // fails find match                 indexofarray = - sbslen + 1;                 return;             }         }         else          {             sbscurmatch = 0;         }     }     return -1; }      
you may find boyer-moore algorithm useful here. convert list array , search. algorithm code taken this post.
static int simpleboyermooresearch(byte[] haystack, byte[] needle) {     int[] lookup = new int[256];     (int = 0; < lookup.length; i++) { lookup[i] = needle.length; }      (int = 0; < needle.length; i++)     {         lookup[needle[i]] = needle.length - - 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;             (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; }   you can search this. if lbyte remain constant after time, can convert array once , pass that.
//index returned, or -1 if 'searchbytes' not found int startindex = simpleboyermooresearch(lbyte.toarray(), searchbytes);   update based on comment. here's ilist implementation means arrays , lists (and else implements ilist can passed)
 static int simpleboyermooresearch(ilist<byte> haystack, ilist<byte> needle)  {     int[] lookup = new int[256];     (int = 0; < lookup.length; i++) { lookup[i] = needle.count; }      (int = 0; < needle.count; i++)     {         lookup[needle[i]] = needle.count - - 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;             (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; }   since arrays , lists implement ilist, there's no conversion necessary when calling in case.
int startindex = simpleboyermooresearch(lbyte, searchbytes);      
Comments
Post a Comment