use crate::{
codec::{codebook::Codebook, codec_config::CodecConfig, rotation_matrix::RotationMatrix},
errors::CodecError,
};
pub struct PreparedCodec {
config: CodecConfig,
codebook: Codebook,
rotation: RotationMatrix,
gpu_state: Option<alloc::boxed::Box<dyn core::any::Any + Send + Sync>>,
}
impl PreparedCodec {
pub fn new(config: CodecConfig, codebook: Codebook) -> Result<Self, CodecError> {
if codebook.bit_width() != config.bit_width() {
return Err(CodecError::CodebookIncompatible {
expected: config.bit_width(),
got: codebook.bit_width(),
});
}
let rotation = RotationMatrix::build(config.seed(), config.dimension());
Ok(Self {
config,
codebook,
rotation,
gpu_state: None,
})
}
#[inline]
pub const fn config(&self) -> &CodecConfig {
&self.config
}
#[inline]
pub const fn codebook(&self) -> &Codebook {
&self.codebook
}
#[inline]
pub const fn rotation(&self) -> &RotationMatrix {
&self.rotation
}
pub fn set_gpu_state(&mut self, state: alloc::boxed::Box<dyn core::any::Any + Send + Sync>) {
self.gpu_state = Some(state);
}
#[inline]
pub fn has_gpu_state(&self) -> bool {
self.gpu_state.is_some()
}
#[inline]
pub fn gpu_state(&self) -> Option<&(dyn core::any::Any + Send + Sync)> {
self.gpu_state.as_deref()
}
}