Guest User

sort-TXT-RUST3

a guest
Jul 8th, 2023
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.24 KB | Source Code | 0 0
  1. use std::{fs::OpenOptions, fs::File, time::Instant, path::Path};
  2. use memmap2::Mmap;
  3. use memmap2::MmapMut;
  4.  
  5. pub fn spease_one(input: impl AsRef<Path>, output: impl AsRef<Path>) -> std::io::Result<()> {
  6.     let (chars_to_write, total_len) = {
  7.         let f = File::open(input)?;
  8.         let fm = unsafe { Mmap::map(&f) }?;
  9.         #[cfg(unix)]
  10.         fm.advise(Advice::Sequential)?;
  11.    
  12.         let mut char_counter = [0u64; 256];
  13.         for c in fm.as_ref() {
  14.             *unsafe { char_counter.get_unchecked_mut(*c as usize) } += 1;
  15.         }
  16.    
  17.         (char_counter, fm.len())
  18.     };
  19.    
  20.     let of = OpenOptions::new().read(true).write(true).create(true).open(output)?;
  21.     of.set_len(total_len as u64)?;
  22.     let mut ofm = unsafe { MmapMut::map_mut(&of) }?;
  23.     let mut writer = ofm.as_mut();
  24.     for (c,v) in chars_to_write.into_iter().enumerate().map(|(c,v)| (c as u8, v as usize)) {
  25.         if c != b' ' {
  26.             let (begin, end) = writer.split_at_mut(v);
  27.             begin.fill(c);
  28.             writer = end;
  29.         }
  30.     }
  31.    
  32.     writer.fill(b' ');
  33.    
  34.     Ok(())
  35. }
  36. fn main() {
  37.     let start = Instant::now();
  38.     spease_one("input.txt", "output").unwrap();
  39.     println!("Rust Time {:?}", start.elapsed());
  40. }
Tags: blazing
Advertisement
Add Comment
Please, Sign In to add comment