, "1" .
- ch
, :
0x01 -> 0
0x02 -> 1
0x04 -> 2
0x08 -> 3
...
ch
. 8- (char) 2 8= 256 :
char naive_table[256];
naive_table[0x01] = 0;
naive_table[0x02] = 1;
naive_table[0x04] = 2;
naive_table[0x08] = 3;
naive_table[0x10] = 4;
naive_table[0x20] = 5;
naive_table[0x40] = 6;
naive_table[0x80] = 7;
:
index = naive_table[ch];
- +
, naive_table
. , ch
, n
- n
.
, 2 8 8 -, ch
.
- , . " Bruijn 1 Word" , :
A length-n
, n
- 2, n 0 1 , 0-1 lg n
.
, 8 00011101. : 3 3- , 000, 001, 011, 111, 110, 101, 010 (), 100 ( ).
- : h (x) = (x * deBruijn) → (n - lg n)
, - :
h(ch) = ((ch * 00011101b) >> (8 - 3)) & 0x7
h(ch) = ((ch * 29) >> 5) & 0x7
ch
, - , , .. :
ch h(ch)
0x01 ((1 * 29) >> 5) & 0x7 = 0
0x02 ((2 * 29) >> 5) & 0x7 = 1
0x04 ((4 * 29) >> 5) & 0x7 = 3
0x08 ((8 * 29) >> 5) & 0x7 = 7
0x10 ((16 * 29) >> 5) & 0x7 = 6
0x20 ((32 * 29) >> 5) & 0x7 = 5
0x40 ((64 * 29) >> 5) & 0x7 = 2
0x80 ((64 * 29) >> 5) & 0x7 = 4
, - ch
.
, :
char compact_table[8];
compact_table[0] = 0;
compact_table[1] = 1;
compact_table[3] = 2;
compact_table[7] = 3;
compact_table[6] = 4;
compact_table[5] = 5;
compact_table[2] = 6;
compact_table[4] = 7;
- :
h = ((ch * 29) >> 5) & 0x7;
index = compact_table[h];
- +
: . 0-7 (.. 3- ), . , .
, ch
- :
ch h(sh) index
0x01 0 0 (000b)
0x02 1 1 (001b)
0x04 3 2 (010b)
0x08 7 3 (011b)
0x10 6 4 (100b)
0x20 5 5 (101b)
0x40 2 6 (110b)
0x80 4 7 (111b)
-:
ch h(sh) index
0x01 0 0 (000b)
0x02 1 1 (001b)
0x40 2 6 (110b)
0x04 3 2 (010b)
0x80 4 7 (111b)
0x20 5 5 (101b)
0x10 6 4 (100b)
0x08 7 3 (011b)
, - :
011 100 101 111 010 110 001 000 = 0x72f588
, . , 3-, - 3:
h = ((ch * 29) >> 5) & 0x7; // just like before
bit_string = 0x72f588;
index = (bit_string >> (h * 3)) & 0x7;
:
index = (0x72f588 >> ((((ch * 29) >> 5) & 0x7) * 3)) & 0x7;
//, .
:
unsigned char ch;
for (ch = 1; ch; ch <<= 1) {
int index = (0x72f588 >> ((((ch * 29) >> 5) & 7) * 3)) & 7;
printf("ch = 0x%02x index = %d\n", ch, index);
}
return 0;