代码之家  ›  专栏  ›  技术社区  ›  andreasw

二郎散列树

  •  0
  • andreasw  · 技术社区  · 6 年前

    我正在开发一个使用哈希树的P2P应用程序。

    我正在编写哈希树构造函数(publ/4和publ-top/4),但我看不到如何修复publ-top/4。

    我尝试用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
    

    相关代码如下:

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

    你认为问题出在哪里?

    谢谢您, 安德烈亚斯

    1 回复  |  直到 16 年前
        1
  •  4
  •   Karim    16 年前

    查看您的代码,我可以看到一个问题,它将生成特定的异常错误。

    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.
    

    在第一个函数声明中,您将与空列表匹配。在第二个声明中,您将与长度列表(至少)2匹配( [F,S|T] )当 FullLevelLen 不同于1和 RestOfLevel 是长度为1的列表吗?(提示:您将得到上述错误)。

    如果您要在函数参数上进行模式匹配,则更容易发现错误,可能类似于:

    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) ->
    %     ...