From 2628856cf6e1b0d9c2e2ac87d1ae9d3ff716a47f Mon Sep 17 00:00:00 2001 From: Devon Sawatsky Date: Sat, 21 Oct 2023 23:40:44 -0700 Subject: stop pushing and popping the buffer, just get slices --- gifed/src/lzw.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/gifed/src/lzw.rs b/gifed/src/lzw.rs index 65ebb06..bbf1d8c 100644 --- a/gifed/src/lzw.rs +++ b/gifed/src/lzw.rs @@ -28,14 +28,11 @@ impl LZW { buffer.push(index); if !dictionary.contains_key(&buffer) { - buffer.pop(); - - if let Some(&code) = dictionary.get(&buffer) { + if let Some(&code) = dictionary.get(&buffer[..buffer.len() - 1]) { out.push_bits(code_size, code); - // Put the code back and add the vec to the dict - buffer.push(indicie); - dictionary.insert(buffer.clone(), next_code); + // add the vec to the dict + dictionary.insert(buffer, next_code); next_code += 1; // If the next_code can't fit in the code_size, we have to increase it @@ -43,10 +40,9 @@ impl LZW { code_size += 1; } - buffer.clear(); - buffer.push(indicie); + buffer = vec![index]; } else { - println!("indicie is: {}", indicie); + println!("index is: {index}"); println!("buffer is: {:?}", buffer); println!("dictionary: {:?}", dictionary); unreachable!() -- cgit 1.4.1-3-g733a5