Advertisement
Guest User

Untitled

a guest
Jun 28th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.15 KB | None | 0 0
  1. -module(hashmap).
  2.  
  3. -export([add/3, delete/2, init/0, hashCode/1, map/2, filter/2, foldl/3, foldr/3, lengthMap/1, mapf/2, join/1, merge/2]).
  4.  
  5. -record(tuple, {key, val}).
  6.  
  7. hashCode(Key) when (Key rem 10) rem 10 == 0 -> 1;
  8.  
  9. hashCode(Key) -> (Key rem 10) rem 10.
  10.  
  11. init() -> [ #tuple{key=null, val=null} || _ <- lists:seq(1, 11)].
  12.  
  13. add(Map, Key, Value) ->
  14.     Hash = hashCode(Key),
  15.     Element =  lists:nth(Hash, Map),
  16.     case Element#tuple.key == Key of
  17.     true ->
  18.         lists:sublist(Map, Hash-1) ++ [#tuple{key=Key, val=Value}] ++ lists:nthtail(Hash,Map);
  19.     false when Element#tuple.key == null ->
  20.         lists:sublist(Map, Hash-1) ++ [#tuple{key=Key, val=Value}] ++ lists:nthtail(Hash,Map);
  21.     false when Element#tuple.key /= null ->
  22.         case Element#tuple.val == Value of
  23.             true -> Map;
  24.             false -> req(Map, Hash, Value, Key)
  25.         end
  26.     end.
  27.  
  28. delete(Map, Key) ->
  29.   Hash = hashCode(Key),
  30.   Element =  lists:nth(Hash, Map),
  31.   case Element#tuple.key == Key of
  32.   true ->
  33.    lists:sublist(Map, Hash-1) ++ [#tuple{key=null, val=null}] ++ lists:nthtail(Hash,Map);
  34.   false when Element#tuple.key == null  ->
  35.     Map;
  36.   false -> delReq(Map, Hash, Key)
  37.   end.
  38.  
  39. filter(Map, Fun) when length(Map) >= 1 ->
  40.     [Element | Tail] = Map,
  41.     case Fun(Element#tuple.key, Element#tuple.val) of
  42.         true -> [Element] ++ filter(Tail, Fun);
  43.         false -> filter(Tail, Fun)
  44.     end;
  45. filter(Map, Fun) when length(Map) < 1 ->
  46.     [].
  47.  
  48. foldl(Map, Fun, Acc) ->
  49.     case length(Map) > 0 of
  50.         true -> [ Element | Tail ] = Map,
  51.             case Element#tuple.key /= null of
  52.                 true -> foldl (Tail, Fun, Fun(Element#tuple.key, Element#tuple.val, Acc) );
  53.                 false -> foldl (Tail, Fun, Acc)
  54.             end;
  55.         false -> Acc
  56.     end.
  57.  
  58. foldr(Map, Fun, Acc) ->
  59.     ReverseMap = lists:reverse(Map),
  60.     foldl(ReverseMap, Fun, Acc).
  61.  
  62. map(Map, Fun) when length(Map) > 0 ->
  63.     [ Element | Tail ] = Map,
  64.         case Element#tuple.key /= null of
  65.             true -> [#tuple{key=Element#tuple.key, val=Fun(Element#tuple.key, Element#tuple.val)}] ++ map(Tail, Fun);
  66.             false -> [Element] ++ map(Tail, Fun)
  67.         end;
  68. map(Map, Fun) when length(Map) =< 0 ->
  69.     [].
  70.  
  71. mapf(Map, Fun) ->
  72.     foldl(Map, fun(K,V,Acc) -> add(Acc, K, Fun(K,V)) end, init()).
  73.  
  74. lengthMap(Map) -> foldl(Map, fun(_,_,Sum) -> Sum + 1 end, 0).
  75.  
  76. join(Map) ->
  77.     Fun = fun(_,V, Str) ->
  78.         String = Str ++ V ,
  79.         String ++ ","
  80.     end,
  81.  
  82.     foldl(Map, Fun, "").
  83.  
  84. merge (Map1, Map2) ->
  85.     F = fun(K,V,Acc) when K /= null ->  
  86.         add(Acc, K, V)
  87.         end,
  88.         fun(K,V,Acc) when K == null ->  
  89.         []
  90.         end,
  91.  
  92.     Acc = Map1 ++ init(),
  93.     foldl(Map2, F, Acc).
  94.  
  95.  
  96. req(Map, KeyNow, Value, Key) when KeyNow < 11 ->
  97.     Element = lists:nth(KeyNow, Map),
  98.     case Element#tuple.key == null of
  99.             true -> lists:sublist(Map, KeyNow-1) ++ [#tuple{key=Key, val=Value}] ++ lists:nthtail(KeyNow,Map);
  100.             false -> req(Map, KeyNow+1, Value, Key)
  101.     end;
  102.  
  103. req(Map, KeyNow, Value, Key) when KeyNow > 11 -> Map.
  104.  
  105.  
  106. delReq(Map, KeyNow, Key) when KeyNow < 11 ->
  107.     Element = lists:nth(KeyNow, Map),
  108.     case Element#tuple.key == Key of
  109.             true -> lists:sublist(Map, KeyNow-1) ++ [#tuple{key=null, val=null}] ++ lists:nthtail(KeyNow,Map);
  110.             false -> delReq(Map, KeyNow+1, Key)
  111.     end;
  112.  
  113. delReq(Map, KeyNow, Key) when KeyNow >= 11 -> Map.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement