From 27c4a0f0416859254098bc9573326f8dc999b7ed Mon Sep 17 00:00:00 2001 From: Andrew Duffy Date: Thu, 15 Aug 2024 12:24:47 -0400 Subject: [PATCH] each generation of table building should only consider up to self.n_symbols --- src/builder.rs | 4 ++-- src/find_longest/naive.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 7e865a8..43a6fd4 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -103,7 +103,7 @@ impl SymbolTable { fn optimize(&self, counters: Counter) -> Self { let mut res = SymbolTable::default(); let mut pqueue = BinaryHeap::new(); - for code1 in 0..511 { + for code1 in 0u16..(256u16 + self.n_symbols as u16) { let symbol1 = self.symbols[code1 as usize]; let gain = counters.count1(code1) * symbol1.len(); pqueue.push(Candidate { @@ -111,7 +111,7 @@ impl SymbolTable { gain, }); - for code2 in 0..511 { + for code2 in 0u16..(256u16 + self.n_symbols as u16) { let symbol2 = &self.symbols[code2 as usize]; // If either symbol is zero-length, or if merging would yield a symbol of // length greater than 8, skip. diff --git a/src/find_longest/naive.rs b/src/find_longest/naive.rs index c75ecad..c9add2d 100644 --- a/src/find_longest/naive.rs +++ b/src/find_longest/naive.rs @@ -15,7 +15,7 @@ impl FindLongestSymbol for SymbolTable { // Start with the code corresponding to the escape of the first character in the text let mut best_code = text[0] as u16; let mut best_overlap = 1; - for code in 256..511 { + for code in 256..(256 + self.n_symbols as u16) { let symbol = &self.symbols[code as usize]; if symbol.is_prefix(text) && symbol.len() > best_overlap { best_code = code;