Решение на упр.10 задача 1 от Георги Стоянов

Обратно към всички решения

Към профила на Георги Стоянов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 2 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::sync::Arc;
use std::thread;
#[derive(Clone, Copy, Debug)]
enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn is_prime(x: i32) -> bool {
if x <= 1 {
return false;
}
if x == 2 {
return true;
}
if x % 2 == 0 {
return false;
}
let mut d: i32 = 3;
while (d as i64) * (d as i64) <= x as i64 {
if x % d == 0 {
return false;
}
d += 2;
}
true
}
fn matches_predicate(x: i32, predicate: Predicate) -> bool {
match predicate {
Predicate::GreaterThan100 => x > 100,
Predicate::Even => x % 2 == 0,
Predicate::Prime => is_prime(x),
Predicate::DivisibleBy7 => x % 7 == 0,
}
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
let len = data.len();
if len == 0 {
return Vec::new();
}
let n = n_threads.max(1);
let shared = Arc::new(data);
let chunk_size = (len + n - 1) / n;
let mut handles = Vec::new();
for t in 0..n {
let start = t * chunk_size;
if start >= len {
break;
}
let end = (start + chunk_size).min(len);
let shared_clone = Arc::clone(&shared);
let pred = predicate;
let handle = thread::spawn(move || {
let mut out = Vec::new();
for &x in &shared_clone[start..end] {
if matches_predicate(x, pred) {
out.push(x);
}
}
out
});
handles.push(handle);
}
let mut result = Vec::new();
for h in handles {
let part = h.join().expect("Thread panicked");
result.extend(part);
}
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 futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling syn v2.0.111
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251218-1757769-1oz3rv9/solution)
warning: enum `Predicate` is never used
 --> src/lib.rs:5:6
  |
5 | enum Predicate {
  |      ^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `is_prime` is never used
  --> src/lib.rs:12:4
   |
12 | fn is_prime(x: i32) -> bool {
   |    ^^^^^^^^

warning: function `matches_predicate` is never used
  --> src/lib.rs:33:4
   |
33 | fn matches_predicate(x: i32, predicate: Predicate) -> bool {
   |    ^^^^^^^^^^^^^^^^^

warning: function `parallel_filter` is never used
  --> src/lib.rs:42:4
   |
42 | fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
   |    ^^^^^^^^^^^^^^^

warning: `solution` (lib) generated 4 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.57s
     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

История (1 версия и 0 коментара)

Георги качи първо решение на 17.12.2025 16:40 (преди 1 ден)