scirs2-fft 0.4.2

Fast Fourier Transform module for SciRS2 (scirs2-fft)
Documentation
//! Adaptive FFT cache system

use super::memory_manager::CacheEvictionPolicy;
use super::types::*;
use crate::error::FFTResult;
use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;
use std::time::Instant;

/// Adaptive FFT cache system
#[derive(Debug)]
#[allow(dead_code)]
pub struct AdaptiveFftCache<F: Float + Debug> {
    /// Cached FFT plans
    pub(crate) plan_cache: HashMap<PlanCacheKey, CachedPlan<F>>,
    /// Cache statistics
    pub cache_stats: CacheStatistics,
    /// Cache policy
    pub(crate) cache_policy: AdaptiveCachePolicy,
    /// Predictive prefetching system
    pub(crate) prefetch_system: PredictivePrefetchSystem<F>,
}

/// Key for plan cache
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct PlanCacheKey {
    /// Signal size
    pub size: usize,
    /// Signal dimensions
    pub dimensions: Vec<usize>,
    /// Algorithm type
    pub algorithm: FftAlgorithmType,
    /// Data type
    pub data_type: String,
}

/// Cached FFT plan
#[derive(Debug, Clone)]
pub struct CachedPlan<F: Float> {
    /// Plan data (serialized)
    pub plan_data: Vec<u8>,
    /// Creation time
    pub creation_time: Instant,
    /// Access count
    pub access_count: usize,
    /// Last access time
    pub last_access: Instant,
    /// Performance metrics
    pub performance_metrics: CachedPlanMetrics<F>,
}

/// Performance metrics for cached plans
#[derive(Debug, Clone)]
pub struct CachedPlanMetrics<F: Float> {
    /// Average execution time
    pub avg_execution_time: F,
    /// Memory usage
    pub memory_usage: usize,
    /// Accuracy score
    pub accuracy_score: F,
}

/// Cache statistics
#[derive(Debug, Default)]
pub struct CacheStatistics {
    /// Cache hit count
    pub hit_count: usize,
    /// Cache miss count
    pub miss_count: usize,
    /// Cache eviction count
    pub eviction_count: usize,
    /// Total cache size (bytes)
    pub total_cache_size: usize,
}

/// Adaptive cache policy
#[derive(Debug)]
#[allow(dead_code)]
pub struct AdaptiveCachePolicy {
    /// Base eviction policy
    pub(crate) base_policy: CacheEvictionPolicy,
    /// Adaptive parameters
    pub(crate) adaptive_params: CacheAdaptiveParams,
    /// Policy learning system
    pub(crate) policy_learning: PolicyLearningSystem,
}

/// Adaptive parameters for cache
#[derive(Debug, Clone)]
pub struct CacheAdaptiveParams {
    /// Hit ratio threshold for policy adaptation
    pub hit_ratio_threshold: f64,
    /// Memory pressure threshold
    pub memory_pressure_threshold: f64,
    /// Access pattern weight
    pub access_pattern_weight: f64,
    /// Temporal locality weight
    pub temporal_locality_weight: f64,
}

impl Default for CacheAdaptiveParams {
    fn default() -> Self {
        Self {
            hit_ratio_threshold: 0.8,
            memory_pressure_threshold: 0.9,
            access_pattern_weight: 0.7,
            temporal_locality_weight: 0.3,
        }
    }
}

/// Policy learning system for cache
#[derive(Debug)]
#[allow(dead_code)]
pub struct PolicyLearningSystem {
    /// Policy performance history
    pub(crate) policy_history: VecDeque<PolicyPerformanceRecord>,
    /// Learning parameters
    pub(crate) learning_params: PolicyLearningParams,
}

/// Policy performance record
#[derive(Debug, Clone)]
pub struct PolicyPerformanceRecord {
    /// Policy used
    pub policy: String,
    /// Hit ratio achieved
    pub hit_ratio: f64,
    /// Memory efficiency
    pub memory_efficiency: f64,
    /// Timestamp
    pub timestamp: Instant,
}

/// Policy learning parameters
#[derive(Debug, Clone)]
pub struct PolicyLearningParams {
    /// Learning rate
    pub learning_rate: f64,
    /// Exploration rate
    pub exploration_rate: f64,
    /// Memory window size
    pub memory_window: usize,
}

impl Default for PolicyLearningParams {
    fn default() -> Self {
        Self {
            learning_rate: 0.01,
            exploration_rate: 0.1,
            memory_window: 1000,
        }
    }
}

/// Predictive prefetching system
#[derive(Debug)]
#[allow(dead_code)]
pub struct PredictivePrefetchSystem<F: Float> {
    /// Access pattern predictor
    pub(crate) pattern_predictor: AccessPatternPredictor,
    /// Prefetch queue
    pub(crate) prefetch_queue: VecDeque<PrefetchRequest<F>>,
    /// Prefetch statistics
    pub(crate) prefetch_stats: PrefetchStatistics,
}

/// Access pattern predictor
#[derive(Debug)]
#[allow(dead_code)]
pub struct AccessPatternPredictor {
    /// Access sequence history
    pub(crate) access_history: VecDeque<PlanCacheKey>,
    /// Pattern models
    pub(crate) pattern_models: Vec<PatternModel>,
    /// Prediction accuracy
    pub(crate) prediction_accuracy: f64,
}

/// Pattern model for access prediction
#[derive(Debug, Clone)]
pub struct PatternModel {
    /// Model name
    pub name: String,
    /// Model parameters
    pub parameters: Vec<f64>,
    /// Model confidence
    pub confidence: f64,
}

/// Prefetch request
#[derive(Debug, Clone)]
pub struct PrefetchRequest<F: Float> {
    /// Plan to prefetch
    pub plan_key: PlanCacheKey,
    /// Prefetch priority
    pub priority: f64,
    /// Estimated access time
    pub estimated_access_time: Instant,
    /// Confidence in prediction
    pub confidence: F,
}

/// Prefetch statistics
#[derive(Debug, Default)]
pub struct PrefetchStatistics {
    /// Successful prefetches
    pub successful_prefetches: usize,
    /// Failed prefetches
    pub failed_prefetches: usize,
    /// Prefetch accuracy
    pub prefetch_accuracy: f64,
    /// Memory overhead from prefetching
    pub memory_overhead: usize,
}

impl<F: Float + Debug> AdaptiveFftCache<F> {
    pub fn new() -> FFTResult<Self> {
        Ok(Self {
            plan_cache: HashMap::new(),
            cache_stats: CacheStatistics::default(),
            cache_policy: AdaptiveCachePolicy::new()?,
            prefetch_system: PredictivePrefetchSystem::new()?,
        })
    }
}

impl AdaptiveCachePolicy {
    pub fn new() -> FFTResult<Self> {
        Ok(Self {
            base_policy: CacheEvictionPolicy::Adaptive,
            adaptive_params: CacheAdaptiveParams::default(),
            policy_learning: PolicyLearningSystem::new()?,
        })
    }
}

impl PolicyLearningSystem {
    pub fn new() -> FFTResult<Self> {
        Ok(Self {
            policy_history: VecDeque::new(),
            learning_params: PolicyLearningParams::default(),
        })
    }
}

impl<F: Float> PredictivePrefetchSystem<F> {
    pub fn new() -> FFTResult<Self> {
        Ok(Self {
            pattern_predictor: AccessPatternPredictor::new()?,
            prefetch_queue: VecDeque::new(),
            prefetch_stats: PrefetchStatistics::default(),
        })
    }
}

impl AccessPatternPredictor {
    pub fn new() -> FFTResult<Self> {
        Ok(Self {
            access_history: VecDeque::new(),
            pattern_models: Vec::new(),
            prediction_accuracy: 0.0,
        })
    }
}