-
Notifications
You must be signed in to change notification settings - Fork 6
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
compact: Introduce Range intersection and merge methods #13
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main concerns are around the side effects of Merge and Intersect, your thoughts on why those are necessary would be much appreciated!
// nodes, and reports them through the visitor function (if non-nil). The other | ||
// range must begin between the current range's begin and end. | ||
// | ||
// Warning: This method modifies both this and the other Range. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: there is no reason for "range" to be capitalized here and throughout.
// nodes, and reports them through the visitor function (if non-nil). The other | ||
// range must begin between the current range's begin and end. | ||
// | ||
// Warning: This method modifies both this and the other Range. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems unnecessary to modify the other range, no? Or at least this feels like it could easily have some awkward and unintended side effects. Why not implement it in a more "pure" way?
begin, end := other.begin, r.end | ||
if begin > end { // The other range is disjoint. | ||
return nil, fmt.Errorf("ranges are disjoint: other.begin=%d, want <= %d", begin, end) | ||
} else if begin == end { // The ranges touch ends. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the distinction here, since the intersection of two disjoint ranges is the empty set regardless of whether or not they "almost" intersect.
// | ||
// Warning: This method modifies both this and the other Range. | ||
// Warning: This method is experimental. | ||
func (r *Range) Intersect(other *Range) (*Range, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The modification of both ranges feels especially confusing here. Really after you call this function you have three compact ranges:
r
is nowr
without the intersecting parts.- the output is the intersection.
other
is nowother
without the intersecting parts.
This seems like it could lead to even more confusion than the behavior ofMerge
above. Surely what you'd want is just the intersection, like the function name suggests, without all of the side effects?
e88a44e
to
b368c6b
Compare
The unit test is an exhaustive test that intersects and merges all compact range
pairs within the tree of size 20.