-
Notifications
You must be signed in to change notification settings - Fork 355
pybind: Add S2CellId iteration (children, cells, ranges) #640
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
deustis
wants to merge
11
commits into
google:master
Choose a base branch
from
deustis:deustis/s2cell_id_iteration
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
73d5817
pybind: Add S2CellId iteration (children, cells, ranges)
deustis 6b3ede8
pybind: Extract S2CellIdRange to s2/s2cellid_range.h/.cc
deustis 5a84d76
pybind: Address review comments on S2CellIdRange
deustis fba2e9d
pybind: Refactor S2CellIdRange binding helpers
deustis bd888b9
pybind: Address second round of S2CellIdRange review comments
deustis 52633d8
pybind: Drop redundant range capability comments from children/cells …
deustis 20d08ab
pybind: Rename StepOrThrow to ValidateStepIsOne
deustis e6567cc
pybind: Address code review comments on S2CellIdRange
deustis 24e489f
Merge branch 'master' into deustis/s2cell_id_iteration
deustis 2a824b3
Merge branch 'master' into deustis/s2cell_id_iteration
deustis aa21a28
pybind: Address review comments on S2CellIdRange (round 3)
deustis 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS-IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "python/s2cell_id_range.h" | ||
|
|
||
| #include "absl/log/absl_check.h" | ||
|
|
||
| int64_t S2CellIdRange::size() const { | ||
| ABSL_DCHECK(begin == end || begin.level() == end.level()) | ||
| << "S2CellIdRange: begin and end must be at the same level"; | ||
| return end.distance_from_begin() - begin.distance_from_begin(); | ||
| } | ||
|
|
||
| S2CellId S2CellIdRange::at(int64_t i) const { | ||
| ABSL_DCHECK_GE(i, 0); | ||
| ABSL_DCHECK_LT(i, size()); | ||
| return begin.advance(i); | ||
| } | ||
|
|
||
| S2CellIdRange S2CellIdRange::slice(int64_t start, int64_t stop) const { | ||
| ABSL_DCHECK_GE(start, 0); | ||
| ABSL_DCHECK_LE(start, stop); | ||
| ABSL_DCHECK_LE(stop, size()); | ||
| return S2CellIdRange{begin.advance(start), begin.advance(stop)}; | ||
| } | ||
|
|
||
| bool S2CellIdRange::contains(S2CellId cell) const { | ||
| return cell >= begin && cell < end && cell.level() == begin.level(); | ||
| } | ||
|
|
||
| std::optional<S2CellId> S2CellIdForwardIterator::next() { | ||
| if (cur == end) return std::nullopt; | ||
| S2CellId result = cur; | ||
| cur = cur.next(); | ||
| return result; | ||
| } | ||
|
|
||
| std::optional<S2CellId> S2CellIdReverseIterator::next() { | ||
| if (cur == begin) return std::nullopt; | ||
| cur = cur.prev(); | ||
| return cur; | ||
| } |
|
deustis marked this conversation as resolved.
|
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS-IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Helpers for S2CellId sequence iteration used by Python bindings. | ||
| // | ||
| // S2CellIdRange represents a contiguous range of S2CellIds at a fixed level, | ||
| // with forward and reverse iterators. These types are exposed to pybind11 to | ||
| // support Python's sequence protocol (len, indexing, slicing, iteration, | ||
| // reversed) on S2CellId ranges returned by methods such as children() and | ||
| // cells_at_level(). | ||
|
|
||
| #ifndef PYTHON_S2CELL_ID_RANGE_H_ | ||
| #define PYTHON_S2CELL_ID_RANGE_H_ | ||
|
|
||
| #include <cstdint> | ||
| #include <optional> | ||
|
|
||
| #include "s2/s2cell_id.h" | ||
|
|
||
| // A contiguous half-open range [begin, end) of S2CellIds at the same level | ||
| // along the Hilbert curve. All operations are O(1). The range may be empty | ||
| // (begin == end). | ||
| struct S2CellIdRange { | ||
| S2CellId begin; | ||
| S2CellId end; | ||
|
|
||
| // Return the number of cells in the range. | ||
| int64_t size() const; | ||
|
|
||
| // Return the cell at 0-based offset i. i must be in [0, size()); behavior | ||
| // is undefined otherwise. | ||
| S2CellId at(int64_t i) const; | ||
|
|
||
| // Return the sub-range [start, stop). start and stop must satisfy | ||
| // 0 <= start <= stop <= size(); behavior is undefined otherwise. | ||
| S2CellIdRange slice(int64_t start, int64_t stop) const; | ||
|
|
||
| // Return true if cell is in the range and at the same level as begin. | ||
| bool contains(S2CellId cell) const; | ||
| }; | ||
|
|
||
| // Forward iterator over an S2CellIdRange. | ||
| struct S2CellIdForwardIterator { | ||
| S2CellId cur; | ||
| S2CellId end; | ||
|
|
||
| // Return the next cell and advance, or std::nullopt if exhausted. | ||
| std::optional<S2CellId> next(); | ||
| }; | ||
|
|
||
| // Reverse iterator over an S2CellIdRange. Construct with cur = range.end; | ||
| // next() decrements cur before yielding, so the empty-range case (begin == | ||
| // end) is handled without a separate flag. | ||
| struct S2CellIdReverseIterator { | ||
| S2CellId cur; // one-past-current; starts at range.end | ||
| S2CellId begin; // stop when cur reaches here | ||
|
|
||
| // Return the next cell (in reverse order) and advance, or std::nullopt if | ||
| // exhausted. | ||
| std::optional<S2CellId> next(); | ||
| }; | ||
|
|
||
| #endif // PYTHON_S2CELL_ID_RANGE_H_ |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.