-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Added move_to_front_encoding implementation #874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vil02
merged 4 commits into
TheAlgorithms:master
from
Divakar-2508:move_to_front_compression
Mar 20, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
329f2d5
Added move_to_front_encoding implementation
Divakar-2508 fd47ae0
Update src/compression/move_to_front.rs
Divakar-2508 da98a3f
Removed unnescessary dbg!() and added macros for test_cases
Divakar-2508 d3cd07e
replaced slices for Vec!
Divakar-2508 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| mod move_to_front; | ||
| mod run_length_encoding; | ||
|
|
||
| pub use self::move_to_front::{move_to_front_decode, move_to_front_encode}; | ||
| pub use self::run_length_encoding::{run_length_decode, run_length_encode}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // https://en.wikipedia.org/wiki/Move-to-front_transform | ||
|
|
||
| pub fn move_to_front_encode(text: &str) -> Vec<u8> { | ||
| dbg!(text); | ||
| let mut char_table: Vec<char> = (0..=255).map(|ch| ch as u8 as char).collect(); | ||
Divakar-2508 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut result = Vec::new(); | ||
|
|
||
| for ch in text.chars() { | ||
| if let Some(position) = char_table.iter().position(|&x| x == ch) { | ||
| result.push(position as u8); | ||
| char_table.remove(position); | ||
| char_table.insert(0, ch); | ||
| } | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| pub fn move_to_front_decode(encoded: &[u8]) -> String { | ||
| let mut char_table: Vec<char> = (0..=255).map(|ch| ch as u8 as char).collect(); | ||
Divakar-2508 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut result = String::new(); | ||
|
|
||
| for &pos in encoded { | ||
| let ch = char_table[pos as usize]; | ||
| result.push(ch); | ||
| char_table.remove(pos as usize); | ||
| char_table.insert(0, ch); | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_move_to_front_encode() { | ||
| assert_eq!(move_to_front_encode(""), []); | ||
| assert_eq!(move_to_front_encode("@"), [64]); | ||
| assert_eq!(move_to_front_encode("aaba"), [97, 0, 98, 1]); | ||
| assert_eq!(move_to_front_encode("aZ!"), [97, 91, 35]); | ||
| assert_eq!(move_to_front_encode("banana"), [98, 98, 110, 1, 1, 1]); | ||
| assert_eq!(move_to_front_encode("\0\n\t"), [0, 10, 10]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_move_to_front_decode() { | ||
| assert_eq!(move_to_front_decode(&[]), ""); | ||
| assert_eq!(move_to_front_decode(&[64]), "@"); | ||
| assert_eq!(move_to_front_decode(&[97, 0, 98, 1]), "aaba"); | ||
| assert_eq!(move_to_front_decode(&[97, 91, 35]), "aZ!"); | ||
| assert_eq!(move_to_front_decode(&[98, 98, 110, 1, 1, 1]), "banana"); | ||
| assert_eq!(move_to_front_decode(&[0, 10, 10]), "\0\n\t"); | ||
| } | ||
Divakar-2508 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.