#[allow(
unused_imports,
reason = "a split file takes its parent's imports whole; each uses some"
)]
use super::*;
pub trait SourceStore<S: KernelScalar>: Clone + fmt::Debug + Send + Sync + 'static {
type Saved: KernelScalar;
fn saved(&self) -> &TrainSources<Self::Saved>;
fn from_saved(
slots: &[DistanceSlot],
values: &[&[Self::Saved]],
n: usize,
) -> Result<Self, GprError>;
fn bind<'a>(
slots: &[DistanceSlot],
sources: impl IntoIterator<Item = DistanceSource<'a>>,
n: usize,
) -> Result<Self, GprError>;
fn storage(&self) -> &TrainSources<S>;
fn exact(&self) -> Option<&TrainSources<f64>>;
fn to_f64(&self) -> Result<Cow<'_, TrainSources<f64>>, GprError> {
widened(self.exact(), self.storage())
}
fn reserve_point(&mut self) -> Result<(), GprError>;
fn check_push(
&self,
cols: &dyn RectSlots<S>,
exact: &dyn RectSlots<f64>,
) -> Result<(), GprError>;
fn write_point(&mut self, cols: &dyn RectSlots<S>, exact: &dyn RectSlots<f64>);
fn remove_work(&self, index: usize) -> usize;
fn ready_to_change(&mut self) -> Result<(), GprError>;
fn check_remove(&self, index: usize) -> Result<(), GprError>;
fn remove_point(&mut self, index: usize);
}
pub(crate) fn widened<'a, S: KernelScalar>(
exact: Option<&'a TrainSources<f64>>,
storage: &TrainSources<S>,
) -> Result<Cow<'a, TrainSources<f64>>, GprError> {
match exact {
Some(exact) => Ok(Cow::Borrowed(exact)),
None => storage.to_f64().map(Cow::Owned),
}
}
impl<S: KernelScalar> SourceStore<S> for TrainSources<S> {
type Saved = S;
fn saved(&self) -> &TrainSources<S> {
self
}
fn from_saved(slots: &[DistanceSlot], values: &[&[S]], n: usize) -> Result<Self, GprError> {
Self::from_packed(slots, values, n)
}
fn bind<'a>(
slots: &[DistanceSlot],
sources: impl IntoIterator<Item = DistanceSource<'a>>,
n: usize,
) -> Result<Self, GprError> {
Self::bind(slots, sources, n)
}
fn storage(&self) -> &TrainSources<S> {
self
}
fn exact(&self) -> Option<&TrainSources<f64>> {
(self as &dyn Any).downcast_ref::<TrainSources<f64>>()
}
fn reserve_point(&mut self) -> Result<(), GprError> {
Self::reserve_point(self)
}
fn check_push(
&self,
cols: &dyn RectSlots<S>,
_exact: &dyn RectSlots<f64>,
) -> Result<(), GprError> {
Self::check_push(self, cols)
}
fn write_point(&mut self, cols: &dyn RectSlots<S>, _exact: &dyn RectSlots<f64>) {
Self::write_point(self, cols);
}
fn check_remove(&self, index: usize) -> Result<(), GprError> {
Self::check_remove(self, index)
}
fn remove_point(&mut self, index: usize) {
Self::remove_point(self, index);
}
fn remove_work(&self, index: usize) -> usize {
Self::remove_work(self, index)
}
fn ready_to_change(&mut self) -> Result<(), GprError> {
Self::ready_to_change(self)
}
}
#[derive(Clone, Debug, Default)]
pub struct RefinedSources {
storage: TrainSources<f32>,
exact: TrainSources<f64>,
}
impl SourceStore<f32> for RefinedSources {
type Saved = f64;
fn saved(&self) -> &TrainSources<f64> {
&self.exact
}
fn from_saved(slots: &[DistanceSlot], values: &[&[f64]], n: usize) -> Result<Self, GprError> {
let exact = TrainSources::from_packed(slots, values, n)?;
Ok(Self {
storage: exact.cast(slots)?,
exact,
})
}
fn bind<'a>(
slots: &[DistanceSlot],
sources: impl IntoIterator<Item = DistanceSource<'a>>,
n: usize,
) -> Result<Self, GprError> {
let exact = TrainSources::<f64>::bind(slots, sources, n)?;
Ok(Self {
storage: exact.cast(slots)?,
exact,
})
}
fn storage(&self) -> &TrainSources<f32> {
&self.storage
}
fn exact(&self) -> Option<&TrainSources<f64>> {
Some(&self.exact)
}
fn reserve_point(&mut self) -> Result<(), GprError> {
self.storage.reserve_point()?;
self.exact.reserve_point()
}
fn check_push(
&self,
cols: &dyn RectSlots<f32>,
exact: &dyn RectSlots<f64>,
) -> Result<(), GprError> {
self.exact.check_push(exact)?;
self.storage.check_push(cols)
}
fn write_point(&mut self, cols: &dyn RectSlots<f32>, exact: &dyn RectSlots<f64>) {
self.exact.write_point(exact);
self.storage.write_point(cols);
}
fn check_remove(&self, index: usize) -> Result<(), GprError> {
self.storage.check_remove(index)?;
self.exact.check_remove(index)
}
fn remove_point(&mut self, index: usize) {
self.storage.remove_point(index);
self.exact.remove_point(index);
}
fn remove_work(&self, index: usize) -> usize {
self.storage
.remove_work(index)
.saturating_add(self.exact.remove_work(index))
}
fn ready_to_change(&mut self) -> Result<(), GprError> {
self.storage.ready_to_change()?;
self.exact.ready_to_change()
}
}