Решение на упр.10 задача 1 от Владимир Великов

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

Към профила на Владимир Великов

Резултати

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

Код

use std::sync::Arc;
use std::thread;
#[derive(Clone, Copy)]
enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn is_prime(n: i32) -> bool {
if n <= 1 { return false; }
let limit = (n as f64).sqrt() as i32;
for i in 2..=limit {
if n % i == 0 { return false; }
}
true
}
fn check_predicate(val: i32, predicate: Predicate) -> bool {
match predicate {
Predicate::GreaterThan100 => val > 100,
Predicate::Even => val % 2 == 0,
Predicate::Prime => is_prime(val),
Predicate::DivisibleBy7 => val % 7 == 0,
}
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
let shared_data = Arc::new(data);
let length = shared_data.len();
let chunk_size = (length + n_threads - 1) / n_threads;
let mut handles = Vec::with_capacity(n_threads);
for i in 0..n_threads {
let data_ref = Arc::clone(&shared_data);
handles.push(thread::spawn(move || {
let start = i * chunk_size;
let end = std::cmp::min(start + chunk_size, length);
let mut local_result = Vec::new();
if start >= length {
return local_result;
}
for idx in start..end {
let val = data_ref[idx];
if check_predicate(val, predicate) {
local_result.push(val);
}
}
local_result
}));
}
let mut final_result = Vec::new();
for handle in handles {
let thread_result = handle.join().unwrap();
final_result.extend(thread_result);
}
final_result
}

Лог от изпълнението

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.42
   Compiling unicode-ident v1.0.22
   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 futures-task v0.3.31
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251218-1757769-1bxcao9/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(n: i32) -> bool {
   |    ^^^^^^^^

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

warning: function `parallel_filter` is never used
  --> src/lib.rs:30:4
   |
30 | 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.58s
     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 01:57 (преди 2 дена)