polars_utils/
min_max.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// These min/max operators don't follow our total order strictly. Instead
// if exactly one of the two arguments is NaN the skip_nan varieties returns
// the non-nan argument, whereas the propagate_nan varieties give the nan
// argument. If both/neither argument is NaN these extrema follow the normal
// total order.
//
// They also violate the regular total order for Option<T>: on top of the
// above rules None's are always ignored, so only if both arguments are
// None is the output None.
pub trait MinMax: Sized {
    // Comparison operators that either consider nan to be the smallest, or the
    // largest possible value. Use tot_eq for equality. Prefer directly using
    // min/max, they're slightly faster.
    fn nan_min_lt(&self, other: &Self) -> bool;
    fn nan_max_lt(&self, other: &Self) -> bool;

    // Binary operators that return either the minimum or maximum.
    #[inline(always)]
    fn min_propagate_nan(self, other: Self) -> Self {
        if self.nan_min_lt(&other) {
            self
        } else {
            other
        }
    }

    #[inline(always)]
    fn max_propagate_nan(self, other: Self) -> Self {
        if self.nan_max_lt(&other) {
            other
        } else {
            self
        }
    }

    #[inline(always)]
    fn min_ignore_nan(self, other: Self) -> Self {
        if self.nan_max_lt(&other) {
            self
        } else {
            other
        }
    }

    #[inline(always)]
    fn max_ignore_nan(self, other: Self) -> Self {
        if self.nan_min_lt(&other) {
            other
        } else {
            self
        }
    }
}

macro_rules! impl_trivial_min_max {
    ($T: ty) => {
        impl MinMax for $T {
            #[inline(always)]
            fn nan_min_lt(&self, other: &Self) -> bool {
                self < other
            }

            #[inline(always)]
            fn nan_max_lt(&self, other: &Self) -> bool {
                self < other
            }
        }
    };
}

// We can't do a blanket impl because Rust complains f32 might implement
// Ord someday.
impl_trivial_min_max!(bool);
impl_trivial_min_max!(u8);
impl_trivial_min_max!(u16);
impl_trivial_min_max!(u32);
impl_trivial_min_max!(u64);
impl_trivial_min_max!(u128);
impl_trivial_min_max!(usize);
impl_trivial_min_max!(i8);
impl_trivial_min_max!(i16);
impl_trivial_min_max!(i32);
impl_trivial_min_max!(i64);
impl_trivial_min_max!(i128);
impl_trivial_min_max!(isize);
impl_trivial_min_max!(char);
impl_trivial_min_max!(&str);
impl_trivial_min_max!(&[u8]);
impl_trivial_min_max!(String);

macro_rules! impl_float_min_max {
    ($T: ty) => {
        impl MinMax for $T {
            #[inline(always)]
            fn nan_min_lt(&self, other: &Self) -> bool {
                !(other.is_nan() | (self >= other))
            }

            #[inline(always)]
            fn nan_max_lt(&self, other: &Self) -> bool {
                !(self.is_nan() | (self >= other))
            }

            #[inline(always)]
            fn min_ignore_nan(self, other: Self) -> Self {
                <$T>::min(self, other)
            }

            #[inline(always)]
            fn max_ignore_nan(self, other: Self) -> Self {
                <$T>::max(self, other)
            }

            #[inline(always)]
            fn min_propagate_nan(self, other: Self) -> Self {
                if (self < other) | self.is_nan() {
                    self
                } else {
                    other
                }
            }

            #[inline(always)]
            fn max_propagate_nan(self, other: Self) -> Self {
                if (self > other) | self.is_nan() {
                    self
                } else {
                    other
                }
            }
        }
    };
}

impl_float_min_max!(f32);
impl_float_min_max!(f64);