Skip to content

Commit

Permalink
diff: omit construction of count-to-words map for right-side histogram
Browse files Browse the repository at this point in the history
This also allows us to borrow Vec<WordPositions> from &self.
  • Loading branch information
yuja committed Sep 27, 2024
1 parent 493f610 commit 5c52b4e
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ impl<'input, 'aux> DiffSource<'input, 'aux> {
}
}

struct Histogram<'a> {
word_to_positions: HashMap<&'a BStr, Vec<WordPosition>>,
count_to_words: BTreeMap<usize, Vec<&'a BStr>>,
struct Histogram<'input> {
word_to_positions: HashMap<&'input BStr, Vec<WordPosition>>,
}

impl Histogram<'_> {
fn calculate<'a>(source: &DiffSource<'a, '_>, max_occurrences: usize) -> Histogram<'a> {
impl<'input> Histogram<'input> {
fn calculate(source: &DiffSource<'input, '_>, max_occurrences: usize) -> Self {
let mut word_to_positions: HashMap<&BStr, Vec<WordPosition>> = HashMap::new();
for (i, range) in source.ranges.iter().enumerate() {
let word = &source.text[range.clone()];
Expand All @@ -120,14 +119,15 @@ impl Histogram<'_> {
positions.push(WordPosition(i));
}
}
Histogram { word_to_positions }
}

fn build_count_to_words(&self) -> BTreeMap<usize, Vec<&'input BStr>> {
let mut count_to_words: BTreeMap<usize, Vec<&BStr>> = BTreeMap::new();
for (word, ranges) in &word_to_positions {
for (word, ranges) in &self.word_to_positions {
count_to_words.entry(ranges.len()).or_default().push(word);
}
Histogram {
word_to_positions,
count_to_words,
}
count_to_words
}
}

Expand Down Expand Up @@ -233,16 +233,16 @@ fn unchanged_ranges_lcs(
) -> Vec<(Range<usize>, Range<usize>)> {
let max_occurrences = 100;
let left_histogram = Histogram::calculate(left, max_occurrences);
if *left_histogram.count_to_words.keys().next().unwrap() > max_occurrences {
let left_count_to_words = left_histogram.build_count_to_words();
if *left_count_to_words.keys().next().unwrap() > max_occurrences {
// If there are very many occurrences of all words, then we just give up.
return vec![];
}
let right_histogram = Histogram::calculate(right, max_occurrences);
// Look for words with few occurrences in `left` (could equally well have picked
// `right`?). If any of them also occur in `right`, then we add the words to
// the LCS.
let Some(uncommon_shared_words) = left_histogram
.count_to_words
let Some(uncommon_shared_words) = left_count_to_words
.iter()
.map(|(left_count, left_words)| -> Vec<&BStr> {
left_words
Expand Down

0 comments on commit 5c52b4e

Please sign in to comment.