-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
266 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,3 @@ edition = "2018" | |
|
||
[dependencies] | ||
thiserror = "1.0.20" | ||
log = "0.4.0" | ||
env_logger = "0.7.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
# range_coder_rust | ||
|
||
Simple implimention of rangecoder in rust language. | ||
Generic over probability models (eg. freq table) by `PModel(trait)`. | ||
|
||
## Installation | ||
|
||
Cargo.toml | ||
Add dependency to your Cargo.toml | ||
|
||
```toml | ||
[dependencies] | ||
range_coder = {git="https://github.com/diegodox/range_coder_rust.git", branch="carryless"} | ||
``` | ||
|
||
## Example | ||
|
||
You can run example by following command. | ||
|
||
These examples use the default syntax for running examples, as found in the [Cargo documentation](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#examples). | ||
|
||
```shell | ||
cargo run --example sample_impl | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use range_coder::{decoder::Decoder, encoder::Encoder, pmodel::PModel}; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
/// アルファベットの出現回数を示す構造体 | ||
struct AlphabetParam { | ||
/// 文字の累積出現頻度 | ||
cum: u32, | ||
/// 文字の出現頻度 | ||
c: u32, | ||
} | ||
struct FreqTable { | ||
/// 全アルファベットの出現頻度 | ||
total_freq: u32, | ||
/// アルファベットのパラメータを保持する配列 | ||
alphabet_params: Vec<AlphabetParam>, | ||
} | ||
|
||
impl PModel for FreqTable { | ||
fn c_freq(&self, index: usize) -> u32 { | ||
self.alphabet_params.get(index).unwrap().c | ||
} | ||
fn cum_freq(&self, index: usize) -> u32 { | ||
self.alphabet_params.get(index).unwrap().cum | ||
} | ||
fn total_freq(&self) -> u32 { | ||
self.total_freq | ||
} | ||
fn find_index(&self, decoder: &Decoder) -> usize { | ||
let mut left = 0; | ||
let mut right = self.alphabet_count() - 1; | ||
let rfreq = (decoder.data() - decoder.range_coder().lower_bound()) | ||
/ decoder.range_coder().range_par_total(self.total_freq()); | ||
while left < right { | ||
let mid = (left + right) / 2; | ||
let mid_cum = self.cum_freq(mid + 1); | ||
if mid_cum as u64 <= rfreq { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
left | ||
} | ||
} | ||
|
||
impl FreqTable { | ||
fn new(alphabet_count: usize) -> Self { | ||
FreqTable { | ||
total_freq: 0, | ||
alphabet_params: vec![AlphabetParam { cum: 0, c: 0 }; alphabet_count], | ||
} | ||
} | ||
fn alphabet_count(&self) -> usize { | ||
self.alphabet_params.len() | ||
} | ||
fn add_alphabet_freq(&mut self, alphabet_index: usize) { | ||
self.alphabet_params[alphabet_index].c += 1; | ||
} | ||
fn calc_cum(&mut self) { | ||
self.total_freq = self | ||
.alphabet_params | ||
.iter_mut() | ||
.fold(0, |cum_total, alphabet| { | ||
alphabet.cum = cum_total; | ||
cum_total + alphabet.c | ||
}) | ||
} | ||
} | ||
|
||
fn main() { | ||
// define test data | ||
let test_data = vec![2, 1, 1, 4, 1, 4, 2, 1, 0, 1, 5, 9, 8, 7, 6, 5]; | ||
|
||
// create freq-table | ||
let mut sd = FreqTable::new(10); | ||
for &i in &test_data { | ||
sd.add_alphabet_freq(i); | ||
} | ||
sd.calc_cum(); | ||
{ | ||
println!("FREQ TABLE"); | ||
for i in 0..sd.alphabet_params.len() { | ||
println!("index:{}, c:{}, cum:{}", i, sd.c_freq(i), sd.cum_freq(i)); | ||
} | ||
println!(); | ||
} | ||
|
||
// encode | ||
println!("ENCODING"); | ||
let mut encoder = Encoder::new(); | ||
print!("encode : "); | ||
for &i in &test_data { | ||
print!("{},", i); | ||
encoder.encode::<FreqTable>(&sd, i); | ||
} | ||
let code = encoder.finish(); | ||
{ | ||
println!(); | ||
print!("output : 0x"); | ||
for i in &code { | ||
print!("{:x}", i); | ||
} | ||
print!("\nlength : {}byte", code.len()); | ||
print!("\n\n"); | ||
} | ||
|
||
// decode | ||
let mut decoder = Decoder::new(code); | ||
println!("DECODING"); | ||
print!("decode : "); | ||
let decodeds = test_data | ||
.iter() | ||
.map(|_| { | ||
let decoded = decoder.decode(&sd); | ||
print!("{},", decoded); | ||
decoded | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
// test | ||
assert_eq!(decodeds, test_data); | ||
|
||
println!(); | ||
println!(); | ||
println!("test passed🎉"); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.