From 60ed7c3574d67dac7192b27eaf69ca371b95c772 Mon Sep 17 00:00:00 2001 From: Lorenzo Cimini Date: Mon, 11 Nov 2024 17:21:09 +0100 Subject: [PATCH] reorganizing --- benches/benchmarks/model4encoder_building.rs | 5 +- src/ans/model4encoder_builder.rs | 2 +- .../entropy_estimator.rs} | 69 +---------- src/bvgraph/estimators/log2_estimator.rs | 65 ++++++++++ src/bvgraph/estimators/mod.rs | 2 + .../bvgraph_decoder_factory.rs} | 36 +----- .../factories/bvgraphseq_decoder_factory.rs | 33 +++++ src/bvgraph/factories/mod.rs | 2 + src/bvgraph/mod.rs | 20 ++-- src/bvgraph/random_access.rs | 8 +- src/bvgraph/sequential.rs | 2 +- .../{writer.rs => writers/bvgraph_encoder.rs} | 113 +----------------- src/bvgraph/writers/bvgraph_model_builder.rs | 109 +++++++++++++++++ src/bvgraph/writers/mod.rs | 2 + src/lib.rs | 3 - tests/test_bvgraph.rs | 11 +- 16 files changed, 250 insertions(+), 232 deletions(-) rename src/bvgraph/{mock_writers.rs => estimators/entropy_estimator.rs} (74%) create mode 100644 src/bvgraph/estimators/log2_estimator.rs create mode 100644 src/bvgraph/estimators/mod.rs rename src/bvgraph/{reader.rs => factories/bvgraph_decoder_factory.rs} (61%) create mode 100644 src/bvgraph/factories/bvgraphseq_decoder_factory.rs create mode 100644 src/bvgraph/factories/mod.rs rename src/bvgraph/{writer.rs => writers/bvgraph_encoder.rs} (63%) create mode 100644 src/bvgraph/writers/bvgraph_model_builder.rs create mode 100644 src/bvgraph/writers/mod.rs diff --git a/benches/benchmarks/model4encoder_building.rs b/benches/benchmarks/model4encoder_building.rs index 1494569..475a1ae 100644 --- a/benches/benchmarks/model4encoder_building.rs +++ b/benches/benchmarks/model4encoder_building.rs @@ -1,10 +1,11 @@ use criterion::{criterion_group, BatchSize, Criterion}; use dsi_bitstream::prelude::BE; -use folded_streaming_rans::bvgraph::mock_writers::{EntropyEstimator, Log2Estimator}; -use folded_streaming_rans::bvgraph::writer::BVGraphModelBuilder; +use folded_streaming_rans::bvgraph::estimators::entropy_estimator::EntropyEstimator; +use folded_streaming_rans::bvgraph::writers::bvgraph_model_builder::BVGraphModelBuilder; use pprof::criterion::{Output, PProfProfiler}; use webgraph::graphs::{BVComp, BVGraph}; use webgraph::prelude::SequentialLabeling; +use folded_streaming_rans::bvgraph::estimators::log2_estimator::Log2Estimator; fn model4encoder_building_bench(c: &mut Criterion) { let graph = BVGraph::with_basename("tests/data/cnr-2000/cnr-2000") diff --git a/src/ans/model4encoder_builder.rs b/src/ans/model4encoder_builder.rs index e2ea946..4644fa2 100644 --- a/src/ans/model4encoder_builder.rs +++ b/src/ans/model4encoder_builder.rs @@ -110,7 +110,7 @@ impl ANSModel4EncoderBuilder { false => n.next_power_of_two(), }; - // We need the list of symbols' indexes sorted by the frequency of the related + // We need the list of symbols indexes sorted by the frequency of the related // symbol, in ascending order. let sorted_indexes = folded_sym_freqs .iter() diff --git a/src/bvgraph/mock_writers.rs b/src/bvgraph/estimators/entropy_estimator.rs similarity index 74% rename from src/bvgraph/mock_writers.rs rename to src/bvgraph/estimators/entropy_estimator.rs index 3fbc63c..a898cb4 100644 --- a/src/bvgraph/mock_writers.rs +++ b/src/bvgraph/estimators/entropy_estimator.rs @@ -1,9 +1,9 @@ -use std::convert::Infallible; use webgraph::prelude::Encode; +use std::convert::Infallible; use crate::ans::model4encoder::ANSModel4Encoder; +use crate::{Freq, Symbol, MAX_RAW_SYMBOL}; use crate::bvgraph::BVGraphComponent; use crate::utils::ans_utils::fold_without_streaming_out; -use crate::{Freq, Symbol, MAX_RAW_SYMBOL}; #[derive(Clone)] pub struct EntropyEstimator { @@ -18,6 +18,7 @@ pub struct EntropyEstimator { } impl EntropyEstimator { + pub fn new(model: &ANSModel4Encoder, component_args: Vec<(usize, usize)>) -> Self { let mut folding_thresholds = Vec::new(); let mut folding_offsets = Vec::new(); @@ -151,66 +152,4 @@ impl Encode for EntropyEstimator { fn end_node(&mut self, _node: usize) -> Result { Ok(0) } -} - -/// An estimator that simply returns the cost of each symbol calculated as the log2 of the value plus 2. -#[derive(Clone, Default)] -pub struct Log2Estimator {} - -impl Log2Estimator { - fn get_symbol_cost(&self, value: u64, _component: BVGraphComponent) -> usize { - u64::ilog2(value + 2) as usize - } -} - -impl Encode for Log2Estimator { - type Error = Infallible; - - fn start_node(&mut self, _node: usize) -> Result { - Ok(0) - } - - fn write_outdegree(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::Outdegree)) - } - - fn write_reference_offset(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::ReferenceOffset)) - } - - fn write_block_count(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::BlockCount)) - } - - fn write_block(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::Blocks)) - } - - fn write_interval_count(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::IntervalCount)) - } - - fn write_interval_start(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::IntervalStart)) - } - - fn write_interval_len(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::IntervalLen)) - } - - fn write_first_residual(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::FirstResidual)) - } - - fn write_residual(&mut self, value: u64) -> Result { - Ok(self.get_symbol_cost(value, BVGraphComponent::Residual)) - } - - fn flush(&mut self) -> Result { - Ok(0) - } - - fn end_node(&mut self, _node: usize) -> Result { - Ok(0) - } -} +} \ No newline at end of file diff --git a/src/bvgraph/estimators/log2_estimator.rs b/src/bvgraph/estimators/log2_estimator.rs new file mode 100644 index 0000000..ae1a83b --- /dev/null +++ b/src/bvgraph/estimators/log2_estimator.rs @@ -0,0 +1,65 @@ +use webgraph::prelude::Encode; +use std::convert::Infallible; +use crate::bvgraph::BVGraphComponent; + +/// An estimator that simply returns the cost of each symbol calculated as the log2 of the value plus 2. +#[derive(Clone, Default)] +pub struct Log2Estimator {} + +impl Log2Estimator { + fn get_symbol_cost(&self, value: u64, _component: BVGraphComponent) -> usize { + u64::ilog2(value + 2) as usize + } +} + +impl Encode for Log2Estimator { + type Error = Infallible; + + fn start_node(&mut self, _node: usize) -> Result { + Ok(0) + } + + fn write_outdegree(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::Outdegree)) + } + + fn write_reference_offset(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::ReferenceOffset)) + } + + fn write_block_count(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::BlockCount)) + } + + fn write_block(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::Blocks)) + } + + fn write_interval_count(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::IntervalCount)) + } + + fn write_interval_start(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::IntervalStart)) + } + + fn write_interval_len(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::IntervalLen)) + } + + fn write_first_residual(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::FirstResidual)) + } + + fn write_residual(&mut self, value: u64) -> Result { + Ok(self.get_symbol_cost(value, BVGraphComponent::Residual)) + } + + fn flush(&mut self) -> Result { + Ok(0) + } + + fn end_node(&mut self, _node: usize) -> Result { + Ok(0) + } +} \ No newline at end of file diff --git a/src/bvgraph/estimators/mod.rs b/src/bvgraph/estimators/mod.rs new file mode 100644 index 0000000..5edafd4 --- /dev/null +++ b/src/bvgraph/estimators/mod.rs @@ -0,0 +1,2 @@ +pub mod log2_estimator; +pub mod entropy_estimator; \ No newline at end of file diff --git a/src/bvgraph/reader.rs b/src/bvgraph/factories/bvgraph_decoder_factory.rs similarity index 61% rename from src/bvgraph/reader.rs rename to src/bvgraph/factories/bvgraph_decoder_factory.rs index 7d86a95..46cb1c1 100644 --- a/src/bvgraph/reader.rs +++ b/src/bvgraph/factories/bvgraph_decoder_factory.rs @@ -1,9 +1,8 @@ +use webgraph::prelude::RandomAccessDecoderFactory; use crate::ans::decoder::ANSDecoder; use crate::ans::model4decoder::ANSModel4Decoder; use crate::ans::Prelude; use crate::EF; -use anyhow::Result; -use webgraph::prelude::{RandomAccessDecoderFactory, SequentialDecoderFactory}; pub struct ANSBVGraphDecoderFactory { /// The EliasFano containing the stream pointers for each of the nodes. @@ -37,7 +36,7 @@ impl ANSBVGraphDecoderFactory { impl RandomAccessDecoderFactory for ANSBVGraphDecoderFactory { type Decoder<'b> = ANSDecoder<'b> where Self: 'b; - fn new_decoder(&self, node: usize) -> Result> { + fn new_decoder(&self, node: usize) -> anyhow::Result> { // nodes' phases are stored in reversed order. Thus, for example, let's // take the last phase if we want the phase of the first node. let pointer = self.phases.get(self.num_nodes - node - 1); @@ -50,33 +49,4 @@ impl RandomAccessDecoderFactory for ANSBVGraphDecoderFactory { state, )) } -} - -pub struct ANSBVGraphSeqDecoderFactory { - /// The prelude resulting from the encoding process of the graph. - prelude: Prelude, - - /// The ANSModel4Decoder used by the decoder to decode the graph. - model: ANSModel4Decoder, -} - -impl ANSBVGraphSeqDecoderFactory { - pub fn new(prelude: Prelude) -> Self { - Self { - model: ANSModel4Decoder::new(&prelude.tables), - prelude, - } - } -} - -impl SequentialDecoderFactory for ANSBVGraphSeqDecoderFactory { - type Decoder<'b> = ANSDecoder<'b> where Self: 'b; - - fn new_decoder(&self) -> Result> { - Ok(ANSDecoder::new( - &self.model, - &self.prelude.stream, - self.prelude.state, - )) - } -} +} \ No newline at end of file diff --git a/src/bvgraph/factories/bvgraphseq_decoder_factory.rs b/src/bvgraph/factories/bvgraphseq_decoder_factory.rs new file mode 100644 index 0000000..248bc51 --- /dev/null +++ b/src/bvgraph/factories/bvgraphseq_decoder_factory.rs @@ -0,0 +1,33 @@ +use webgraph::prelude::SequentialDecoderFactory; +use crate::ans::decoder::ANSDecoder; +use crate::ans::model4decoder::ANSModel4Decoder; +use crate::ans::Prelude; + +pub struct ANSBVGraphSeqDecoderFactory { + /// The prelude resulting from the encoding process of the graph. + prelude: Prelude, + + /// The ANSModel4Decoder used by the decoder to decode the graph. + model: ANSModel4Decoder, +} + +impl ANSBVGraphSeqDecoderFactory { + pub fn new(prelude: Prelude) -> Self { + Self { + model: ANSModel4Decoder::new(&prelude.tables), + prelude, + } + } +} + +impl SequentialDecoderFactory for ANSBVGraphSeqDecoderFactory { + type Decoder<'b> = ANSDecoder<'b> where Self: 'b; + + fn new_decoder(&self) -> anyhow::Result> { + Ok(ANSDecoder::new( + &self.model, + &self.prelude.stream, + self.prelude.state, + )) + } +} \ No newline at end of file diff --git a/src/bvgraph/factories/mod.rs b/src/bvgraph/factories/mod.rs new file mode 100644 index 0000000..7c12255 --- /dev/null +++ b/src/bvgraph/factories/mod.rs @@ -0,0 +1,2 @@ +pub mod bvgraphseq_decoder_factory; +pub mod bvgraph_decoder_factory; \ No newline at end of file diff --git a/src/bvgraph/mod.rs b/src/bvgraph/mod.rs index 384b064..f9e0299 100644 --- a/src/bvgraph/mod.rs +++ b/src/bvgraph/mod.rs @@ -1,13 +1,12 @@ use std::fmt::Display; -pub mod mock_writers; pub mod random_access; -pub mod reader; +pub mod factories; pub mod sequential; -pub mod writer; +pub mod estimators; +pub mod writers; -/// An enumeration of the components getting a different model in the Rust -/// implementation of the BV format. +/// An enumeration of the components composing the BVGraph format. #[derive(Clone, Copy, Debug)] pub enum BVGraphComponent { Outdegree, @@ -21,6 +20,12 @@ pub enum BVGraphComponent { Residual, } +impl BVGraphComponent { + + /// The number of components in the BVGraph format. + pub const COMPONENTS: usize = 9; +} + impl Display for BVGraphComponent { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -53,8 +58,3 @@ impl From for BVGraphComponent { } } } - -impl BVGraphComponent { - /// The number of components in the BVGraph format. - pub const COMPONENTS: usize = 9; -} diff --git a/src/bvgraph/random_access.rs b/src/bvgraph/random_access.rs index 8cf6688..14bfca4 100644 --- a/src/bvgraph/random_access.rs +++ b/src/bvgraph/random_access.rs @@ -1,7 +1,7 @@ use crate::ans::{ANSCompressorPhase, Prelude}; -use crate::bvgraph::mock_writers::{EntropyEstimator, Log2Estimator}; -use crate::bvgraph::reader::ANSBVGraphDecoderFactory; -use crate::bvgraph::writer::{ANSBVGraphEncodeAndEstimate, BVGraphModelBuilder}; +use crate::bvgraph::estimators::entropy_estimator::EntropyEstimator; +use crate::bvgraph::factories::bvgraph_decoder_factory::ANSBVGraphDecoderFactory; +use crate::bvgraph::writers::bvgraph_encoder::ANSBVGraphEncodeAndEstimate; use crate::{State, EF}; use anyhow::{Context, Result}; use dsi_bitstream::prelude::BE; @@ -14,6 +14,8 @@ use std::io::BufWriter; use std::path::PathBuf; use sux::dict::EliasFanoBuilder; use webgraph::prelude::{BvComp, BvGraph, BvGraphSeq, SequentialLabeling}; +use crate::bvgraph::estimators::log2_estimator::Log2Estimator; +use crate::bvgraph::writers::bvgraph_model_builder::BVGraphModelBuilder; /// An ANS-encoded BVGraph that can be accessed both randomly and sequentially. pub struct ANSBVGraph(); diff --git a/src/bvgraph/sequential.rs b/src/bvgraph/sequential.rs index bcb17d8..3b19e05 100644 --- a/src/bvgraph/sequential.rs +++ b/src/bvgraph/sequential.rs @@ -1,5 +1,5 @@ use crate::ans::Prelude; -use crate::bvgraph::reader::ANSBVGraphSeqDecoderFactory; +use crate::bvgraph::factories::bvgraphseq_decoder_factory::ANSBVGraphSeqDecoderFactory; use epserde::prelude::*; use std::path::PathBuf; use webgraph::prelude::BvGraphSeq; diff --git a/src/bvgraph/writer.rs b/src/bvgraph/writers/bvgraph_encoder.rs similarity index 63% rename from src/bvgraph/writer.rs rename to src/bvgraph/writers/bvgraph_encoder.rs index 396e3ee..518354e 100644 --- a/src/bvgraph/writer.rs +++ b/src/bvgraph/writers/bvgraph_encoder.rs @@ -1,118 +1,13 @@ +use webgraph::prelude::{Encode, EncodeAndEstimate}; use std::convert::Infallible; use tempfile::{Builder, NamedTempFile}; -use webgraph::prelude::{Encode, EncodeAndEstimate}; +use crate::ans::{ANSCompressorPhase, Prelude}; use crate::ans::encoder::ANSEncoder; use crate::ans::model4encoder::ANSModel4Encoder; -use crate::ans::model4encoder_builder::ANSModel4EncoderBuilder; -use crate::ans::{ANSCompressorPhase, Prelude}; -use crate::bvgraph::mock_writers::EntropyEstimator; use crate::bvgraph::BVGraphComponent; +use crate::bvgraph::estimators::entropy_estimator::EntropyEstimator; use crate::utils::rev::RevBuffer; -/// An [`Encoder`] that writes to an [`ANSModel4EncoderBuilder`]. to collect data for each -/// -/// Data for each [component](BVGraphComponent) is pushed into the [`ANSModel4EncoderBuilder`]. The [`ANSModel4Encoder`] -/// is then built from the collected data. -pub struct BVGraphModelBuilder { - model_builder: ANSModel4EncoderBuilder, - - /// The type of the mock writer used by this builder. It may either be a `Log2Estimator` or an `EntropyEstimator`. - mock: MW, -} - -impl BVGraphModelBuilder { - pub fn new(mock: MW) -> Self { - Self { - model_builder: ANSModel4EncoderBuilder::default(), - mock, - } - } - - /// Build an [`ANSModel4Encoder`] from the symbols written to this - /// [`BVGraphModelBuilder`]. - pub fn build(self) -> ANSModel4Encoder { - self.model_builder.build() - } -} - -impl EncodeAndEstimate for BVGraphModelBuilder { - type Estimator<'a> = &'a mut MW where Self: 'a; - - fn estimator(&mut self) -> Self::Estimator<'_> { - &mut self.mock - } -} - -impl Encode for BVGraphModelBuilder { - type Error = Infallible; - - fn start_node(&mut self, _node: usize) -> Result { - Ok(0) - } - - fn write_outdegree(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::Outdegree); - Ok(self.mock.write_outdegree(value).unwrap()) - } - - fn write_reference_offset(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::ReferenceOffset); - Ok(self.mock.write_reference_offset(value).unwrap()) - } - - fn write_block_count(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::BlockCount); - Ok(self.mock.write_block_count(value).unwrap()) - } - - fn write_block(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::Blocks); - Ok(self.mock.write_block(value).unwrap()) - } - - fn write_interval_count(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::IntervalCount); - Ok(self.mock.write_interval_count(value).unwrap()) - } - - fn write_interval_start(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::IntervalStart); - Ok(self.mock.write_interval_start(value).unwrap()) - } - - fn write_interval_len(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::IntervalLen); - Ok(self.mock.write_interval_len(value).unwrap()) - } - - fn write_first_residual(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::FirstResidual); - Ok(self.mock.write_first_residual(value).unwrap()) - } - - fn write_residual(&mut self, value: u64) -> Result { - self.model_builder - .push_symbol(value, BVGraphComponent::Residual); - Ok(self.mock.write_residual(value).unwrap()) - } - - fn flush(&mut self) -> Result { - Ok(0) - } - - fn end_node(&mut self, _node: usize) -> Result { - Ok(0) - } -} - /// An [`Encoder`] that writes to an [`ANSEncoder`]. pub struct ANSBVGraphEncodeAndEstimate { /// A buffer containing a [`ANSCompressorPhase`], one for each node. @@ -275,4 +170,4 @@ impl Encode for ANSBVGraphEncodeAndEstimate { fn end_node(&mut self, _node: usize) -> Result { Ok(0) } -} +} \ No newline at end of file diff --git a/src/bvgraph/writers/bvgraph_model_builder.rs b/src/bvgraph/writers/bvgraph_model_builder.rs new file mode 100644 index 0000000..409fac2 --- /dev/null +++ b/src/bvgraph/writers/bvgraph_model_builder.rs @@ -0,0 +1,109 @@ +use webgraph::prelude::{Encode, EncodeAndEstimate}; +use std::convert::Infallible; +use crate::ans::model4encoder::ANSModel4Encoder; +use crate::ans::model4encoder_builder::ANSModel4EncoderBuilder; +use crate::bvgraph::BVGraphComponent; + +/// An [`Encoder`] that writes to an [`ANSModel4EncoderBuilder`]. to collect data for each +/// +/// Data for each [component](BVGraphComponent) is pushed into the [`ANSModel4EncoderBuilder`]. The [`ANSModel4Encoder`] +/// is then built from the collected data. +pub struct BVGraphModelBuilder { + model_builder: ANSModel4EncoderBuilder, + + /// The type of the mock writer used by this builder. It may either be a `Log2Estimator` or an `EntropyEstimator`. + mock: MW, +} + +impl BVGraphModelBuilder { + pub fn new(mock: MW) -> Self { + Self { + model_builder: ANSModel4EncoderBuilder::default(), + mock, + } + } + + /// Build an [`ANSModel4Encoder`] from the symbols written to this + /// [`BVGraphModelBuilder`]. + pub fn build(self) -> ANSModel4Encoder { + self.model_builder.build() + } +} + +impl EncodeAndEstimate for BVGraphModelBuilder { + type Estimator<'a> = &'a mut MW where Self: 'a; + + fn estimator(&mut self) -> Self::Estimator<'_> { + &mut self.mock + } +} + +impl Encode for BVGraphModelBuilder { + type Error = Infallible; + + fn start_node(&mut self, _node: usize) -> Result { + Ok(0) + } + + fn write_outdegree(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::Outdegree); + Ok(self.mock.write_outdegree(value).unwrap()) + } + + fn write_reference_offset(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::ReferenceOffset); + Ok(self.mock.write_reference_offset(value).unwrap()) + } + + fn write_block_count(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::BlockCount); + Ok(self.mock.write_block_count(value).unwrap()) + } + + fn write_block(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::Blocks); + Ok(self.mock.write_block(value).unwrap()) + } + + fn write_interval_count(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::IntervalCount); + Ok(self.mock.write_interval_count(value).unwrap()) + } + + fn write_interval_start(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::IntervalStart); + Ok(self.mock.write_interval_start(value).unwrap()) + } + + fn write_interval_len(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::IntervalLen); + Ok(self.mock.write_interval_len(value).unwrap()) + } + + fn write_first_residual(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::FirstResidual); + Ok(self.mock.write_first_residual(value).unwrap()) + } + + fn write_residual(&mut self, value: u64) -> Result { + self.model_builder + .push_symbol(value, BVGraphComponent::Residual); + Ok(self.mock.write_residual(value).unwrap()) + } + + fn flush(&mut self) -> Result { + Ok(0) + } + + fn end_node(&mut self, _node: usize) -> Result { + Ok(0) + } +} \ No newline at end of file diff --git a/src/bvgraph/writers/mod.rs b/src/bvgraph/writers/mod.rs new file mode 100644 index 0000000..99180bf --- /dev/null +++ b/src/bvgraph/writers/mod.rs @@ -0,0 +1,2 @@ +pub mod bvgraph_model_builder; +pub mod bvgraph_encoder; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d00912b..4997e04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,6 @@ mod traits; /// The parameter with the same name in Duda's paper. In this case we store the logarithm of the /// parameter since, if b = 2^k, we extract/insert k bits from the state at once. -/// -/// Having said that, in this project b is fixed to be 32 in order to extract/insert 32 bits from/to -/// the state at once. pub const B: usize = 16; /// The maximum symbol that can be encoded/decoded. diff --git a/tests/test_bvgraph.rs b/tests/test_bvgraph.rs index 9c5a51d..490e30a 100644 --- a/tests/test_bvgraph.rs +++ b/tests/test_bvgraph.rs @@ -1,18 +1,19 @@ use anyhow::Result; use std::iter::Iterator; -use folded_streaming_rans::bvgraph::reader::{ - ANSBVGraphDecoderFactory, ANSBVGraphSeqDecoderFactory, -}; -use folded_streaming_rans::bvgraph::writer::{ANSBVGraphEncodeAndEstimate, BVGraphModelBuilder}; +use folded_streaming_rans::bvgraph::factories::bvgraph_decoder_factory::ANSBVGraphDecoderFactory; +use folded_streaming_rans::bvgraph::writers::bvgraph_encoder::ANSBVGraphEncodeAndEstimate; use dsi_bitstream::prelude::BE; -use folded_streaming_rans::bvgraph::mock_writers::{EntropyEstimator, Log2Estimator}; +use folded_streaming_rans::bvgraph::estimators::entropy_estimator::EntropyEstimator; use folded_streaming_rans::bvgraph::random_access::ANSBVGraph; use folded_streaming_rans::bvgraph::sequential::ANSBVGraphSeq; use folded_streaming_rans::State; use lender::for_; use webgraph::prelude::*; +use folded_streaming_rans::bvgraph::estimators::log2_estimator::Log2Estimator; +use folded_streaming_rans::bvgraph::factories::bvgraphseq_decoder_factory::ANSBVGraphSeqDecoderFactory; +use folded_streaming_rans::bvgraph::writers::bvgraph_model_builder::BVGraphModelBuilder; #[test] fn decodes_correctly_dummy_graph() -> Result<()> {