Skip to content

Implement IntervalUnion.plus#42

Merged
Whathecode merged 3 commits intodevelopfrom
intervalunion-plus-minus
Aug 3, 2024
Merged

Implement IntervalUnion.plus#42
Whathecode merged 3 commits intodevelopfrom
intervalunion-plus-minus

Conversation

@Whathecode
Copy link
Owner

@Whathecode Whathecode commented Jul 24, 2024

The previous MutableIntervalUnion data structure only allowed adding disjoint, non-adjacent intervals which followed the last addition.

This PR changes the internal data structure to be a tree of IntervalUnionPair nodes.

Interval.plus has been promoted to the IntervalUnion interface and is now fully supported.

@Whathecode Whathecode force-pushed the intervalunion-plus-minus branch 15 times, most recently from 178271c to 1fe06db Compare July 31, 2024 16:57
The concrete `MutableIntervalUnion` implementation is replaced with a `EmptyIntervalUnion` (closes #28) and `IntervalUnionPair`, the latter of which always holds exactly two other non-empty intervals. Combined with `Interval` (which is also a union), these can serve as nodes in an interval tree.

As part of this, `getBounds()` is redefined to always return intervals in canonical form. Not only is this a more well-defined contract, it also prevents having to canonicalize or call `lowerBound/upperBound` within `IntervalUnion`; `start` and `end` can safely be used.
@Whathecode Whathecode force-pushed the intervalunion-plus-minus branch 4 times, most recently from 093a11f to ae52908 Compare August 1, 2024 08:42
There was an incorrect assumption that intervals using real numbers weren't adjacent if they had the same endpoints but those bounds had differing inclusion.

`Interval.plus` an interval which preceded it also failed since `IntervalUnionPair` expected intervals to be passed in order.
@Whathecode Whathecode force-pushed the intervalunion-plus-minus branch 2 times, most recently from 83d8225 to ceb6b49 Compare August 2, 2024 06:49
@Whathecode
Copy link
Owner Author

Whathecode commented Aug 2, 2024

Currently, recursion is used to implement plus on IntervalUnion.

    // When `toAdd` only impacts lower or upper, recursively add the new interval to the impacted union.
   // If neither are impacted, the new interval must lie in between lower and upper.

To determine whether the interval toAdd only impacts the lower or upper interval, IntervalUnionComparison.of() is run for them. This same call will happen at the start of .plus one level down. Would it be worth optimizing this redudant calculation by having an internal overload method for .plus which takes the existing IntervalUnionComparison?

@Whathecode Whathecode force-pushed the intervalunion-plus-minus branch 3 times, most recently from 9ca4990 to 31f71f8 Compare August 2, 2024 22:51
@Whathecode Whathecode changed the title Implement plus and minus on IntervalUnion Implement IntervalUnion.plus Aug 3, 2024
@Whathecode Whathecode marked this pull request as ready for review August 3, 2024 06:06
@Whathecode Whathecode merged commit 6111156 into develop Aug 3, 2024
@Whathecode Whathecode deleted the intervalunion-plus-minus branch August 7, 2024 06:56
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