You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I finally found out what the real problem is with this library. CompareTo does not report INTERSECT_OR_SUPERSET when s is a subset of other, even though this implies that they also intersect. This means the algorithm for inserting the intervals in the tree can not be implemented correctly:
// CompareTo compares two Segments and returns: DISJOINT, SUBSET or INTERSECT_OR_SUPERSETfunc (s*Segment) CompareTo(other*Segment) int {
ifother.From>s.To||other.To<s.From {
returnDISJOINT
}
ifother.From<=s.From&&other.To>=s.To {
returnSUBSET// this means there is also an intersection
}
returnINTERSECT_OR_SUPERSET
}
// Inserts interval into given tree structurefuncinsertInterval(node*node, intrvl*Interval) {
switchnode.segment.CompareTo(&intrvl.Segment) {
caseSUBSET:
// interval of node is a subset of the specified interval or equalifnode.overlap==nil {
node.overlap=make([]*Interval, 0, 10)
}
node.overlap=append(node.overlap, intrvl)
caseINTERSECT_OR_SUPERSET:
// In the original algorithm we should directly check if `node.left` intersects with `intrvl`.// In this algorithm we check this after calling `insertInterval`. However, we never land// here when `s.left` is a subset of `intrvl`.ifnode.left!=nil {
insertInterval(node.left, intrvl)
}
// In the original algorithm we should directly check if `node.right` intersects with `intrvl`.// In this algorithm we check this after calling `insertInterval`. However, we never land// here when `s.right` is a subset of `intrvl`.ifnode.right!=nil {
insertInterval(node.right, intrvl)
}
caseDISJOINT:
// nothing to do
}
}
My suggestion is to use separate function to check if an interval is a subset of another interval, and a function to check if intervals intersect. This makes it possible to implement the correct algorithm.
I already tested this in my code, and it should be fairly easy to fix this in your library. Shall I create a pull request for this? If yes, could you first please merge #2?
The text was updated successfully, but these errors were encountered:
@cenkalti I’m not sure why I didn’t create a PR for my latest changes, but it might be I never properly finished it. I think I gave up writing unit tests at some point.
Also, note that I made a seperate library based on this one that has a more concurrency friendly interface...
@toberndo feel free to merge in any fixes from fork.
Hi
I finally found out what the real problem is with this library.
CompareTo
does not report INTERSECT_OR_SUPERSET whens
is a subset ofother
, even though this implies that they also intersect. This means the algorithm for inserting the intervals in the tree can not be implemented correctly:My suggestion is to use separate function to check if an interval is a subset of another interval, and a function to check if intervals intersect. This makes it possible to implement the correct algorithm.
On page 43 of http://www.cs.uu.nl/geobook/pseudo.pdf there is some pretty good pseudo code. This is the pseudo code used in Computational Geometry: Algorithms and Applications, that is referenced (and pretty much copy-pasted) a lot in the segment tree wiki page.
I already tested this in my code, and it should be fairly easy to fix this in your library. Shall I create a pull request for this? If yes, could you first please merge #2?
The text was updated successfully, but these errors were encountered: