compact: Introduce Range intersection and merge methods#13
compact: Introduce Range intersection and merge methods#13
Conversation
smeiklej
left a comment
There was a problem hiding this comment.
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.
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.
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.
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.
The modification of both ranges feels especially confusing here. Really after you call this function you have three compact ranges:
ris nowrwithout the intersecting parts.- the output is the intersection.
otheris nowotherwithout the intersecting parts.
This seems like it could lead to even more confusion than the behavior ofMergeabove. 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.