Skip to content

Commit b4cf559

Browse files
committed
Add [[nodiscard]] to pure value-returning functions
1 parent c4fe0f0 commit b4cf559

4 files changed

Lines changed: 34 additions & 34 deletions

File tree

include/geo/detail/math.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,66 +25,66 @@ inline constexpr double kEarthRadius = 6371009.0;
2525
// not portable to MSVC without _USE_MATH_DEFINES) or std::numbers::pi (C++20).
2626
inline constexpr double kPi = 3.14159265358979323846;
2727

28-
inline double deg2rad(double degrees) noexcept {
28+
[[nodiscard]] inline double deg2rad(double degrees) noexcept {
2929
return degrees * kPi / 180.0;
3030
}
3131

32-
inline double rad2deg(double angle) noexcept {
32+
[[nodiscard]] inline double rad2deg(double angle) noexcept {
3333
return angle * 180.0 / kPi;
3434
}
3535

3636
// Returns the non-negative remainder of x / m.
37-
inline double mod(double x, double m) noexcept {
37+
[[nodiscard]] inline double mod(double x, double m) noexcept {
3838
return std::fmod(std::fmod(x, m) + m, m);
3939
}
4040

4141
// Wraps the given value into the inclusive-exclusive interval [min, max).
42-
inline double wrap(double n, double min, double max) noexcept {
42+
[[nodiscard]] inline double wrap(double n, double min, double max) noexcept {
4343
return (n >= min && n < max) ? n : (mod(n - min, max - min) + min);
4444
}
4545

4646
// Returns mercator Y corresponding to latitude.
47-
inline double mercator(double lat) noexcept {
47+
[[nodiscard]] inline double mercator(double lat) noexcept {
4848
return std::log(std::tan(lat * 0.5 + kPi / 4.0));
4949
}
5050

5151
// Returns latitude from mercator Y.
52-
inline double inverse_mercator(double y) noexcept {
52+
[[nodiscard]] inline double inverse_mercator(double y) noexcept {
5353
return 2.0 * std::atan(std::exp(y)) - kPi / 2.0;
5454
}
5555

5656
// Returns haversine(angle-in-radians).
5757
// hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2.
58-
inline double hav(double x) noexcept {
58+
[[nodiscard]] inline double hav(double x) noexcept {
5959
double sin_half = std::sin(x * 0.5);
6060
return sin_half * sin_half;
6161
}
6262

6363
// Inverse haversine. arc_hav(x) == 2 * asin(sqrt(x)). Argument must be in [0, 1].
64-
inline double arc_hav(double x) noexcept {
64+
[[nodiscard]] inline double arc_hav(double x) noexcept {
6565
return 2.0 * std::asin(std::sqrt(std::clamp(x, 0.0, 1.0)));
6666
}
6767

6868
// Given h == hav(x), returns sin(abs(x)).
69-
inline double sin_from_hav(double h) noexcept {
69+
[[nodiscard]] inline double sin_from_hav(double h) noexcept {
7070
return 2.0 * std::sqrt(h * (1.0 - h));
7171
}
7272

7373
// Returns hav(asin(x)).
74-
inline double hav_from_sin(double x) noexcept {
74+
[[nodiscard]] inline double hav_from_sin(double x) noexcept {
7575
double x2 = x * x;
7676
return x2 / (1.0 + std::sqrt(1.0 - x2)) * 0.5;
7777
}
7878

7979
// Returns sin(arc_hav(x) + arc_hav(y)).
80-
inline double sin_sum_from_hav(double x, double y) noexcept {
80+
[[nodiscard]] inline double sin_sum_from_hav(double x, double y) noexcept {
8181
double a = std::sqrt(x * (1.0 - x));
8282
double b = std::sqrt(y * (1.0 - y));
8383
return 2.0 * (a + b - 2.0 * (a * y + b * x));
8484
}
8585

8686
// Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.
87-
inline double hav_distance(double lat1, double lat2, double d_lng) noexcept {
87+
[[nodiscard]] inline double hav_distance(double lat1, double lat2, double d_lng) noexcept {
8888
return hav(lat1 - lat2) + hav(d_lng) * std::cos(lat1) * std::cos(lat2);
8989
}
9090

include/geo/latlng.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ struct LatLng {
3838
* latitude and longitude). Longitudes are compared modulo 360 so that 180°
3939
* and -180° are treated as equal (same meridian).
4040
*/
41-
bool approx_equal(const LatLng& other, double eps = kDefaultEpsilon) const noexcept {
41+
[[nodiscard]] bool approx_equal(const LatLng& other, double eps = kDefaultEpsilon) const noexcept {
4242
if (std::fabs(lat - other.lat) >= eps) return false;
4343
double diff = std::fabs(std::fmod(lng - other.lng, 360.0));
4444
if (diff > 180.0) diff = 360.0 - diff;
4545
return diff < eps;
4646
}
4747

48-
bool operator==(const LatLng& other) const noexcept {
48+
[[nodiscard]] bool operator==(const LatLng& other) const noexcept {
4949
return approx_equal(other);
5050
}
5151

52-
bool operator!=(const LatLng& other) const noexcept {
52+
[[nodiscard]] bool operator!=(const LatLng& other) const noexcept {
5353
return !(*this == other);
5454
}
5555

include/geo/poly.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ inline constexpr double kDefaultTolerance = 0.1; // meters
2828
namespace detail {
2929

3030
// Returns tan(latitude-at-lng3) on the great circle (lat1, 0) to (lat2, lng2).
31-
inline double tan_lat_gc(double lat1, double lat2, double lng2, double lng3) noexcept {
31+
[[nodiscard]] inline double tan_lat_gc(double lat1, double lat2, double lng2, double lng3) noexcept {
3232
return (std::tan(lat1) * std::sin(lng2 - lng3) + std::tan(lat2) * std::sin(lng3)) / std::sin(lng2);
3333
}
3434

3535
// Returns mercator(latitude-at-lng3) on the Rhumb line (lat1, 0) to (lat2, lng2).
36-
inline double mercator_lat_rhumb(double lat1, double lat2, double lng2, double lng3) noexcept {
36+
[[nodiscard]] inline double mercator_lat_rhumb(double lat1, double lat2, double lng2, double lng3) noexcept {
3737
return (mercator(lat1) * (lng2 - lng3) + mercator(lat2) * lng3) / lng2;
3838
}
3939

4040
// Computes whether the vertical segment (lat3, lng3) to South Pole intersects
4141
// the segment (lat1, 0) to (lat2, lng2). Longitudes are offset so lng1 == 0.
42-
inline bool intersects(double lat1, double lat2, double lng2, double lat3, double lng3, bool geodesic) noexcept {
42+
[[nodiscard]] inline bool intersects(double lat1, double lat2, double lng2, double lat3, double lng3, bool geodesic) noexcept {
4343
if ((lng3 >= 0 && lng3 >= lng2) || (lng3 < 0 && lng3 < lng2)) {
4444
return false;
4545
}
@@ -66,7 +66,7 @@ inline bool intersects(double lat1, double lat2, double lng2, double lat3, doubl
6666

6767
// Returns sin(initial bearing from (lat1,lng1) to (lat3,lng3) minus initial
6868
// bearing from (lat1,lng1) to (lat2,lng2)).
69-
inline double sin_delta_bearing(double lat1, double lng1, double lat2, double lng2, double lat3, double lng3) noexcept {
69+
[[nodiscard]] inline double sin_delta_bearing(double lat1, double lng1, double lat2, double lng2, double lat3, double lng3) noexcept {
7070
double sin_lat1 = std::sin(lat1);
7171
double cos_lat2 = std::cos(lat2);
7272
double cos_lat3 = std::cos(lat3);
@@ -82,7 +82,7 @@ inline double sin_delta_bearing(double lat1, double lng1, double lat2, double ln
8282
return denom <= 0 ? 1 : (a * d - b * c) / std::sqrt(denom);
8383
}
8484

85-
inline bool is_on_segment_gc(double lat1, double lng1, double lat2, double lng2, double lat3, double lng3, double hav_tolerance) noexcept {
85+
[[nodiscard]] inline bool is_on_segment_gc(double lat1, double lng1, double lat2, double lng2, double lat3, double lng3, double hav_tolerance) noexcept {
8686
double hav_dist13 = hav_distance(lat1, lat3, lng1 - lng3);
8787
if (hav_dist13 <= hav_tolerance) {
8888
return true;
@@ -114,7 +114,7 @@ inline bool is_on_segment_gc(double lat1, double lng1, double lat2, double lng2,
114114

115115
// Computes whether a given point lies on or near a polyline within a tolerance.
116116
template <typename Path>
117-
bool on_edge_or_path(const LatLng& point, const Path& poly, bool closed, bool geodesic, double tolerance_earth) {
117+
[[nodiscard]] bool on_edge_or_path(const LatLng& point, const Path& poly, bool closed, bool geodesic, double tolerance_earth) {
118118
std::size_t size = poly.size();
119119
if (size == 0U) {
120120
return false;
@@ -187,7 +187,7 @@ bool on_edge_or_path(const LatLng& point, const Path& poly, bool closed, bool ge
187187
* the South Pole. Edges are great circle arcs if geodesic is true, rhumb lines otherwise.
188188
*/
189189
template <typename Path>
190-
bool contains(const LatLng& point, const Path& polygon, bool geodesic = false) {
190+
[[nodiscard]] bool contains(const LatLng& point, const Path& polygon, bool geodesic = false) {
191191
std::size_t size = polygon.size();
192192
if (size == 0) {
193193
return false;
@@ -223,7 +223,7 @@ bool contains(const LatLng& point, const Path& polygon, bool geodesic = false) {
223223
* within a specified tolerance in meters. The polygon is implicitly closed.
224224
*/
225225
template <typename Path>
226-
bool on_edge(const LatLng& point, const Path& polygon, bool geodesic = true, double tolerance = kDefaultTolerance) {
226+
[[nodiscard]] bool on_edge(const LatLng& point, const Path& polygon, bool geodesic = true, double tolerance = kDefaultTolerance) {
227227
return detail::on_edge_or_path(point, polygon, true, geodesic, tolerance);
228228
}
229229

@@ -232,15 +232,15 @@ bool on_edge(const LatLng& point, const Path& polygon, bool geodesic = true, dou
232232
* specified tolerance in meters. The polyline is not closed.
233233
*/
234234
template <typename Path>
235-
bool on_path(const LatLng& point, const Path& polyline, bool geodesic = true, double tolerance = kDefaultTolerance) {
235+
[[nodiscard]] bool on_path(const LatLng& point, const Path& polyline, bool geodesic = true, double tolerance = kDefaultTolerance) {
236236
return detail::on_edge_or_path(point, polyline, false, geodesic, tolerance);
237237
}
238238

239239
/**
240240
* Computes the spherical distance between the point p and the line segment
241241
* (start, end), in meters.
242242
*/
243-
inline double distance_to_segment(const LatLng& p, const LatLng& start, const LatLng& end) noexcept {
243+
[[nodiscard]] inline double distance_to_segment(const LatLng& p, const LatLng& start, const LatLng& end) noexcept {
244244
if (start == end) {
245245
return distance_between(end, p);
246246
}

include/geo/spherical.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace geo {
2727
* Returns the heading from one LatLng to another LatLng. Headings are
2828
* expressed in degrees clockwise from North within the range [-180, 180).
2929
*/
30-
inline double heading(const LatLng& from, const LatLng& to) noexcept {
30+
[[nodiscard]] inline double heading(const LatLng& from, const LatLng& to) noexcept {
3131
double from_lat = detail::deg2rad(from.lat);
3232
double from_lng = detail::deg2rad(from.lng);
3333
double to_lat = detail::deg2rad(to.lat);
@@ -44,7 +44,7 @@ inline double heading(const LatLng& from, const LatLng& to) noexcept {
4444
* Returns the LatLng resulting from moving a distance from an origin
4545
* in the specified heading (expressed in degrees clockwise from north).
4646
*/
47-
inline LatLng offset(const LatLng& from, double distance, double heading_deg) noexcept {
47+
[[nodiscard]] inline LatLng offset(const LatLng& from, double distance, double heading_deg) noexcept {
4848
distance /= detail::kEarthRadius;
4949
double heading_rad = detail::deg2rad(heading_deg);
5050
double from_lat = detail::deg2rad(from.lat);
@@ -66,7 +66,7 @@ inline LatLng offset(const LatLng& from, double distance, double heading_deg) no
6666
* Returns the location of origin when provided with a destination,
6767
* meters travelled and original heading. Returns nullopt when no solution exists.
6868
*/
69-
inline std::optional<LatLng> offset_origin(const LatLng& to, double distance, double heading_deg) noexcept {
69+
[[nodiscard]] inline std::optional<LatLng> offset_origin(const LatLng& to, double distance, double heading_deg) noexcept {
7070
double heading_rad = detail::deg2rad(heading_deg);
7171
distance /= detail::kEarthRadius;
7272
double n1 = std::cos(distance);
@@ -96,7 +96,7 @@ inline std::optional<LatLng> offset_origin(const LatLng& to, double distance, do
9696
* Returns the angle between two LatLngs, in radians. This is the same as the
9797
* distance on the unit sphere.
9898
*/
99-
inline double angle_between(const LatLng& from, const LatLng& to) noexcept {
99+
[[nodiscard]] inline double angle_between(const LatLng& from, const LatLng& to) noexcept {
100100
double lat1 = detail::deg2rad(from.lat);
101101
double lng1 = detail::deg2rad(from.lng);
102102
double lat2 = detail::deg2rad(to.lat);
@@ -107,15 +107,15 @@ inline double angle_between(const LatLng& from, const LatLng& to) noexcept {
107107
/**
108108
* Returns the distance between two LatLngs, in meters.
109109
*/
110-
inline double distance_between(const LatLng& from, const LatLng& to) noexcept {
110+
[[nodiscard]] inline double distance_between(const LatLng& from, const LatLng& to) noexcept {
111111
return angle_between(from, to) * detail::kEarthRadius;
112112
}
113113

114114
/**
115115
* Returns the LatLng which lies the given fraction of the way between the
116116
* origin and the destination (spherical linear interpolation).
117117
*/
118-
inline LatLng interpolate(const LatLng& from, const LatLng& to, double fraction) noexcept {
118+
[[nodiscard]] inline LatLng interpolate(const LatLng& from, const LatLng& to, double fraction) noexcept {
119119
double from_lat = detail::deg2rad(from.lat);
120120
double from_lng = detail::deg2rad(from.lng);
121121
double to_lat = detail::deg2rad(to.lat);
@@ -143,7 +143,7 @@ inline LatLng interpolate(const LatLng& from, const LatLng& to, double fraction)
143143
* Returns the length of the given path, in meters, on Earth.
144144
*/
145145
template <typename Path>
146-
double path_length(const Path& path) {
146+
[[nodiscard]] double path_length(const Path& path) {
147147
const std::size_t size = path.size();
148148
if (size < 2U) {
149149
return 0.0;
@@ -169,7 +169,7 @@ double path_length(const Path& path) {
169169
* "Inside" is the surface that does not contain the South Pole.
170170
*/
171171
template <typename Path>
172-
double signed_area(const Path& path) {
172+
[[nodiscard]] double signed_area(const Path& path) {
173173
std::size_t size = path.size();
174174
if (size < 3U) { return 0.0; }
175175
double total = 0.0;
@@ -196,7 +196,7 @@ double signed_area(const Path& path) {
196196
* Returns the area of a closed path on Earth.
197197
*/
198198
template <typename Path>
199-
double area(const Path& path) {
199+
[[nodiscard]] double area(const Path& path) {
200200
return std::abs(signed_area(path));
201201
}
202202

0 commit comments

Comments
 (0)