1.1 --- a/imiptools/period.py Sun Oct 26 19:44:25 2014 +0100
1.2 +++ b/imiptools/period.py Sun Oct 26 22:21:56 2014 +0100
1.3 @@ -14,12 +14,13 @@
1.4
1.5 conflicts = []
1.6 for start, end in periods:
1.7 - period = period_overlaps(freebusy, (start, end))
1.8 - if period:
1.9 + overlapping = period_overlaps(freebusy, (start, end), get_conflicts)
1.10 + if overlapping:
1.11 if get_conflicts:
1.12 - conflicts.append(period)
1.13 + conflicts += overlapping
1.14 else:
1.15 return True
1.16 +
1.17 if get_conflicts:
1.18 return conflicts
1.19 else:
1.20 @@ -37,21 +38,44 @@
1.21 else:
1.22 i += 1
1.23
1.24 -def period_overlaps(freebusy, period):
1.25 +def period_overlaps(freebusy, period, get_periods=False):
1.26
1.27 """
1.28 - Return from 'freebusy' any period overlapping with the given 'period', or
1.29 - None if no overlap occurs.
1.30 + Return whether any period in 'freebusy' overlaps with the given 'period',
1.31 + returning a collection of overlapping periods if 'get_periods' is set to a
1.32 + true value.
1.33 """
1.34
1.35 dtstart, dtend = period[:2]
1.36 - i = bisect_left(freebusy, (dtstart, dtend, None))
1.37 - return (
1.38 - i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) and freebusy[i]
1.39 - or
1.40 - i > 0 and freebusy[i - 1][1] > dtstart and freebusy[i - 1]
1.41 - or
1.42 - None
1.43 - )
1.44 + found = bisect_left(freebusy, (dtstart, dtend, None))
1.45 +
1.46 + overlapping = []
1.47 +
1.48 + # Find earlier overlapping periods.
1.49 +
1.50 + i = found
1.51 +
1.52 + while i > 0 and freebusy[i - 1][1] > dtstart:
1.53 + if get_periods:
1.54 + overlapping.insert(0, freebusy[i - 1])
1.55 + else:
1.56 + return True
1.57 + i -= 1
1.58 +
1.59 + # Find later overlapping periods.
1.60 +
1.61 + i = found
1.62 +
1.63 + while i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend):
1.64 + if get_periods:
1.65 + overlapping.append(freebusy[i])
1.66 + else:
1.67 + return True
1.68 + i += 1
1.69 +
1.70 + if get_periods:
1.71 + return overlapping
1.72 + else:
1.73 + return False
1.74
1.75 # vim: tabstop=4 expandtab shiftwidth=4