@@ -4,13 +4,15 @@ use arithmetic_coding::fixed_length;
44
55mod common;
66
7+ /// The possible symbols
78#[ derive( Debug ) ]
89pub enum Symbol {
910 A ,
1011 B ,
1112 C ,
1213}
1314
15+ /// A model for encoding/decoding a set of 3 symbols from {A, B, C}
1416#[ derive( Clone ) ]
1517pub struct MyModel ;
1618
@@ -19,6 +21,11 @@ impl fixed_length::Model for MyModel {
1921 type Symbol = Symbol ;
2022 type ValueError = Infallible ;
2123
24+ /// Given a symbol, return a unique interval representing the probability of
25+ /// that symbol occurring.
26+ ///
27+ /// Since the number of symbols in the message is fixed, no 'stop' symbol is
28+ /// needed.
2229 fn probability ( & self , symbol : & Self :: Symbol ) -> Result < Range < u32 > , Self :: ValueError > {
2330 match symbol {
2431 Symbol :: A => Ok ( 0 ..1 ) ,
@@ -27,6 +34,7 @@ impl fixed_length::Model for MyModel {
2734 }
2835 }
2936
37+ /// For decoding, for each possible value, a symbol is returned.
3038 fn symbol ( & self , value : u32 ) -> Self :: Symbol {
3139 match value {
3240 0 ..1 => Symbol :: A ,
@@ -36,10 +44,21 @@ impl fixed_length::Model for MyModel {
3644 }
3745 }
3846
47+ /// The maximum denominator used for probability ranges.
48+ ///
49+ /// The trait also includes a 'denominator' method, which is allowed to vary
50+ /// for each symbol, but must never exceed `max_denominator`.
51+ /// For non-adaptive models, this value is the same as `max_denominator`
52+ /// (and this is the default value of the trait method).
3953 fn max_denominator ( & self ) -> u32 {
4054 3
4155 }
4256
57+ /// The total number of symbols to encode.
58+ ///
59+ /// Because the number of symbols is known ahead of time, we can take
60+ /// advantage of this to optimise the compression by removing the need
61+ /// for a 'stop' symbol.
4362 fn length ( & self ) -> usize {
4463 3
4564 }
0 commit comments