Change packedbits variables names so code is actually readable
class PackedBitsWriter:
def __init__(self):
- self.r = 8
- self.current_byte = 0;
+ self.bits_left_in_byte = 8
+ self.current_byte = 0
self.bytes = []
- # Writes a value `v` that is `l` bits long
- def write(self, v, l):
- while l:
- left = self.r if l >= self.r else l
- m = (1 << left) - 1
- self.current_byte |= (v & m) << (8 - self.r)
- self.r -= left
- l -= left
- v >>= left
- if self.r:
+ def write(self, value, length):
+ while length:
+ if length >= self.bits_left_in_byte:
+ bits_to_read = self.bits_left_in_byte
+ else:
+ bits_to_read = length
+
+ mask = (1 << bits_to_read) - 1
+ self.current_byte |= (value & mask) << (8 - self.bits_left_in_byte)
+
+ self.bits_left_in_byte -= bits_to_read
+ length -= bits_to_read
+ value >>= bits_to_read
+
+ if self.bits_left_in_byte:
continue
+
self.flush()
def flush(self):
self.bytes.append(self.current_byte)
self.current_byte = 0
- self.r = 8
+ self.bits_left_in_byte = 8
class PackedBitsReader:
- def __init__(self, buf):
- self.bytes = buf
- self.current_byte = 0;
- self.idx = 0
-
- # Reads `s` bits
- def read(self, s):
- l = 0;
- v = 0;
- left = s
- while l != s:
- cons = 8 if left > 8 else left
- if (cons + self.idx > 8):
- cons = 8 - self.idx;
- m = ((1 << cons) - 1) << self.idx
- v = ((self.bytes[self.current_byte] & m) >> self.idx) << l | v
- self.idx += cons
- self.current_byte += self.idx >> 3
- self.idx %= 8
- left -= cons
- l += cons
- return v
+ def __init__(self, bytes):
+ self.bytes = bytes
+ self.current_byte_index = 0
+ self.current_bit_index = 0
+
+ def read(self, length):
+ bits_read = 0
+ value = 0
+ bits_left_to_read = length
+
+ while bits_read != length:
+ if bits_left_to_read > 8:
+ bits_to_read = 8
+ else:
+ bits_to_read = bits_left_to_read
+ if (bits_to_read + self.current_bit_index > 8):
+ bits_to_read = 8 - self.current_bit_index
+
+ mask = ((1 << bits_to_read) - 1) << self.current_bit_index
+ current_byte = self.bytes[self.current_byte_index]
+ value = ((current_byte & mask) >> self.current_bit_index) << bits_read | value
+
+ self.current_bit_index += bits_to_read
+ self.current_byte_index += self.current_bit_index >> 3
+ self.current_bit_index %= 8
+ bits_left_to_read -= bits_to_read
+ bits_read += bits_to_read
+
+ return value
