Skip to content

Commit

Permalink
refactor preferable candidate inner loop
Browse files Browse the repository at this point in the history
  • Loading branch information
milselarch committed May 5, 2024
1 parent 52d2109 commit 0a7e23c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trie_rcv"
version = "1.1.0"
version = "1.1.1"
edition = "2021"
description = "Ranked Choice Voting implementation using Tries in Rust"
license = "Apache-2.0"
Expand Down
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,13 @@ impl RankedChoiceVoteTrie {
unspecified_candidates.remove(candidate);
}

for preferable_candidate in search_path {
for candidate in &unspecified_candidates {
let ranked_pair = (*preferable_candidate, *candidate);
let pairwise_votes =
ranked_pairs_map.entry(ranked_pair).or_insert(0);
// println!("INSERT {:?}", (ranked_pair, terminating_votes));
*pairwise_votes += terminating_votes;
}
let pairs = iproduct!(search_path, &unspecified_candidates);
for (preferable_candidate, candidate) in pairs {
let ranked_pair = (*preferable_candidate, *candidate);
let pairwise_votes =
ranked_pairs_map.entry(ranked_pair).or_insert(0);
// println!("INSERT {:?}", (ranked_pair, terminating_votes));
*pairwise_votes += terminating_votes;
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,4 @@ fn test_all_elimination() {
let winner = rcv.run_election(votes);
println!("WINNER = {:?}", winner);
assert_eq!(winner, Some(1));
}

#[test]
fn test_ranked_pairs_elimination() {
let votes = RankedVote::from_vectors(&vec![
vec![1, 6, 15],
vec![1, 2, 6, 15, 5, 4, 7, 3, 11],
vec![6, 15, 1, 11, 10, 16, 17, 8, 2, 3, 5, 7],
vec![9, 8, 6, 11, 13, 3, 1],
vec![13, 14, 16, 6, 3, 4, 5, 2, 1, 8, 9]
]).unwrap();

let mut rcv = RankedChoiceVoteTrie::new();
rcv.set_elimination_strategy(EliminationStrategies::RankedPairs);
let winner = rcv.run_election(votes);
println!("WINNER = {:?}", winner);
assert_eq!(winner, Some(6));
}

0 comments on commit 0a7e23c

Please sign in to comment.