Verified Commit 794749df authored by Jelle van der Waa's avatar Jelle van der Waa 🚧
Browse files

Use a Hashset to not append duplicates

When building the reverse deps HashMap we insert duplicates into the
mapping as we use a Vector to store it in. A HashSet prevents duplicates
from being added.

Closes: #2
parent e1562ea3
Pipeline #3036 passed with stages
in 1 minute and 37 seconds
...@@ -28,8 +28,8 @@ fn find_package_anywhere<'a>( ...@@ -28,8 +28,8 @@ fn find_package_anywhere<'a>(
/// Retrieve a HashMap of all reverse dependencies. /// Retrieve a HashMap of all reverse dependencies.
fn get_reverse_deps_map( fn get_reverse_deps_map(
pacman: &alpm::Alpm, pacman: &alpm::Alpm,
) -> Result<HashMap<String, Vec<String>>, Box<dyn Error>> { ) -> Result<HashMap<String, HashSet<String>>, Box<dyn Error>> {
let mut reverse_deps: HashMap<String, Vec<String>> = HashMap::new(); let mut reverse_deps: HashMap<String, HashSet<String>> = HashMap::new();
let dbs = pacman.syncdbs(); let dbs = pacman.syncdbs();
for db in dbs { for db in dbs {
...@@ -40,15 +40,27 @@ fn get_reverse_deps_map( ...@@ -40,15 +40,27 @@ fn get_reverse_deps_map(
for dep in pkg.depends() { for dep in pkg.depends() {
reverse_deps reverse_deps
.entry(dep.name().to_string()) .entry(dep.name().to_string())
.and_modify(|e| e.push(pkg.name().to_string())) .and_modify(|e| {
.or_insert_with(|| vec![pkg.name().to_string()]); e.insert(pkg.name().to_string());
})
.or_insert_with(|| {
let mut modify = HashSet::new();
modify.insert(pkg.name().to_string());
modify
});
} }
for dep in pkg.makedepends() { for dep in pkg.makedepends() {
reverse_deps reverse_deps
.entry(dep.name().to_string()) .entry(dep.name().to_string())
.and_modify(|e| e.push(pkg.name().to_string())) .and_modify(|e| {
.or_insert_with(|| vec![pkg.name().to_string()]); e.insert(pkg.name().to_string());
})
.or_insert_with(|| {
let mut modify = HashSet::new();
modify.insert(pkg.name().to_string());
modify
});
} }
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment