2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,45 +1,36 @@
|
|||
defmodule LZW do
|
||||
def encode(str), do: encode(to_char_list(str), init, 256, [])
|
||||
@encode_map Enum.into(0..255, Map.new, &{[&1],&1})
|
||||
@decode_map Enum.into(0..255, Map.new, &{&1,[&1]})
|
||||
|
||||
defp encode([h], d, _, out), do: Enum.reverse([Dict.get(d, [h]) | out])
|
||||
def encode(str), do: encode(to_char_list(str), @encode_map, 256, [])
|
||||
|
||||
defp encode([h], d, _, out), do: Enum.reverse([d[[h]] | out])
|
||||
defp encode([h|t], d, free, out) do
|
||||
val = Dict.get(d, [h])
|
||||
val = d[[h]]
|
||||
find_match(t, [h], val, d, free, out)
|
||||
end
|
||||
|
||||
defp find_match([h|t], l, lastval, d, free, out) do
|
||||
case Dict.fetch(d, [h|l]) do
|
||||
case Map.fetch(d, [h|l]) do
|
||||
{:ok, val} -> find_match(t, [h|l], val, d, free, out)
|
||||
:error ->
|
||||
d1 = Dict.put(d, [h|l], free)
|
||||
encode([h|t], d1, free+1, [lastval | out])
|
||||
:error -> d1 = Map.put(d, [h|l], free)
|
||||
encode([h|t], d1, free+1, [lastval | out])
|
||||
end
|
||||
end
|
||||
defp find_match([], _, lastval, _, _, out), do: Enum.reverse([lastval | out])
|
||||
|
||||
defp init, do: init(255, Map.new)
|
||||
|
||||
defp init(0, d), do: d
|
||||
defp init(n, d), do: init(n-1, Dict.put(d,[n],n))
|
||||
|
||||
def decode([h|t]) do
|
||||
d = init1(Map.new)
|
||||
val = Dict.get(d, h)
|
||||
decode(t, val, 256, d, val)
|
||||
val = @decode_map[h]
|
||||
decode(t, val, 256, @decode_map, val)
|
||||
end
|
||||
|
||||
defp decode([], _, _, _, l), do: Enum.reverse(l) |> to_string
|
||||
defp decode([h|t], old, free, d, l) do
|
||||
val = Dict.get(d, h)
|
||||
val = d[h]
|
||||
add = [List.last(val) | old]
|
||||
d1 = Dict.put(d, free, add)
|
||||
d1 = Map.put(d, free, add)
|
||||
decode(t, val, free+1, d1, val++l)
|
||||
end
|
||||
|
||||
defp init1(d), do: init1(255, d)
|
||||
|
||||
defp init1(0, d), do: d
|
||||
defp init1(n, d), do: init1(n-1, Dict.put(d, n, [n]))
|
||||
end
|
||||
|
||||
str = "TOBEORNOTTOBEORTOBEORNOT"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue