1.1 --- a/imiptools/period.py Tue Nov 25 23:14:03 2014 +0100
1.2 +++ b/imiptools/period.py Thu Nov 27 16:29:21 2014 +0100
1.3 @@ -78,4 +78,70 @@
1.4 else:
1.5 return False
1.6
1.7 +# Period layout.
1.8 +
1.9 +def get_scale(l):
1.10 +
1.11 + """
1.12 + Return an ordered time scale from the given list 'l' of tuples, with the
1.13 + first two elements of each tuple being start and end times.
1.14 + """
1.15 +
1.16 + scale = {}
1.17 +
1.18 + for t in l:
1.19 + start, end = t[:2]
1.20 +
1.21 + # Add a point and this event to the starting list.
1.22 +
1.23 + if not scale.has_key(start):
1.24 + scale[start] = [], []
1.25 + scale[start][0].append(t)
1.26 +
1.27 + # Add a point and this event to the ending list.
1.28 +
1.29 + if not scale.has_key(end):
1.30 + scale[end] = [], []
1.31 + scale[end][1].append(t)
1.32 +
1.33 + scale = scale.items()
1.34 + scale.sort()
1.35 + return scale
1.36 +
1.37 +def get_slots(l):
1.38 +
1.39 + """
1.40 + Return an ordered list of time slots from the given list 'l' of tuples, with
1.41 + the first two elements of each tuple being start and end times.
1.42 +
1.43 + Each slot is a tuple containing a point in time for the start of the slot,
1.44 + together with a list of parallel event tuples, each tuple containing the
1.45 + original details of an event.
1.46 + """
1.47 +
1.48 + slots = []
1.49 + active = []
1.50 +
1.51 + for point, (starting, ending) in get_scale(l):
1.52 +
1.53 + # Discard all active events ending at or before this start time.
1.54 +
1.55 + for t in ending:
1.56 + i = active.index(t)
1.57 + active[i] = None
1.58 +
1.59 + for t in starting:
1.60 + try:
1.61 + i = active.index(None)
1.62 + active[i] = t
1.63 + except ValueError:
1.64 + active.append(t)
1.65 +
1.66 + while active and active[-1] is None:
1.67 + active.pop()
1.68 +
1.69 + slots.append((point, active[:]))
1.70 +
1.71 + return slots
1.72 +
1.73 # vim: tabstop=4 expandtab shiftwidth=4