Permalink
Browse files

use ets internal counters for sample size and use lists:unzip for get…

…ting values
  • Loading branch information...
1 parent 573c1ea commit 703cf479c2ce35a13cf4c246ebba5884e88e241a @joewilliams joewilliams committed Apr 20, 2012
Showing with 9 additions and 8 deletions.
  1. +1 −2 src/folsom_sample_exdec.erl
  2. +4 −3 src/folsom_sample_none.erl
  3. +4 −3 src/folsom_sample_uniform.erl
@@ -56,8 +56,7 @@ update(#exdec{reservoir = Reservoir, alpha = Alpha, start = Start, next = Next}
update(Sample#exdec{reservoir = NewRes, next = NewNext, start = NewStart}, Value, Timestamp).
get_values(#exdec{reservoir = Reservoir}) ->
- ResList = ets:tab2list(Reservoir),
- {_, Values} = lists:unzip(ResList),
+ {_, Values} = lists:unzip(ets:tab2list(Reservoir)),
Values.
% internal api
@@ -39,12 +39,13 @@ new(Size) ->
update(#none{size = Size, reservoir = Reservoir, n = N} = Sample, Value)
when N < Size ->
ets:insert(Reservoir, {N, Value}),
- Sample#none{n = N+1};
+ Sample#none{n = folsom_utils:get_ets_size(Reservoir) + 1};
update(#none{reservoir = Reservoir, n = N} = Sample, Value) ->
Oldest = ets:first(Reservoir),
ets:delete(Reservoir, Oldest),
ets:insert(Reservoir, {N, Value}),
- Sample#none{n = N+1}.
+ Sample#none{n = folsom_utils:get_ets_size(Reservoir) + 1}.
get_values(#none{reservoir = Reservoir}) ->
- [Val || {_,Val} <- ets:tab2list(Reservoir)].
+ {_, Values} = lists:unzip(ets:tab2list(Reservoir)),
+ Values.
@@ -45,16 +45,17 @@ new(Size) ->
update(#uniform{size = Size, reservoir = Reservoir, n = N} = Sample, Value) when N =< Size ->
ets:insert(Reservoir, {N, Value}),
- Sample#uniform{n = N+1};
+ Sample#uniform{n = folsom_utils:get_ets_size(Reservoir) + 1};
update(#uniform{reservoir = Reservoir, size = Size, n = N, seed = Seed} = Sample,
Value) ->
{Rnd, New_seed} = random:uniform_s(N, Seed),
maybe_update(Rnd, Size, Value, Reservoir),
- Sample#uniform{n = N+1, seed=New_seed}.
+ Sample#uniform{n = folsom_utils:get_ets_size(Reservoir) + 1, seed=New_seed}.
get_values(#uniform{reservoir = Reservoir}) ->
- [Val || {_,Val} <- ets:tab2list(Reservoir)].
+ {_, Values} = lists:unzip(ets:tab2list(Reservoir)),
+ Values.
maybe_update(Rnd, Size, Value, Reservoir) when Rnd < Size ->
ets:insert(Reservoir, {Rnd, Value});

0 comments on commit 703cf47

Please sign in to comment.