Решение на упр.10 задача 1 от Мариян Момчилов
Към профила на Мариян Момчилов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::{sync::Mutex, thread::scope};
enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn filter(s: &[i32], locks: &[Mutex<bool>], cond: fn(i32) -> bool) {
for (i, m) in s.iter().zip(locks) {
let mut guard = m.lock().unwrap();
if cond(*i) {
*guard = true;
} else {
*guard = false;
}
}
}
fn greater_100(s: &[i32], locks: &[Mutex<bool>]) {
filter(s, locks, |i| i > 100)
}
fn even(s: &[i32], locks: &[Mutex<bool>]) {
filter(s, locks, |i| i % 2 == 0)
}
fn prime(s: &[i32], locks: &[Mutex<bool>]) {
filter(s, locks, |i| {
for j in 2..i {
if i % j == 0 {
return false;
}
}
true
})
}
fn div7(s: &[i32], locks: &[Mutex<bool>]) {
filter(s, locks, |i| i % 7 == 0)
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
if n_threads == 0 {
return data;
}
let pred = match predicate {
Predicate::Even => even,
Predicate::GreaterThan100 => greater_100,
Predicate::DivisibleBy7 => div7,
Predicate::Prime => prime,
};
let mut locks = Vec::new();
for _ in data.iter() {
locks.push(Mutex::new(false));
}
let items_per_thread = data.len() / n_threads;
scope(|s| {
for th in 0..n_threads {
let start = th * items_per_thread;
let end = (th + 1) * items_per_thread
+ if th == n_threads - 1 {
data.len() % n_threads
} else {
0
};
let _slice_ref = &data[start..end];
let _locks_ref = locks[start..end].as_ref();
s.spawn(|| pred(_slice_ref, _locks_ref));
}
});
data.into_iter()
.zip(locks.into_iter())
.filter(|(_, lock)| *lock.lock().unwrap())
.map(|(v, _)| v)
.collect()
}
Лог от изпълнението
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 futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling pin-utils v0.1.0
Compiling syn v2.0.111
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling pin-project-lite v0.2.16
Compiling solution v0.1.0 (/tmp/d20251218-1757769-1uspvzg/solution)
warning: enum `Predicate` is never used
--> src/lib.rs:3:6
|
3 | enum Predicate {
| ^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function `filter` is never used
--> src/lib.rs:10:4
|
10 | fn filter(s: &[i32], locks: &[Mutex<bool>], cond: fn(i32) -> bool) {
| ^^^^^^
warning: function `greater_100` is never used
--> src/lib.rs:21:4
|
21 | fn greater_100(s: &[i32], locks: &[Mutex<bool>]) {
| ^^^^^^^^^^^
warning: function `even` is never used
--> src/lib.rs:25:4
|
25 | fn even(s: &[i32], locks: &[Mutex<bool>]) {
| ^^^^
warning: function `prime` is never used
--> src/lib.rs:29:4
|
29 | fn prime(s: &[i32], locks: &[Mutex<bool>]) {
| ^^^^^
warning: function `div7` is never used
--> src/lib.rs:40:4
|
40 | fn div7(s: &[i32], locks: &[Mutex<bool>]) {
| ^^^^
warning: function `parallel_filter` is never used
--> src/lib.rs:44:4
|
44 | fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
| ^^^^^^^^^^^^^^^
warning: `solution` (lib) generated 7 warnings
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.48s
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
