geographdb-core 0.1.0

Geometric graph database core - 3D spatial indexing for code analysis
Documentation
//! Compute backend trait for GPU acceleration

use glam::Vec3;

/// Distance metric for spatial queries
#[derive(Debug, Clone, Copy)]
pub enum DistanceMetric {
    Euclidean,
    Manhattan,
    Chebyshev,
}

/// Bitset operation type
#[derive(Debug, Clone, Copy)]
pub enum BitsetOp {
    And,
    Or,
    Xor,
}

/// Compute backend trait (CPU or GPU)
pub trait ComputeBackend: Send + Sync {
    /// Compute distance between two points
    fn distance(&self, a: &Vec3, b: &Vec3, metric: DistanceMetric) -> f32;

    /// Batch distance computation
    fn distance_batch(&self, points: &[Vec3], query: &Vec3, metric: DistanceMetric) -> Vec<f32>;

    /// Bitset operation
    fn bitset_op(&self, a: &[u64], b: &[u64], op: BitsetOp) -> Vec<u64>;
}

/// CPU backend (default)
pub struct CpuBackend;

impl ComputeBackend for CpuBackend {
    fn distance(&self, a: &Vec3, b: &Vec3, _metric: DistanceMetric) -> f32 {
        // Default to Euclidean
        (*a - *b).length()
    }

    fn distance_batch(&self, points: &[Vec3], query: &Vec3, _metric: DistanceMetric) -> Vec<f32> {
        points.iter().map(|p| (*p - *query).length()).collect()
    }

    fn bitset_op(&self, a: &[u64], b: &[u64], op: BitsetOp) -> Vec<u64> {
        match op {
            BitsetOp::And => a.iter().zip(b.iter()).map(|(x, y)| x & y).collect(),
            BitsetOp::Or => a.iter().zip(b.iter()).map(|(x, y)| x | y).collect(),
            BitsetOp::Xor => a.iter().zip(b.iter()).map(|(x, y)| x ^ y).collect(),
        }
    }
}