Advertisement
frankiek3

reorder.py

Sep 18th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import re, glob
  3. # Globals
  4. filename = ""
  5. extralines = []
  6. linelist = []
  7. lenll = 0;
  8. best = []
  9. regex1 = re.compile(r"\t*?(.+?) (.+)")
  10. regex2 = re.compile(r"[ ,;\\]")
  11. regex3 = re.compile(r"\$o")
  12. regex4 = re.compile(r"\$\-1")
  13. exitcount = 0
  14.  
  15.  
  16. #load file
  17. def init():
  18.     global filename
  19.     return [getregs(line) for line in open(filename) if getextralines(line)]
  20.  
  21.  
  22. def getextralines(line):
  23.     global extralines
  24.     line = line.rstrip("\n");
  25.     if len(extralines) < 2:
  26.         extralines.append(line)
  27.         return False
  28.     return True
  29.  
  30.  
  31. def getregs(line):
  32.     line = line.rstrip("\n");
  33.     ins = regex1.sub(r"\1", line)
  34.     line = regex1.sub(r"\2", line)
  35.     line = regex2.sub("", line)
  36.     line = line.lstrip("$")
  37.     return [ins, re.findall('\d+|o\d|\-1', line)]
  38.  
  39.  
  40. #check if line mapping seqcount is less(better) than the current best
  41. def checkbest(linemap):
  42.     global best, filename
  43.     seqcount = getseqcount(linemap)
  44.     #print "checkbest", seqcount, validate(linemap)
  45.     #save only the first linemap with the lowest seqcount
  46.     if seqcount < best[1] and validate(linemap):
  47.         best = [linemap[:], seqcount]
  48.         print "Current Best count: ", best[1]
  49.         #print best[0]
  50.         writesbox(filename)
  51.         if best[1] == 0:
  52.             return True
  53.     return False
  54.  
  55.  
  56. def remap(linemap, placement):
  57.     global linelist, exitcount, lenll, best
  58.     if exitcount == -1:
  59.         return False
  60.     lrange = [line[0] for line in linemap if line[1] == -1]
  61.     #random.shuffle(lrange)
  62.     placement += 1
  63.     for linenum in lrange:
  64.         #lines between abovecount and belowcount
  65.         if placement in xrange(linelist[linenum][2], lenll - linelist[linenum][3]):
  66.             valid = True
  67.             #loop through all linemap-lines above placement/position
  68.             for top in range(0, placement):
  69.                 #check if any linemap above placement has linenum
  70.                 if linenum in linelist[linemap[top][0]][0] and linemap[top][0] != linenum:
  71.                     valid = False
  72.                     break
  73.             #loop through all linemap-lines below placement/position
  74.             for bottom in range(placement, lenll):
  75.                 if linenum in linelist[linemap[bottom][0]][1] and linemap[bottom][0] != linenum:
  76.                     valid = False
  77.                     break
  78.             if valid:
  79.                 lmap = linemap[:]
  80.                 lmap.remove([linenum, -1])
  81.                 lmap.insert(placement, [linenum, placement])
  82.                 if checkbest(lmap):
  83.                     exitcount = -1
  84.                     return True
  85.                 if placement != lenll - 1 and aboveseqcount(lmap,placement) < best[1]:
  86.                     remap(lmap, placement)
  87.     placement -= 1
  88.     return True
  89.  
  90.  
  91. def aboveseqcount(linemap,line):
  92.     global linelist
  93.     count = 0
  94.     for i in range(line+1):
  95.         if linemap[i][0] in linelist[linemap[i + 1][0]][0]:
  96.             count += 1
  97.     return count
  98.  
  99.  
  100. def getseqcount(linemap):
  101.     global linelist
  102.     count = 0
  103.     for i in range(len(linemap) - 1):
  104.         if linemap[i][0] in linelist[linemap[i + 1][0]][0]:
  105.             count += 1
  106.     return count
  107.  
  108.  
  109. def countlines(l, ab):
  110.     global linelist
  111.     lines = set(linelist[l][ab])
  112.     for line in lines.copy():
  113.         lines.update(countlines(line, ab))
  114.     return lines
  115.  
  116.  
  117. #validate a line mapping
  118. def validate(mapping):
  119.     global linelist
  120.     #if lenll != len(mapping):
  121.     #   print "Invalid line arrangement"
  122.     #   return False
  123.     #loop through all new lines
  124.     for l in range(len(mapping)):
  125.         #loop through all new lines above new line
  126.         for top in range(0, l):
  127.             #check if any new lines above new line have oldline
  128.             if mapping[l][0] in linelist[mapping[top][0]][0]:
  129.                 #print "Invalid line arrangement"
  130.                 return False
  131.         #loop through all lines below oldline
  132.         for bottom in range(l + 1, lenll):
  133.             if mapping[l][0] in linelist[mapping[bottom][0]][1]:
  134.                 #print "Invalid line arrangement"
  135.                 return False
  136.     return True
  137.  
  138.  
  139. #save the current best seqcount line mapping
  140. def writesbox(filename):
  141.     with open(filename, "w") as sboxfile:
  142.         sboxfile.writelines(extraline + "\n" for extraline in extralines)
  143.         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])
  144.         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")
  145.  
  146.  
  147.  
  148.  
  149. #if __name__ == "__main__":
  150.  
  151. #check all files that match t*.h
  152. for filename in glob.glob('t*.h'):
  153.     extralines = []
  154.     linelist = []
  155.     best = []
  156.     exitcount = 0
  157.     sbox = init()
  158.     print filename
  159.     for i in range(len(sbox)):
  160.         reglist=[[], [], 0, 0]
  161.         if i != 0:
  162.             regs = sbox[i][1]#TODO: skip 0
  163.             for j in range(0, i):
  164.                 prevreg1 = sbox[j][1][0]
  165.                 if prevreg1 != "-1" and prevreg1 in regs:
  166.                     #add linenum to this line's aboves
  167.                     reglist[0].append(j)
  168.         if i != len(sbox) - 1:
  169.             reg1 = sbox[i][1][0]
  170.             if not reg1.startswith("o") and reg1 != "-1":
  171.                 for j in range(i + 1, len(sbox)):
  172.                     nextregs = sbox[j][1]#TODO: skip 0
  173.                     if reg1 in nextregs:
  174.                         #add linenum to this line's belows
  175.                         reglist[1].append(j)
  176.         linelist.append(reglist)
  177.  
  178.     lenll = len(linelist)
  179.  
  180.     for i in range(lenll):
  181.         linelist[i][2] = len(countlines(i, 0))
  182.         linelist[i][3] = len(countlines(i, 1))
  183.  
  184.     #linemap[linenum]=[oldline, position/placement]
  185.     best = [[[i, -1] for i in range(lenll)], 0]
  186.     best[1] = getseqcount(best[0])
  187.     print "Was", best[1]
  188.     if best[1] != 0:
  189.         validate(best[0])
  190.         if best[1] != 0:
  191.             #recursive sort
  192.             remap([[line[0], -1] for line in best[0]], -1)
  193.             validate(best[0])
  194.         print "Best count: ", best[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement