Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/tile_coordinates_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class LossyTileCoordinatesSet : public TileCoordinatesSet {
size_t size() const override;
size_t zoom() const override;
void set(TileCoordinate x, TileCoordinate y) override;
void forEachSet(const std::function<void(TileCoordinate x, TileCoordinate y)>& f) const override;

private:
unsigned int zoom_;
Expand Down
21 changes: 21 additions & 0 deletions src/tile_coordinates_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,24 @@ void LossyTileCoordinatesSet::set(TileCoordinate x, TileCoordinate y) {
throw std::logic_error("LossyTileCoordinatesSet::set() is not implemented; LossyTileCoordinatesSet is read-only");
}

void LossyTileCoordinatesSet::forEachSet(const std::function<void(TileCoordinate x, TileCoordinate y)>& f) const {
// Expand the underlying set instead of testing all 4^zoom tiles (2^34 at z17).
// Its tiles arrive x-major, y-minor; one column of them becomes `scale`
// columns here, which keeps this set's order x-major, y-minor too.
std::vector<TileCoordinate> column;
TileCoordinate columnX = 0;
auto flush = [&]() {
for (TileCoordinate dx = 0; dx < scale; dx++)
for (TileCoordinate y : column)
for (TileCoordinate dy = 0; dy < scale; dy++)
f(columnX * scale + dx, y * scale + dy);
column.clear();
};
tiles.forEachSet([&](TileCoordinate x, TileCoordinate y) {
if (!column.empty() && x != columnX) flush();
columnX = x;
column.push_back(y);
});
if (!column.empty()) flush();
}

16 changes: 16 additions & 0 deletions test/tile_coordinates_set.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ MU_TEST(test_tile_coordinates_set) {
mu_check(z2.test(1, 1) == true);
mu_check(z2.test(2, 2) == false);
}

// forEachSet on a wrapped set visits the same tiles as a full scan, in x-major, y-minor order
{
PreciseTileCoordinatesSet z3(3);
z3.set(0, 0); z3.set(0, 5); z3.set(2, 1); z3.set(2, 7); z3.set(7, 3);
LossyTileCoordinatesSet z5(5, z3);

std::vector<std::pair<TileCoordinate, TileCoordinate>> expected, visited;
for (TileCoordinate x = 0; x < 32; x++)
for (TileCoordinate y = 0; y < 32; y++)
if (z5.test(x, y)) expected.push_back({x, y});
z5.forEachSet([&](TileCoordinate x, TileCoordinate y) { visited.push_back({x, y}); });

mu_check(expected.size() == 5 * 16);
mu_check(visited == expected);
}
}

MU_TEST_SUITE(test_suite_tile_coordinates_set) {
Expand Down
Loading