malachite_base/comparison/
macros.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9/// Computes the minimum of a list of expressions.
10///
11/// The list must be nonempty, the expressions must all have the same type, and that type must
12/// implement [`Ord`]. Each expression is only evaluated once.
13///
14/// # Examples
15/// ```
16/// use malachite_base::min;
17///
18/// assert_eq!(min!(3), 3);
19/// assert_eq!(min!(3, 1), 1);
20/// assert_eq!(min!(3, 1, 4), 1);
21/// ```
22#[macro_export]
23macro_rules! min {
24    ($first: expr $(,$next: expr)*) => {
25        {
26            let mut min = $first;
27            $(
28                let next = $next;
29                if next < min {
30                    min = next;
31                }
32            )*
33            min
34        }
35    };
36}
37
38/// Computes the maximum of a list of expressions.
39///
40/// The list must be nonempty, the expressions must all have the same type, and that type must
41/// implement [`Ord`]. Each expression is only evaluated once.
42///
43/// # Examples
44/// ```
45/// use malachite_base::max;
46///
47/// assert_eq!(max!(3), 3);
48/// assert_eq!(max!(3, 1), 3);
49/// assert_eq!(max!(3, 1, 4), 4);
50/// ```
51#[macro_export]
52macro_rules! max {
53    ($first: expr $(,$next: expr)*) => {
54        {
55            let mut max = $first;
56            $(
57                let next = $next;
58                if next > max {
59                    max = next;
60                }
61            )*
62            max
63        }
64    };
65}