Skip to content

Commit

Permalink
💄
Browse files Browse the repository at this point in the history
  • Loading branch information
philipbrown committed Jun 19, 2023
1 parent 0663ddf commit 4f988a8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
43 changes: 20 additions & 23 deletions lib/nilsimsa.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Nilsimsa do

alias Nilsimsa.{Hamming, Transition}

@tran3 Transition.generate(53)
@tran Transition.generate(53)
@popc Hamming.generate()

@type t :: %__MODULE__{
Expand Down Expand Up @@ -59,9 +59,8 @@ defmodule Nilsimsa do
end)
end

def compare(a, b) do
compare(digest(a), digest(b))
end
def compare(a, b),
do: compare(digest(a), digest(b))

@doc """
Generate the digest of a hash
Expand Down Expand Up @@ -100,9 +99,8 @@ defmodule Nilsimsa do
"""
@spec process(String.t()) :: t
def process(binary) do
process(binary, %__MODULE__{})
end
def process(binary),
do: process(binary, %__MODULE__{})

@doc """
Process the given string as a Nilsimsa hash using the given accumulator struct
Expand All @@ -117,28 +115,28 @@ defmodule Nilsimsa do
def process(<<c::utf8, rest::binary>>, %{acc: acc, window: win} = nilsimsa) do
acc =
if at(win, 1) > -1 do
List.update_at(acc, tran3(c, at(win, 0), at(win, 1), 0), &(&1 + 1))
List.update_at(acc, tran(c, at(win, 0), at(win, 1), 0), &(&1 + 1))
else
acc
end

acc =
if at(win, 2) > -1 do
acc
|> List.update_at(tran3(c, at(win, 0), at(win, 2), 1), &(&1 + 1))
|> List.update_at(tran3(c, at(win, 1), at(win, 2), 2), &(&1 + 1))
|> List.update_at(tran(c, at(win, 0), at(win, 2), 1), &(&1 + 1))
|> List.update_at(tran(c, at(win, 1), at(win, 2), 2), &(&1 + 1))
else
acc
end

acc =
if at(win, 3) > -1 do
acc
|> List.update_at(tran3(c, at(win, 0), at(win, 3), 3), &(&1 + 1))
|> List.update_at(tran3(c, at(win, 1), at(win, 3), 4), &(&1 + 1))
|> List.update_at(tran3(c, at(win, 2), at(win, 3), 5), &(&1 + 1))
|> List.update_at(tran3(at(win, 3), at(win, 0), c, 6), &(&1 + 1))
|> List.update_at(tran3(at(win, 3), at(win, 2), c, 7), &(&1 + 1))
|> List.update_at(tran(c, at(win, 0), at(win, 3), 3), &(&1 + 1))
|> List.update_at(tran(c, at(win, 1), at(win, 3), 4), &(&1 + 1))
|> List.update_at(tran(c, at(win, 2), at(win, 3), 5), &(&1 + 1))
|> List.update_at(tran(at(win, 3), at(win, 0), c, 6), &(&1 + 1))
|> List.update_at(tran(at(win, 3), at(win, 2), c, 7), &(&1 + 1))
else
acc
end
Expand All @@ -152,20 +150,19 @@ defmodule Nilsimsa do
process(rest, nilsimsa)
end

def process(<<>>, nilsimsa) do
nilsimsa
end
def process(<<>>, nilsimsa),
do: nilsimsa

###########
# Private #
###########

defp tran3(a, b, c, n) do
@tran3
defp tran(a, b, c, n) do
@tran
|> at(a + n)
|> band(255)
|> bxor(at(@tran3, b) * (n + n + 1))
|> Kernel.+(at(@tran3, bxor(c, at(@tran3, n))))
|> bxor(at(@tran, b) * (n + n + 1))
|> Kernel.+(at(@tran, bxor(c, at(@tran, n))))
|> band(255)
end

Expand All @@ -177,7 +174,7 @@ end

defimpl String.Chars, for: Nilsimsa do
@moduledoc """
String.Chars protocol for Nilsimsa
String.Chars protocol for Nilsimsa.
"""

import Kernel, except: [to_string: 1]
Expand Down
2 changes: 2 additions & 0 deletions lib/nilsimsa/hamming.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ defmodule Nilsimsa.Hamming do
@doc """
Generate a hamming distance optimisation table.
## Examples
iex> table = Hamming.generate()
iex> Enum.at(table, bxor(9, 14))
3
Expand Down
2 changes: 2 additions & 0 deletions lib/nilsimsa/transition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule Nilsimsa.Transition do
@doc """
Generate a transition table.
## Examples
iex> Transition.generate(53) |> Enum.take(3)
[2, 214, 158]
Expand Down

0 comments on commit 4f988a8

Please sign in to comment.