Alternate implementation

This commit is contained in:
martinsumner 2017-01-24 17:15:39 +00:00
parent 9d95057518
commit 8c3d0fc493

View file

@ -39,28 +39,26 @@
create_bloom(HashList) -> create_bloom(HashList) ->
SlotSplit =
case length(HashList) of case length(HashList) of
L when L > 64 -> 0 ->
15; <<>>;
L when L > 32 -> L when L > 32 ->
7; add_hashlist(HashList,
15,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0);
L when L > 16 -> L when L > 16 ->
3;
_ ->
1
end,
case SlotSplit of
15 ->
add_hashlist(HashList, add_hashlist(HashList,
SlotSplit, array:new([{size, 4}, {default, 0}]),
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 3);
_ -> _ ->
add_hashlist(HashList, add_hashlist(HashList,
array:new([{size, SlotSplit + 1}, {default, 0}]), array:new([{size, 2}, {default, 0}]),
SlotSplit) 1)
end. end.
check_hash(_Hash, <<>>) ->
false;
check_hash(Hash, BloomBin) -> check_hash(Hash, BloomBin) ->
SlotSplit = (byte_size(BloomBin) div 4) - 1, SlotSplit = (byte_size(BloomBin) div 4) - 1,
{Slot, H0, H1} = split_hash(Hash, SlotSplit), {Slot, H0, H1} = split_hash(Hash, SlotSplit),
@ -265,6 +263,12 @@ check_neg_hashes(BloomBin, HashList, Counters) ->
end, end,
lists:foldl(CheckFun, Counters, HashList). lists:foldl(CheckFun, Counters, HashList).
empty_bloom_test() ->
BloomBin0 = create_bloom([]),
?assertMatch({0, 4},
check_neg_hashes(BloomBin0, [0, 10, 100, 100000], {0, 0})).
bloom_test() -> bloom_test() ->
test_bloom(128), test_bloom(128),
test_bloom(64), test_bloom(64),
@ -272,7 +276,6 @@ bloom_test() ->
test_bloom(16), test_bloom(16),
test_bloom(8). test_bloom(8).
test_bloom(N) -> test_bloom(N) ->
HashList1 = get_hashlist(N), HashList1 = get_hashlist(N),
HashList2 = get_hashlist(N), HashList2 = get_hashlist(N),
@ -288,17 +291,15 @@ test_bloom(N) ->
case N of case N of
128 -> 128 ->
?assertMatch(64, byte_size(BloomBin1)), ?assertMatch(64, byte_size(BloomBin1));
?assertMatch(64, byte_size(BloomBin2)),
?assertMatch(64, byte_size(BloomBin3)),
?assertMatch(64, byte_size(BloomBin4));
64 -> 64 ->
?assertMatch(32, byte_size(BloomBin1)), ?assertMatch(64, byte_size(BloomBin1));
?assertMatch(32, byte_size(BloomBin2)), 32 ->
?assertMatch(32, byte_size(BloomBin3)), ?assertMatch(16, byte_size(BloomBin1));
?assertMatch(32, byte_size(BloomBin4)); 16 ->
_ -> ?assertMatch(8, byte_size(BloomBin1));
ok 8 ->
?assertMatch(8, byte_size(BloomBin1))
end, end,
SWb = os:timestamp(), SWb = os:timestamp(),