Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added store, fold, and append methods to manipulate the sgte_dict str…
…ucture
  • Loading branch information
Hunter Morris committed Apr 29, 2008
1 parent afc11da commit 60fda23
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/sgte_dict.erl
Expand Up @@ -32,7 +32,11 @@
-import(.dict). % ditto -import(.dict). % ditto
-endif. -endif.


-export([rfind/2, -export([rfind/2,
rstore/3,
rfoldl/2,
rfoldr/2,
rappend/3,
find/2, find/2,
store/3, store/3,
merge/3, merge/3,
Expand All @@ -58,6 +62,35 @@ rfind([H|T], Dict) ->
?MODULE:rfind(T, D) ?MODULE:rfind(T, D)
end. end.


rstore([Key], V, D) ->
dict:store(Key, V, D);
rstore([Key|T], V, D) ->
D1 = case find(Key, D) of
error ->
dict:new();
SubDict ->
SubDict
end,
dict:store(Key, rstore(T, V, D1), D).

rfoldacc({K, V}, Acc) ->
rstore(K, V, Acc).

rfoldl(KV, L) ->
lists:foldl(fun rfoldacc/2, L, KV).

rfoldr(KV, L) ->
lists:foldr(fun rfoldacc/2, L, KV).

rappend(K, V, L) ->
case rfind(K, L) of
{error, _} ->
rstore(K, [V], L);
{ok, A} ->
V1 = lists:reverse([V|lists:reverse(A)]),
rstore(K, V1, L)
end.

find(Key, Dict) when is_list(Dict) -> find(Key, Dict) when is_list(Dict) ->
dict:find(Key, dict:from_list(Dict)); dict:find(Key, dict:from_list(Dict));
find(Key, Dict) -> find(Key, Dict) ->
Expand Down

0 comments on commit 60fda23

Please sign in to comment.