Function malachite_base::vecs::vec_pad_left
source · Expand description
Inserts several copies of a value at the left (beginning) of a Vec
.
Using this function is more efficient than inserting the values one by one.
Worst-case complexity
$T(n) = O(n + m)$
$M(n) = O(n + m)$
where $T$ is time, $M$ is additional memory, $n$ = xs.len()
before the function is called, and
$m$ = pad_size
.
Examples
use malachite_base::vecs::vec_pad_left;
let mut xs = vec![1, 2, 3];
vec_pad_left::<u32>(&mut xs, 5, 10);
assert_eq!(xs, [10, 10, 10, 10, 10, 1, 2, 3]);
Examples found in repository?
src/num/conversion/string/to_string.rs (line 300)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
fn to_string_base_signed<U: Digits<u8>, S: Copy + Eq + Ord + UnsignedAbs<Output = U> + Zero>(
x: &S,
base: u8,
) -> String {
assert!((2..=36).contains(&base), "base out of range");
if *x == S::ZERO {
"0".to_string()
} else {
let mut digits = x.unsigned_abs().to_digits_desc(&u8::wrapping_from(base));
for digit in &mut digits {
*digit = digit_to_display_byte_lower(*digit).unwrap();
}
if *x < S::ZERO {
vec_pad_left(&mut digits, 1, b'-');
}
String::from_utf8(digits).unwrap()
}
}
fn to_string_base_upper_signed<
U: Digits<u8>,
S: Copy + Eq + Ord + UnsignedAbs<Output = U> + Zero,
>(
x: &S,
base: u8,
) -> String {
assert!((2..=36).contains(&base), "base out of range");
if *x == S::ZERO {
"0".to_string()
} else {
let mut digits = x.unsigned_abs().to_digits_desc(&base);
for digit in &mut digits {
*digit = digit_to_display_byte_upper(*digit).unwrap();
}
if *x < S::ZERO {
vec_pad_left(&mut digits, 1, b'-');
}
String::from_utf8(digits).unwrap()
}
}