Решение на упр.10 задача 1 от Йоан Грозев
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::thread;
#[derive(Clone, Copy)]
enum Predicate
{
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn isPrime(n: i32) -> bool
{
if n < 2
{
return false;
}
for i in (2..=n/2)
{
if n % i == 0
{
return false;
}
}
true
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32>
{
if data.is_empty() || n_threads == 0
{
return Vec::new();
}
let len = data.len();
let chunkSize = (len + n_threads - 1) / n_threads;
let mut handles = Vec::new();
for chunk in data.chunks(chunkSize)
{
let chunk = chunk.to_vec();
let pred = predicate;
let handle = thread::spawn(move ||
{
chunk.into_iter().filter(|&x| match pred
{
Predicate::GreaterThan100 => x > 100,
Predicate::Even => x % 2 == 0,
Predicate::Prime => isPrime(x),
Predicate::DivisibleBy7 => x % 7 == 0,
}).collect::<Vec<i32>>()
});
handles.push(handle);
}
let mut result = Vec::new();
for handle in handles
{
let mut partial = handle.join().expect("Thread panicked!");
result.append(&mut partial);
}
result
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.103
Compiling unicode-ident v1.0.22
Compiling quote v1.0.42
Compiling futures-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling pin-utils v0.1.0
Compiling slab v0.4.11
Compiling syn v2.0.111
Compiling pin-project-lite v0.2.16
Compiling memchr v2.7.6
Compiling futures-io v0.3.31
Compiling futures-task v0.3.31
Compiling solution v0.1.0 (/tmp/d20251218-1757769-1wnk1t8/solution)
warning: unnecessary parentheses around `for` iterator expression
--> src/lib.rs:18:14
|
18 | for i in (2..=n/2)
| ^ ^
|
= note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
|
18 - for i in (2..=n/2)
18 + for i in 2..=n/2
|
warning: enum `Predicate` is never used
--> src/lib.rs:4:6
|
4 | enum Predicate
| ^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function `isPrime` is never used
--> src/lib.rs:12:4
|
12 | fn isPrime(n: i32) -> bool
| ^^^^^^^
warning: function `parallel_filter` is never used
--> src/lib.rs:28:4
|
28 | fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32>
| ^^^^^^^^^^^^^^^
warning: function `isPrime` should have a snake case name
--> src/lib.rs:12:4
|
12 | fn isPrime(n: i32) -> bool
| ^^^^^^^ help: convert the identifier to snake case: `is_prime`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `chunkSize` should have a snake case name
--> src/lib.rs:36:9
|
36 | let chunkSize = (len + n_threads - 1) / n_threads;
| ^^^^^^^^^ help: convert the identifier to snake case: `chunk_size`
warning: `solution` (lib) generated 6 warnings (run `cargo fix --lib -p solution` to apply 1 suggestion)
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
warning: unnecessary parentheses around `for` iterator expression
--> tests/../src/lib.rs:18:14
|
18 | for i in (2..=n/2)
| ^ ^
|
= note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
|
18 - for i in (2..=n/2)
18 + for i in 2..=n/2
|
warning: function `isPrime` should have a snake case name
--> tests/../src/lib.rs:12:4
|
12 | fn isPrime(n: i32) -> bool
| ^^^^^^^ help: convert the identifier to snake case: `is_prime`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `chunkSize` should have a snake case name
--> tests/../src/lib.rs:36:9
|
36 | let chunkSize = (len + n_threads - 1) / n_threads;
| ^^^^^^^^^ help: convert the identifier to snake case: `chunk_size`
warning: `solution` (test "solution_test") generated 3 warnings (run `cargo fix --test "solution_test"` to apply 1 suggestion)
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.55s
Running tests/solution_test.rs (target/debug/deps/solution_test-ee0783488e12dce9)
running 2 tests
test solution_test::test_basic ... ok
test solution_test::test_uneven_split ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
