Skip to main content

bixverse_core/utils/
mod.rs

1//! All type of utility modules with shared code that does not fit fully into
2//! other modules. Has assertion macros, structures designed for the heap,
3//! traits and their implementations, R <> Rust interface functions and more.
4
5pub mod gemm;
6pub mod heap_structures;
7pub mod macros;
8pub mod matrix_utils;
9pub mod simd;
10pub mod traits;
11pub mod vec_utils;
12
13use faer::Par;
14use rustc_hash::{FxBuildHasher, FxHashSet};
15use std::num::NonZeroUsize;
16use std::sync::OnceLock;
17
18///////////////////
19// General utils //
20///////////////////
21
22/// String slice to FxHashSet
23///
24/// ### Params
25///
26/// * `x` - The string slice.
27///
28/// ### Returns
29///
30/// A HashSet with borrowed String values
31pub fn string_vec_to_set(x: &[String]) -> FxHashSet<&String> {
32    let mut set = FxHashSet::with_capacity_and_hasher(x.len(), FxBuildHasher);
33    for s in x {
34        set.insert(s);
35    }
36    set
37}
38
39/// Returns the faer parallelism enum (all available cores via Rayon).
40#[inline]
41pub fn faer_parallelism() -> Par {
42    static N_THREADS: OnceLock<NonZeroUsize> = OnceLock::new();
43    Par::Rayon(*N_THREADS.get_or_init(|| std::thread::available_parallelism().unwrap()))
44}