Skip to content

Commit

Permalink
Mark tile ID methods noexcept (#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored Oct 31, 2023
1 parent bb998a4 commit 5a17559
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 68 deletions.
127 changes: 64 additions & 63 deletions include/mbgl/tile/tile_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class UnwrappedTileID;
// there. z is never larger than the source's maxzoom
class CanonicalTileID {
public:
CanonicalTileID(uint8_t z, uint32_t x, uint32_t y);
bool operator==(const CanonicalTileID&) const;
bool operator!=(const CanonicalTileID&) const;
bool operator<(const CanonicalTileID&) const;
bool isChildOf(const CanonicalTileID&) const;
CanonicalTileID scaledTo(uint8_t z) const;
std::array<CanonicalTileID, 4> children() const;
CanonicalTileID(uint8_t z, uint32_t x, uint32_t y) noexcept;
bool operator==(const CanonicalTileID&) const noexcept;
bool operator!=(const CanonicalTileID&) const noexcept;
bool operator<(const CanonicalTileID&) const noexcept;
bool isChildOf(const CanonicalTileID&) const noexcept;
CanonicalTileID scaledTo(uint8_t z) const noexcept;
std::array<CanonicalTileID, 4> children() const noexcept;

uint8_t z;
uint32_t x;
Expand All @@ -47,19 +47,19 @@ std::string toString(const CanonicalTileID&);
// the
class OverscaledTileID {
public:
OverscaledTileID(uint8_t overscaledZ, int16_t wrap, CanonicalTileID);
OverscaledTileID(uint8_t overscaledZ, int16_t wrap, uint8_t z, uint32_t x, uint32_t y);
OverscaledTileID(uint8_t z, uint32_t x, uint32_t y);
explicit OverscaledTileID(const CanonicalTileID&);
explicit OverscaledTileID(CanonicalTileID&&);
bool operator==(const OverscaledTileID&) const;
bool operator!=(const OverscaledTileID&) const;
bool operator<(const OverscaledTileID&) const;
bool isChildOf(const OverscaledTileID&) const;
uint32_t overscaleFactor() const;
OverscaledTileID scaledTo(uint8_t z) const;
UnwrappedTileID toUnwrapped() const;
OverscaledTileID unwrapTo(int16_t wrap) const;
OverscaledTileID(uint8_t overscaledZ, int16_t wrap, CanonicalTileID) noexcept;
OverscaledTileID(uint8_t overscaledZ, int16_t wrap, uint8_t z, uint32_t x, uint32_t y) noexcept;
OverscaledTileID(uint8_t z, uint32_t x, uint32_t y) noexcept;
explicit OverscaledTileID(const CanonicalTileID&) noexcept;
explicit OverscaledTileID(CanonicalTileID&&) noexcept;
bool operator==(const OverscaledTileID&) const noexcept;
bool operator!=(const OverscaledTileID&) const noexcept;
bool operator<(const OverscaledTileID&) const noexcept;
bool isChildOf(const OverscaledTileID&) const noexcept;
uint32_t overscaleFactor() const noexcept;
OverscaledTileID scaledTo(uint8_t z) const noexcept;
UnwrappedTileID toUnwrapped() const noexcept;
OverscaledTileID unwrapTo(int16_t wrap) const noexcept;

uint8_t overscaledZ;
int16_t wrap;
Expand All @@ -77,16 +77,16 @@ std::string toString(const OverscaledTileID&);
// rendered at (= calc the matrix) z is never larger than the source's maxzoom
class UnwrappedTileID {
public:
UnwrappedTileID(uint8_t z, int64_t x, int64_t y);
UnwrappedTileID(int16_t wrap, CanonicalTileID);
bool operator==(const UnwrappedTileID&) const;
bool operator!=(const UnwrappedTileID&) const;
bool operator<(const UnwrappedTileID&) const;
bool isChildOf(const UnwrappedTileID&) const;
std::array<UnwrappedTileID, 4> children() const;
OverscaledTileID overscaleTo(uint8_t z) const;
float pixelsToTileUnits(float pixelValue, float zoom) const;
UnwrappedTileID unwrapTo(int16_t wrap) const;
UnwrappedTileID(uint8_t z, int64_t x, int64_t y) noexcept;
UnwrappedTileID(int16_t wrap, CanonicalTileID) noexcept;
bool operator==(const UnwrappedTileID&) const noexcept;
bool operator!=(const UnwrappedTileID&) const noexcept;
bool operator<(const UnwrappedTileID&) const noexcept;
bool isChildOf(const UnwrappedTileID&) const noexcept;
std::array<UnwrappedTileID, 4> children() const noexcept;
OverscaledTileID overscaleTo(uint8_t z) const noexcept;
float pixelsToTileUnits(float pixelValue, float zoom) const noexcept;
UnwrappedTileID unwrapTo(int16_t wrap) const noexcept;

int16_t wrap;
CanonicalTileID canonical;
Expand All @@ -97,7 +97,7 @@ namespace util {
std::string toString(const UnwrappedTileID&);
} // namespace util

inline CanonicalTileID::CanonicalTileID(uint8_t z_, uint32_t x_, uint32_t y_)
inline CanonicalTileID::CanonicalTileID(uint8_t z_, uint32_t x_, uint32_t y_) noexcept
: z(z_),
x(x_),
y(y_) {
Expand All @@ -106,32 +106,32 @@ inline CanonicalTileID::CanonicalTileID(uint8_t z_, uint32_t x_, uint32_t y_)
assert(y < (1ull << z));
}

inline bool CanonicalTileID::operator==(const CanonicalTileID& rhs) const {
inline bool CanonicalTileID::operator==(const CanonicalTileID& rhs) const noexcept {
return z == rhs.z && x == rhs.x && y == rhs.y;
}

inline bool CanonicalTileID::operator!=(const CanonicalTileID& rhs) const {
inline bool CanonicalTileID::operator!=(const CanonicalTileID& rhs) const noexcept {
return z != rhs.z || x != rhs.x || y != rhs.y;
}

inline bool CanonicalTileID::operator<(const CanonicalTileID& rhs) const {
inline bool CanonicalTileID::operator<(const CanonicalTileID& rhs) const noexcept {
return std::tie(z, x, y) < std::tie(rhs.z, rhs.x, rhs.y);
}

inline bool CanonicalTileID::isChildOf(const CanonicalTileID& parent) const {
inline bool CanonicalTileID::isChildOf(const CanonicalTileID& parent) const noexcept {
// We're first testing for z == 0, to avoid a 32 bit shift, which is undefined.
return parent.z == 0 || (parent.z < z && parent.x == (x >> (z - parent.z)) && parent.y == (y >> (z - parent.z)));
}

inline CanonicalTileID CanonicalTileID::scaledTo(uint8_t targetZ) const {
inline CanonicalTileID CanonicalTileID::scaledTo(uint8_t targetZ) const noexcept {
if (targetZ <= z) {
return {targetZ, x >> (z - targetZ), y >> (z - targetZ)}; // parent or same
} else {
return {targetZ, x << (targetZ - z), y << (targetZ - z)}; // child
}
}

inline std::array<CanonicalTileID, 4> CanonicalTileID::children() const {
inline std::array<CanonicalTileID, 4> CanonicalTileID::children() const noexcept {
const uint8_t childZ = z + 1;
const uint32_t childX = x * 2;
const uint32_t childY = y * 2;
Expand All @@ -143,103 +143,104 @@ inline std::array<CanonicalTileID, 4> CanonicalTileID::children() const {
}};
}

inline OverscaledTileID::OverscaledTileID(uint8_t overscaledZ_, int16_t wrap_, CanonicalTileID canonical_)
inline OverscaledTileID::OverscaledTileID(uint8_t overscaledZ_, int16_t wrap_, CanonicalTileID canonical_) noexcept
: overscaledZ(overscaledZ_),
wrap(wrap_),
canonical(canonical_) {
assert(overscaledZ >= canonical.z);
}

inline OverscaledTileID::OverscaledTileID(uint8_t overscaledZ_, int16_t wrap_, uint8_t z, uint32_t x, uint32_t y)
inline OverscaledTileID::OverscaledTileID(
uint8_t overscaledZ_, int16_t wrap_, uint8_t z, uint32_t x, uint32_t y) noexcept
: overscaledZ(overscaledZ_),
wrap(wrap_),
canonical(z, x, y) {
assert(overscaledZ >= canonical.z);
}

inline OverscaledTileID::OverscaledTileID(uint8_t z, uint32_t x, uint32_t y)
inline OverscaledTileID::OverscaledTileID(uint8_t z, uint32_t x, uint32_t y) noexcept
: overscaledZ(z),
wrap(0),
canonical(z, x, y) {}

inline OverscaledTileID::OverscaledTileID(const CanonicalTileID& canonical_)
inline OverscaledTileID::OverscaledTileID(const CanonicalTileID& canonical_) noexcept
: overscaledZ(canonical_.z),
wrap(0),
canonical(canonical_) {
assert(overscaledZ >= canonical.z);
}

inline OverscaledTileID::OverscaledTileID(CanonicalTileID&& canonical_)
inline OverscaledTileID::OverscaledTileID(CanonicalTileID&& canonical_) noexcept
: overscaledZ(canonical_.z),
wrap(0),
canonical(std::forward<CanonicalTileID>(canonical_)) {
assert(overscaledZ >= canonical.z);
}

inline bool OverscaledTileID::operator==(const OverscaledTileID& rhs) const {
inline bool OverscaledTileID::operator==(const OverscaledTileID& rhs) const noexcept {
return overscaledZ == rhs.overscaledZ && wrap == rhs.wrap && canonical == rhs.canonical;
}

inline bool OverscaledTileID::operator!=(const OverscaledTileID& rhs) const {
inline bool OverscaledTileID::operator!=(const OverscaledTileID& rhs) const noexcept {
return overscaledZ != rhs.overscaledZ || wrap != rhs.wrap || canonical != rhs.canonical;
}

inline bool OverscaledTileID::operator<(const OverscaledTileID& rhs) const {
inline bool OverscaledTileID::operator<(const OverscaledTileID& rhs) const noexcept {
return std::tie(overscaledZ, wrap, canonical) < std::tie(rhs.overscaledZ, rhs.wrap, rhs.canonical);
}

inline uint32_t OverscaledTileID::overscaleFactor() const {
inline uint32_t OverscaledTileID::overscaleFactor() const noexcept {
return 1u << (overscaledZ - canonical.z);
}

inline bool OverscaledTileID::isChildOf(const OverscaledTileID& rhs) const {
inline bool OverscaledTileID::isChildOf(const OverscaledTileID& rhs) const noexcept {
return wrap == rhs.wrap && overscaledZ > rhs.overscaledZ &&
(canonical == rhs.canonical || canonical.isChildOf(rhs.canonical));
}

inline OverscaledTileID OverscaledTileID::scaledTo(uint8_t z) const {
inline OverscaledTileID OverscaledTileID::scaledTo(uint8_t z) const noexcept {
return {z, wrap, z >= canonical.z ? canonical : canonical.scaledTo(z)};
}

inline UnwrappedTileID OverscaledTileID::toUnwrapped() const {
inline UnwrappedTileID OverscaledTileID::toUnwrapped() const noexcept {
return {wrap, canonical};
}

inline OverscaledTileID OverscaledTileID::unwrapTo(int16_t newWrap) const {
inline OverscaledTileID OverscaledTileID::unwrapTo(int16_t newWrap) const noexcept {
return {overscaledZ, newWrap, canonical};
}

inline UnwrappedTileID::UnwrappedTileID(uint8_t z_, int64_t x_, int64_t y_)
inline UnwrappedTileID::UnwrappedTileID(uint8_t z_, int64_t x_, int64_t y_) noexcept
: wrap(static_cast<int16_t>((x_ < 0 ? x_ - (1ll << z_) + 1 : x_) / (1ll << z_))),
canonical(z_,
static_cast<uint32_t>(x_ - wrap * (1ll << z_)),
y_ < 0 ? 0 : std::min(static_cast<uint32_t>(y_), static_cast<uint32_t>(1ull << z_) - 1)) {}

inline UnwrappedTileID::UnwrappedTileID(int16_t wrap_, CanonicalTileID canonical_)
inline UnwrappedTileID::UnwrappedTileID(int16_t wrap_, CanonicalTileID canonical_) noexcept
: wrap(wrap_),
canonical(canonical_) {}

inline bool UnwrappedTileID::operator==(const UnwrappedTileID& rhs) const {
inline bool UnwrappedTileID::operator==(const UnwrappedTileID& rhs) const noexcept {
return wrap == rhs.wrap && canonical == rhs.canonical;
}

inline bool UnwrappedTileID::operator!=(const UnwrappedTileID& rhs) const {
inline bool UnwrappedTileID::operator!=(const UnwrappedTileID& rhs) const noexcept {
return wrap != rhs.wrap || canonical != rhs.canonical;
}

inline bool UnwrappedTileID::operator<(const UnwrappedTileID& rhs) const {
inline bool UnwrappedTileID::operator<(const UnwrappedTileID& rhs) const noexcept {
return std::tie(wrap, canonical) < std::tie(rhs.wrap, rhs.canonical);
}

inline UnwrappedTileID UnwrappedTileID::unwrapTo(int16_t newWrap) const {
inline UnwrappedTileID UnwrappedTileID::unwrapTo(int16_t newWrap) const noexcept {
return {newWrap, canonical};
}

inline bool UnwrappedTileID::isChildOf(const UnwrappedTileID& parent) const {
inline bool UnwrappedTileID::isChildOf(const UnwrappedTileID& parent) const noexcept {
return wrap == parent.wrap && canonical.isChildOf(parent.canonical);
}

inline std::array<UnwrappedTileID, 4> UnwrappedTileID::children() const {
inline std::array<UnwrappedTileID, 4> UnwrappedTileID::children() const noexcept {
const uint8_t childZ = canonical.z + 1;
const uint32_t childX = canonical.x * 2;
const uint32_t childY = canonical.y * 2;
Expand All @@ -251,12 +252,12 @@ inline std::array<UnwrappedTileID, 4> UnwrappedTileID::children() const {
}};
}

inline OverscaledTileID UnwrappedTileID::overscaleTo(const uint8_t overscaledZ) const {
inline OverscaledTileID UnwrappedTileID::overscaleTo(const uint8_t overscaledZ) const noexcept {
assert(overscaledZ >= canonical.z);
return {overscaledZ, wrap, canonical};
}

inline float UnwrappedTileID::pixelsToTileUnits(const float pixelValue, const float zoom) const {
inline float UnwrappedTileID::pixelsToTileUnits(const float pixelValue, const float zoom) const noexcept {
return pixelValue * (static_cast<float>(util::EXTENT) /
(static_cast<float>(util::tileSize_D) * std::pow(2.f, zoom - canonical.z)));
}
Expand All @@ -267,17 +268,17 @@ namespace std {

template <>
struct hash<mbgl::CanonicalTileID> {
size_t operator()(const mbgl::CanonicalTileID& id) const;
size_t operator()(const mbgl::CanonicalTileID& id) const noexcept;
};

template <>
struct hash<mbgl::UnwrappedTileID> {
size_t operator()(const mbgl::UnwrappedTileID& id) const;
size_t operator()(const mbgl::UnwrappedTileID& id) const noexcept;
};

template <>
struct hash<mbgl::OverscaledTileID> {
size_t operator()(const mbgl::OverscaledTileID& id) const;
size_t operator()(const mbgl::OverscaledTileID& id) const noexcept;
};

} // namespace std
6 changes: 3 additions & 3 deletions src/mbgl/tile/tile_id_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

namespace std {

size_t hash<mbgl::CanonicalTileID>::operator()(const mbgl::CanonicalTileID& id) const {
size_t hash<mbgl::CanonicalTileID>::operator()(const mbgl::CanonicalTileID& id) const noexcept {
std::size_t seed = 0;
mbgl::util::hash_combine(seed, id.x);
mbgl::util::hash_combine(seed, id.y);
mbgl::util::hash_combine(seed, id.z);
return seed;
}

size_t hash<mbgl::UnwrappedTileID>::operator()(const mbgl::UnwrappedTileID& id) const {
size_t hash<mbgl::UnwrappedTileID>::operator()(const mbgl::UnwrappedTileID& id) const noexcept {
std::size_t seed = 0;
mbgl::util::hash_combine(seed, std::hash<mbgl::CanonicalTileID>{}(id.canonical));
mbgl::util::hash_combine(seed, id.wrap);
return seed;
}

size_t hash<mbgl::OverscaledTileID>::operator()(const mbgl::OverscaledTileID& id) const {
size_t hash<mbgl::OverscaledTileID>::operator()(const mbgl::OverscaledTileID& id) const noexcept {
std::size_t seed = 0;
mbgl::util::hash_combine(seed, std::hash<mbgl::CanonicalTileID>{}(id.canonical));
mbgl::util::hash_combine(seed, id.overscaledZ);
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/util/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace mbgl {
namespace util {

template <class T>
void hash_combine(std::size_t& seed, const T& v) {
void hash_combine(std::size_t& seed, const T& v) noexcept {
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

template <class... Args>
std::size_t hash(Args&&... args) {
std::size_t hash(Args&&... args) noexcept {
std::size_t seed = 0;
ignore({(hash_combine(seed, args), 0)...});
return seed;
Expand Down

0 comments on commit 5a17559

Please sign in to comment.