Skip to content

Commit

Permalink
Merge branch 'nothing-to-randomize' into pr21+pr22
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/squeeze.rs
  • Loading branch information
Pr0methean committed Jun 25, 2023
2 parents 11b26ec + c488d0f commit 875cdca
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/squeeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ impl Default for SymbolStats {
}

impl SymbolStats {

/// Calculates the entropy of each symbol, based on the counts of each symbol. The
/// result is similar to the result of length_limited_code_lengths, but with the
/// actual theoritical bit lengths according to the entropy. Since the resulting
Expand Down Expand Up @@ -509,17 +508,17 @@ pub fn lz77_optimal<C: Cache>(
stats.calculate_entropy();
}
if current_iteration > 5 && (cost - lastcost).abs() < f64::EPSILON {
if beststats
.litlens
// If they're all the same frequency, that frequency must be 1
// because of the end symbol. If that's the case, there's nothing
// to change by randomizing.
let can_randomize_litlens = beststats.litlens.iter().copied().any(|litlen| litlen != 1);
let can_randomize_dists = beststats
.dists
.iter()
.skip(1)
.copied()
.any(|litlen| litlen != beststats.litlens[0])
|| beststats
.dists
.iter()
.copied()
.any(|dist| dist != beststats.dists[0])
{
.any(|dist| dist != beststats.dists[0]);
if can_randomize_litlens || can_randomize_dists {
stats = beststats;
/// Returns true if it actually made a change.
fn randomize_freqs(freqs: &mut [usize], state: &mut RanState) -> bool {
Expand All @@ -541,15 +540,19 @@ pub fn lz77_optimal<C: Cache>(
}
let mut changed = false;
while !changed {
changed = randomize_freqs(&mut stats.litlens, &mut ran_state);

// Pull into a separate variable to prevent short-circuiting
let dists_changed = randomize_freqs(&mut stats.dists, &mut ran_state);
changed |= dists_changed;
if can_randomize_litlens {
changed = randomize_freqs(&mut stats.litlens, &mut ran_state);
}
if can_randomize_dists {
// Pull into a separate variable to prevent short-circuiting
let dists_changed = randomize_freqs(&mut stats.dists, &mut ran_state);
changed |= dists_changed;
}
}
stats.calculate_entropy();
lastrandomstep = current_iteration;
} else {
// No way to further improve the result
break;
}
}
Expand Down

0 comments on commit 875cdca

Please sign in to comment.