* Fix decode_int32

This commit is contained in:
Matthew Honnibal 2015-07-21 20:09:59 +00:00
parent 304d0e2633
commit 424854028f

View File

@ -105,6 +105,7 @@ cdef class HuffmanCodec:
@cython.boundscheck(False) @cython.boundscheck(False)
cpdef int decode_int32(self, BitArray bits, int32_t[:] msg) except -1: cpdef int decode_int32(self, BitArray bits, int32_t[:] msg) except -1:
assert bits.i % 8 == 0
cdef Node node = self.root cdef Node node = self.root
cdef int branch cdef int branch
@ -112,26 +113,23 @@ cdef class HuffmanCodec:
cdef bytes bytes_ = bits.as_bytes() cdef bytes bytes_ = bits.as_bytes()
cdef unsigned char byte cdef unsigned char byte
cdef int i_msg = 0 cdef int i_msg = 0
cdef int i_byte = 0 cdef int i_byte = bits.i // 8
cdef int i_bit = 0 cdef unsigned char i_bit = 0
cdef unsigned char bit cdef unsigned char one = 1
cdef int32_t one = 1
while i_msg < n_msg: while i_msg < n_msg:
byte = bytes_[i_byte] byte = ord(bytes_[i_byte])
i_byte += 1
for i_bit in range(8): for i_bit in range(8):
bit = byte & (one << i_bit) branch = node.right if (byte & (one << i_bit)) else node.left
branch = node.right if bit else node.left bits.i += 1
if branch >= 0: if branch >= 0:
node = self.nodes.at(branch) node = self.nodes.at(branch)
else: else:
msg[i_msg] = self.leaves[-(branch + 1)] msg[i_msg] = self.leaves[-(branch + 1)]
node = self.nodes.back()
i_msg += 1 i_msg += 1
if i_msg == n_msg: if i_msg == n_msg:
break break
i_byte += 1 node = self.root
# as_bytes doesn't seek forward, so consume the number of bits we used
bits.seek(bits.i + (i_byte * 8) + i_bit)
property strings: property strings:
@cython.boundscheck(False) @cython.boundscheck(False)