From: Philippe Pepos Petitclerc Date: Fri, 13 May 2016 19:04:07 +0000 (-0400) Subject: lib/crypto: Introduce hamming distance on Bytes X-Git-Url: http://nitlanguage.org lib/crypto: Introduce hamming distance on Bytes New method to calculate the bitwise hamming distance on Bytes. Signed-off-by: Philippe Pepos Petitclerc --- diff --git a/lib/crypto.nit b/lib/crypto.nit index 2abe5d7..0b52c99 100644 --- a/lib/crypto.nit +++ b/lib/crypto.nit @@ -172,6 +172,24 @@ redef class Bytes return xored end + + # Computes the edit/hamming distance of two sequences of bytes. + # + # assert "this is a test".to_bytes.hamming_distance("wokka wokka!!!".bytes) == 37 + # assert "this is a test".to_bytes.hamming_distance("this is a test".bytes) == 0 + # + fun hamming_distance(other: SequenceRead[Byte]): Int do + var diff = 0 + for idx in self.length.times do + var res_byte = self[idx] ^ other[idx] + for bit in [0..8[ do + if res_byte & 1u8 == 1u8 then diff += 1 + res_byte = res_byte >> 1 + end + end + return diff + end + end redef class Int