Write more efficient implementation for CandidateMap which does not rely on hashing

This commit is contained in:
RunasSudo 2022-08-21 05:24:54 +10:00
parent 4ad02c052c
commit 876be9c55a
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A
5 changed files with 211 additions and 38 deletions

View File

@ -17,72 +17,92 @@
use crate::election::Candidate; use crate::election::Candidate;
use nohash_hasher::BuildNoHashHasher; use std::ops::Index;
use std::{collections::{HashMap, hash_map}, ops::Index};
/// Newtype for [HashMap] on [Candidate]s /// Newtype for [HashMap] on [Candidate]s
#[derive(Clone)] #[derive(Clone)]
pub struct CandidateMap<'e, V> { pub struct CandidateMap<'e, V> {
// TODO: Can we implement this more efficiently as a Vec? keys: Vec<Option<&'e Candidate>>,
map: HashMap<&'e Candidate, V, BuildNoHashHasher<Candidate>> values: Vec<Option<V>>
} }
impl<'e, V> CandidateMap<'e, V> { impl<'e, V> CandidateMap<'e, V> {
/// See [HashMap::new] /// See [HashMap::new]
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
map: HashMap::with_hasher(BuildNoHashHasher::default()) keys: Vec::new(),
values: Vec::new()
} }
} }
/// See [HashMap::with_capacity] /// See [HashMap::with_capacity]
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self { let mut ret = Self {
map: HashMap::with_capacity_and_hasher(capacity, BuildNoHashHasher::default()) keys: Vec::with_capacity(capacity),
values: Vec::with_capacity(capacity)
};
ret.maybe_resize(capacity);
return ret;
}
fn maybe_resize(&mut self, len: usize) {
if len < self.keys.len() {
return;
} }
self.keys.resize_with(len, || None);
self.values.resize_with(len, || None);
} }
/// See [HashMap::len] /// See [HashMap::len]
#[inline] #[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
return self.map.len(); return self.keys.iter().filter(|k| k.is_some()).count();
} }
/// See [HashMap::insert] /// See [HashMap::insert]
#[inline] #[inline]
pub fn insert(&mut self, candidate: &'e Candidate, value: V) { pub fn insert(&mut self, candidate: &'e Candidate, value: V) {
self.map.insert(candidate, value); self.maybe_resize(candidate.index + 1);
self.keys[candidate.index] = Some(candidate);
self.values[candidate.index] = Some(value);
} }
/// See [HashMap::get] /// See [HashMap::get]
#[inline] #[inline]
pub fn get(&self, candidate: &'e Candidate) -> Option<&V> { pub fn get(&self, candidate: &'e Candidate) -> Option<&V> {
return self.map.get(candidate); return self.values.get(candidate.index).unwrap_or(&None).as_ref();
} }
/// See [HashMap::get_mut] /// See [HashMap::get_mut]
#[inline] #[inline]
pub fn get_mut(&mut self, candidate: &'e Candidate) -> Option<&mut V> { pub fn get_mut(&mut self, candidate: &'e Candidate) -> Option<&mut V> {
return self.map.get_mut(candidate); match self.values.get_mut(candidate.index) {
Some(v) => {
return v.as_mut();
}
None => {
return None;
}
}
} }
/// See [HashMap::iter] /// See [HashMap::iter]
#[inline] #[inline]
pub fn iter(&self) -> hash_map::Iter<&'e Candidate, V> { pub fn iter(&self) -> Iter<'_, 'e, V> {
return self.map.iter(); return Iter { map: &self, index: 0 };
} }
/// See [HashMap::iter_mut] /// See [HashMap::iter_mut]
#[inline] #[inline]
pub fn iter_mut(&mut self) -> hash_map::IterMut<&'e Candidate, V> { pub fn iter_mut(&mut self) -> IterMut<'_, 'e, V> {
return self.map.iter_mut(); return IterMut { map: self, index: 0 };
} }
/// See [HashMap::values] /// See [HashMap::values]
#[inline] #[inline]
pub fn values(&self) -> hash_map::Values<&'e Candidate, V> { pub fn values(&self) -> Values<'_, 'e, V> {
return self.map.values(); return Values { map: &self, index: 0 };
} }
} }
@ -90,16 +110,170 @@ impl<'e, V> Index<&Candidate> for CandidateMap<'e, V> {
type Output = V; type Output = V;
fn index(&self, candidate: &Candidate) -> &Self::Output { fn index(&self, candidate: &Candidate) -> &Self::Output {
return &self.map[candidate]; return self.values.get(candidate.index).unwrap_or(&None).as_ref().unwrap();
}
}
pub struct Iter<'m, 'e, V> {
map: &'m CandidateMap<'e, V>,
index: usize
}
impl<'m, 'e, V> Iterator for Iter<'m, 'e, V> {
type Item = (&'e Candidate, &'m V);
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.map.keys.get(self.index) {
Some(k) => {
// Key within range
match k {
Some(kk) => {
// Key is set
// SAFETY: Guaranteed to be set, as we update key and value at the same time
let v = unsafe { self.map.values.get_unchecked(self.index).as_ref().unwrap_unchecked() };
self.index += 1;
return Some((kk, v));
}
None => {
// Key is unset
self.index += 1;
continue;
}
}
}
None => {
// Key outside range
return None;
}
}
}
}
}
pub struct IterMut<'m, 'e, V> {
map: &'m mut CandidateMap<'e, V>,
index: usize
}
impl<'m, 'e, V> Iterator for IterMut<'m, 'e, V> {
type Item = (&'e Candidate, &'m mut V);
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.map.keys.get(self.index) {
Some(k) => {
// Key within range
match k {
Some(kk) => {
// Key is set
// SAFETY: Guaranteed to be set, as we update key and value at the same time
let v = unsafe { self.map.values.get_unchecked_mut(self.index).as_mut().unwrap_unchecked() };
let v_ptr = v as *mut V;
// SAFETY: Need unsafe pointer magic for IterMut
let vv = unsafe { &mut *v_ptr };
self.index += 1;
return Some((kk, vv));
}
None => {
// Key is unset
self.index += 1;
continue;
}
}
}
None => {
// Key outside range
return None;
}
}
}
}
}
pub struct Values<'m, 'e, V> {
map: &'m CandidateMap<'e, V>,
index: usize
}
impl<'m, 'e, V> Iterator for Values<'m, 'e, V> {
type Item = &'m V;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.map.values.get(self.index) {
Some(v) => {
// Key within range
match v {
Some(vv) => {
// Key is set
self.index += 1;
return Some(vv);
}
None => {
// Key is unset
self.index += 1;
continue;
}
}
}
None => {
// Key outside range
return None;
}
}
}
}
}
pub struct IntoIter<'e, V> {
map: CandidateMap<'e, V>,
index: usize
}
impl<'e, V> Iterator for IntoIter<'e, V> {
type Item = (&'e Candidate, V);
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.map.keys.get(self.index) {
Some(k) => {
// Key within range
match k {
Some(kk) => {
// Key is set
// SAFETY: Guaranteed to be set, as we update key and value at the same time
let v = unsafe { self.map.values.get_unchecked_mut(self.index).take().unwrap_unchecked() };
self.index += 1;
return Some((kk, v));
}
None => {
// Key is unset
self.index += 1;
continue;
}
}
}
None => {
// Key outside range
return None;
}
}
}
} }
} }
impl<'e, V> IntoIterator for CandidateMap<'e, V> { impl<'e, V> IntoIterator for CandidateMap<'e, V> {
type Item = (&'e Candidate, V); type Item = (&'e Candidate, V);
type IntoIter = hash_map::IntoIter<&'e Candidate, V>; type IntoIter = IntoIter<'e, V>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
return self.map.into_iter(); return IntoIter { map: self, index: 0 };
} }
} }

