def get(self, f, pos, i): f.seek(pos) base = 0 value = unpack_uint_le(f.read(4)) key = value >> self._bitsize num = self._num[key] while i > base + num: base += num value = unpack_uint_le(f.read(4)) key = value >> self._bitsize num = self._num[key] offset = i - base if offset: value = value >> sum(self._bits[key][:offset]) return value & (2**self._bits[key][offset] - 1)
def get(self, f, pos, i): f.seek(pos) base = 0 value = unpack_uint_le(f.read(4)) key = value >> self._bitsize num = self._num[key] while i > base + num: base += num value = unpack_uint_le(f.read(4)) key = value >> self._bitsize num = self._num[key] offset = i - base if offset: value = value >> sum(self._bits[key][:offset]) return value & (2 ** self._bits[key][offset] - 1)
def read_nums(self, f, n): _decompress = self._decompress i = 0 while i < n: value = unpack_uint_le(f.read(4))[0] for v in _decompress(value, n - i): yield v i += 1
def read_gints(dbfile, n): """Read N integers from the bytes stream dbfile. Expects that the file starts at a key byte. """ count = 0 read = dbfile.read for _ in xrange(n): if count == 0: key = ord(dbfile.read(1)) code = key >> (count * 2) & 3 if code == 0: yield ord(read(1)) elif code == 1: yield unpack_ushort_le(read(2))[0] elif code == 2: yield unpack_uint_le(read(3) + "\x00")[0] else: yield unpack_uint_le(read(4))[0] count = (count + 1) % 4
def read_nums(self, f, n): """Read N integers from the bytes stream dbfile. Expects that the file is positioned at a key byte. """ count = 0 key = None for _ in xrange(n): if count == 0: key = f.read_byte() code = key >> (count * 2) & 3 if code == 0: yield f.read_byte() elif code == 1: yield f.read_ushort_le() elif code == 2: yield unpack_uint_le(f.read(3) + "\x00")[0] else: yield f.read_uint_le() count = (count + 1) % 4
def read_uint_le(self): return unpack_uint_le(self.read(_INT_SIZE))[0]