Skip to content

Commit 5aadef0

Browse files
authored
implement 'BitStore' for usize (#41)
1 parent 544160c commit 5aadef0

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

arithmetic-coding-core/src/bitstore.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::ops::{Add, AddAssign, Div, Mul, Shl, ShlAssign, Sub};
55
pub trait BitStore:
66
Shl<u32, Output = Self>
77
+ ShlAssign<u32>
8-
+ Sized
9-
+ From<u32>
108
+ Sub<Output = Self>
119
+ Add<Output = Self>
1210
+ Mul<Output = Self>
@@ -46,3 +44,4 @@ macro_rules! impl_bitstore {
4644
impl_bitstore! {u32}
4745
impl_bitstore! {u64}
4846
impl_bitstore! {u128}
47+
impl_bitstore! {usize}

examples/sherlock.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ impl StringModel {
2424
pub struct Error(char);
2525

2626
impl Model for StringModel {
27+
type B = usize;
2728
type Symbol = char;
2829
type ValueError = Error;
2930

30-
fn probability(&self, symbol: Option<&Self::Symbol>) -> Result<Range<u32>, Error> {
31+
fn probability(&self, symbol: Option<&Self::Symbol>) -> Result<Range<usize>, Error> {
3132
if let Some(char) = symbol {
3233
match self.alphabet.iter().position(|x| x == char) {
33-
Some(index) => Ok((index as u32)..(index as u32 + 1)),
34+
Some(index) => Ok(index..(index + 1)),
3435
None => Err(Error(*char)),
3536
}
3637
} else {
37-
let alphabet_length = self.alphabet.len() as u32;
38+
let alphabet_length = self.alphabet.len();
3839
Ok(alphabet_length..(alphabet_length + 1))
3940
}
4041
}
4142

42-
fn symbol(&self, value: u32) -> Option<Self::Symbol> {
43-
self.alphabet.get(value as usize).copied()
43+
fn symbol(&self, value: usize) -> Option<Self::Symbol> {
44+
self.alphabet.get(value).copied()
4445
}
4546

46-
fn max_denominator(&self) -> u32 {
47-
self.alphabet.len() as u32 + 1
47+
fn max_denominator(&self) -> usize {
48+
self.alphabet.len() + 1
4849
}
4950
}
5051

fenwick-model/src/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Builder {
2929
Self { model }
3030
}
3131

32-
pub fn panic_on_saturation(mut self) -> Self {
32+
pub const fn panic_on_saturation(mut self) -> Self {
3333
self.model.panic_on_saturation = true;
3434
self
3535
}

src/encoder.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ where
8181
}
8282
}
8383

84-
/// todo
84+
/// Create an encoder from an existing [`State`].
85+
///
86+
/// This is useful for manually chaining a shared buffer through multiple
87+
/// encoders.
8588
pub const fn with_state(state: State<'a, M::B, W>, model: M) -> Self {
8689
Self { model, state }
8790
}
@@ -185,7 +188,10 @@ where
185188
B: BitStore,
186189
W: BitWrite,
187190
{
188-
/// todo
191+
/// Manually construct a [`State`].
192+
///
193+
/// Normally this would be done automatically using the [`Encoder::new`]
194+
/// method.
189195
pub fn new(precision: u32, output: &'a mut W) -> Self {
190196
let low = B::ZERO;
191197
let high = B::ONE << precision;
@@ -228,7 +234,6 @@ where
228234
self.high <<= 1;
229235
self.low <<= 1;
230236
} else {
231-
// self.low >= self.half()
232237
self.emit(true)?;
233238
self.low = (self.low - self.half()) << 1;
234239
self.high = (self.high - self.half()) << 1;
@@ -253,7 +258,11 @@ where
253258
Ok(())
254259
}
255260

256-
/// todo
261+
/// Flush the internal buffer and write all remaining bits to the output
262+
///
263+
/// # Errors
264+
///
265+
/// This method can fail if the output cannot be written to
257266
pub fn flush(&mut self) -> io::Result<()> {
258267
self.pending += 1;
259268
if self.low <= self.quarter() {

tests/sherlock.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ pub struct StringModel;
1515
pub struct Error(char);
1616

1717
impl Model for StringModel {
18+
type B = usize;
1819
type Symbol = char;
1920
type ValueError = Error;
2021

2122
fn probability(&self, symbol: Option<&Self::Symbol>) -> Result<Range<Self::B>, Error> {
2223
if let Some(char) = symbol {
2324
match ALPHABET.chars().position(|x| &x == char) {
24-
Some(index) => Ok((index as u32)..(index as u32 + 1)),
25+
Some(index) => Ok(index..(index + 1)),
2526
None => Err(Error(*char)),
2627
}
2728
} else {
28-
let alphabet_length = ALPHABET.len() as u32;
29-
Ok(alphabet_length..(alphabet_length + 1))
29+
Ok(ALPHABET.len()..(ALPHABET.len() + 1))
3030
}
3131
}
3232

3333
fn symbol(&self, value: Self::B) -> Option<Self::Symbol> {
34-
ALPHABET.chars().nth(value as usize)
34+
ALPHABET.chars().nth(value)
3535
}
3636

3737
fn max_denominator(&self) -> Self::B {
38-
ALPHABET.len() as u32 + 1
38+
ALPHABET.len() + 1
3939
}
4040
}
4141

0 commit comments

Comments
 (0)