from encoder.subsets_first_match import SubsetsFirstMatch
from encoder.subsets_greedy import SubsetsGreedy
from WriterVarint import WriterVarint
from ByteString import ByteString
from sampledata import sample

def verify(encoder, values):
	bytes = encoder.encode(values)
	buffer = ByteString(bytes)

	decoded_values = encoder.decode(buffer)

	assert set(values) == set(decoded_values)


if __name__ == '__main__':
	values = sample()

	encoders = [
		SubsetsFirstMatch('first match', 6, WriterVarint()),
		SubsetsGreedy('greedy', 0, WriterVarint())
	]

	for encoder in encoders:
		print "testing", encoder.name
		verify(encoder, values)
		print "... OK"
def demo(values):
	enc = Encoder(values)
	sequence, pairs = enc.encode()

	orig_size   = sum(encoded_size(v) for v in values)

	seq_size    = sum(encoded_size(v) for v in sequence)

	pairs_size  = sum(encoded_size(v) for v in pairs)
	pairs_size += sum(encoded_size(pair[0]) + encoded_size(pair[1]) for pair in pairs.itervalues())

	print 'original size: ', orig_size
	print
	print 'sequence size: ', seq_size
	print '   pairs size: ', pairs_size
	print '   total size: ', seq_size + pairs_size


if __name__ == '__main__':

	def get_diff(values):
		return [values[0]] + [b - a for a, b in zip(values, values[1:])]

	for index, count, name, values in get_all():
		print "verifying %d/%d..." % (index + 1, count)
		verify(get_diff(values))
		print "ok"

	print "encoding example"
	demo(get_diff(sample()))