Use bel for powers of two
D’oh. That’s much much faster of course!
This commit is contained in:
parent
8c3d0fc493
commit
f8f2e02d92
1 changed files with 34 additions and 39 deletions
|
@ -9,22 +9,6 @@
|
||||||
|
|
||||||
-include("include/leveled.hrl").
|
-include("include/leveled.hrl").
|
||||||
|
|
||||||
-define(TWO_POWER,
|
|
||||||
list_to_tuple(
|
|
||||||
lists:reverse(
|
|
||||||
element(2,
|
|
||||||
lists:foldl(
|
|
||||||
fun(_I, {AccLast, AccList}) ->
|
|
||||||
{AccLast * 2,
|
|
||||||
[(AccLast * 2)|AccList]}
|
|
||||||
end,
|
|
||||||
{1, [1]},
|
|
||||||
lists:seq(2, 32))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
).
|
|
||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -48,13 +32,9 @@ create_bloom(HashList) ->
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0);
|
0, 0, 0, 0, 0, 0);
|
||||||
L when L > 16 ->
|
L when L > 16 ->
|
||||||
add_hashlist(HashList,
|
add_hashlist(HashList, 3, 0, 0, 0, 0);
|
||||||
array:new([{size, 4}, {default, 0}]),
|
|
||||||
3);
|
|
||||||
_ ->
|
_ ->
|
||||||
add_hashlist(HashList,
|
add_hashlist(HashList, 1, 0, 0)
|
||||||
array:new([{size, 2}, {default, 0}]),
|
|
||||||
1)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_hash(_Hash, <<>>) ->
|
check_hash(_Hash, <<>>) ->
|
||||||
|
@ -87,23 +67,43 @@ split_hash(Hash, SlotSplit) ->
|
||||||
get_mask(H0, H1) ->
|
get_mask(H0, H1) ->
|
||||||
case H0 == H1 of
|
case H0 == H1 of
|
||||||
true ->
|
true ->
|
||||||
element(H0 + 1, ?TWO_POWER);
|
1 bsl H0;
|
||||||
false ->
|
false ->
|
||||||
element(H0 + 1, ?TWO_POWER) + element(H1 + 1, ?TWO_POWER)
|
(1 bsl H0) + (1 bsl H1)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
add_hashlist([], SlotArray, SlotSplit) ->
|
|
||||||
BuildBinFun =
|
%% This looks ugly and clunky, but in tests it was quicker than modifying an
|
||||||
fun(I, Acc) ->
|
%% Erlang term like an array as it is passed around the loop
|
||||||
Bloom = array:get(I, SlotArray),
|
|
||||||
<<Acc/binary, Bloom:32/integer>>
|
add_hashlist([], _S, S0, S1) ->
|
||||||
end,
|
<<S0:32/integer, S1:32/integer>>;
|
||||||
lists:foldl(BuildBinFun, <<>>, lists:seq(0, SlotSplit));
|
add_hashlist([TopHash|T], SlotSplit, S0, S1) ->
|
||||||
add_hashlist([TopHash|T], SlotArray, SlotSplit) ->
|
{Slot, H0, H1} = split_hash(TopHash, SlotSplit),
|
||||||
|
SW = os:timestamp(),
|
||||||
|
Mask = get_mask(H0, H1),
|
||||||
|
case Slot of
|
||||||
|
0 ->
|
||||||
|
add_hashlist(T, SlotSplit, S0 bor Mask, S1);
|
||||||
|
1 ->
|
||||||
|
add_hashlist(T, SlotSplit, S0, S1 bor Mask)
|
||||||
|
end.
|
||||||
|
|
||||||
|
add_hashlist([], _S, S0, S1, S2, S3) ->
|
||||||
|
<<S0:32/integer, S1:32/integer, S2:32/integer, S3:32/integer>>;
|
||||||
|
add_hashlist([TopHash|T], SlotSplit, S0, S1, S2, S3) ->
|
||||||
{Slot, H0, H1} = split_hash(TopHash, SlotSplit),
|
{Slot, H0, H1} = split_hash(TopHash, SlotSplit),
|
||||||
Mask = get_mask(H0, H1),
|
Mask = get_mask(H0, H1),
|
||||||
I = array:get(Slot, SlotArray),
|
case Slot of
|
||||||
add_hashlist(T, array:set(Slot, I bor Mask, SlotArray), SlotSplit).
|
0 ->
|
||||||
|
add_hashlist(T, SlotSplit, S0 bor Mask, S1, S2, S3);
|
||||||
|
1 ->
|
||||||
|
add_hashlist(T, SlotSplit, S0, S1 bor Mask, S2, S3);
|
||||||
|
2 ->
|
||||||
|
add_hashlist(T, SlotSplit, S0, S1, S2 bor Mask, S3);
|
||||||
|
3 ->
|
||||||
|
add_hashlist(T, SlotSplit, S0, S1, S2, S3 bor Mask)
|
||||||
|
end.
|
||||||
|
|
||||||
add_hashlist([], _S, S0, S1, S2, S3, S4, S5, S6, S7, S8, S9,
|
add_hashlist([], _S, S0, S1, S2, S3, S4, S5, S6, S7, S8, S9,
|
||||||
SA, SB, SC, SD, SE, SF) ->
|
SA, SB, SC, SD, SE, SF) ->
|
||||||
|
@ -330,11 +330,6 @@ test_bloom(N) ->
|
||||||
++ " build ~w check ~w neg_check ~w and fpr ~w~n",
|
++ " build ~w check ~w neg_check ~w and fpr ~w~n",
|
||||||
[N, TSa, TSb, TSc, FPR]).
|
[N, TSa, TSb, TSc, FPR]).
|
||||||
|
|
||||||
twopower_test() ->
|
|
||||||
?assertMatch(1, element(1, ?TWO_POWER)),
|
|
||||||
?assertMatch(128, element(8, ?TWO_POWER)),
|
|
||||||
?assertMatch(2147483648, element(32, ?TWO_POWER)).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-endif.
|
-endif.
|
Loading…
Add table
Add a link
Reference in a new issue