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

More efficient interval algorithms #1191

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Lucretiel
Copy link
Contributor

@Lucretiel Lucretiel commented May 7, 2024

This PR implements linear time and constant space algorithms for a few of the interval operations:

  • canonicalize: track the current "merge target", and merge each interval into the target if they overlap. The merge target is always shifted left as appropriate, so that we can simply truncate the list afterwards.
  • negate: replace each interval with the negated interval to its left, then finally add the trailing interval on the right. Take care to avoid the leftmost and rightmost intervals if they're empty.
  • union: two possible algorithms:
    • If the current vector has enough extra space: merge the two lists, in-place, into the vec, then perform a canonicalization.
    • If the current vector needs to be reallocated: merge the two lists of ranges with an iterator that outputs them all in sorted order. Merge them as they come, if they're adjacent, and construct a new vec out of them.

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.

None yet

1 participant