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
use std::io;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

#[cfg(test)]
mod test;

pub trait BitsWriter {
    fn put_bits(&mut self, x: u32, bits: u32) -> io::Result<()>;
    fn flush_write_word(&mut self) -> io::Result<()>;
}

pub trait BitsReader {
    fn get_bits(&mut self, bits: u32) -> io::Result<u32>;
    fn get_zeros(&mut self, max_zeros: u32) -> io::Result<u32>;
    fn flush_read_word(&mut self);
}

pub struct BitsIOWriter<'a, W: WriteBytesExt> {
    write_stream: &'a mut W,
    write_cache: u32,
    index_bits: u32,
}

impl<'a, W: WriteBytesExt> BitsIOWriter<'a, W> {
    pub fn new(stream: &'a mut W) -> Self {
        BitsIOWriter {
            write_stream: stream,
            write_cache: 0,
            index_bits: 0,
        }
    }
}

impl<W: WriteBytesExt> BitsWriter for BitsIOWriter<'_, W> {
    fn put_bits(&mut self, x: u32, bits: u32) -> io::Result<()> {
        let mut new_bits = self.index_bits + bits;

        if new_bits < 32 {
            self.write_cache = (self.write_cache << bits) | x;
        } else {
            new_bits -= 32;

            self.write_stream.write_u32::<LittleEndian>(
                (self.write_cache << (bits - new_bits)) | (x >> new_bits),
            )?;

            self.write_cache = x;
        }

        self.index_bits = new_bits;

        Ok(())
    }

    fn flush_write_word(&mut self) -> io::Result<()> {
        let index_bits = self.index_bits;
        self.put_bits(0, (32 - index_bits) % 32)?;

        Ok(())
    }
}

#[derive(Debug)]
pub struct BitsIOReader<'a, R: ReadBytesExt> {
    read_stream: &'a mut R,
    read_cache: u32,
    index_bits: u32,
    cache_filled: bool,
}

impl<'a, R: ReadBytesExt> BitsIOReader<'a, R> {
    pub fn new(stream: &'a mut R) -> Self {
        BitsIOReader {
            read_cache: 0xff_ff_ff_ff,
            read_stream: stream,
            index_bits: 0,
            cache_filled: false,
        }
    }

    fn next_u32(&mut self) -> io::Result<u32> {
        match self.read_stream.read_u32::<LittleEndian>() {
            Ok(n) => {
                self.cache_filled = true;
                Ok(n)
            }
            Err(e) => {
                self.cache_filled = false;
                Err(e)
            }
        }
    }
}

impl<'a, R: ReadBytesExt> BitsReader for BitsIOReader<'a, R> {
    fn get_bits(&mut self, bits: u32) -> io::Result<u32> {
        let mut new_bits = self.index_bits + bits;
        if !self.cache_filled {
            self.read_cache = self.next_u32()?;
        }

        let mut x = self.read_cache << self.index_bits;

        if new_bits >= 32 {
            if new_bits != 32 {
                let prev_index_bits = self.index_bits;
                self.read_cache = self.next_u32()?;
                x |= self.read_cache >> (32 - prev_index_bits);
            } else {
                // read whole word
                self.cache_filled = false;
            }
            new_bits -= 32;
        }
        self.index_bits = new_bits;

        Ok(x >> (32 - bits))
    }

    fn get_zeros(&mut self, max_zeros: u32) -> io::Result<u32> {
        let mut new_bits = self.index_bits;
        if !self.cache_filled {
            self.read_cache = self.next_u32()?;
        }

        let mut b = self.read_cache;
        let mut x = 0;
        loop {
            if new_bits == 31 {
                let lsb_set = (b & 1u32) != 0;
                if !lsb_set {
                    x += 1;
                }
                if lsb_set || x == max_zeros {
                    self.index_bits = 0;
                    self.cache_filled = false;
                    return Ok(x);
                }

                self.read_cache = self.next_u32()?;
                b = self.read_cache;
                new_bits = 0;
                continue;
            }

            let msb_set = ((b << new_bits) & (1u32 << 31)) != 0;
            if !msb_set {
                x += 1;
            }
            if msb_set || x == max_zeros {
                self.index_bits = new_bits + 1;
                return Ok(x);
            }

            new_bits += 1;
        }
    }

    fn flush_read_word(&mut self) {
        self.cache_filled = false;
        self.index_bits = 0;
    }
}