use crate::header::Header;
use d4_framefile::Directory;
use std::fs::File;
use std::io::Result;
mod bit_array;
pub const PRIMARY_TABLE_NAME: &str = ".ptab";
pub enum DecodeResult {
Definitely(i32),
Maybe(i32),
}
pub trait PrimaryTableWriter: Sized {
type Partition: PTablePartitionWriter;
fn create(directory: &mut Directory<File>, header: &Header) -> Result<Self>;
fn split(&mut self, header: &Header, size_limit: Option<usize>)
-> Result<Vec<Self::Partition>>;
}
pub trait PTablePartitionWriter: Send {
type EncoderType: Encoder;
fn make_encoder(&mut self) -> Self::EncoderType;
fn region(&self) -> (&str, u32, u32);
fn can_encode(&self, value: i32) -> bool;
fn bit_width(&self) -> usize;
}
pub trait Encoder {
fn encode(&mut self, pos: usize, value: i32) -> bool;
}
pub trait PrimaryTableReader: Sized {
type Partition: PrimaryTablePartReader + Send;
fn create(directory: &mut Directory<File>, header: &Header) -> Result<Self>;
fn split(&mut self, header: &Header, size_limit: Option<usize>)
-> Result<Vec<Self::Partition>>;
}
pub trait PrimaryTablePartReader: Send {
type DecoderType: Decoder;
fn make_decoder(&mut self) -> Self::DecoderType;
fn region(&self) -> (&str, u32, u32);
fn bit_width(&self) -> usize;
fn default_value(&self) -> Option<i32>;
}
pub trait DecodeBlockHandle {
fn handle(&mut self, pos: usize, result: DecodeResult);
}
impl<F: FnMut(usize, DecodeResult)> DecodeBlockHandle for F {
fn handle(&mut self, pos: usize, result: DecodeResult) {
self(pos, result)
}
}
pub trait Decoder {
fn decode(&mut self, pos: usize) -> DecodeResult;
fn decode_block<F: DecodeBlockHandle>(&mut self, pos: usize, count: usize, mut handle: F) {
for idx in 0..count {
handle.handle(pos + idx, self.decode(pos + idx));
}
}
}
pub type BitArrayWriter = bit_array::PrimaryTable<bit_array::Writer>;
pub type BitArrayReader = bit_array::PrimaryTable<bit_array::Reader>;
pub type BitArrayPartWriter = bit_array::PartialPrimaryTable<bit_array::Writer>;
pub type BitArrayPartReader = bit_array::PartialPrimaryTable<bit_array::Reader>;
pub type BitArrayDecoder = bit_array::PrimaryTableCodec<bit_array::Reader>;
pub type BitArrayEncoder = bit_array::PrimaryTableCodec<bit_array::Writer>;
pub use bit_array::MatrixDecoder;