Handle near-converged state
This commit is contained in:
parent
5a3bfa9618
commit
b4da1bdc13
29
src/main.rs
29
src/main.rs
@ -16,21 +16,46 @@ fn get_symbol(model: &Model, low: f64, high: f64) -> Option<u8> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn encode(input: &[u8], model: &Model) -> Vec<u8> {
|
fn encode(input: &[u8], model: &Model) -> Vec<u8> {
|
||||||
|
const HALF: u64 = 1 << (u64::BITS - 1);
|
||||||
|
const LOW_CONVERGE: u64 = 0b10 << (u64::BITS - 2);
|
||||||
|
const HIGH_CONVERGE: u64 = 0b01 << (u64::BITS - 2);
|
||||||
|
|
||||||
let mut output = BitWriter::new();
|
let mut output = BitWriter::new();
|
||||||
|
|
||||||
let mut high = u64::MAX;
|
let mut high = u64::MAX;
|
||||||
let mut low = u64::MIN;
|
let mut low = u64::MIN;
|
||||||
|
let mut pending_bits = 0;
|
||||||
|
|
||||||
for symbol in input {
|
for symbol in input {
|
||||||
let range = high - low;
|
let range = high - low;
|
||||||
let p = model.get(symbol).expect("Invalid/Unsupported data");
|
let p = model.get(symbol).expect("Invalid/Unsupported data");
|
||||||
high = low + (range as f64 * p.1) as u64;
|
high = low + (range as f64 * p.1) as u64;
|
||||||
low = low + (range as f64 * p.0) as u64;
|
low = low + (range as f64 * p.0) as u64;
|
||||||
loop {
|
loop {
|
||||||
if high < 1 << 63 {
|
if high < HALF {
|
||||||
output.write(false);
|
output.write(false);
|
||||||
print!("0");
|
print!("0");
|
||||||
} else if low >= 1 << (u64::BITS - 1) {
|
while pending_bits > 0 {
|
||||||
output.write(true);
|
output.write(true);
|
||||||
print!("1");
|
print!("1");
|
||||||
|
pending_bits -= 1;
|
||||||
|
}
|
||||||
|
} else if low >= HALF {
|
||||||
|
output.write(true);
|
||||||
|
print!("1");
|
||||||
|
while pending_bits > 0 {
|
||||||
|
output.write(true);
|
||||||
|
print!("0");
|
||||||
|
pending_bits -= 1;
|
||||||
|
}
|
||||||
|
} else if low >= LOW_CONVERGE && high < HIGH_CONVERGE {
|
||||||
|
println!("BET");
|
||||||
|
pending_bits += 1;
|
||||||
|
low <<= 1;
|
||||||
|
low &= HALF - 1;
|
||||||
|
high <<= 1;
|
||||||
|
high &= HALF + 1;
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user