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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::{collections::HashMap, fmt, hash::Hash};

use bit_vec::BitVec;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("No such key in encoding dictionary: {0}")]
    NoSuchKey(String),
    #[error("Invalid weight nodes: {0}")]
    InvalidWeights(String),
}

type Result<T> = std::result::Result<T, Error>;

struct Node<T> {
    freq: u32,
    value: Option<T>,
    left: Option<Box<Node<T>>>,
    right: Option<Box<Node<T>>>,
}

impl<T> Node<T> {
    fn new(freq: u32, value: Option<T>) -> Self {
        Self {
            freq,
            value,
            left: None,
            right: None,
        }
    }
}

pub struct Encoder<T> {
    encoding: HashMap<T, BitVec>,
}

impl<T> Encoder<T>
where
    T: Eq + Hash + Clone + fmt::Debug,
{
    fn new(root: &Node<T>) -> Self {
        fn assign<T>(node: &Node<T>, encoding: &mut HashMap<T, BitVec>, mut current_bits: BitVec)
        where
            T: Eq + Hash + Clone,
        {
            if let Some(ch) = node.value.as_ref() {
                encoding.insert(ch.clone(), current_bits);
            } else {
                if let Some(ref l) = node.left {
                    let mut bits = current_bits.clone();
                    bits.push(false);
                    assign(l, encoding, bits);
                }
                if let Some(ref r) = node.right {
                    current_bits.push(true);
                    assign(r, encoding, current_bits);
                }
            }
        }
        let mut encoding = HashMap::new();
        let bits = BitVec::new();
        assign(&root, &mut encoding, bits);
        Self { encoding }
    }

    pub fn encode(&self, data: &[T]) -> Result<BitVec> {
        let mut vec = BitVec::new();
        for item in data {
            let mut encoding = self
                .encoding
                .get(item)
                .ok_or_else(|| Error::NoSuchKey(format!("{:?}", item)))?
                .clone();
            vec.append(&mut encoding);
        }
        Ok(vec)
    }
}

pub struct Decoder<T> {
    root: Node<T>,
}

impl<T> Decoder<T> {
    fn new(root: Node<T>) -> Self {
        Self { root }
    }

    pub fn decode_iter<'a>(&'a self, encoded: &'a BitVec) -> impl Iterator<Item = &'a T> {
        DecoderIter {
            input: encoded.iter(),
            root: &self.root,
            current_node: &self.root,
        }
    }

    pub fn decode<'a>(&'a self, encoded: &'a BitVec) -> Vec<&'a T> {
        self.decode_iter(encoded).collect()
    }

    pub fn decode_owned(&self, encoded: &BitVec) -> Vec<T>
    where
        T: Clone,
    {
        self.decode_iter(encoded).cloned().collect()
    }
}

struct DecoderIter<'a, T> {
    root: &'a Node<T>,
    input: bit_vec::Iter<'a>,
    current_node: &'a Node<T>,
}

impl<'a, T> Iterator for DecoderIter<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        let bit = self.input.next()?;
        if bit {
            if let Some(ref right) = self.current_node.right {
                self.current_node = right;
            }
        } else if let Some(ref left) = self.current_node.left {
            self.current_node = left;
        }
        if let Some(value) = self.current_node.value.as_ref() {
            self.current_node = &self.root;
            Some(value)
        } else {
            self.next()
        }
    }
}

pub struct Huffman<T> {
    encoder: Encoder<T>,
    decoder: Decoder<T>,
}

impl<T> Huffman<T>
where
    T: Eq + Hash + Clone + fmt::Debug,
{
    pub fn new(weights: impl IntoIterator<Item = (T, u32)>) -> Result<Self> {
        let mut nodes = weights
            .into_iter()
            .map(|(value, frequency)| Box::new(Node::new(frequency, Some(value))))
            .collect::<Vec<_>>();

        while nodes.len() > 1 {
            nodes.sort_by(|a, b| (&(b.freq)).cmp(&(a.freq)));
            let a = nodes
                .pop()
                .ok_or_else(|| Error::InvalidWeights("Expected at least 1 node".to_string()))?;
            let b = nodes
                .pop()
                .ok_or_else(|| Error::InvalidWeights("Expected at least 1 node".to_string()))?;
            let mut c = Node::new(a.freq + b.freq, None);
            c.left = Some(a);
            c.right = Some(b);
            nodes.push(Box::new(c));
        }

        let root = *nodes
            .pop()
            .ok_or_else(|| Error::InvalidWeights("Expected root node".to_string()))?;
        let encoder = Encoder::new(&root);
        let decoder = Decoder::new(root);
        Ok(Self { encoder, decoder })
    }

    /// Encodes data into a BitVec. Fails if any of the data is not present in the dictionary.
    pub fn encode(&self, data: &[T]) -> Result<BitVec> {
        self.encoder.encode(data)
    }

    pub fn decode<'a>(&'a self, encoded: &'a BitVec) -> Vec<&'a T> {
        self.decoder.decode(encoded)
    }

    pub fn decode_iter<'a>(&'a self, encoded: &'a BitVec) -> impl Iterator<Item = &'a T> {
        self.decoder.decode_iter(encoded)
    }

    pub fn decode_owned(&self, encoded: &BitVec) -> Vec<T> {
        self.decoder.decode_owned(encoded)
    }

    /// Split into a Encoder and Decoder.
    pub fn split(self) -> (Encoder<T>, Decoder<T>) {
        (self.encoder, self.decoder)
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_encode_decode_i32() {
        let weights = vec![(0, 10), (1, 1), (2, 5)];
        let huffman = Huffman::new(weights).unwrap();
        let data = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let encoded = huffman.encode(&data).unwrap();
        let decoded = huffman.decode_owned(&encoded);
        assert_eq!(data, decoded);
    }

    #[test]
    fn test_split() {
        let weights = vec![(0, 10), (1, 1), (2, 5)];
        let (encoder, decoder) = Huffman::new(weights).unwrap().split();
        let data = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let encoded = encoder.encode(&data).unwrap();
        let decoded = decoder.decode_owned(&encoded);
        assert_eq!(data, decoded);
    }

    #[test]
    fn test_encode_decode_string() {
        let weights = vec![
            ("hello".to_string(), 2),
            ("hey".to_string(), 3),
            ("howdy".to_string(), 1),
        ];
        let huffman = Huffman::new(weights).unwrap();
        let data = vec!["howdy".into(), "howdy".into(), "hey".into(), "hello".into()];
        let encoded = huffman.encode(&data).unwrap();
        let decoded = huffman.decode_owned(&encoded);
        assert_eq!(data, decoded);
    }
}