N ,
vector<unsigned char> pattern;
(N + 7) / 8
, . , , , N == 19, :
|<- v[0] ->|<- v[1] ->|<- v[2] ->|
0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
| |<- pattern ->|
, , , , .
, , . window
vector<unsigned char> window;
N , 8, window . :
unsigned char mask = (1 << (N % 8)) - 1;
, window , , window ==,
window[0] &= mask;
bool isMatch = (window == pattern);
. N , , , , N + 1:
vector<int> shifts;
, , , window.
0001001100. window . missmatch, 1, 1 2, 0, . , , , , , window, 2. , ( 2, 0), window 7, 3 . 4, window 8 . sifts i , window, i. , window , shifts[N]. 8.
, , window ( ), , , .
if(window[i] != pattern[i])
{
int j = 0;
unsigned char mismatches = window[i] ^ pattern[i];
while((mismatches & 1) == 0)
{
mismatches >>= 1;
++j;
}
mismatch_position = 8 * (window.size() - i - 1) + j;
}
, , window. #, ++ . # , , , ++. unsigned char byte, vector<unsigned char> & byte [], size() Length , , . , , , , , , , , , , . .
public static void shiftBitsIntoWindow_MSbFirst(byte[] window, byte[] source,
int startBitPosition, int numberOfBits)
{
int nob = numberOfBits / 8;
int ntsh = numberOfBits % 8;
int nfstbb = (8 - startBitPosition % 8);
int nfbbte = (startBitPosition + numberOfBits) % 8;
int sbtci;
if(nob < window.Length)
{
sbtci = (startBitPosition + ntsh) / 8;
for(int i = 0; i < window.Length - nob - 1; ++i)
{
window[i] = (byte)((window[i + nob] << ntsh)
| (window[i + nob + 1] >> (8 - ntsh)));
}
int si = startBitPosition / 8;
byte byteSecondPart;
int mask = (1 << ntsh) - 1;
if(ntsh <= nfstbb)
{
byteSecondPart = (byte)((source[si] >> (nfstbb - ntsh)) & mask);
}
else
{
byteSecondPart = (byte)(((source[si] << (ntsh - nfstbb))
| (source[si + 1] >> (8 - ntsh + nfstbb))) & mask);
}
window[window.Length - nob - 1] = (byte)((window[window.Length - 1] << ntsh)
| byteSecondPart);
}
else
{
sbtci = (startBitPosition + numberOfBits) / 8 - window.Length;
nob = window.Length;
}
if(nfbbte > 0)
{
for(int i = 0; i < nob; ++i)
{
window[window.Length - nob + i] = (byte)((source[sbtci + i] << nfbbte)
| (source[sbtci + 1 + i] >> (8 - nfbbte)));
}
}
else
{
for(int i = 0; i < nob; ++i)
{
window[window.Length - nob + i] = source[sbtci + i];
}
}
}