Skip to content

Commit 7ecf351

Browse files
committed
WIP wc command
1 parent 198bd6c commit 7ecf351

File tree

2 files changed

+68
-21
lines changed

2 files changed

+68
-21
lines changed

challenges/wc-command/src/lib.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
1-
use std::time::Instant;
2-
use std::io::{BufRead, BufReader};
3-
use std::fs::*;
1+
use core::num;
2+
use std::sync::atomic::AtomicUsize;
43
use rand::Rng;
5-
use rayon::prelude::*;
4+
use rayon::{prelude::*, vec};
5+
use rust_coding_challenges::utils::read_text_file_from_args;
6+
use std::cmp::min;
7+
use std::{fs::*, os};
8+
use std::io::{BufRead, BufReader, Read};
9+
use std::sync::{mpsc, Arc};
10+
use std::thread;
11+
use std::time::Instant;
12+
use std::sync::atomic::Ordering;
13+
14+
pub fn solve(text: String) -> usize {
15+
let num_threads = std::thread::available_parallelism().unwrap().get();
16+
count_words_parallel(text, num_threads)
17+
}
18+
19+
fn count_words_parallel(text: String, num_threads: usize) -> usize {
20+
let lines: Vec<String> = text.lines().map(|line| line.to_string()).collect();
21+
let lines_per_thread = (lines.len() + num_threads - 1) / num_threads;
22+
let total_word_count = Arc::new(AtomicUsize::new(0));
23+
let mut handles = Vec::new();
24+
25+
for i in 0..num_threads {
26+
let start = lines_per_thread * i;
27+
let end = min(start + lines_per_thread, lines.len());
28+
29+
let total_word_count_clone = Arc::clone(&total_word_count);
30+
let chunk = lines[start..end].to_vec();
631

7-
pub fn solve(file: File) -> usize {
8-
let reader = BufReader::new(file);
9-
let str = reader
10-
.lines()
11-
.filter_map(Result::ok)
12-
.collect::<Vec<String>>()
13-
.join("\n");
14-
count_words(&str)
32+
let handle = thread::spawn(move || {
33+
let count = count_words_in_chunk(&chunk);
34+
total_word_count_clone.fetch_add(count, Ordering::Relaxed)
35+
});
36+
handles.push(handle);
37+
}
38+
for handle in handles {
39+
handle.join().unwrap();
40+
}
41+
total_word_count.load(Ordering::Relaxed)
1542
}
1643

17-
fn count_words(text: &str) -> usize {
18-
text.lines()
19-
.flat_map(|f| f.split_whitespace())
20-
.count()
44+
fn count_words_in_chunk(chunk: &[String]) -> usize {
45+
chunk
46+
.iter()
47+
.flat_map(|line| line.split_whitespace())
48+
.count()
2149
}

challenges/wc-command/src/main.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11

22
use rust_coding_challenges::utils::read_text_file_from_args;
33
use wc_command::solve;
4-
use std::{fs::File, time::Instant};
4+
use std::{fs::File, io::{self, BufRead, BufReader, Read}, time::Instant};
55
fn main() -> std::io::Result<()> {
6+
// let start = Instant::now();
7+
let file_path = "/Users/namdo/Desktop/learntocodetogether/random_1m_lines.txt";
8+
let text = read_file_to_string(file_path)?;
69
let start = Instant::now();
7-
let file = File::open("/Users/namdo/Desktop/learntocodetogether/rust-coding-challenges/challenges/wc-command/tests/inputs/random_1m_lines.txt").expect("OK");
8-
let lines = solve(file);
9-
println!("total words: {:?}", lines);
10+
let wc = solve(text);
11+
println!("total words: {:?}", wc);
1012
println!("total time: {:?}", start.elapsed());
11-
// Print results or perform other actions as needed
13+
println!("OK: {}", 8503930 == wc);
1214
Ok(())
1315
}
16+
17+
18+
fn million_lines(n: usize, file_path: &str, new_file_path: &str) {
19+
20+
}
21+
22+
fn read_file_to_string(file_path: &str) -> io::Result<String>{
23+
let file = File::open(file_path)?;
24+
let mut reader = BufReader::new(file);
25+
let mut buffer = String::new();
26+
let mut content = String::new();
27+
while reader.read_line(&mut buffer)? > 0 {
28+
content.push_str(&buffer);
29+
buffer.clear();
30+
}
31+
Ok(content)
32+
}

0 commit comments

Comments
 (0)