Add min_of/max_of

This commit is contained in:
Victor Zverovich 2024-09-29 10:23:29 -07:00
parent d64b100a30
commit 3faf6f181e
3 changed files with 19 additions and 16 deletions

View File

@ -332,6 +332,13 @@ struct monostate {
# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0
#endif
template <typename T> constexpr auto min_of(T a, T b) -> T {
return a < b ? a : b;
}
template <typename T> constexpr auto max_of(T a, T b) -> T {
return a > b ? a : b;
}
namespace detail {
// Suppresses "unused variable" warnings with the method described in
// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/.
@ -562,8 +569,8 @@ template <typename Char> class basic_string_view {
// Lexicographically compare this string reference to other.
FMT_CONSTEXPR auto compare(basic_string_view other) const -> int {
size_t str_size = size_ < other.size_ ? size_ : other.size_;
int result = detail::compare(data_, other.data_, str_size);
int result =
detail::compare(data_, other.data_, min_of(size_, other.size_));
if (result != 0) return result;
return size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
}
@ -1627,12 +1634,12 @@ template <typename... T> struct arg_pack {};
template <typename Char, int NUM_ARGS, int NUM_NAMED_ARGS, bool DYNAMIC_NAMES>
class format_string_checker {
private:
type types_[NUM_ARGS > 0 ? NUM_ARGS : 1];
named_arg_info<Char> named_args_[NUM_NAMED_ARGS > 0 ? NUM_NAMED_ARGS : 1];
type types_[max_of(1, NUM_ARGS)];
named_arg_info<Char> named_args_[max_of(1, NUM_NAMED_ARGS)];
compile_parse_context<Char> context_;
using parse_func = auto (*)(parse_context<Char>&) -> const Char*;
parse_func parse_funcs_[NUM_ARGS > 0 ? NUM_ARGS : 1];
parse_func parse_funcs_[max_of(1, NUM_ARGS)];
public:
template <typename... T>
@ -1740,7 +1747,7 @@ template <typename T> class buffer {
// the new elements may not be initialized.
FMT_CONSTEXPR void try_resize(size_t count) {
try_reserve(count);
size_ = count <= capacity_ ? count : capacity_;
size_ = min_of(count, capacity_);
}
// Tries increasing the buffer capacity to `new_capacity`. It can increase the
@ -1804,7 +1811,7 @@ class fixed_buffer_traits {
FMT_CONSTEXPR auto limit(size_t size) -> size_t {
size_t n = limit_ > count_ ? limit_ - count_ : 0;
count_ += size;
return size < n ? size : n;
return min_of(size, n);
}
};
@ -2255,8 +2262,7 @@ template <typename Context, size_t NUM_ARGS, size_t NUM_NAMED_ARGS,
unsigned long long DESC>
struct named_arg_store {
// args_[0].named_args points to named_args to avoid bloating format_args.
// +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning.
arg_t<Context, NUM_ARGS> args[1 + (NUM_ARGS != 0 ? NUM_ARGS : +1)];
arg_t<Context, NUM_ARGS> args[1 + NUM_ARGS];
named_arg_info<typename Context::char_type> named_args[NUM_NAMED_ARGS];
template <typename... T>
@ -2290,7 +2296,7 @@ struct format_arg_store {
// +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning.
using type =
conditional_t<NUM_NAMED_ARGS == 0,
arg_t<Context, NUM_ARGS>[NUM_ARGS != 0 ? NUM_ARGS : +1],
arg_t<Context, NUM_ARGS>[max_of<size_t>(1, NUM_ARGS)],
named_arg_store<Context, NUM_ARGS, NUM_NAMED_ARGS, DESC>>;
type args;
};

View File

@ -1071,7 +1071,7 @@ void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) {
}
} else if (precision > 0) {
*out++ = '.';
leading_zeroes = (std::min)(leading_zeroes, precision);
leading_zeroes = min_of(leading_zeroes, precision);
int remaining = precision - leading_zeroes;
out = detail::fill_n(out, leading_zeroes, '0');
if (remaining < num_digits) {

View File

@ -2680,9 +2680,6 @@ class bigint {
return bigits_[to_unsigned(index)];
}
static constexpr auto minimum(int a, int b) -> int { return a < b ? a : b; }
static constexpr auto maximum(int a, int b) -> int { return a > b ? a : b; }
FMT_CONSTEXPR auto get_bigit(int i) const -> bigit {
return i >= exp_ && i < num_bigits() ? bigits_[i - exp_] : 0;
};
@ -2820,12 +2817,12 @@ class bigint {
// Returns compare(lhs1 + lhs2, rhs).
friend FMT_CONSTEXPR auto add_compare(const bigint& lhs1, const bigint& lhs2,
const bigint& rhs) -> int {
int max_lhs_bigits = maximum(lhs1.num_bigits(), lhs2.num_bigits());
int max_lhs_bigits = max_of(lhs1.num_bigits(), lhs2.num_bigits());
int num_rhs_bigits = rhs.num_bigits();
if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
if (max_lhs_bigits > num_rhs_bigits) return 1;
double_bigit borrow = 0;
int min_exp = minimum(minimum(lhs1.exp_, lhs2.exp_), rhs.exp_);
int min_exp = min_of(min_of(lhs1.exp_, lhs2.exp_), rhs.exp_);
for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
double_bigit sum =
static_cast<double_bigit>(lhs1.get_bigit(i)) + lhs2.get_bigit(i);