Minor cleanups to math utils

* Dropped bounded_add for clamp. There's a slight behavior change with this, namely that
  base is no longer returned if it falls outside the bounds. However, for both usecases of
  bounded_add, that behavior doesn't make sense. The wiki clearly states min/max_light=
  define the bounds for light=.

* Replaced gcd with Boost's gcd function. We already use the latter in the Preferences
  dialog, so this is more consistent.

* Simplified the implementation of in_ranges.
This commit is contained in:
Charles Dang 2018-01-21 18:12:06 +11:00
parent 8ba6e5f40e
commit ecbb15e1c6
4 changed files with 15 additions and 31 deletions

View file

@ -31,6 +31,8 @@
#include "utils/functional.hpp"
#include <boost/math/common_factor_rt.hpp>
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
@ -260,7 +262,7 @@ void slider::set_value_range(int min_value, int max_value)
int diff = max_value - min_value;
int old_value = get_value();
step_size_ = gcd(diff, step_size_);
step_size_ = boost::math::gcd(diff, step_size_);
minimum_value_ = min_value;
slider_set_item_last(diff / step_size_);
@ -279,7 +281,7 @@ void slider::set_step_size(int step_size)
const int range_diff = get_item_count() - 1;
const int old_value = get_value();
step_size_ = gcd(range_diff, step_size);
step_size_ = boost::math::gcd(range_diff, step_size);
slider_set_item_last(range_diff / step_size_);
set_value(old_value);

View file

@ -16,6 +16,7 @@
#include "config.hpp"
#include "terrain/translation.hpp"
#include "utils/general.hpp"
#include "utils/math.hpp"
class terrain_type
@ -53,7 +54,9 @@ public:
/// Returns the light (lawful) bonus for this terrain when the time of day
/// gives a @a base bonus.
int light_bonus(int base) const
{ return bounded_add(base, light_modification_, max_light_, min_light_); }
{
return utils::clamp(base + light_modification_, min_light_, max_light_);
}
int unit_height_adjust() const { return height_adjust_; }
double unit_submerge() const { return submerge_; }

View file

@ -250,8 +250,7 @@ const time_of_day tod_manager::get_illuminated_time_of_day(const unit_map & unit
int best_result = terrain_light;
const int base_light = terrain_light + (net_darker ? most_add : most_sub);
for ( size_t i = 0; i != mod_list.size(); ++i ) {
int result =
bounded_add(base_light, mod_list[i], max_list[i], min_list[i]);
int result = utils::clamp(base_light + mod_list[i], min_list[i], max_list[i]);
if ( net_darker && result < best_result )
best_result = result;

View file

@ -34,18 +34,6 @@ inline bool is_even(T num) { return num % 2 == 0; }
template<typename T>
inline bool is_odd(T num) { return !is_even(num); }
/**
* Returns base + increment, but will not increase base above max_sum, nor
* decrease it below min_sum.
* (If base is already beyond the applicable limit, base will be returned.)
*/
inline int bounded_add(int base, int increment, int max_sum, int min_sum=0) {
if ( increment >= 0 )
return std::min(base+increment, std::max(base, max_sum));
else
return std::max(base+increment, std::min(base, min_sum));
}
/** Guarantees portable results for division by 100; round half up, to the nearest integer. */
inline int div100rounded(int num) {
return (num < 0) ? -(((-num) + 50) / 100) : (num + 50) / 100;
@ -76,15 +64,11 @@ inline double round_portable(double d) {
}
template<typename Cmp>
bool in_ranges(const Cmp c, const std::vector<std::pair<Cmp, Cmp> >&ranges) {
typename std::vector<std::pair<Cmp,Cmp> >::const_iterator range,
range_end = ranges.end();
for (range = ranges.begin(); range != range_end; ++range) {
if(range->first <= c && c <= range->second) {
return true;
}
}
return false;
bool in_ranges(const Cmp c, const std::vector<std::pair<Cmp, Cmp>>& ranges)
{
return std::any_of(ranges.begin(), ranges.end(), [c](const std::pair<Cmp, Cmp>& range) {
return range.first <= c && c <= range.second;
});
}
/**
@ -297,15 +281,11 @@ inline unsigned int count_leading_ones(N n) {
return count_leading_zeros<N>(~n);
}
inline int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
//Probably not postable.
inline int rounded_division(int a, int b)
{
auto res = std::div(a,b);
return 2 * res.rem > b ? (res.quot + 1) : res.quot;
return 2 * res.rem > b ? (res.quot + 1) : res.quot;
}