Replaced Vec with HashSet for second_sequence_popular

This commit is contained in:
Dima Kudosh
2018-03-24 18:01:27 +03:00
parent 8748d65b8c
commit 41c8240cb7
2 changed files with 6 additions and 6 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "difflib" name = "difflib"
version = "0.3.0" version = "0.3.1"
authors = ["Dima Kudosh <dimakudosh@gmail.com>"] authors = ["Dima Kudosh <dimakudosh@gmail.com>"]
description = "Port of Python's difflib library to Rust." description = "Port of Python's difflib library to Rust."
documentation = "https://github.com/DimaKudosh/difflib/wiki" documentation = "https://github.com/DimaKudosh/difflib/wiki"
+5 -5
View File
@@ -1,4 +1,4 @@
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::cmp::{max, min}; use std::cmp::{max, min};
use utils::calculate_ratio; use utils::calculate_ratio;
use std::fmt::Debug; use std::fmt::Debug;
@@ -110,7 +110,7 @@ pub struct SequenceMatcher<'a, T: 'a + ?Sized + Sequence> {
opcodes: Option<Vec<Opcode>>, opcodes: Option<Vec<Opcode>>,
is_junk: Option<fn(&str) -> bool>, is_junk: Option<fn(&str) -> bool>,
second_sequence_elements: HashMap<&'a str, Vec<usize>>, second_sequence_elements: HashMap<&'a str, Vec<usize>>,
second_sequence_popular: Vec<&'a str>, second_sequence_popular: HashSet<&'a str>,
} }
@@ -123,7 +123,7 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
opcodes: None, opcodes: None,
is_junk: None, is_junk: None,
second_sequence_elements: HashMap::new(), second_sequence_elements: HashMap::new(),
second_sequence_popular: Vec::new(), second_sequence_popular: HashSet::new(),
}; };
matcher.set_seqs(first_sequence, second_sequence); matcher.set_seqs(first_sequence, second_sequence);
matcher matcher
@@ -171,13 +171,13 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
second_sequence_elements.remove(element); second_sequence_elements.remove(element);
} }
} }
let mut popular = Vec::new(); let mut popular = HashSet::new();
let len = second_sequence.len(); let len = second_sequence.len();
if len >= 200 { if len >= 200 {
let test_len = (len as f32 / 100.0).floor() as usize + 1; let test_len = (len as f32 / 100.0).floor() as usize + 1;
for (element, indexes) in second_sequence_elements.iter() { for (element, indexes) in second_sequence_elements.iter() {
if indexes.len() > test_len { if indexes.len() > test_len {
popular.push(element.clone()); popular.insert(element.clone());
} }
} }
for element in &popular { for element in &popular {