Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Insert and delete.

  • Loading branch information...
commit e22ba59b619c2b7aba9d2dbf83446924dcd5e39f 1 parent facccd8
@knutin authored
Showing with 47 additions and 7 deletions.
  1. +47 −7 src/btrie.erl
View
54 src/btrie.erl
@@ -6,6 +6,10 @@
-define(VALUE, "v").
+-include_lib("eunit/include/eunit.hrl").
+
+
+
%% new() ->
%% <<?MAGIC>>.
@@ -45,18 +49,16 @@ find(<<?MAGIC, B/binary>>, Key) ->
find(<<K:8, Size:32/integer, Value:Size/binary, _/binary>>, <<K:8>>) ->
Value;
-find(<<K, ChildrenSize:32/integer, Children:ChildrenSize/binary, _/binary>> = B,
+find(<<>>, _Key) ->
+ error(badarg);
+
+find(<<K, ChildrenSize:32/integer, Children:ChildrenSize/binary, _/binary>>,
<<K, KeyRest/binary>>) ->
- io:format("B: ~p~n", [B]),
- io:format("children: ~p, key: ~p, keyrest: ~p~n", [Children, K, KeyRest]),
find(Children, KeyRest);
-find(<<_:8, ChildrenSize:32/integer, Children:ChildrenSize/binary, Siblings/binary>>,
+find(<<_:8, ChildrenSize:32/integer, _:ChildrenSize/binary, Siblings/binary>>,
Key) ->
- io:format("children size: ~p~n", [ChildrenSize]),
- io:format("children: ~p~n", [Children]),
- io:format("siblings: ~p~n", [Siblings]),
find(Siblings, Key).
@@ -66,6 +68,9 @@ insert(<<?MAGIC, B/binary>>, Key, Value) when is_binary(Key) andalso is_binary(V
insert(<<>>, <<Key>>, Value) ->
node(Key, Value);
+insert(<<?VALUE, _/binary>>, <<_Key>>, _Value) ->
+ error(badarg);
+
insert(<<>>, <<K:8, KeyRest/binary>>, Value) ->
node(K, insert(<<>>, KeyRest, Value));
@@ -78,9 +83,44 @@ insert(<<K:8, Size:32/integer, Children:Size/binary, Siblings/binary>>,
insert(<<K:8, Size:32/integer, Children:Size/binary, Siblings/binary>>,
Key, Value) ->
NewSiblings = insert(Siblings, Key, Value),
+ <<K, Size:32, Children/binary, NewSiblings/binary>>.
+
+
+delete(<<?MAGIC, B/binary>>, Key) when is_binary(Key) ->
+ delete(B, Key);
+
+delete(<<K:8, Size:32/integer, _:Size/binary, Siblings/binary>>, <<K:8>>) ->
+ Siblings;
+
+delete(<<K:8, Size:32/integer, Children:Size/binary, Siblings/binary>>,
+ <<K, KeyRest/binary>>) ->
+ NewChildren = delete(Children, KeyRest),
+ <<K, (size(NewChildren)):32, NewChildren/binary, Siblings/binary>>;
+
+delete(<<K:8, Size:32/integer, Children:Size/binary, Siblings/binary>>,
+ Key) ->
+ NewSiblings = delete(Siblings, Key),
<<K, Size:32, Children/binary, NewSiblings/binary>>.
+%%
+%% TESTS
+%%
+
+find_test() ->
+ ?assertEqual(<<"foo">>, find(new(), <<"aa">>)).
+
+insert_test() ->
+ ?assertEqual(<<"new">>, find(insert(new(), <<"def">>, <<"new">>), <<"def">>)),
+ ?assertError(badarg, insert(new(), <<"aaa">>, <<"foo">>)).
+
+delete_test() ->
+ ?assertEqual(not_found, find(
+ delete(
+ insert(new(), <<"def">>, <<"new">>),
+ <<"def">>),
+ <<"def">>)).
+

3 comments on commit e22ba59

@krestenkrab

Would be good to have a mass-insert option; I can imagine it's expensive to build it dynamically?

@knutin
Owner

You mean a mass-insert from a pre-sorted structure? I'm not sure if it can be done in a cheap binary append operation, need to look into it.

Being a binary, the resulting structure can easily be dumped to disk for "persistence" to avoid rebuilding a big structure all the time.

@krestenkrab

Yes I was thinking something from a presorted structure. Perhaps building an iolist and ultimately doing iolist_to_binary.

Please sign in to comment.
Something went wrong with that request. Please try again.