Skip to content

adt: split interval tree by right endpoint on matched left endpoints #19768

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions pkg/adt/interval_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,16 @@ func (ivt *intervalTree) Insert(ivl Interval, val any) {
x := ivt.root
for x != ivt.sentinel {
y = x
if z.iv.Ivl.Begin.Compare(x.iv.Ivl.Begin) < 0 {
// Split on left endpoint. If left endpoints match, instead split on right endpoint.
beginCompare := z.iv.Ivl.Begin.Compare(x.iv.Ivl.Begin)
if beginCompare < 0 {
x = x.left
} else if beginCompare == 0 {
if z.iv.Ivl.End.Compare(x.iv.Ivl.End) < 0 {
x = x.left
} else {
x = x.right
}
} else {
x = x.right
}
Expand All @@ -483,8 +491,15 @@ func (ivt *intervalTree) Insert(ivl Interval, val any) {
if y == ivt.sentinel {
ivt.root = z
} else {
if z.iv.Ivl.Begin.Compare(y.iv.Ivl.Begin) < 0 {
beginCompare := z.iv.Ivl.Begin.Compare(y.iv.Ivl.Begin)
if beginCompare < 0 {
y.left = z
} else if beginCompare == 0 {
if z.iv.Ivl.End.Compare(y.iv.Ivl.End) < 0 {
y.left = z
} else {
y.right = z
}
} else {
y.right = z
}
Expand Down Expand Up @@ -701,16 +716,29 @@ func (ivt *intervalTree) Visit(ivl Interval, ivv IntervalVisitor) {

// find the exact node for a given interval
func (ivt *intervalTree) find(ivl Interval) *intervalNode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to find a proper tests for the Find method. Would you mind preparing some tests before we change the logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,I'll add tests. I was thinking of just adding assertions on the Find operation to the existing TestIntervalTreeRandom here as say

	for ab := range ivs {
		for xy := range ivs {
			v := xy.x + int64(rand.Intn(int(xy.y-xy.x)))
			require.NotEmptyf(t, ivt.Stab(NewInt64Point(v)), "expected %v stab non-zero for [%+v)", v, xy)
			require.Truef(t, ivt.Intersects(NewInt64Point(v)), "did not get %d as expected for [%+v)", v, xy)
		}
		iv := ivt.Find(NewInt64Interval(ab.x, ab.y))
		assert.NotNil(t, iv)
		assert.Equal(t, NewInt64Interval(ab.x, ab.y), iv.Ivl)
		assert.Truef(t, ivt.Delete(NewInt64Interval(ab.x, ab.y)), "did not delete %v as expected", ab)
		delete(ivs, ab)
	}

In addition I can add a separate dedicated unit test for the Find op to test edge cases if that's warranted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, please send a separate PR. It should be much easier to review and merge a new tests that don't change the logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serathius opened up a separate MR with IntervalTree.Find() tests: #19801

ret := ivt.sentinel
f := func(n *intervalNode) bool {
if n.iv.Ivl != ivl {
return true
x := ivt.root
// Search until hit sentinel or exact match.
for x != ivt.sentinel {
beginCompare := ivl.Begin.Compare(x.iv.Ivl.Begin)
endCompare := ivl.End.Compare(x.iv.Ivl.End)
if beginCompare == 0 && endCompare == 0 {
return x
}
// Split on left endpoint. If left endpoints match,
// instead split on right endpoints.
if beginCompare < 0 {
x = x.left
} else if beginCompare == 0 {
if endCompare < 0 {
x = x.left
} else {
x = x.right
}
} else {
x = x.right
}
ret = n
return false
}
ivt.root.visit(&ivl, ivt.sentinel, f)
return ret
return x
}

// Find gets the IntervalValue for the node matching the given interval
Expand Down