+
Skip to content

Conversation

cnaples79
Copy link

Summary

Implements a general validation function isValidIndex() that checks if an H3 index is valid for any mode (cell, directed edge, or vertex), as requested in #1043.

Changes Made

  • API Addition: Added isValidIndex() function declaration to h3api.h.in
  • Implementation: Implemented function in h3Index.c that checks all three validation types:
    int H3_EXPORT(isValidIndex)(H3Index h) {
        return H3_EXPORT(isValidCell)(h) ||
               H3_EXPORT(isValidDirectedEdge)(h) ||
               H3_EXPORT(isValidVertex)(h);
    }
  • Dependencies: Added includes for directedEdge.h and vertex.h to h3Index.c
  • Tests: Added comprehensive test coverage in testH3Index.c covering:
    • Valid cell indices
    • Valid directed edge indices
    • Valid vertex indices
    • Invalid indices
    • Corrupted indices

API Usage

H3Index index = ...; // Can be a cell, edge, or vertex
if (isValidIndex(index)) {
    // Index is valid for some mode
}

Test Plan

  • Added unit test isValidIndex in testH3Index.c
  • Tests validate the function returns true for valid cells, directed edges, and vertices
  • Tests validate the function returns false for invalid and corrupted indices
  • All existing tests should continue to pass

Fixes #1043

Implement a general validation function that checks if an H3 index
is valid for any mode (cell, directed edge, or vertex).

Changes:
- Add isValidIndex() function declaration to h3api.h.in
- Implement isValidIndex() in h3Index.c
- Add includes for directedEdge.h and vertex.h in h3Index.c
- Add comprehensive test coverage in testH3Index.c

The function returns true if the index is valid as a cell, directed edge,
or vertex, providing a single validation function for any H3 index type.

Fixes uber#1043
@CLAassistant
Copy link

CLAassistant commented Oct 11, 2025

CLA assistant check
All committers have signed the CLA.

// Test with valid directed edge
H3Index neighbor;
t_assertSuccess(H3_EXPORT(latLngToCell)(&coord, 5, &neighbor));
neighbor++; // Get a neighboring cell
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. Simply incrementing will almost never give you a neighboring cell.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajfriend I just pushed a commit to fix this. Thanks for the feedback. I used gridRingUnsafe to call the neighboring cells. Let me know what you think.

Replace incorrect neighbor++ with proper gridRingUnsafe call to get
an actual neighboring cell for directed edge testing. Simply incrementing
an H3Index will almost never produce a valid neighboring cell.

This addresses the review feedback from ajfriend on lines 224-227.
@ajfriend
Copy link
Collaborator

Thanks for this! It looks like you may need to run/test this locally to get the formatting that the CI is expecting.

Apply clang-format style to match project formatting conventions:
- Put first two conditions on same line
- Wrap long comment to 80 characters
@cnaples79
Copy link
Author

@ajfriend happy to contribute. I fixed the formatting manually based on the logs then confirmed using clang-format. CI should hopefully pass now.

Apply clang-format style to isValidIndex documentation comment:
- Wrap comment across 3 lines for proper formatting
@cnaples79
Copy link
Author

CI formatting failed on the header file, I re-formatted it with clang-format.

Copy link
Collaborator

@isaacbrodsky isaacbrodsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please note we will need to add this to fuzzers (e.g. https://github.com/uber/h3/blob/master/src/apps/fuzzers/fuzzerCellProperties.c) & docs as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add isValidIndex function

5 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载