use arithmetic_coding_adder_dep::{Decoder, Encoder, Model}; use bitstream_io::{BigEndian, BitReader, BitWrite, BitWriter}; pub fn round_trip(model: M, input: &[M::Symbol]) where M: Model + Clone, M::Symbol: Copy + std::fmt::Debug + PartialEq, { let buffer = encode(model.clone(), input.iter().copied()); let mut output = Vec::with_capacity(input.len()); for symbol in decode(model, &buffer) { output.push(symbol); } assert_eq!(input, output.as_slice()); } pub fn encode(model: M, input: I) -> Vec where M: Model, I: IntoIterator, { let mut bitwriter = BitWriter::endian(Vec::new(), BigEndian); let mut encoder = Encoder::new(model, &mut bitwriter); encoder.encode_all(input).unwrap(); bitwriter.byte_align().unwrap(); bitwriter.into_writer() } pub fn decode(model: M, buffer: &[u8]) -> Vec where M: Model, { let bitreader = BitReader::endian(buffer, BigEndian); let mut decoder = Decoder::new(model, bitreader); decoder.decode_all().map(Result::unwrap).collect() }