Normalize raw frequencies to sum to PROB_SCALE.
(freqs: &[u32; 256], total: usize)
| 263 | |
| 264 | /// Normalize raw frequencies to sum to PROB_SCALE. |
| 265 | fn normalize_frequencies(freqs: &[u32; 256], total: usize) -> [u32; 256] { |
| 266 | let mut norm = [0u32; 256]; |
| 267 | let mut sum = 0u32; |
| 268 | let total_f64 = total as f64; |
| 269 | |
| 270 | // First pass: proportional scaling. |
| 271 | for i in 0..256 { |
| 272 | if freqs[i] > 0 { |
| 273 | // Ensure every present symbol gets at least frequency 1. |
| 274 | norm[i] = ((freqs[i] as f64 / total_f64 * PROB_SCALE as f64).round() as u32).max(1); |
| 275 | sum += norm[i]; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Adjust to make sum exactly PROB_SCALE. |
| 280 | if sum > 0 { |
| 281 | while sum > PROB_SCALE { |
| 282 | // Find the symbol with the highest frequency and reduce it. |
| 283 | let max_idx = norm |
| 284 | .iter() |
| 285 | .enumerate() |
| 286 | .filter(|(_, f)| **f > 1) |
| 287 | .max_by_key(|(_, f)| **f) |
| 288 | .map(|(i, _)| i) |
| 289 | .unwrap_or(0); |
| 290 | norm[max_idx] -= 1; |
| 291 | sum -= 1; |
| 292 | } |
| 293 | while sum < PROB_SCALE { |
| 294 | let max_idx = norm |
| 295 | .iter() |
| 296 | .enumerate() |
| 297 | .max_by_key(|(_, f)| **f) |
| 298 | .map(|(i, _)| i) |
| 299 | .unwrap_or(0); |
| 300 | norm[max_idx] += 1; |
| 301 | sum += 1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | norm |
| 306 | } |
| 307 | |
| 308 | /// Build cumulative frequency table. |
| 309 | fn build_cum_table(freqs: &[u32; 256]) -> ([u32; 257], [u32; 256]) { |