Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import re, glob
- # Globals
- filename = ""
- extralines = []
- linelist = []
- lenll = 0;
- best = []
- regex1 = re.compile(r"\t*?(.+?) (.+)")
- regex2 = re.compile(r"[ ,;\\]")
- regex3 = re.compile(r"\$o")
- regex4 = re.compile(r"\$\-1")
- exitcount = 0
- #load file
- def init():
- global filename
- return [getregs(line) for line in open(filename) if getextralines(line)]
- def getextralines(line):
- global extralines
- line = line.rstrip("\n");
- if len(extralines) < 2:
- extralines.append(line)
- return False
- return True
- def getregs(line):
- line = line.rstrip("\n");
- ins = regex1.sub(r"\1", line)
- line = regex1.sub(r"\2", line)
- line = regex2.sub("", line)
- line = line.lstrip("$")
- return [ins, re.findall('\d+|o\d|\-1', line)]
- #check if line mapping seqcount is less(better) than the current best
- def checkbest(linemap):
- global best, filename
- seqcount = getseqcount(linemap)
- #print "checkbest", seqcount, validate(linemap)
- #save only the first linemap with the lowest seqcount
- if seqcount < best[1] and validate(linemap):
- best = [linemap[:], seqcount]
- print "Current Best count: ", best[1]
- #print best[0]
- writesbox(filename)
- if best[1] == 0:
- return True
- return False
- def remap(linemap, placement):
- global linelist, exitcount, lenll, best
- if exitcount == -1:
- return False
- lrange = [line[0] for line in linemap if line[1] == -1]
- #random.shuffle(lrange)
- placement += 1
- for linenum in lrange:
- #lines between abovecount and belowcount
- if placement in xrange(linelist[linenum][2], lenll - linelist[linenum][3]):
- valid = True
- #loop through all linemap-lines above placement/position
- for top in range(0, placement):
- #check if any linemap above placement has linenum
- if linenum in linelist[linemap[top][0]][0] and linemap[top][0] != linenum:
- valid = False
- break
- #loop through all linemap-lines below placement/position
- for bottom in range(placement, lenll):
- if linenum in linelist[linemap[bottom][0]][1] and linemap[bottom][0] != linenum:
- valid = False
- break
- if valid:
- lmap = linemap[:]
- lmap.remove([linenum, -1])
- lmap.insert(placement, [linenum, placement])
- if checkbest(lmap):
- exitcount = -1
- return True
- if placement != lenll - 1 and aboveseqcount(lmap,placement) < best[1]:
- remap(lmap, placement)
- placement -= 1
- return True
- def aboveseqcount(linemap,line):
- global linelist
- count = 0
- for i in range(line+1):
- if linemap[i][0] in linelist[linemap[i + 1][0]][0]:
- count += 1
- return count
- def getseqcount(linemap):
- global linelist
- count = 0
- for i in range(len(linemap) - 1):
- if linemap[i][0] in linelist[linemap[i + 1][0]][0]:
- count += 1
- return count
- def countlines(l, ab):
- global linelist
- lines = set(linelist[l][ab])
- for line in lines.copy():
- lines.update(countlines(line, ab))
- return lines
- #validate a line mapping
- def validate(mapping):
- global linelist
- #if lenll != len(mapping):
- # print "Invalid line arrangement"
- # return False
- #loop through all new lines
- for l in range(len(mapping)):
- #loop through all new lines above new line
- for top in range(0, l):
- #check if any new lines above new line have oldline
- if mapping[l][0] in linelist[mapping[top][0]][0]:
- #print "Invalid line arrangement"
- return False
- #loop through all lines below oldline
- for bottom in range(l + 1, lenll):
- if mapping[l][0] in linelist[mapping[bottom][0]][1]:
- #print "Invalid line arrangement"
- return False
- return True
- #save the current best seqcount line mapping
- def writesbox(filename):
- with open(filename, "w") as sboxfile:
- sboxfile.writelines(extraline + "\n" for extraline in extralines)
- sboxfile.writelines(sbox[bestlines[0]][0] + regex4.sub("-1", regex3.sub("o", " $" + ',$'.join(str(x) for x in sbox[bestlines[0]][1]))) + " ;\\\n" for bestlines in best[0] if bestlines != best[0][len(best[0]) - 1])
- sboxfile.writelines(sbox[best[0][len(best[0]) - 1][0]][0] + regex4.sub("-1", regex3.sub("o", " $" + ',$'.join(str(x) for x in sbox[best[0][len(best[0]) - 1][0]][1]))) + "\n")
- #if __name__ == "__main__":
- #check all files that match t*.h
- for filename in glob.glob('t*.h'):
- extralines = []
- linelist = []
- best = []
- exitcount = 0
- sbox = init()
- print filename
- for i in range(len(sbox)):
- reglist=[[], [], 0, 0]
- if i != 0:
- regs = sbox[i][1]#TODO: skip 0
- for j in range(0, i):
- prevreg1 = sbox[j][1][0]
- if prevreg1 != "-1" and prevreg1 in regs:
- #add linenum to this line's aboves
- reglist[0].append(j)
- if i != len(sbox) - 1:
- reg1 = sbox[i][1][0]
- if not reg1.startswith("o") and reg1 != "-1":
- for j in range(i + 1, len(sbox)):
- nextregs = sbox[j][1]#TODO: skip 0
- if reg1 in nextregs:
- #add linenum to this line's belows
- reglist[1].append(j)
- linelist.append(reglist)
- lenll = len(linelist)
- for i in range(lenll):
- linelist[i][2] = len(countlines(i, 0))
- linelist[i][3] = len(countlines(i, 1))
- #linemap[linenum]=[oldline, position/placement]
- best = [[[i, -1] for i in range(lenll)], 0]
- best[1] = getseqcount(best[0])
- print "Was", best[1]
- if best[1] != 0:
- validate(best[0])
- if best[1] != 0:
- #recursive sort
- remap([[line[0], -1] for line in best[0]], -1)
- validate(best[0])
- print "Best count: ", best[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement