Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

generalize the ets interface and behaviour

  • Loading branch information...
commit 3644298d8b7f72e6fe59228408bb906966c0cc74 1 parent 9218de6
@etrepum authored
View
3  rebar.config
@@ -3,3 +3,6 @@
{cover_enabled, true}.
{eunit_opts, [verbose, {report, {eunit_surefire,[{dir,"."}]}}]}.
{clean_files, ["*.eunit", "ebin/*.beam"]}.
+%%{deps,
+%% [{lets, ".*",
+%% {git, "git://github.com/norton/lets.git", {branch, "master"}}}]}.
View
15 src/markov.erl
@@ -1,15 +1,20 @@
-module(markov).
--export([new/0]).
--export([new/1, input/2, output/1, output/2, from_list/2, to_list/1]).
+-export([new/0, new/1, new/2]).
+-export([input/2, output/1, output/2, from_list/2, to_list/1]).
-define(WORD_MAX, 100).
-define(DEFAULT_STORAGE, markov_ets).
new() ->
- new(?DEFAULT_STORAGE).
+ new(?DEFAULT_STORAGE, []).
-new(M) ->
- {M, M:new()}.
+new(M) when is_atom(M) ->
+ new(M, []);
+new(L) when is_list(L) ->
+ new(?DEFAULT_STORAGE, L).
+
+new(M, Opts) ->
+ {M, M:new(Opts)}.
from_list(L, {M, T}) ->
{M, M:from_list(L, T)}.
View
70 src/markov_ets.erl
@@ -1,68 +1,74 @@
-module(markov_ets).
--export([new/0, from_list/1, from_list/2, to_list/1, append/3, lookup/2,
- choose_nth/2]).
+-export([new/1, from_list/2, to_list/1, append/3, lookup/2, choose_nth/2]).
-define(WORD_MAX, 100).
-new() ->
+new(Opts) ->
%% {{{token(), token()}, pair}, 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) ->
- from_list(L, new()).
+optfold({module, Mod}, {_Mod, Name, Acc}) ->
+ {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(
fun ({Token, C}, Acc) ->
- true = ets:insert_new(T, {{K, Token}, C}),
+ true = Ets:insert_new(T, {{K, Token}, C}),
C + Acc
end,
0,
V),
- true = ets:insert_new(T, {{K, pair}, Total}),
- from_list(Rest, T);
-from_list([], T) ->
- T.
+ true = Ets:insert_new(T, {{K, pair}, Total}),
+ from_list(Rest, State);
+from_list([], State) ->
+ State.
-to_list(T) ->
- [{K, unroll(K, T)}
- || K <- ets:select(T, [{{{'$1',pair},'_'},[],['$1']}])].
+to_list(State={Ets, T}) ->
+ [{K, unroll(K, State)}
+ || K <- Ets:select(T, [{{{'$1',pair},'_'},[],['$1']}])].
-unroll(K, T) ->
- ets:select(
+unroll(K, {Ets, T}) ->
+ Ets:select(
T,
[{{{K, '$1'}, '$2'}, [{'=/=', '$1', pair}], [{{'$1', '$2'}}]}]).
-inc(T, K) ->
- try ets:update_counter(T, K, 1)
+inc({Ets, T}, K) ->
+ try Ets:update_counter(T, K, 1)
catch error:badarg ->
- ets:insert_new(T, {K, 1}),
+ Ets:insert_new(T, {K, 1}),
1
end.
-append(K, V, T) ->
- inc(T, {K, pair}),
- inc(T, {K, V}),
- T.
+append(K, V, State) ->
+ inc(State, {K, pair}),
+ inc(State, {K, V}),
+ State.
-lookup(K, T) ->
- try ets:lookup_element(T, {K, pair}, 2)
+lookup(K, State={Ets, T}) ->
+ try Ets:lookup_element(T, {K, pair}, 2)
of C ->
- {value, {C, {K, T}}}
+ {value, {C, {K, State}}}
catch error:badarg ->
none
end.
-choose_nth(N, {K, T}) ->
+choose_nth(N, {K, State={Ets, T}}) ->
%% NOTE: It's important that 'pair' sorts lower than
%% '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) ->
- case ets:lookup_element(T, K, 2) of
+choose_nth_iter(N, K={_Prev, Next}, State={Ets, T}) ->
+ case Ets:lookup_element(T, K, 2) of
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
end.
View
7 src/markov_gb.erl
@@ -1,15 +1,12 @@
-module(markov_gb).
--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]).
-define(WORD_MAX, 100).
-new() ->
+new([]) ->
gb_trees:empty().
-from_list(L) ->
- from_list(L, new()).
-
from_list(L, EmptyT) ->
lists:foldl(fun ({K, V}, T) ->
gb_trees:insert(K, subtree_from_list(V), T)
Please sign in to comment.
Something went wrong with that request. Please try again.