pub fn slice_trailing_zeros<T: Eq + Zero>(xs: &[T]) -> usize
Expand description

Counts the number of zeros that a slice ends with.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is xs.len().

Examples

use malachite_base::slices::slice_trailing_zeros;

assert_eq!(slice_trailing_zeros::<u32>(&[1, 2, 3]), 0);
assert_eq!(slice_trailing_zeros::<u32>(&[1, 2, 3, 0, 0, 0]), 3);
Examples found in repository?
src/num/conversion/string/to_sci.rs (line 138)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
fn fmt_sci_unsigned<T: PrimitiveUnsigned>(
    mut x: T,
    f: &mut Formatter,
    options: ToSciOptions,
) -> std::fmt::Result
where
    BaseFmtWrapper<T>: Display,
{
    match options.size_options {
        SciSizeOptions::Complete | SciSizeOptions::Scale(0) => write_helper(x, f, options),
        SciSizeOptions::Scale(scale) => {
            write_helper(x, f, options)?;
            if options.include_trailing_zeros {
                f.write_char('.')?;
                for _ in 0..scale {
                    f.write_char('0')?;
                }
            }
            Ok(())
        }
        SciSizeOptions::Precision(precision) => {
            let t_base = T::from(options.base);
            let log = if x == T::ZERO {
                0
            } else {
                x.floor_log_base(t_base)
            };
            if log < precision {
                // no exponent
                write_helper(x, f, options)?;
                if options.include_trailing_zeros {
                    let extra_zeros = precision - log - 1;
                    if extra_zeros != 0 {
                        f.write_char('.')?;
                        for _ in 0..extra_zeros {
                            f.write_char('0')?;
                        }
                    }
                }
                Ok(())
            } else {
                // exponent
                let mut e = log;
                let neg_scale = log - precision + 1;
                if let Some(base_log) = options.base.checked_log_base_2() {
                    x.shr_round_assign(base_log * neg_scale, options.rounding_mode);
                } else {
                    x.div_round_assign(Pow::pow(t_base, neg_scale), options.rounding_mode);
                }
                let mut chars = x.to_digits_desc(&options.base);
                let mut len = chars.len();
                let p = usize::exact_from(precision);
                if len > p {
                    // rounded up to a power of the base, need to reduce precision
                    chars.pop();
                    len -= 1;
                    e += 1;
                }
                assert_eq!(len, p);
                if !options.include_trailing_zeros {
                    chars.truncate(len - slice_trailing_zeros(&chars));
                }
                if options.lowercase {
                    for digit in &mut chars {
                        *digit = digit_to_display_byte_lower(*digit).unwrap();
                    }
                } else {
                    for digit in &mut chars {
                        *digit = digit_to_display_byte_upper(*digit).unwrap();
                    }
                }
                len = chars.len();
                if len != 1 {
                    chars.push(b'0');
                    chars.copy_within(1..len, 2);
                    chars[1] = b'.';
                }
                f.write_str(&String::from_utf8(chars).unwrap())?;
                write_exponent(f, options, e)
            }
        }
    }
}