Решение на упр.10 задача 1 от Илиян Копривчин

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

Към профила на Илиян Копривчин

Резултати

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

Код

enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn matches_predicate(value: i32, predicate: &Predicate) -> bool {
match predicate {
Predicate::GreaterThan100 => value > 100,
Predicate::Even => value % 2 == 0,
Predicate::DivisibleBy7 => value % 7 == 0,
Predicate::Prime => is_prime(value),
}
}
fn is_prime(value: i32) -> bool {
if value <= 1 {
return false;
}
let limit: i32 = (value as f64).sqrt() as i32;
let mut divisor: i32 = 2;
while divisor <= limit {
if value % divisor == 0 {
return false;
}
divisor += 1;
}
true
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
let data_len: usize = data.len();
let chunk_size: usize = (data_len + n_threads - 1) / n_threads;
let mut handles: Vec<std::thread::JoinHandle<Vec<i32>>> = Vec::new();
let shared_predicate: std::sync::Arc<Predicate> =
std::sync::Arc::new(predicate);
let shared_data: std::sync::Arc<Vec<i32>> =
std::sync::Arc::new(data);
let mut thread_index: usize = 0;
while thread_index < n_threads {
let start: usize = thread_index * chunk_size;
let end: usize = std::cmp::min(start + chunk_size, data_len);
if start >= end {
break;
}
let predicate_clone: std::sync::Arc<Predicate> =
std::sync::Arc::clone(&shared_predicate);
let data_clone: std::sync::Arc<Vec<i32>> =
std::sync::Arc::clone(&shared_data);
let handle: std::thread::JoinHandle<Vec<i32>> =
std::thread::spawn(move || {
let mut result: Vec<i32> = Vec::new();
let mut index: usize = start;
while index < end {
let value: i32 = data_clone[index];
if matches_predicate(value, &predicate_clone) {
result.push(value);
}
index += 1;
}
result
});
handles.push(handle);
thread_index += 1;
}
let mut final_result: Vec<i32> = Vec::new();
for handle in handles {
let mut partial: Vec<i32> = handle.join().unwrap();
final_result.append(&mut partial);
}
final_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 slab v0.4.11
   Compiling syn v2.0.111
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251218-1757769-fx0706/solution)
warning: enum `Predicate` is never used
 --> src/lib.rs:1:6
  |
1 | enum Predicate {
  |      ^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

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

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

warning: function `parallel_filter` is never used
  --> src/lib.rs:35:4
   |
35 | 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.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

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

Илиян качи първо решение на 17.12.2025 23:51 (преди 1 ден)