View File

@ -788,7 +788,7 @@ pub fn init_repeat_count_rollback<'a, N: Number>(state: &mut CountState<'a, N>,
// Copy ballot papers to rollback state // Copy ballot papers to rollback state
for (candidate, count_card) in state.candidates.iter_mut() { for (candidate, count_card) in state.candidates.iter_mut() {
rollback_candidates.insert(*candidate, count_card.clone()); rollback_candidates.insert(candidate, count_card.clone());
} }
state.rollback_state = RollbackState::NeedsRollback { candidates: Some(rollback_candidates), exhausted: Some(rollback_exhausted), constraint, group }; state.rollback_state = RollbackState::NeedsRollback { candidates: Some(rollback_candidates), exhausted: Some(rollback_exhausted), constraint, group };

View File

@ -261,8 +261,7 @@ impl<'a, N: Number> CountState<'a, N> {
if opts.sort_votes { if opts.sort_votes {
// Sort by votes if requested // Sort by votes if requested
candidates = self.candidates.iter() candidates = self.candidates.iter().collect();
.map(|(c, cc)| (*c, cc)).collect();
// First sort by order of election (as a tie-breaker, if votes are equal) // First sort by order of election (as a tie-breaker, if votes are equal)
candidates.sort_unstable_by(|a, b| b.1.order_elected.cmp(&a.1.order_elected)); candidates.sort_unstable_by(|a, b| b.1.order_elected.cmp(&a.1.order_elected));
// Then sort by votes // Then sort by votes

View File

@ -146,13 +146,13 @@ impl<'e, N: Number> TransferTable<'e, N> {
// Candidate votes // Candidate votes
for (candidate, cell) in column.cells.iter_mut() { for (candidate, cell) in column.cells.iter_mut() {
column.total.ballots += &cell.ballots; column.total.ballots += &cell.ballots;
self.total.add_transfers(*candidate, &cell.ballots); self.total.add_transfers(candidate, &cell.ballots);
self.total.total.ballots += &cell.ballots; self.total.total.ballots += &cell.ballots;
let votes_in = cell.ballots.clone() * &column.value_fraction; let votes_in = cell.ballots.clone() * &column.value_fraction;
cell.votes_in += &votes_in; cell.votes_in += &votes_in;
column.total.votes_in += &votes_in; column.total.votes_in += &votes_in;
self.total.cells.get_mut(*candidate).unwrap().votes_in += &votes_in; self.total.cells.get_mut(candidate).unwrap().votes_in += &votes_in;
self.total.total.votes_in += votes_in; self.total.total.votes_in += votes_in;
} }

View File

@ -1312,7 +1312,7 @@ where
for<'r> &'r N: ops::Sub<&'r N, Output=N> for<'r> &'r N: ops::Sub<&'r N, Output=N>
{ {
// Do not defer if this could change the last 2 candidates // Do not defer if this could change the last 2 candidates
let mut hopefuls: Vec<(&&Candidate, &CountCard<N>)> = state.candidates.iter() let mut hopefuls: Vec<(&Candidate, &CountCard<N>)> = state.candidates.iter()
.filter(|(_, cc)| cc.state == CandidateState::Hopeful || cc.state == CandidateState::Guarded) .filter(|(_, cc)| cc.state == CandidateState::Hopeful || cc.state == CandidateState::Guarded)
.collect(); .collect();
@ -1501,7 +1501,7 @@ fn hopefuls_below_threshold<'a, N: Number>(state: &CountState<'a, N>, opts: &STV
let excluded_candidates: Vec<&Candidate> = state.candidates.iter() let excluded_candidates: Vec<&Candidate> = state.candidates.iter()
.filter_map(|(c, cc)| .filter_map(|(c, cc)|
if cc.state == CandidateState::Hopeful && cc.votes <= min_threshold { if cc.state == CandidateState::Hopeful && cc.votes <= min_threshold {
Some(*c) Some(c)
} else { } else {
None None
}) })
@ -1520,7 +1520,7 @@ fn hopefuls_below_threshold<'a, N: Number>(state: &CountState<'a, N>, opts: &STV
fn hopefuls_to_bulk_exclude<'a, N: Number>(state: &CountState<'a, N>, _opts: &STVOptions) -> Vec<&'a Candidate> { fn hopefuls_to_bulk_exclude<'a, N: Number>(state: &CountState<'a, N>, _opts: &STVOptions) -> Vec<&'a Candidate> {
let mut excluded_candidates = Vec::new(); let mut excluded_candidates = Vec::new();
let mut hopefuls: Vec<(&&Candidate, &CountCard<N>)> = state.candidates.iter() let mut hopefuls: Vec<(&Candidate, &CountCard<N>)> = state.candidates.iter()
.filter(|(_, cc)| cc.state == CandidateState::Hopeful) .filter(|(_, cc)| cc.state == CandidateState::Hopeful)
.collect(); .collect();
@ -1547,7 +1547,7 @@ fn hopefuls_to_bulk_exclude<'a, N: Number>(state: &CountState<'a, N>, _opts: &ST
continue; continue;
} }
let try_exclude: Vec<&Candidate> = try_exclude.iter().map(|(c, _)| **c).collect(); let try_exclude: Vec<&Candidate> = try_exclude.iter().map(|(c, _)| *c).collect();
// Do not exclude if this violates constraints // Do not exclude if this violates constraints
match constraints::test_constraints_any_time(state, &try_exclude, CandidateState::Excluded) { match constraints::test_constraints_any_time(state, &try_exclude, CandidateState::Excluded) {
@ -1639,7 +1639,7 @@ where
for<'r> &'r N: ops::Div<&'r N, Output=N>, for<'r> &'r N: ops::Div<&'r N, Output=N>,
{ {
// Cannot filter by raw vote count, as candidates may have 0.00 votes but still have recorded ballot papers // Cannot filter by raw vote count, as candidates may have 0.00 votes but still have recorded ballot papers
let mut excluded_with_votes: Vec<(&&Candidate, &CountCard<N>)> = state.candidates.iter() let mut excluded_with_votes: Vec<(&Candidate, &CountCard<N>)> = state.candidates.iter()
.filter(|(_, cc)| cc.state == CandidateState::Excluded && !cc.finalised) .filter(|(_, cc)| cc.state == CandidateState::Excluded && !cc.finalised)
.collect(); .collect();
@ -1649,7 +1649,7 @@ where
let order_excluded = excluded_with_votes[0].1.order_elected; let order_excluded = excluded_with_votes[0].1.order_elected;
let excluded_candidates: Vec<&Candidate> = excluded_with_votes.into_iter() let excluded_candidates: Vec<&Candidate> = excluded_with_votes.into_iter()
.filter(|(_, cc)| cc.order_elected == order_excluded) .filter(|(_, cc)| cc.order_elected == order_excluded)
.map(|(c, _)| *c) .map(|(c, _)| c)
.collect(); .collect();
let names: Vec<&str> = excluded_candidates.iter().map(|c| c.name.as_str()).sorted().collect(); let names: Vec<&str> = excluded_candidates.iter().map(|c| c.name.as_str()).sorted().collect();
@ -1757,9 +1757,9 @@ fn init_tiebreaks<N: Number>(state: &mut CountState<N>, opts: &STVOptions) {
} }
// Sort candidates in this stage by votes, grouping by ties // Sort candidates in this stage by votes, grouping by ties
let mut sorted_candidates: Vec<(&&Candidate, &CountCard<N>)> = state.candidates.iter().collect(); let mut sorted_candidates: Vec<(&Candidate, &CountCard<N>)> = state.candidates.iter().collect();
sorted_candidates.sort_unstable_by(|a, b| a.1.votes.cmp(&b.1.votes)); sorted_candidates.sort_unstable_by(|a, b| a.1.votes.cmp(&b.1.votes));
let sorted_candidates: Vec<Vec<(&&Candidate, &CountCard<N>)>> = sorted_candidates.into_iter() let sorted_candidates: Vec<Vec<(&Candidate, &CountCard<N>)>> = sorted_candidates.into_iter()
.group_by(|(_, cc)| &cc.votes) .group_by(|(_, cc)| &cc.votes)
.into_iter() .into_iter()
.map(|(_, candidates)| candidates.collect()) .map(|(_, candidates)| candidates.collect())
@ -1795,23 +1795,23 @@ fn update_tiebreaks<N: Number>(state: &mut CountState<N>, _opts: &STVOptions) {
} }
// Sort candidates in this stage by votes, grouping by ties // Sort candidates in this stage by votes, grouping by ties
let mut sorted_candidates: Vec<(&&Candidate, &CountCard<N>)> = state.candidates.iter().collect(); let mut sorted_candidates: Vec<(&Candidate, &CountCard<N>)> = state.candidates.iter().collect();
sorted_candidates.sort_unstable_by(|a, b| a.1.votes.cmp(&b.1.votes)); sorted_candidates.sort_unstable_by(|a, b| a.1.votes.cmp(&b.1.votes));
let sorted_candidates: Vec<Vec<&Candidate>> = sorted_candidates.into_iter() let sorted_candidates: Vec<Vec<&Candidate>> = sorted_candidates.into_iter()
.group_by(|(_, cc)| &cc.votes) .group_by(|(_, cc)| &cc.votes)
.into_iter() .into_iter()
.map(|(_, candidates)| candidates.map(|(c, _)| *c).collect()) .map(|(_, candidates)| candidates.map(|(c, _)| c).collect())
.collect(); .collect();
// Update forwards tie-breaking order // Update forwards tie-breaking order
if let Some(hm) = state.forwards_tiebreak.as_mut() { if let Some(hm) = state.forwards_tiebreak.as_mut() {
// TODO: Check if already completely sorted // TODO: Check if already completely sorted
let mut sorted_last_round: Vec<(&&Candidate, &usize)> = hm.iter().collect(); let mut sorted_last_round: Vec<(&Candidate, &usize)> = hm.iter().collect();
sorted_last_round.sort_unstable_by(|a, b| a.1.cmp(b.1)); sorted_last_round.sort_unstable_by(|a, b| a.1.cmp(b.1));
let sorted_last_round: Vec<Vec<&Candidate>> = sorted_last_round.into_iter() let sorted_last_round: Vec<Vec<&Candidate>> = sorted_last_round.into_iter()
.group_by(|(_, v)| **v) .group_by(|(_, v)| **v)
.into_iter() .into_iter()
.map(|(_, group)| group.map(|(c, _)| *c).collect()) .map(|(_, group)| group.map(|(c, _)| c).collect())
.collect(); .collect();
let mut i: usize = 0; let mut i: usize = 0;