/// Run lengths and bytes are sampled from a uniform distribution.
/// Data created by this function achieves roughly 50% size reduction after compression.
Run lengths and bytes are sampled from a uniform distribution.
Data created by this function achieves roughly 50% size reduction after compression.
Run lengths and bytes are sampled from a uniform distribution . Data created by this function achieves roughly 50% size reduction after compression .
fn generate_compressible_data_bytes(rng: &mut SmallRng, size_bytes: u64) -> Vec<u8> {
    let mut bytes: Vec<u8> = Vec::with_capacity(size_bytes as usize);
    // The file is filled with compressible runs of one of the following bytes.
    let byte_choices: [u8; 6] = [0xde, 0xad, 0xbe, 0xef, 0x42, 0x0];
    let mut ptr = 0;
    while ptr < size_bytes {
        // A run is 10..1024 bytes long
        let mut run_length = rng.gen_range(10, 1024);
        // In case the run goes past the file size
        run_length = min(run_length, size_bytes - ptr);
        // Decide whether this run should be compressible or not.
        // This results in files that compress reasonably well (target 50% size reduction).
        if rng.gen_bool(0.5) {
            // Choose a byte for this run.
            let choice = byte_choices.choose(rng).unwrap();
            // Generate a run of compressible data.
            for _ in 0..run_length {
                bytes.push(*choice);
            }
        } else {
            // Generate a run of random data.
            for _ in 0..run_length {
                bytes.push(rng.gen());
            }
        }
        ptr += run_length;
    }
    // The file must be of the expected size
    assert!(bytes.len() == size_bytes as usize);
    bytes
}
fn generate_compressible_data_bytes ( rng : & mut SmallRng , size_bytes : u64 ) -> Vec < u8 > { let mut bytes : Vec < u8 > = Vec :: with_capacity ( size_bytes as usize ) ; // The file is filled with compressible runs of one of the following bytes. let byte_choices : [ u8 ; 6 ] = [ 0xde , 0xad , 0xbe , 0xef , 0x42 , 0x0 ] ; let mut ptr = 0 ; while ptr < size_bytes { // A run is 10..1024 bytes long let mut run_length = rng . gen_range ( 10 , 1024 ) ; // In case the run goes past the file size run_length = min ( run_length , size_bytes - ptr ) ; // Decide whether this run should be compressible or not. // This results in files that compress reasonably well (target 50% size reduction). if rng . gen_bool ( 0.5 ) { // Choose a byte for this run. let choice = byte_choices . choose ( rng ) . unwrap ( ) ; // Generate a run of compressible data. for _ in 0 .. run_length { bytes . push ( * choice ) ; } } else { // Generate a run of random data. for _ in 0 .. run_length { bytes . push ( rng . gen ( ) ) ; } } ptr += run_length ; } // The file must be of the expected size assert ! ( bytes len ( ) size_bytes as usize ) ; bytes }
Run lengths and bytes are sampled from a uniform distribution.
Run lengths and bytes are sampled from a uniform distribution .
rng
&mut SmallRng
size_bytes
u64