You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.5 KiB
Python
74 lines
1.5 KiB
Python
def read_mapfile(fn):
|
|
comments = []
|
|
status = None
|
|
blockstrings = []
|
|
with open(fn, 'r') as f:
|
|
for line in f:
|
|
if line.startswith("#"):
|
|
comments.append(line)
|
|
elif status is None:
|
|
status = line
|
|
else:
|
|
blockstrings.append(line)
|
|
|
|
# Post-processing
|
|
status = status.split()
|
|
status[0] = int(status[0], base=0)
|
|
status[2] = int(status[2], base=0)
|
|
status = tuple(status)
|
|
|
|
blocks = []
|
|
for blockstring in blockstrings:
|
|
spl = blockstring.split()
|
|
start = int(spl[0], base=0)
|
|
length = int(spl[1], base=0)
|
|
char = spl[2]
|
|
blocks.append((start, length, char))
|
|
|
|
return (status, blocks, comments)
|
|
|
|
|
|
class MapFile:
|
|
def __init__(self, filename):
|
|
self.mapfilename = filename
|
|
|
|
def load(self):
|
|
self.status, self.blocks, self.comments = read_mapfile(self.mapfilename)
|
|
self.size = max(map(lambda blk: blk[0]+blk[1], self.blocks))
|
|
|
|
def get_state(self, pos):
|
|
# TODO: this is lame
|
|
for blk in self.blocks:
|
|
if pos >= blk[0] and pos < blk[0] + blk[1]:
|
|
return blk[2]
|
|
|
|
def get_hist(self, start, end):
|
|
relevant_blocks = []
|
|
for blk in self.blocks:
|
|
if start < blk[0] + blk[1] and end >= blk[0]:
|
|
relevant_blocks.append(blk)
|
|
|
|
# Trimming:
|
|
trmblks = []
|
|
for blk in relevant_blocks:
|
|
bg, lt, symb = blk
|
|
if bg < start:
|
|
lt -= start - bg
|
|
bg = start
|
|
if bg + lt > end:
|
|
lt -= bg+lt - end
|
|
trmblks.append((bg, lt, symb))
|
|
|
|
# Grouping by symbols:
|
|
grp = {}
|
|
for blk in trmblks:
|
|
if blk[2] not in grp:
|
|
grp[blk[2]] = 0
|
|
grp[blk[2]] += blk[1]
|
|
|
|
return grp
|
|
|
|
|
|
|
|
|