:
~ 1000 , , , , 1 8, . . , , set1 = { e1 } set2 = { e2 }, e1 != e2, set1 intersect set2 = { }, bf(set1) interesect bf(set2) <> {}. : - bf(set1) intersect bf(set2) = {}, set1 intersect set2 = {}.
, BF R W, , 2 .
, C:
const unsigned N = 1024 * 8;
const unsigned BPW = 8 * sizeof ulong;
typedef unsigned long ulong;
typedef struct BF { ulong bits[N/BPW]; } BF;
unsigned hash(ulong e) { return foo(e) % N; }
void clear(BF* pbf) { memset(pbf->bits, 0, sizeof(pbf->bits)); }
void add(BF* pbf, ulong e) { unsigned h = hash(e); bf.bits[h/BPW] |= 1 << (h%BPW); }
bool hit(BF* pbf, ulong e) { unsigned h = hash(e); return (bf.bits[h/BPW]>>(h%BPW)) & 1; }
bool intersect(BF* pbfResult, BF* pbf1, BF* pbf2) {
bool empty = TRUE;
for (unsigned i = 0; i < N/BPW; i++)
if ((pbfResult->bits[i] = pbf1->bits[i] & pbf2->bits[i]) != 0)
empty = FALSE;
return !empty;
}
void intersectRW(unsigned nr, ulong* r, unsigned nw, ulong* w) {
BF bfR, bfW, bfIntesection;
unsigned i;
clear(&bfR);
for (i = 0; i < nr; i++)
add(&bfR, r[i]);
for (i = 0; i < nw; i++)
if (hit(&bfR, w[i]))
... w[i] ...
clear(&bfW);
for (i = 0; i < nw; i++)
add(&bfW, w[i]);
bool any = intersect(&bfIntersection, &bfR, &bfW);
...
}
?
- 3 BF 1 KB, . 128 ulongs, TOS L1 $, , ;
- 100-1000 bfR, . ~ 1000 add, ;
- 100-1000 bfR, . ~ 1000 , , , ;
- 2, AND ~ 128 ulongs
( , , /% .)
- L1 L2; 2 , , .
-, 64- . , 64- 16 , xors .
* - MS V++ 4.0 " " (http://msdn.microsoft.com/en-us/library/kfz8ad09(VS.80).aspx) - . , , ... *
?
!
, :
- Overkill, SIMD (, SSE).
- , . , - R W, , , , .
- . , , , ( add() s intersect().)
- , , R W , , BF R W, (OR) BF (R) s BF (W) s .