def code(self, data, verbose=False): """ Return coded sequence """ index = 0 bw = BitStreamWriter() while index < len(data): for n,border in zip(xrange(len(self.border),0,-1), self.border): data1 = data[index:index + self.counts[n - 1] ] if verbose: print n, data1 if (len(data1) == self.counts[n - 1]) and all([ d <= border for d in data1 ]): if verbose: print ''.join([ str(bit) for bit in self.bits(n - 1)[-self.n_ctrl:] ]), ' ', for bit in self.bits(n - 1)[-self.n_ctrl:]: bw.add(bit) for number in data1: if verbose: print ''.join([ str(b) for b in self.bits(number)[-(self.n_data / self.counts[n - 1]):] ]), for bit in self.bits(number)[-(self.n_data / self.counts[n - 1]):]: bw.add(bit) if n == 7 or n == 3: if verbose: print '0' bw.add(0) elif n == 5: if verbose: print '000' for i in xrange(3): bw.add(0) elif verbose: print index += self.counts[n - 1] break else: raise ValueError return bw.getbytes()
def code(self, data, verbose=False): """ Найти max число Фибоначчи отнять, найти, отнять... получить последовательность 1 и нулей развернуть её, записать 1, записать в файл data - list """ all_bits = [] for number in data: if number <= 0: raise ValueError("Fibonacci archiver: can't " + \ "code non-positive integer " + str(number)) self.fibonacci_to_upper(number) bits = [1] if verbose: print "number=%d\n" % number index = 0 while number: n_index = bisect(self.fib_series, number) - 1 if verbose: print number, self.fib_series[n_index] number -= self.fib_series[n_index] if verbose: print ('index=%d fib=%d number=%d' % (n_index, self.fib_series[n_index], number)) for i in range(index - n_index - 1): bits.append(0) bits.append(1) index = n_index while index: bits.append(0) index -= 1 if verbose: print '\n', ''.join(str(b) for b in bits[::-1]) all_bits += bits[::-1] bw = BitStreamWriter() for b in all_bits: bw.add(b) decoded = bw.getbytes() bw.clean() return decoded
def code(self, data, verbose=False): """ Найти max число Фибоначчи отнять, найти, отнять... получить последовательность 1 и нулей развернуть её, записать 1, записать в файл data - list """ all_bits = [] for number in data: bits = [1] if verbose: print "number=%d\n" % number index = 0 while number: n_index = bisect(self.fib_series, number) - 1 if verbose: print number, self.fib_series[n_index] number -= self.fib_series[n_index] if verbose: print ('index=%d fib=%d number=%d' % (n_index, self.fib_series[n_index], number)) for i in range(index - n_index - 1): bits.append(0) bits.append(1) index = n_index while index: bits.append(0) index -= 1 if verbose: print '\n', ''.join(str(b) for b in bits[::-1]) all_bits += bits[::-1] bw = BitStreamWriter() for b in all_bits: bw.add(b) return bw.getbytes()
class Simple9Archiver(object): """ Simple9 - код, выравненный по 32 бита. 4 бита, 28 бит управление, данные 0 , 1 - 28-битное 268 435 455 1 , 2 - 14-битных 16 383 2 , 3 - 9-битных 511 --------- +1 3 , 4 - 7-битных 127 4 , 5 - 5-битных 31 --------- +3 5 , 7 - 4-битных 15 6 , 9 - 3-битных 7 --------- +1 7 , 14 - 2-битных 3|0|1|2 8 , 28 - 1-битных 1|0 > 28-бит ??? """ def __init__(self): """ """ self.n_ctrl = 4 self.n_data = 28 self.bw = BitStreamWriter() self.borders = ( (28,1), (14,3), (9,7), (7,15), \ (5,31), (4,127), (3,511), \ (2,16383), (1,268435455) ) self.counts = ( 1, 2, 3, 4, 5, 7, 9, 14, 28 ) self.border = [ 1, 3, 7, 15, 31, 127, 511, 16383, 268435455 ] @staticmethod def bits(n): """ Bits of 4byte """ return [n >> i & 1 for i in range(31,-1,-1)] def code(self, data, verbose=False): """ Return coded sequence """ index = 0 bw = BitStreamWriter() while index < len(data): for n,border in zip(xrange(len(self.border),0,-1), self.border): data1 = data[index:index + self.counts[n - 1] ] if verbose: print n, data1 if (len(data1) == self.counts[n - 1]) and all([ d <= border for d in data1 ]): if verbose: print ''.join([ str(bit) for bit in self.bits(n - 1)[-self.n_ctrl:] ]), ' ', for bit in self.bits(n - 1)[-self.n_ctrl:]: bw.add(bit) for number in data1: if verbose: print ''.join([ str(b) for b in self.bits(number)[-(self.n_data / self.counts[n - 1]):] ]), for bit in self.bits(number)[-(self.n_data / self.counts[n - 1]):]: bw.add(bit) if n == 7 or n == 3: if verbose: print '0' bw.add(0) elif n == 5: if verbose: print '000' for i in xrange(3): bw.add(0) elif verbose: print index += self.counts[n - 1] break else: raise ValueError return bw.getbytes() def code1(self, data, verbose=False): """ Return coded sequence """ index = 0 while index < len(data): n = (len(self.borders) - 1) for count, border in self.borders: batch = data[index:index + count] if verbose: print n, batch if all([ 0 < d <= border for d in batch ]): if verbose: print ''.join([ str(bit) for bit in self.bits(n)[-self.n_ctrl:] ]), ' ', for bit in self.bits(n)[-self.n_ctrl:]: self.bw.add(bit) for number in batch: if verbose: print ''.join([ str(b) for b in self.bits(number)[-(self.n_data / count):] ]), for bit in self.bits(number)[-(self.n_data / count):]: self.bw.add(bit) if n == 6 or n == 2: if verbose: print '0' self.bw.add(0) elif n == 4: if verbose: print '000' for i in xrange(3): self.bw.add(0) elif verbose: print index += count break n -= 1 else: raise ValueError("Simple9 archiver: number is too big") coded = self.bw.getbytes() self.bw.clean() return coded def decode1(self, data, verbose=False): """ Return decoded sequence """ decoded = [] if verbose: print coded, j, k, control = [0] * self.n_data, 0, 0, 0 br = BitStreamReader(data) while not br.finished(): bit = br.get() if j < self.n_ctrl: control |= (bit << ((self.n_ctrl - 1) - j)) j += 1 else: coded[k] = bit k += 1 if k >= self.n_data: n_bits = (self.borders[control][0] - 1) if verbose: print ''.join([ str(b) for b in self.bits(control)[-self.n_ctrl:]]), ' ', print ''.join([ str(c) for c in coded[:k]]) count = self.counts[control] n_packed = (self.n_data / count) for n in xrange(count): number = 0 for m in xrange(n_packed): index = n * n_packed + m number |= ((1 if coded[index] else 0) << (n_bits - m)) decoded.append(number) if verbose: print decoded[-count:] j, k, control = 0, 0, 0 return decoded def decode(self, data, verbose=False): """ Return decoded sequence """ decoded = [] if verbose: print coded, j, k, control = [0] * self.n_data, 0, 0, 0 br = BitStreamReader(data) while not br.finished(): bit = br.get() if j < self.n_ctrl: control |= (bit << ((self.n_ctrl - 1) - j)) j += 1 else: coded[k] = bit k += 1 if k >= self.n_data: n_bits = (self.counts[::-1][control] - 1) if verbose: print ''.join([ str(b) for b in self.bits(control)[-self.n_ctrl:]]), ' ', print ''.join([ str(c) for c in coded[:k]]) n_packed = (self.n_data / (self.counts[control])) for n in xrange(self.counts[control]): number = 0 for m in xrange(n_packed): index = n * n_packed + m number |= ((1 if coded[index] else 0) << (n_bits - m)) decoded.append(number) if verbose: print decoded[-self.counts[control]:] j, k, control = 0, 0, 0 return decoded