Permalink
Browse files

Pull in rvirdings code as well for now. This is just testing.

  • Loading branch information...
1 parent 39c7da5 commit af1fdd53bb1ecc059c5e7312d8220f30444d741f @jlouis committed Feb 17, 2011
Showing with 1,416 additions and 9 deletions.
  1. +2 −1 ebin/myapp.app
  2. +2 −0 rebar.config
  3. +49 −8 src/bench_map.erl
  4. +17 −0 src/h_rb_set.erl
  5. +110 −0 src/patricia.erl
  6. +425 −0 src/rbdict.erl
  7. +403 −0 src/rbdict1.erl
  8. +408 −0 src/rbsets.erl
View
@@ -5,4 +5,5 @@
{applications,[kernel,stdlib]},
{mod,{myapp_app,[]}},
{env,[]},
- {modules,[bench_map,myapp_app,myapp_sup]}]}.
+ {modules,[bench_map,h_rb_set,myapp_app,myapp_sup,patricia,
+ rbdict,rbdict1,rbsets]}]}.
View
@@ -19,3 +19,5 @@
{xref_checks, [undefined_function_calls,
locals_not_used,
exports_not_used]}.
+
+%{deps, [{rb, ".*", {git, "git://github.com/rvirding/rb.git", "master"}}]}.
View
@@ -1,23 +1,33 @@
-module(bench_map).
--export([foo/0]).
+-export([run/0]).
-ifdef(TEST).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-endif.
-foo() ->
- foo.
-
-
--ifdef(EUNIT).
--ifdef(EQC).
+run() ->
+ [{sets, runs(
+ fun () ->
+ timer:tc(fun() -> set_test(), ok end, [])
+ end)},
+ {dict, timer:tc(fun() -> dict_test(), ok end, [])},
+ {gb_sets, runs(
+ fun () ->
+ timer:tc(fun() -> gb_sets_test(), ok end, [])
+ end)},
+ {gb_trees, timer:tc(fun() -> gb_trees_test(), ok end, [])},
+ {h_rb_sets, timer:tc(fun() -> h_rb_set_test(), ok end, [])},
+ {rb_sets, timer:tc(fun() -> rb_sets_test(), ok end, [])}].
+
+runs(F) ->
+ [F() || _ <- lists:seq(1, 10)].
words() ->
Words = "/usr/share/dict/words",
{ok, Content} = file:read_file(Words),
- binary:split(Content, <<"\n">>, [global]).
+ [binary_to_list(W) || W <- binary:split(Content, <<"\n">>, [global])].
list_shuffle(L) ->
random:seed(), %% Reset Random function
@@ -32,6 +42,20 @@ test_sets_words(Words, Set) ->
end,
Words).
+test_h_rb_set_words(Words, Set) ->
+ lists:foreach(
+ fun(Word) ->
+ true = h_rb_set:is_element(Word, Set)
+ end,
+ Words).
+
+test_rb_sets_words(Words, Set) ->
+ lists:foreach(
+ fun(Word) ->
+ true = rbsets:is_element(Word, Set)
+ end,
+ Words).
+
test_gb_sets_words(Words, Set) ->
lists:foreach(
fun(Word) ->
@@ -60,6 +84,12 @@ test_map(Generator, TestFun) ->
TestFun(lists:reverse(Ws), S),
TestFun(list_shuffle(Ws), S).
+h_rb_set_test() ->
+ test_map(fun(Ws) ->
+ h_rb_set:from_list(Ws)
+ end,
+ fun test_h_rb_set_words/2).
+
set_test() ->
test_map(fun(Ws) ->
sets:from_list(Ws)
@@ -89,6 +119,17 @@ gb_trees_test() ->
end,
fun test_gb_trees_words/2).
+rb_sets_test() ->
+ test_map(fun(Ws) ->
+ rbsets:from_list(Ws)
+ end,
+ fun test_rb_sets_words/2).
+
+-ifdef(EUNIT).
+-ifdef(EQC).
+
+
+
-endif.
-endif.
View
@@ -0,0 +1,17 @@
+-module(h_rb_set).
+
+-export([from_list/1, is_element/2]).
+
+from_list(L) ->
+ lists:foldl(
+ fun(K, T) ->
+ H = erlang:phash2(K),
+ rbdict:append(H, K, T)
+ end,
+ rbdict:new(),
+ L).
+
+is_element(Key, Tree) ->
+ H = erlang:phash2(Key),
+ Elems = rbdict:fetch(H, Tree),
+ lists:member(Key, Elems).
View
@@ -0,0 +1,110 @@
+-module(patricia).
+%% @todo Go over all the ordering values and check them.
+
+-export([insert/2, is_element/2, from_list/1, new/0]).
+
+-define(BIT_MAX, 27). % Set by virtue of erlang:phash2
+-type ptree(A) :: empty | {leaf, [A]} | {node, pos_integer(), ptree(A), ptree(A)}.
+
+-spec insert(A, ptree(A)) -> ptree(A) | already.
+
+hash(X) ->
+ erlang:phash2(X).
+
+from_list(L) ->
+ lists:foldl(
+ fun insert/2,
+ new(),
+ L).
+
+new() ->
+ empty.
+
+insert(E, empty) ->
+ {leaf, [E]};
+insert(E, Tree) ->
+ H = hash(E),
+ {Bit, Lt} = find_bit(H, Tree),
+ insert(H, E, Bit, Lt, Tree).
+
+find_bit(H, {leaf, [A | _]}) ->
+ H1 = hash(A),
+ crit_bit(H, H1);
+find_bit(H, {node, Bit, Left, Right}) ->
+ case inspect_bit(H, Bit) of
+ left -> find_bit(H, Left);
+ right -> find_bit(H, Right)
+ end.
+
+crit_bit(I1, I2) ->
+ crit_bit(I1, I2, ?BIT_MAX).
+
+cmp_lt_bit(I1, I2, N) ->
+ Bit = (1 bsr N),
+ (Bit band I1) < (Bit band I2).
+
+crit_bit(I1, I2, N) ->
+ Bit = (1 bsr N),
+ case (Bit band I1) bxor (Bit band I2) of
+ 0 ->
+ crit_bit(I1, I2, N-1);
+ _ ->
+ {N, cmp_lt_bit(I1, I2, N)}
+ end.
+
+inspect_bit(H, Bit) ->
+ case H band (1 bsr Bit) of
+ 0 -> left;
+ _ -> right
+ end.
+
+
+insert(_H, E, Bit, Lt, {leaf, Es} = Lf) ->
+ case lists:member(E, Es) of
+ true ->
+ {leaf, Es};
+ false ->
+ case Lt of
+ true ->
+ {node, Bit, {leaf, [E]}, Lf};
+ false ->
+ {node, Bit, Lf, {leaf, [E]}}
+ end
+ end;
+insert(H, E, Bit, Lt, {node, CBit, Left, Right}) when Bit < CBit ->
+ case inspect_bit(H, CBit) of
+ left ->
+ {node, CBit, insert(H, E, Lt, Bit, Left), Right};
+ right ->
+ {node, CBit, Left, insert(H, E, Lt, Bit, Right)}
+ end;
+insert(_H, E, Bit, Lt, {node, CBit, _Left, _Right} = N) when Bit > CBit ->
+ case Lt of
+ true ->
+ {node, Bit, {leaf, [E]}, N};
+ false ->
+ {node, Bit, N, {leaf, [E]}}
+ end.
+
+is_element(Key, Tree) ->
+ H = hash(Key),
+ is_element1(H, Key, 0, Tree).
+
+is_element1(_, _, _, empty) -> false;
+is_element1(_H, Key, _Cnt, {leaf, Elems}) ->
+ lists:member(Key, Elems);
+is_element1(H, Key, Cnt, {node, Bit, L, R}) ->
+ case H band (1 bsr (Cnt + Bit)) of
+ 0 -> is_element1(H, Key, Cnt+Bit, L);
+ 1 -> is_element1(H, Key, Cnt+Bit, R)
+ end.
+
+
+
+
+
+
+
+
+
+
Oops, something went wrong.

0 comments on commit af1fdd5

Please sign in to comment.