-
Notifications
You must be signed in to change notification settings - Fork 190
CellUnionFromDifference: Optimize #252
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
benguild
wants to merge
1
commit into
golang:master
Choose a base branch
from
benguild:cellUnionFromDifference
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,15 +130,48 @@ func CellUnionFromIntersectionWithCellID(x CellUnion, id CellID) CellUnion { | |
| return cu | ||
| } | ||
|
|
||
| // cellUnionDifferenceInternal adds (xid - y) to the CellUnion. It subdivides | ||
| // xid when there is partial overlap, narrowing y before recursing. | ||
| func (cu *CellUnion) cellUnionDifferenceInternal(xid CellID, y CellUnion) { | ||
| var lo, hi int | ||
| if len(y) > 0 { | ||
| idMin := xid.RangeMin() | ||
|
|
||
| // Find first cell that could overlap. | ||
| lo = sort.Search(len(y), func(i int) bool { | ||
| return y[i].RangeMax() >= idMin | ||
| }) | ||
|
|
||
| idMax := xid.RangeMax() | ||
|
|
||
| // Find first cell past our range. | ||
| hi = lo + sort.Search(len(y)-lo, func(i int) bool { | ||
| return y[(lo+i)].RangeMin() > idMax | ||
| }) | ||
| } | ||
|
|
||
| if lo >= hi { | ||
| *cu = append(*cu, xid) | ||
| return | ||
| } | ||
|
|
||
| y = y[lo:hi] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By reducing the size of "y" we're narrowing the search in the call to "ContainsCellID" and in recursive calls. |
||
|
|
||
| if y.ContainsCellID(xid) { | ||
| return | ||
| } | ||
|
|
||
| for _, child := range xid.Children() { | ||
| cu.cellUnionDifferenceInternal(child, y) | ||
| } | ||
| } | ||
|
|
||
| // CellUnionFromDifference creates a CellUnion from the difference (x - y) | ||
| // of the given CellUnions. | ||
| func CellUnionFromDifference(x, y CellUnion) CellUnion { | ||
| // TODO(roberts): This is approximately O(N*log(N)), but could probably | ||
| // use similar techniques as CellUnionFromIntersectionWithCellID to be more efficient. | ||
|
|
||
| var cu CellUnion | ||
| for _, xid := range x { | ||
| cu.cellUnionDifferenceInternal(xid, &y) | ||
| cu.cellUnionDifferenceInternal(xid, y) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seemed OK to pass by value |
||
| } | ||
|
|
||
| // The output is generated in sorted order, and there should not be any | ||
|
|
@@ -418,22 +451,6 @@ func (cu *CellUnion) lowerBound(begin, end int, id CellID) int { | |
| return end | ||
| } | ||
|
|
||
| // cellUnionDifferenceInternal adds the difference between the CellID and the union to | ||
| // the result CellUnion. If they intersect but the difference is non-empty, it divides | ||
| // and conquers. | ||
| func (cu *CellUnion) cellUnionDifferenceInternal(id CellID, other *CellUnion) { | ||
| if !other.IntersectsCellID(id) { | ||
| (*cu) = append((*cu), id) | ||
| return | ||
| } | ||
|
|
||
| if !other.ContainsCellID(id) { | ||
| for _, child := range id.Children() { | ||
| cu.cellUnionDifferenceInternal(child, other) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ExpandAtLevel expands this CellUnion by adding a rim of cells at expandLevel | ||
| // around the unions boundary. | ||
| // | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 moved this prior to the exported method to match C++ and have better proximity.