Skip to content

Commit

Permalink
Small optimization: Update child iterator in place
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Rabinowitz committed Oct 3, 2023
1 parent 66a946f commit e3102ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/h3lib/include/iterators.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct {
} IterCellsChildren;

DECLSPEC IterCellsChildren iterInitParent(H3Index h, int childRes);
void _iterInitParent(H3Index h, int childRes, IterCellsChildren *iter);
DECLSPEC IterCellsChildren iterInitBaseCellNum(int baseCellNum, int childRes);
DECLSPEC void iterStepChild(IterCellsChildren *iter);

Expand Down
26 changes: 16 additions & 10 deletions src/h3lib/lib/iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,33 @@ and so on.
*/
IterCellsChildren iterInitParent(H3Index h, int childRes) {
IterCellsChildren it;
_iterInitParent(h, childRes, &it);
return it;
}

it._parentRes = H3_GET_RESOLUTION(h);
/**
* Internal function - initialize a parent iterator in-place
*/
void _iterInitParent(H3Index h, int childRes, IterCellsChildren *iter) {
iter->_parentRes = H3_GET_RESOLUTION(h);

if (childRes < it._parentRes || childRes > MAX_H3_RES || h == H3_NULL) {
return _null_iter();
if (childRes < iter->_parentRes || childRes > MAX_H3_RES || h == H3_NULL) {
*iter = _null_iter();
return;
}

it.h = _zeroIndexDigits(h, it._parentRes + 1, childRes);
H3_SET_RESOLUTION(it.h, childRes);
iter->h = _zeroIndexDigits(h, iter->_parentRes + 1, childRes);
H3_SET_RESOLUTION(iter->h, childRes);

if (H3_EXPORT(isPentagon)(it.h)) {
if (H3_EXPORT(isPentagon)(iter->h)) {
// The skip digit skips `1` for pentagons.
// The "_skipDigit" moves to the left as we count up from the
// child resolution to the parent resolution.
it._skipDigit = childRes;
iter->_skipDigit = childRes;
} else {
// if not a pentagon, we can ignore "skip digit" logic
it._skipDigit = -1;
iter->_skipDigit = -1;
}

return it;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/h3lib/lib/polyfill.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,9 @@ void iterStepPolygon(IterCellsPolygon *iter) {
// Otherwise, increment the polyfill iterator
iterStepPolygonCompact(&(iter->_cellIter));
if (iter->_cellIter.cell) {
IterCellsChildren childIter =
iterInitParent(iter->_cellIter.cell, iter->_cellIter._res);
iter->cell = childIter.h;
iter->_childIter = childIter;
_iterInitParent(iter->_cellIter.cell, iter->_cellIter._res,
&(iter->_childIter));
iter->cell = iter->_childIter.h;
return;
}

Expand Down

0 comments on commit e3102ce

Please sign in to comment.