Skip to main content

ipfrs_semantic/semantic_graph_builder/
functions.rs

1//! Auto-generated module
2//!
3//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
4
5use std::time::{SystemTime, UNIX_EPOCH};
6
7pub(super) fn now_secs() -> u64 {
8    SystemTime::now()
9        .duration_since(UNIX_EPOCH)
10        .map(|d| d.as_secs())
11        .unwrap_or(0)
12}
13#[allow(dead_code)]
14pub(super) fn xorshift64(state: &mut u64) -> u64 {
15    let mut x = *state;
16    x ^= x << 13;
17    x ^= x >> 7;
18    x ^= x << 17;
19    *state = x;
20    x
21}
22pub(super) fn cosine_similarity(a: &[f64], b: &[f64]) -> f64 {
23    if a.len() != b.len() || a.is_empty() {
24        return 0.0;
25    }
26    let dot: f64 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
27    let na: f64 = a.iter().map(|x| x * x).sum::<f64>().sqrt();
28    let nb: f64 = b.iter().map(|x| x * x).sum::<f64>().sqrt();
29    if na < 1e-10 || nb < 1e-10 {
30        0.0
31    } else {
32        (dot / (na * nb)).clamp(-1.0, 1.0)
33    }
34}