Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uniqueIntervals Method for Non-Overlapping Interval Calculation #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yehia-khalil
Copy link

@yehia-khalil yehia-khalil commented Nov 5, 2024

Problem:

When working with multiple overlapping periods, I faced a challenge in breaking down these intervals into unique, non-overlapping segments. Existing methods in the library were not sufficient to address this scenario, especially when multiple periods partially overlap or fully contain each other.

For example, given the following intervals:

•	Period A: August 1, 2024 - August 5, 2024
•	Period B: August 3, 2024 - August 7, 2024
•	Period C: August 6, 2024 - August 10, 2024

The desired result would be a collection of distinct, non-overlapping intervals:

•	August 1, 2024 - August 2, 2024
•	August 3, 2024 - August 5, 2024
•	August 6, 2024 - August 7, 2024
•	August 8, 2024 - August 10, 2024

In this case, the overlapping segments needed to be separated to provide a clear view of all unique time intervals.

Solution:

This pull request introduces a new uniqueIntervals method to the PeriodCollection class, which generates a collection of non-overlapping intervals from any set of overlapping or intersecting periods. The method recursively breaks down intervals until all overlaps are resolved, resulting in distinct intervals that fully represent the original periods without overlaps.

The solution includes:

uniqueIntervals() Method: A public method that initiates the process of breaking down overlapping intervals into unique segments.
Recursive Processing: An internal processIntervalsRecursively() function that iterates over each period, identifies overlaps, and recursively splits intervals as necessary. This ensures that all possible overlaps are handled and only unique intervals remain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant