Skip to content

Commit

Permalink
Add keys/1
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysiekJ committed Oct 7, 2018
1 parent 1687f8b commit e1d01df
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PROJECT = gb_merkle_trees
PROJECT_DESCRIPTION = General balanced Merkle trees
PROJECT_VERSION = 0.2.1
PROJECT_VERSION = 0.2.2

TEST_DEPS = triq
dep_triq = git https://gitlab.com/triq/triq.git e5ba907a11985bf8150f5b5b332d39516ab15857
Expand Down
2 changes: 1 addition & 1 deletion ebin/gb_merkle_trees.app
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, 'gb_merkle_trees', [
{description, "General balanced Merkle trees"},
{vsn, "0.2.0"},
{vsn, "0.2.2"},
{modules, ['gb_bench','gb_merkle_trees']},
{registered, []},
{applications, [kernel,stdlib,crypto]},
Expand Down
11 changes: 11 additions & 0 deletions src/gb_merkle_trees.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from_list/1,
from_orddict/1,
from_orddict/2,
keys/1,
lookup/2,
merkle_proof/2,
root_hash/1,
Expand Down Expand Up @@ -198,6 +199,14 @@ to_orddict(Tree) ->
[],
Tree).

-spec keys(tree()) -> list(key()).
%% @doc Return the keys as an ordered list.
keys(Tree) ->
foldr(
fun ({Key, _}, Acc) -> [Key|Acc] end,
[],
Tree).

-spec foldr(fun(({key(), value()}, Acc :: any()) -> any()), Acc :: any(), tree()) -> Acc :: any().
%% @doc Iterate through keys and values, from those with highest keys to lowest.
foldr(Fun, Acc, {_, RootNode}) ->
Expand Down Expand Up @@ -510,6 +519,8 @@ prop_orddict_conversion_idempotence() ->
?FORALL(KVO, kv_orddict(), KVO =:= to_orddict(from_orddict(KVO))).
prop_from_orddict_returns_a_perfectly_balanced_tree() ->
?FORALL(KVO, kv_orddict(), is_perfectly_balanced(from_orddict(KVO))).
prop_keys() ->
?FORALL(Tree, tree(), keys(Tree) =:= [Key || {Key, _} <- to_orddict(Tree)]).
from_list_sometimes_doesnt_return_a_perfectly_balanced_tree_test() ->
?assertNotEqual(
true,
Expand Down

0 comments on commit e1d01df

Please sign in to comment.