Skip to content

Commit

Permalink
fixes a bug in doing schlinkert prune on reversed list
Browse files Browse the repository at this point in the history
  • Loading branch information
sts10 committed Apr 24, 2023
1 parent 8ac7782 commit 73d8f1f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
18 changes: 4 additions & 14 deletions src/display_information/uniquely_decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
/// very closely.
use std::collections::HashSet;

/// Return true if the list is uniquely decodable, false if not
/// Return true if the list is uniquely decodable, false if not. I
/// don't _think_ we need to check reversed words in this case.
pub fn check_decodability(c: &[String]) -> bool {
let is_c_uniquely_decodable_forwards = sardinas_patterson_theorem(vec_to_hash(c));
// We need to check both forward and reverse
let c_reversed = reverse_all_words(c);
let is_c_uniquely_decodable_backwards = sardinas_patterson_theorem(vec_to_hash(&c_reversed));
is_c_uniquely_decodable_forwards || is_c_uniquely_decodable_backwards
let is_c_uniquely_decodable = sardinas_patterson_theorem(vec_to_hash(c));
is_c_uniquely_decodable
}

fn vec_to_hash(v: &[String]) -> HashSet<String> {
Expand Down Expand Up @@ -77,14 +75,6 @@ fn generate_c_infinity_with_a_halt_break(c: HashSet<String>) -> HashSet<String>
c_infinity
}

fn reverse_all_words(list: &[String]) -> Vec<String> {
let mut reversed_list = vec![];
for word in list {
reversed_list.push(word.chars().rev().collect::<String>());
}
reversed_list
}

/// Returns true if c is uniquely decodable
fn sardinas_patterson_theorem(c: HashSet<String>) -> bool {
let c_infinity = generate_c_infinity_with_a_halt_break(c.clone());
Expand Down
8 changes: 5 additions & 3 deletions src/list_manipulations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ pub fn guarantee_maximum_prefix_length(
pub fn schlinkert_prune(list: &[String]) -> Vec<String> {
let offenders_to_remove_forwards = get_sardinas_patterson_final_intersection(list);
// Reversing all words before running the Schlinkert prune gives a
// different list of offending words
let offenders_to_remove_backwards =
get_sardinas_patterson_final_intersection(&reverse_all_words(list));
// different list of offending words. (We then have to un-reverse all the
// offending words.)
let offenders_to_remove_backwards = reverse_all_words(
&get_sardinas_patterson_final_intersection(&reverse_all_words(list)),
);
let mut new_list = list.to_owned();
// If running the prune on the reversed words yielded fewer offenders
// we'll use that list, since our assumed goal is to remove the fewest
Expand Down

0 comments on commit 73d8f1f

Please sign in to comment.