#!/usr/bin/env python
# -*- coding: utf-8 -*-

import scipy as sp
import sys
import time
from optparse import OptionParser
import myutil
import re

CONCURRENT_MAX = 5

def main(argv=None):
	if argv is None:
		argv = sys.argv[1:]
		
	parser = OptionParser(usage = "usage: %prog [options] FILE1 ...", version = "%prog 0.1")
	parser.add_option('-l', '--linenumber-size', dest='line_size', choices = range(1,8), default = 6, help = "number of characters that the line number takes")
	parser.add_option('-f', '--output', dest='output_file', metavar="FILE", help = "text output file name")
	(options, datafiles) = parser.parse_args(args = argv)
	
	if len(datafiles) != 2:
		parser.error("incorrect number of arguments")

	time_start = time.time()
	
	setCover = []

	bitmapFilePath = datafiles[1]	
	bitmapFile = open(bitmapFilePath, 'r')
	signatureLine = bitmapFile.readline()
	matches = re.match("^([\d ]{6})([01]*)$", signatureLine)
	if matches != None:
		numberOfLines = int(matches.group(1))
		vectorLength = len(matches.group(2))	
		
	print "loading ... "
	inputData = sp.genfromtxt(bitmapFilePath, delimiter = [options.line_size] + [1] * vectorLength, dtype = int, skip_header = 1)
	inputData = sp.array(inputData)
#	print inputData
	print "processing ..."	
	
	remainingColumnsSet = set(range(0, vectorLength))	
	remainingColumnsList = list(remainingColumnsSet)	
	nonZeroIndiceSet = set([0])
	sums = sp.sum(inputData[:,1:][:, remainingColumnsList], axis = 1)
		
# 		print "max table line number", maxTableLineNumber
	stageCounter = 0 
	while sp.count_nonzero(sums) > 0:
		currentMax = []
			
		for i in range(CONCURRENT_MAX):
			argmax = sp.argmax(sums)
#			print argmax, sp.count_nonzero(sums)
			if sums[argmax] <= 0: break		
			currentMax.append(argmax)			
			sums[currentMax[i]] = 0			
		
		nonZeroIndiceSet = set()
		for i in currentMax:			
			setCover.append(inputData[i, 0])
			nonZeroIndices = sp.argwhere(inputData[:,1:][i,:])					
			nonZeroIndices = nonZeroIndices.reshape(len(nonZeroIndices))
			nonZeroIndiceSet = nonZeroIndiceSet | set(nonZeroIndices)
#			print "nonzeroindicesset", nonZeroIndiceSet
		remainingColumnsSet = remainingColumnsSet - nonZeroIndiceSet
#		print "remainintcolumnssetlen", len(remainingColumnsSet)
		remainingColumnsList = list(remainingColumnsSet)
#		print '--------'

		sums = sp.sum(inputData[:,1:][:, remainingColumnsList], axis = 1)
		

		sys.stdout.write("|")
		sys.stdout.flush()
		stageCounter += 1	 
		
	sys.stdout.write("\n")
	print "len, checksum", len(setCover), sum(setCover)
	setCover.sort()
	print setCover		
			
	textFilePath = datafiles[0]
	f = open(textFilePath, 'r')
	out = open(options.output_file, 'w')
				
	coverLinesIndex = 0
	lineCounter = 0
	coverLinesLength = len(setCover)
	textLine = f.readline()
	while coverLinesIndex < coverLinesLength:
		while lineCounter < setCover[coverLinesIndex]:
			textLine = f.readline()
			lineCounter += 1
		out.write("%s" % textLine)
		coverLinesIndex += 1	
	f.close()	
	out.close()			
		
	time_end = time.time()
	print "Took time %s" % (myutil.hrtime(time_end - time_start))	
	

if __name__ == "__main__":
	sys.exit(main())
