erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

77 řádky
1.9 KiB

před 5 roky
  1. -module(nifHashb).
  2. -on_load(init/0).
  3. -export([
  4. new/0
  5. , get/2
  6. , put/3
  7. , hash1/2
  8. , hash2/2
  9. , hash3/2
  10. , cb1/2
  11. , cb2/2
  12. , compareBin1/2
  13. , compareBin2/2
  14. ]).
  15. init() ->
  16. SoName =
  17. case code:priv_dir(?MODULE) of
  18. {error, _} ->
  19. case code:which(?MODULE) of
  20. Filename when is_list(Filename) ->
  21. filename:join([filename:dirname(Filename), "../priv", "nifHashb"]);
  22. _ ->
  23. filename:join("../priv", "nifHashb")
  24. end;
  25. Dir ->
  26. filename:join(Dir, "nifHashb")
  27. end,
  28. erlang:load_nif(SoName, 0).
  29. new() ->
  30. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  31. get(Ref, Key) ->
  32. KeyBin = erlang:term_to_binary(Key),
  33. Hash1 = erlang:phash2(KeyBin),
  34. Hash2 = erlang:phash2(KeyBin, 123211111),
  35. get(Ref, Hash1, Hash2, KeyBin).
  36. get(Ref, Hash1, Hash2, KeyBin) ->
  37. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  38. put(Ref, Key, Value) ->
  39. KeyBin = erlang:term_to_binary(Key),
  40. ValueBin = erlang:term_to_binary(Value),
  41. Hash1 = erlang:phash2(KeyBin),
  42. Hash2 = erlang:phash2(KeyBin, 123211111),
  43. put(Ref, Hash1, Hash2, KeyBin, ValueBin).
  44. put(Ref, Hash1, Hash2, Key, Value) ->
  45. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  46. hash1(Term, Range) ->
  47. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  48. hash2(Term, Range) ->
  49. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  50. hash3(Term, Range) ->
  51. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  52. cb1(Term1, Term2) ->
  53. compareBin1(term_to_binary(Term1), term_to_binary(Term2)).
  54. cb2(Term1, Term2) ->
  55. compareBin2(term_to_binary(Term1), term_to_binary(Term2)).
  56. compareBin1(Bin1, Bin2) ->
  57. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).
  58. compareBin2(Bin1, Bin2) ->
  59. erlang:nif_error({nif_not_loaded, module, ?MODULE, line, ?LINE}).