1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Hamming distance calculation

/// A trait for calculating hamming distance
pub trait HammingDistancable<RHS = Self> {
    /// The output type of the hamming distance
    type Output;
    fn hamming_distance(self, other: RHS) -> Self::Output;
}

impl<'a, 'b, T> HammingDistancable<&'b Vec<T>> for &'a Vec<T>
    where T : Eq {
    type Output = Result<u32, &'static str>;
    /// Calculate the hamming distance between vectors
    fn hamming_distance(self, other: &'b Vec<T>) -> Result<u32, &'static str> {
        if self.len() != other.len() {
            return Err("Vectors do not have equal length");
        }

        let mut distance = 0;
        for (a, b) in self.iter().zip(other.iter()) {
            if a != b {
                distance += 1;
            }
        }

        return Ok(distance);
    }
}

impl<'a, 'b, T> HammingDistancable<&'b [T]> for &'a [T]
    where T : Eq {
    type Output = Result<u32, &'static str>;
    /// Calculate the hamming distance between slices
    fn hamming_distance(self, other: &'b [T]) -> Result<u32, &'static str> {
        if self.len() != other.len() {
            return Err("Slices do not have equal length");
        }

        let mut distance = 0;
        for (a, b) in self.iter().zip(other.iter()) {
            if a != b {
                distance += 1;
            }
        }

        return Ok(distance);
    }
}

impl <'a, 'b> HammingDistancable<&'b String> for &'a String {
    type Output = Result<u32, &'static str>;
    /// Calculate the hamming distance between strings
     fn hamming_distance(self, other: &'b String) -> Result<u32, &'static str> {
        if self.len() != other.len() {
            return Err("Strings do not have equal length");
        }

        let mut distance = 0;
        for (a, b) in self.chars().zip(other.chars()) {
            if a != b {
                distance += 1;
            }
        }

        return Ok(distance);
    }
}

impl <'a, 'b> HammingDistancable<&'b str> for &'a str {
    type Output = Result<u32, &'static str>;
    /// Calculate the hamming distance between borrowed strings
     fn hamming_distance(self, other: &'b str) -> Result<u32, &'static str> {
        if self.len() != other.len() {
            return Err("Strings do not have equal length");
        }

        let mut distance = 0;
        for (a, b) in self.chars().zip(other.chars()) {
            if a != b {
                distance += 1;
            }
        }

        return Ok(distance);
    }
}

#[cfg(test)]
mod tests {
    use hamming_distance::HammingDistancable;
    
    #[test]
    fn vec_hamming_distance() {
        let mut vec1 : Vec<char> = Vec::new();
        let mut vec2 : Vec<char> = Vec::new();

        vec1.push('a');
        vec1.push('b');

        vec2.push('c');
        vec2.push('d');

        assert!(vec1.hamming_distance(&vec2).unwrap() == 2);
    }

    #[test]
    fn vec_hamming_distance_error() {
        let mut vec1 : Vec<char> = Vec::new();
        let mut vec2 : Vec<char> = Vec::new();

        vec1.push('a');
        vec1.push('b');

        vec2.push('c');

        assert!(vec1.hamming_distance(&vec2).unwrap_err() == 
            "Vectors do not have equal length");
    }

    #[test]
    fn slice_hamming_distance() {
        let byte_slice1 : &[u8] = &[0x01, 0x01];
        let byte_slice2 : &[u8] = &[0x01, 0xFF];

        assert!(byte_slice1.hamming_distance(byte_slice2).unwrap() == 1);
    }

    #[test]
    fn slice_hamming_distance_error() {
        let byte_slice1 : &[u8] = &[0x01];
        let byte_slice2 : &[u8] = &[0x01, 0xFF];

        assert!(byte_slice1.hamming_distance(byte_slice2).unwrap_err() == 
            "Slices do not have equal length");
    }

    #[test]
    fn string_hamming_distance() {
        let string1 : String = "Cat".to_owned();
        let string2 : String = "Hat".to_owned();

        assert!(string1.hamming_distance(&string2).unwrap() == 1);
    }

    #[test]
    fn string_hamming_distance_error() {
        let string1 : String = "Cats".to_owned();
        let string2 : String = "Hat".to_owned();

        assert!(string1.hamming_distance(&string2).unwrap_err() == 
            "Strings do not have equal length");
    }

    #[test]
    fn borrowed_string_hamming_distance() {
        let string1 : &str = "Cat";
        let string2 : &str = "Hat";

        assert!(string1.hamming_distance(string2).unwrap() == 1);
    }

    #[test]
    fn borrowed_string_hamming_distance_error() {
        let string1 : &str = "Cats";
        let string2 : &str = "Hat";

        assert!(string1.hamming_distance(string2).unwrap_err() == 
            "Strings do not have equal length");
    }
}