@@ -15,15 +15,13 @@ def merge_overlapping_time_span_elements(time_span_elements: list[TimeSpanElemen
1515
1616 merged_time_span_elements : list [TimeSpanElement ] = []
1717 for current_time_span in time_span_elements :
18- # If the `merged_time_span_elements` list is empty, simply append the current time span
18+ # If the selected_list is empty, simply append the current time span
1919 if not merged_time_span_elements :
2020 merged_time_span_elements .append (current_time_span )
2121 continue
2222
23- # If the `merged_time_span_elements` contains a time span that overlaps with the current time span, combine them
23+ # If the selected_list contains a time span that overlaps with the current time span, combine them.
2424 # The only time span that can overlap with the current time span is the last time span in the list.
25- last_time_span = merged_time_span_elements [- 1 ]
26-
2725 # ┌────────────────────────┐
2826 # │ LEGEND │
2927 # │ █ = Reservation │
@@ -32,7 +30,7 @@ def merge_overlapping_time_span_elements(time_span_elements: list[TimeSpanElemen
3230 # ├────────────────────────┼──────────────────────────────────────────┐
3331 # │ ████ -> ███████ │ Overlapping │
3432 # │ █████ -> │ The last time span is extended │
35- # │ │ │
33+ # ├────────────────────────┼──────────────────────────────────────────┤
3634 # │ ████ -> ███████ │ Current time span ends at the same time │
3735 # │ ███ -> │ as the last last time span ends. │
3836 # │ │ The last time span is extended │
@@ -46,25 +44,35 @@ def merge_overlapping_time_span_elements(time_span_elements: list[TimeSpanElemen
4644 # │ ▄▄▄██▄▄ -> │ │
4745 # ├────────────────────────┼──────────────────────────────────────────┤
4846 # │ ███████ -> ███████ │ Time span is fully inside the last one │
49- # │ ███ -> │ Later can be skipped │
50- # │ │ │
51- # │ █████ -> ▄█████▄ │ Buffers extend outside earlier time span │
52- # │ ▄▄███▄▄ -> │ The buffered start/end times are kept │
47+ # │ ███ -> │ │
48+ # │ │ The buffered start/end times are kept │
49+ # │ █████ -> ▄█████▄ │ │
50+ # │ ▄▄███▄▄ -> │ │
5351 # └────────────────────────┴──────────────────────────────────────────┘
54- # Last time span overlaps with the current time span, so they can be merged.
52+ last_time_span = merged_time_span_elements [- 1 ]
53+
54+ # Last time span overlaps with the current time span, combine them.
5555 if last_time_span .end_datetime >= current_time_span .start_datetime :
56- # Adjust the before buffer of the last time span
57- if current_time_span .buffer_time_before :
58- min_buffered_dt = min (last_time_span .buffered_start_datetime , current_time_span .buffered_start_datetime )
59- last_time_span .buffer_time_before = last_time_span .start_datetime - min_buffered_dt
56+ # This time span is fully inside the last one.
57+ if last_time_span .end_datetime > current_time_span .end_datetime :
58+ # Even with after buffers, this time span is fully inside the last one, so it can be skipped.
59+ if last_time_span .buffered_end_datetime > current_time_span .buffered_end_datetime :
60+ continue
61+
62+ # Current time span's buffer ends after the last's buffer ends, so we can extend the last's buffer.
63+ buffer_delta = last_time_span .buffered_end_datetime - current_time_span .buffered_end_datetime
64+ last_time_span .buffer_time_after += buffer_delta
65+ continue
6066
61- # Adjust the after buffer of the last time span
62- # If the current time span ends after the last time span ends, extend the last time span's end datetime.
67+ # Extend the last time span's before buffer.
68+ min_buffered_dt = min (last_time_span .buffered_start_datetime , current_time_span .buffered_start_datetime )
69+ last_time_span .buffer_time_before = last_time_span .start_datetime - min_buffered_dt
70+
71+ # Extend the last time span and it's after buffer.
72+ # The buffered end datetime should stay the same.
6373 max_buffered_dt = max (last_time_span .buffered_end_datetime , current_time_span .buffered_end_datetime )
64- last_time_span .end_datetime = max ( last_time_span . end_datetime , current_time_span .end_datetime )
74+ last_time_span .end_datetime = current_time_span .end_datetime
6575 last_time_span .buffer_time_after = max_buffered_dt - last_time_span .end_datetime
66-
67- # The current time span is discarded after the above adjustments.
6876 continue
6977
7078 # No overlapping time spans, check for overlapping buffers and add the current time span to the merged list.
0 commit comments