imbalanced_core/
traits.rs

1// imbalanced-core/src/traits.rs
2use ndarray::{Array1, Array2, ArrayView1, ArrayView2};
3use std::marker::PhantomData;
4
5/// Error type for resampling operations
6#[derive(Debug, Clone)]
7pub enum ResamplingError {
8    /// Invalid input data
9    InvalidInput(String),
10    /// Insufficient samples for resampling
11    InsufficientSamples,
12    /// Configuration error
13    ConfigError(String),
14    /// Computation error
15    ComputationError(String),
16}
17
18impl std::fmt::Display for ResamplingError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            ResamplingError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
22            ResamplingError::InsufficientSamples => write!(f, "Insufficient samples for resampling"),
23            ResamplingError::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
24            ResamplingError::ComputationError(msg) => write!(f, "Computation error: {}", msg),
25        }
26    }
27}
28
29impl std::error::Error for ResamplingError {}
30
31/// Performance hint for consciousness-aligned optimization
32#[derive(Debug, Clone, Copy)]
33pub enum PerformanceHint {
34    /// Prefer cache-friendly access patterns
35    CacheFriendly,
36    /// Use SIMD when possible
37    Vectorize,
38    /// Parallelize operations
39    Parallel,
40    /// Use GPU acceleration if available
41    GpuAccelerated,
42}
43
44/// Collection of performance hints
45#[derive(Debug, Clone, Default)]
46pub struct PerformanceHints {
47    hints: Vec<PerformanceHint>,
48}
49
50impl PerformanceHints {
51    /// Create new performance hints
52    pub fn new() -> Self {
53        Self::default()
54    }
55    
56    /// Add a performance hint
57    pub fn with_hint(mut self, hint: PerformanceHint) -> Self {
58        self.hints.push(hint);
59        self
60    }
61    
62    /// Check if hint is present
63    pub fn has_hint(&self, hint: PerformanceHint) -> bool {
64        self.hints.iter().any(|&h| std::mem::discriminant(&h) == std::mem::discriminant(&hint))
65    }
66}
67
68/// Zero-cost abstraction for resampling strategies
69pub trait ResamplingStrategy: Send + Sync {
70    /// Input data type
71    type Input;
72    /// Output data type  
73    type Output;
74    /// Configuration type for this strategy
75    type Config;
76    
77    /// Resample the input data to balance class distribution
78    fn resample(
79        &self,
80        x: ArrayView2<f64>,
81        y: ArrayView1<i32>,
82        config: &Self::Config,
83    ) -> Result<(Array2<f64>, Array1<i32>), ResamplingError>;
84    
85    /// Consciousness-aligned performance hints
86    fn performance_hints(&self) -> PerformanceHints {
87        PerformanceHints::default()
88    }
89}
90
91/// Type-state pattern for compile-time correctness
92/// Uninitialized state
93pub struct Uninitialized;
94/// Configured state
95pub struct Configured;
96/// Fitted state  
97pub struct Fitted;
98
99/// Type-safe resampler with compile-time state checking
100pub struct Resampler<Strategy, State> {
101    strategy: Strategy,
102    _state: PhantomData<State>,
103}
104
105impl<S: ResamplingStrategy> Resampler<S, Uninitialized> {
106    /// Create a new uninitialized resampler
107    pub fn new(strategy: S) -> Self {
108        Self {
109            strategy,
110            _state: PhantomData,
111        }
112    }
113    
114    /// Configure the resampler, transitioning to configured state
115    pub fn configure(self, _config: S::Config) -> Resampler<S, Configured> {
116        // State transition enforced at compile time
117        Resampler {
118            strategy: self.strategy,
119            _state: PhantomData,
120        }
121    }
122}