Use vec![elt; n] where possible
The common pattern `iter::repeat(elt).take(n).collect::<Vec<_>>()` is exactly equivalent to `vec![elt; n]`, do this replacement in the whole tree. (Actually, vec![] is smart enough to only call clone n - 1 times, while the former solution would call clone n times, and this fact is virtually irrelevant in practice.)
This commit is contained in:
parent
5b6a464358
commit
836f32e769
31 changed files with 61 additions and 91 deletions
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
#![feature(rand, vec_push_all, duration, duration_span)]
|
||||
|
||||
use std::iter::repeat;
|
||||
use std::mem::swap;
|
||||
use std::env;
|
||||
use std::__rand::{thread_rng, Rng};
|
||||
|
|
@ -56,7 +55,7 @@ fn maybe_run_test<F>(argv: &[String], name: String, test: F) where F: FnOnce() {
|
|||
}
|
||||
|
||||
fn shift_push() {
|
||||
let mut v1 = repeat(1).take(30000).collect::<Vec<_>>();
|
||||
let mut v1 = vec![1; 30000];
|
||||
let mut v2 = Vec::new();
|
||||
|
||||
while !v1.is_empty() {
|
||||
|
|
@ -70,7 +69,7 @@ fn vec_plus() {
|
|||
let mut v = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < 1500 {
|
||||
let rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>();
|
||||
let rv = vec![i; r.gen_range(0, i + 1)];
|
||||
if r.gen() {
|
||||
v.extend(rv);
|
||||
} else {
|
||||
|
|
@ -88,7 +87,7 @@ fn vec_append() {
|
|||
let mut v = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < 1500 {
|
||||
let rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>();
|
||||
let rv = vec![i; r.gen_range(0, i + 1)];
|
||||
if r.gen() {
|
||||
let mut t = v.clone();
|
||||
t.push_all(&rv);
|
||||
|
|
@ -108,7 +107,7 @@ fn vec_push_all() {
|
|||
|
||||
let mut v = Vec::new();
|
||||
for i in 0..1500 {
|
||||
let mut rv = repeat(i).take(r.gen_range(0, i + 1)).collect::<Vec<_>>();
|
||||
let mut rv = vec![i; r.gen_range(0, i + 1)];
|
||||
if r.gen() {
|
||||
v.push_all(&rv);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ use std::cmp::min;
|
|||
use std::env;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::iter::repeat;
|
||||
|
||||
const LINE_LEN: usize = 60;
|
||||
const LOOKUP_SIZE: usize = 4 * 1024;
|
||||
|
|
@ -121,7 +120,7 @@ impl<'a, W: Write> RepeatFasta<'a, W> {
|
|||
|
||||
fn make(&mut self, n: usize) -> io::Result<()> {
|
||||
let alu_len = self.alu.len();
|
||||
let mut buf = repeat(0).take(alu_len + LINE_LEN).collect::<Vec<_>>();
|
||||
let mut buf = vec![0; alu_len + LINE_LEN];
|
||||
let alu: &[u8] = self.alu.as_bytes();
|
||||
|
||||
for (slot, val) in buf.iter_mut().zip(alu) {
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@
|
|||
|
||||
#![feature(iter_cmp)]
|
||||
|
||||
use std::iter::repeat;
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::channel;
|
||||
use std::thread;
|
||||
|
|
@ -221,7 +220,7 @@ fn get_id(m: u64) -> u8 {
|
|||
|
||||
// Converts a list of mask to a Vec<u8>.
|
||||
fn to_vec(raw_sol: &List<u64>) -> Vec<u8> {
|
||||
let mut sol = repeat('.' as u8).take(50).collect::<Vec<_>>();
|
||||
let mut sol = vec![b'.'; 50];
|
||||
for &m in raw_sol.iter() {
|
||||
let id = '0' as u8 + get_id(m);
|
||||
for i in 0..50 {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@
|
|||
#![allow(non_snake_case)]
|
||||
#![feature(unboxed_closures, iter_arith, core_simd, scoped)]
|
||||
|
||||
use std::iter::repeat;
|
||||
use std::thread;
|
||||
use std::env;
|
||||
use std::simd::f64x2;
|
||||
|
|
@ -62,7 +61,7 @@ fn main() {
|
|||
|
||||
fn spectralnorm(n: usize) -> f64 {
|
||||
assert!(n % 2 == 0, "only even lengths are accepted");
|
||||
let mut u = repeat(1.0).take(n).collect::<Vec<_>>();
|
||||
let mut u = vec![1.0; n];
|
||||
let mut v = u.clone();
|
||||
let mut tmp = v.clone();
|
||||
for _ in 0..10 {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@
|
|||
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
use std::iter::repeat;
|
||||
use std::env;
|
||||
|
||||
// Computes a single solution to a given 9x9 sudoku
|
||||
|
|
@ -59,8 +58,7 @@ impl Sudoku {
|
|||
reader.read_line(&mut s).unwrap();
|
||||
assert_eq!(s, "9,9\n");
|
||||
|
||||
let mut g = repeat(vec![0, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
.take(10).collect::<Vec<_>>();
|
||||
let mut g = vec![vec![0, 0, 0, 0, 0, 0, 0, 0, 0]; 10];
|
||||
for line in reader.lines() {
|
||||
let line = line.unwrap();
|
||||
let comps: Vec<&str> = line
|
||||
|
|
|
|||
|
|
@ -68,12 +68,9 @@ impl Drop for AsciiArt {
|
|||
// If there is a canonical constructor it is typically named the same as the type.
|
||||
// Other constructor sort of functions are typically named from_foo, from_bar, etc.
|
||||
fn AsciiArt(width: usize, height: usize, fill: char) -> AsciiArt {
|
||||
// Use an anonymous function to build a vector of vectors containing
|
||||
// blank characters for each position in our canvas.
|
||||
let mut lines = Vec::new();
|
||||
for _ in 0..height {
|
||||
lines.push(repeat('.').take(width).collect::<Vec<_>>());
|
||||
}
|
||||
// Build a vector of vectors containing blank characters for each position in
|
||||
// our canvas.
|
||||
let lines = vec![vec!['.'; width]; height];
|
||||
|
||||
// Rust code often returns values by omitting the trailing semi-colon
|
||||
// instead of using an explicit return statement.
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ extern crate alloc;
|
|||
|
||||
use alloc::heap;
|
||||
use std::ptr;
|
||||
use std::iter::repeat;
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
|
|
@ -29,7 +28,7 @@ fn main() {
|
|||
|
||||
unsafe fn test_triangle() -> bool {
|
||||
static COUNT : usize = 16;
|
||||
let mut ascend = repeat(ptr::null_mut()).take(COUNT).collect::<Vec<_>>();
|
||||
let mut ascend = vec![ptr::null_mut(); COUNT];
|
||||
let ascend = &mut *ascend;
|
||||
static ALIGN : usize = 1;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue