1.1 --- a/imiptools/period.py Wed Feb 11 17:06:21 2015 +0100
1.2 +++ b/imiptools/period.py Wed Feb 11 17:07:32 2015 +0100
1.3 @@ -73,6 +73,32 @@
1.4 else:
1.5 i += 1
1.6
1.7 +def get_overlapping(freebusy, period):
1.8 +
1.9 + """
1.10 + Return the indexes in 'freebusy' providing periods overlapping with
1.11 + 'period'.
1.12 + """
1.13 +
1.14 + dtstart, dtend = period[:2]
1.15 + found = bisect_left(freebusy, (dtstart, dtend))
1.16 +
1.17 + # Find earlier overlapping periods.
1.18 +
1.19 + start = found
1.20 +
1.21 + while start > 0 and freebusy[start - 1][1] > dtstart:
1.22 + start -= 1
1.23 +
1.24 + # Find later overlapping periods.
1.25 +
1.26 + end = found
1.27 +
1.28 + while end < len(freebusy) and (dtend is None or freebusy[end][0] < dtend):
1.29 + end += 1
1.30 +
1.31 + return start, end
1.32 +
1.33 def period_overlaps(freebusy, period, get_periods=False):
1.34
1.35 """
1.36 @@ -81,37 +107,32 @@
1.37 true value.
1.38 """
1.39
1.40 - dtstart, dtend = period[:2]
1.41 - found = bisect_left(freebusy, (dtstart, dtend, None, None))
1.42 -
1.43 - overlapping = []
1.44 -
1.45 - # Find earlier overlapping periods.
1.46 -
1.47 - i = found
1.48 -
1.49 - while i > 0 and freebusy[i - 1][1] > dtstart:
1.50 - if get_periods:
1.51 - overlapping.insert(0, freebusy[i - 1])
1.52 - else:
1.53 - return True
1.54 - i -= 1
1.55 -
1.56 - # Find later overlapping periods.
1.57 -
1.58 - i = found
1.59 -
1.60 - while i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend):
1.61 - if get_periods:
1.62 - overlapping.append(freebusy[i])
1.63 - else:
1.64 - return True
1.65 - i += 1
1.66 + start, end = get_overlapping(freebusy, period)
1.67
1.68 if get_periods:
1.69 - return overlapping
1.70 + return freebusy[start:end]
1.71 else:
1.72 - return False
1.73 + return start != end
1.74 +
1.75 +def remove_overlapping(freebusy, period):
1.76 +
1.77 + "Remove from 'freebusy' all periods overlapping with 'period'."
1.78 +
1.79 + start, end = get_overlapping(freebusy, period)
1.80 +
1.81 + if start != end:
1.82 + del freebusy[start:end]
1.83 +
1.84 +def replace_overlapping(freebusy, period, replacements):
1.85 +
1.86 + """
1.87 + Replace existing periods in 'freebusy' within the given 'period', using the
1.88 + given 'replacements'.
1.89 + """
1.90 +
1.91 + remove_overlapping(freebusy, period)
1.92 + for replacement in replacements:
1.93 + insert_period(freebusy, replacement)
1.94
1.95 # Period layout mostly with datetime objects.
1.96