Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.84 KB | None | 0 0
  1. -module(assoc).
  2.  
  3. -export([new/0, from_list/1, to_list/1, store/3, erase/2, fold/3, map/2, find/2, size/1, is_key/2, deletesp/1]).
  4.  
  5. %Бинарное дерево - дерево. Кортеж {Ключ, Значение, Меньшее_поддерево, Большее_поддерево}.
  6. %В левом поддереве все ключи меньше Ключа, в правом — больше.
  7. %Пустое дерево - {}.
  8.  
  9. %Новый пустой массив:
  10. new() -> {};
  11.  
  12. %Список -> ассоциативный массив
  13.  
  14. from_list (Xs) ->
  15.     from_list(Xs, new()).
  16. from_list([], Btree) ->
  17.     Btree;
  18. from_list( [{K, V}| S], Btree) ->
  19.     from_list (S, store (K, V, Btree).
  20.  
  21. %Ассоциативный массив -> список
  22.  
  23. to_list (Btree) ->
  24.     to_list (Btree, []).
  25. to_list (_, Xs) ->
  26.     Xs;
  27. to_list ({K, V, S, B}, Xs) ->
  28.     to_list(S, to_list(B, [{K, V|Xs}]).
  29.  
  30. %Запись нового узла дерева
  31.  
  32. store(K, V, {}) ->
  33.     {K, V, new(), new()};
  34. store(Key, Value, {Key, _, S, B}) ->
  35.     {K, Value, S, B};
  36. store(Key, Value, {K, V, S, B}) when Key < K ->
  37.     store(K, V, store(Key, Value, S), B);
  38. store(Key, Value, {K, _, S, B}) when Key > K ->
  39.     store(K, S, store(Key, Value, B)).
  40.  
  41. %Поиск узла по ключу
  42. find(_, {}) ->
  43.     error;
  44. find(Key, {Key, V, _, _}) ->
  45.     {ok, V};
  46. find(Key, {Key1, _, S, B}) ->
  47.     if
  48.         Key < Key1 -> find(Key, S);
  49.         Key > Key1 -> find(Key, B)
  50.     end.
  51.  
  52. %Удаление узла
  53. erase(Key, {}) ->
  54.     new();
  55. erase(Key, {Key, _, {}, {}}) ->
  56.     new();
  57. erase(Key, {Key, _, S, {}}) ->
  58.     S;
  59. erase(Key, {Key, _, {}, Bigger}) ->
  60.     Bigger;
  61. erase(Key, {Key, _, S, B}) ->
  62.     {K2, V2, S2} = deletesp(S),
  63.     {K2, V2, S2, B};
  64. erase(Key, {K,V,S,B}) when Key < K ->
  65.     {K, V, assoc:erase(Key, S), B};
  66. erase(Key, {K,V,S,B}) when Key > K ->
  67.     {K, V, S, assoc:erase(Key, B)}.
  68.  
  69. %При удалении узла в середине дерева возникает брешь, которую надо прикрыть
  70. deletesp({K, V, {}, {}) ->
  71.     {K, V, {}};
  72. deletesp({K,V,S,{}}) ->
  73.     {K, V, S};
  74. deletesp({K, V, S, B}) ->
  75.     {K2, V2, B2} = deletesp(B),
  76.     {K2, V2, {K, V, S, B2}}.
  77.  
  78. %Размер ассоциативного массива
  79. size({}) ->
  80.     0;
  81. size({_, _, S, B}) ->
  82.     1 + assoc:size(B) + assoc:size(S).
  83.  
  84.  
  85. %Проверка наличия элемента с указанным ключем в массиве
  86. is_key(_, {}) ->
  87.     false;
  88. is_key(Key, {Key, _, _, _}) ->
  89.     true;
  90. is_key(Key, {K, _, S, _}) when Key < K ->
  91.     is_key(Key, S);
  92. is_key(Key, {_, _, _, B}) -> is_key(Key, B).
  93.  
  94. %Свертка
  95. fold(_Fun, Z, []) -> Z;
  96. fold(Fun, Z, {K, V, S, B}) ->
  97.     fold(Fun, Z, to_list({K, V, S, B}));
  98. fold(Fun, Z, [{S, B}|S])->
  99.     Fun(K, V, fold(Fun, Z, S)).
  100.  
  101. %Функтор
  102. map(_Fun, {}) -> {};
  103. map(Fun, {K, V, S, B}) ->
  104.     {K, Fun(K, V), map(Fun, S), map(Fun, B)}.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement