c# - Pattern matching in byte arrays -


let's have 2 byte array each contain series of values like:

byte[] b = {50,60,70,80,90,10,20,1,2,3,4,5,50,2,3,1,2,3,4,5}; byte[] b2 = {1,2,3,4,5} 

i can compare these 2 arrays , equal values using linq methods. in way, if make comparison between these 2 arrays, result index of b array value in index of b2 array match.

i've been trying find range b2 array recurring in b array. mean

if (thelenghtofsearch==5) {now indexes of 2 regions must return } result ->(7, 11), (15, 19)  if (thelenghtofsearch==2) {now indexes of around 9 regions 2 consecutive values in b2 recurred in b must returned} result ->(7, 8), (15, 16), (8, 9), (13, 14), (16, 17), (9, 10), (17, 18), (10, 11), (18, 19) 

i guess solution more mathematical.

i decided use list, not array, because have more helpers kind of operations. understand depth = amount of items have equals in each array. 1 works, check out:

class program {     static void main(string[] args)     {         list<byte> b = new list<byte>() { 50, 60, 70, 80, 90, 10, 20, 1, 2, 3, 4, 5, 50, 2, 3, 1, 2, 3, 4, 5 };         list<byte> b2 = new list<byte>() { 1, 2, 3, 4, 5 };         smartcomparer comparer = new smartcomparer();         //setting depth here, depth = 5         var result = comparer.comparearrayswithdepth(b, b2, 5);         foreach (var keyvaluepair in result)         {             console.writeline(string.format("b[{0}]->b[{1}] equal b2[{2}]->b2[{3}]", keyvaluepair.key.key,                                             keyvaluepair.key.value, keyvaluepair.value.key, keyvaluepair.value.value));         }     } }  public class smartcomparer {     public boolean comparerange(list<byte> a, list<byte> b)     {         (int = 0; < a.count; i++)         {             if (a[i] != b[i])             {                 return false;             }         }         return true;     }      /// <summary>     /// |     /// </summary>     /// <param name="a"></param>     /// <param name="b"></param>     /// <param name="depth"></param>     /// <returns>key->range in 'a', value->range in 'b'</returns>     public list<keyvaluepair<keyvaluepair<int, int>, keyvaluepair<int, int>>> comparearrayswithdepth(         list<byte> a, list<byte> b, int depth)     {         var result = new list<keyvaluepair<keyvaluepair<int, int>, keyvaluepair<int, int>>>();         if (depth > b.count)             throw new argumentexception("array 'b' item count should more depth");         if(a.count<b.count)             throw new argumentexception("array 'a' item count should more array 'b' item count");         (int = 0; <= a.count - depth; i++)         {             (int j = 0; j <= b.count - depth; j++)             {                 if (comparerange(a.getrange(i, depth), b.getrange(j, depth)))                 {                     result.add(new keyvaluepair<keyvaluepair<int, int>, keyvaluepair<int, int>>(new keyvaluepair<int, int>(i, + depth-1), new keyvaluepair<int, int>(j, j + depth-1)));                 }             }         }         return result;     } } 

added

the result of operation depth = 3:

b[7]->b[9] equal b2[0]->b2[2] b[8]->b[10] equal b2[1]->b2[3] b[9]->b[11] equal b2[2]->b2[4] b[15]->b[17] equal b2[0]->b2[2] b[16]->b[18] equal b2[1]->b2[3] b[17]->b[19] equal b2[2]->b2[4] 

the result of operation depth = 2:

b[7]->b[8] equal b2[0]->b2[1] b[8]->b[9] equal b2[1]->b2[2] b[9]->b[10] equal b2[2]->b2[3] b[10]->b[11] equal b2[3]->b2[4] b[13]->b[14] equal b2[1]->b2[2] b[15]->b[16] equal b2[0]->b2[1] b[16]->b[17] equal b2[1]->b2[2] b[17]->b[18] equal b2[2]->b2[3] b[18]->b[19] equal b2[3]->b2[4] 

the result of operation depth = 5:

b[7]->b[11] equal b2[0]->b2[4] b[15]->b[19] equal b2[0]->b2[4] 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -