Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generalize the ets interface and behaviour
- Loading branch information
Showing
4 changed files
with
53 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,68 +1,74 @@ | |||
-module(markov_ets). | -module(markov_ets). | ||
-export([new/0, from_list/1, from_list/2, to_list/1, append/3, lookup/2, | -export([new/1, from_list/2, to_list/1, append/3, lookup/2, choose_nth/2]). | ||
choose_nth/2]). | |||
|
|
||
-define(WORD_MAX, 100). | -define(WORD_MAX, 100). | ||
|
|
||
new() -> | new(Opts) -> | ||
%% {{{token(), token()}, pair}, integer()} | %% {{{token(), token()}, pair}, integer()} | ||
%% {{{token(), token()}, token()}, integer()} | %% {{{token(), token()}, token()}, integer()} | ||
ets:new(?MODULE, [ordered_set]). | {Ets, Name, Opts1} = lists:foldr(fun optfold/2, | ||
{ets, ?MODULE, [ordered_set]}, | |||
Opts), | |||
{Ets, Ets:new(Name, Opts1)}. | |||
|
|
||
from_list(L) -> | optfold({module, Mod}, {_Mod, Name, Acc}) -> | ||
from_list(L, new()). | {Mod, Name, Acc}; | ||
optfold({name, Name}, {Mod, _Name, Acc}) -> | |||
{Mod, Name, Acc}; | |||
optfold(Opt, {Mod, Name, Acc}) -> | |||
{Mod, Name, [Opt | Acc]}. | |||
|
|
||
from_list([{K, V} | Rest], T) -> | from_list([{K, V} | Rest], State={Ets, T}) -> | ||
Total = lists:foldl( | Total = lists:foldl( | ||
fun ({Token, C}, Acc) -> | fun ({Token, C}, Acc) -> | ||
true = ets:insert_new(T, {{K, Token}, C}), | true = Ets:insert_new(T, {{K, Token}, C}), | ||
C + Acc | C + Acc | ||
end, | end, | ||
0, | 0, | ||
V), | V), | ||
true = ets:insert_new(T, {{K, pair}, Total}), | true = Ets:insert_new(T, {{K, pair}, Total}), | ||
from_list(Rest, T); | from_list(Rest, State); | ||
from_list([], T) -> | from_list([], State) -> | ||
T. | State. | ||
|
|
||
to_list(T) -> | to_list(State={Ets, T}) -> | ||
[{K, unroll(K, T)} | [{K, unroll(K, State)} | ||
|| K <- ets:select(T, [{{{'$1',pair},'_'},[],['$1']}])]. | || K <- Ets:select(T, [{{{'$1',pair},'_'},[],['$1']}])]. | ||
|
|
||
unroll(K, T) -> | unroll(K, {Ets, T}) -> | ||
ets:select( | Ets:select( | ||
T, | T, | ||
[{{{K, '$1'}, '$2'}, [{'=/=', '$1', pair}], [{{'$1', '$2'}}]}]). | [{{{K, '$1'}, '$2'}, [{'=/=', '$1', pair}], [{{'$1', '$2'}}]}]). | ||
|
|
||
inc(T, K) -> | inc({Ets, T}, K) -> | ||
try ets:update_counter(T, K, 1) | try Ets:update_counter(T, K, 1) | ||
catch error:badarg -> | catch error:badarg -> | ||
ets:insert_new(T, {K, 1}), | Ets:insert_new(T, {K, 1}), | ||
1 | 1 | ||
end. | end. | ||
|
|
||
append(K, V, T) -> | append(K, V, State) -> | ||
inc(T, {K, pair}), | inc(State, {K, pair}), | ||
inc(T, {K, V}), | inc(State, {K, V}), | ||
T. | State. | ||
|
|
||
lookup(K, T) -> | lookup(K, State={Ets, T}) -> | ||
try ets:lookup_element(T, {K, pair}, 2) | try Ets:lookup_element(T, {K, pair}, 2) | ||
of C -> | of C -> | ||
{value, {C, {K, T}}} | {value, {C, {K, State}}} | ||
catch error:badarg -> | catch error:badarg -> | ||
none | none | ||
end. | end. | ||
|
|
||
choose_nth(N, {K, T}) -> | choose_nth(N, {K, State={Ets, T}}) -> | ||
%% NOTE: It's important that 'pair' sorts lower than | %% NOTE: It's important that 'pair' sorts lower than | ||
%% 'start', 'stop', and any binary! | %% 'start', 'stop', and any binary! | ||
choose_nth_iter(N, ets:next(T, {K, pair}), T). | choose_nth_iter(N, Ets:next(T, {K, pair}), State). | ||
|
|
||
choose_nth_iter(N, K={_Prev, Next}, T) -> | choose_nth_iter(N, K={_Prev, Next}, State={Ets, T}) -> | ||
case ets:lookup_element(T, K, 2) of | case Ets:lookup_element(T, K, 2) of | ||
V when N > V -> | V when N > V -> | ||
choose_nth_iter(N - V, ets:next(T, K), T); | choose_nth_iter(N - V, Ets:next(T, K), State); | ||
_ -> | _ -> | ||
Next | Next | ||
end. | end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters