imbalanced_core/
traits.rs1use ndarray::{Array1, Array2, ArrayView1, ArrayView2};
3use std::marker::PhantomData;
4
5#[derive(Debug, Clone)]
7pub enum ResamplingError {
8 InvalidInput(String),
10 InsufficientSamples,
12 ConfigError(String),
14 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#[derive(Debug, Clone, Copy)]
33pub enum PerformanceHint {
34 CacheFriendly,
36 Vectorize,
38 Parallel,
40 GpuAccelerated,
42}
43
44#[derive(Debug, Clone, Default)]
46pub struct PerformanceHints {
47 hints: Vec<PerformanceHint>,
48}
49
50impl PerformanceHints {
51 pub fn new() -> Self {
53 Self::default()
54 }
55
56 pub fn with_hint(mut self, hint: PerformanceHint) -> Self {
58 self.hints.push(hint);
59 self
60 }
61
62 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
68pub trait ResamplingStrategy: Send + Sync {
70 type Input;
72 type Output;
74 type Config;
76
77 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 fn performance_hints(&self) -> PerformanceHints {
87 PerformanceHints::default()
88 }
89}
90
91pub struct Uninitialized;
94pub struct Configured;
96pub struct Fitted;
98
99pub struct Resampler<Strategy, State> {
101 strategy: Strategy,
102 _state: PhantomData<State>,
103}
104
105impl<S: ResamplingStrategy> Resampler<S, Uninitialized> {
106 pub fn new(strategy: S) -> Self {
108 Self {
109 strategy,
110 _state: PhantomData,
111 }
112 }
113
114 pub fn configure(self, _config: S::Config) -> Resampler<S, Configured> {
116 Resampler {
118 strategy: self.strategy,
119 _state: PhantomData,
120 }
121 }
122}