简体   繁体   English

二郎哈希树

[英]Erlang Hash Tree

I'm working on a p2p app that uses hash trees. 我正在使用哈希树的p2p应用程序。

I am writing the hash tree construction functions (publ/4 and publ_top/4) but I can't see how to fix publ_top/4. 我正在编写哈希树构造函数(publ / 4和publ_top / 4),但看不到如何修复publ_top / 4。

I try to build a tree with publ/1: 我尝试用publ / 1建立一棵树:

nivd:publ("file.txt").

prints hashes...

** exception error: no match of right hand side value [67324168]
     in function  nivd:publ_top/4
     in call from nivd:publ/1

The code in question is here: 有问题的代码在这里:

http://github.com/AndreasBWagner/nivoa/blob/886c624c116c33cc821b15d371d1090d3658f961/nivd.erl http://github.com/AndreasBWagner/nivoa/blob/886c624c116c33cc821b15d371d1090d3658f961/nivd.erl

Where do you think the problem is? 您认为问题出在哪里?

Thank You, Andreas 谢谢你,安德里亚斯

Looking at your code I can see one issue that would generate that particular exception error 查看您的代码,我可以看到一个会产生特定异常错误的问题

publ_top(_,[],Accumulated,Level) ->
    %% Go through the accumulated list of hashes from the prior level
    publ_top(string:len(Accumulated),Accumulated,[],Level+1);

publ_top(FullLevelLen,RestofLevel,Accumulated,Level) ->
  case FullLevelLen =:= 1 of
    false -> [F,S|T]=RestofLevel,
      io:format("~w---~w~n",[F,S]),
      publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
    true -> done
  end.

In the first function declaration you match against the empty list. 在第一个函数声明中,您与空列表匹配。 In the second declaration you match against a list of length (at least) 2 ( [F,S|T] ). 在第二个声明中,您匹配一个长度至少为2( [F,S|T] )的列表。 What happens when FullLevelLen is different from 1 and RestOfLevel is a list of length 1? FullLevelLen不同于1并且RestOfLevel是长度为1的列表时会发生什么? (Hint: You'll get the above error). (提示:您将收到上述错误)。

The error would be easier to spot if you would pattern match on the function arguments, perhaps something like: 如果您对函数参数进行模式匹配,则可能更容易发现错误,例如:

publ_top(_,[],Accumulated,Level) ->
    %% Go through the accumulated list of hashes from the prior level
    publ_top(string:len(Accumulated),Accumulated,[],Level+1);

publ_top(1, _, _, _) ->
    done;

publ_top(_, [F,S|T], Accumulated, Level) ->
    io:format("~w---~w~n",[F,S]),
    publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);

%% Missing case:
% publ_top(_, [H], Accumulated, Level) ->
%     ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM