1 #pragma once
2 
3 #include <mbgl/tile/tile_id.hpp>
4 
5 namespace mbgl {
6 namespace algorithm {
7 
8 template <typename Iterator>
coveredByChildren(const UnwrappedTileID & id,Iterator it,const Iterator & end)9 bool coveredByChildren(const UnwrappedTileID& id, Iterator it, const Iterator& end) {
10     for (const auto& child : id.children()) {
11         it = std::lower_bound(it, end, child, [](const auto& a, const auto& b) { return std::get<0>(a) < b; });
12 
13         // Child is not present, neither its grandchildren.
14         if (it == end) {
15             return false;
16         }
17 
18         // Child is not present, but its grandchildren are.
19         if (std::get<0>(*it) != child) {
20             // This child is not covered by its grandchildren.
21             if (!coveredByChildren(child, it, end)) {
22                 return false;
23             }
24         }
25     }
26 
27     // We looked at all four children (recursively) and verified that they're covered.
28     return true;
29 }
30 
31 template <typename Container>
coveredByChildren(const UnwrappedTileID & id,const Container & container)32 bool coveredByChildren(const UnwrappedTileID& id, const Container& container) {
33     return coveredByChildren(
34         id, container.upper_bound(id),
35         container.lower_bound(UnwrappedTileID{ static_cast<int16_t>(id.wrap + 1), { 0, 0, 0 } }));
36 }
37 
38 } // namespace algorithm
39 } // namespace mbgl
40