From c140ef0a9005fb1df823507e8939d662285047d1 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 11 Jun 2023 21:01:29 +1000 Subject: [PATCH] Fix rounding of subtransfers by parcel Was erroneously segmenting transfers by outgoing parcel rather than incoming parcel --- src/stv/gregory/mod.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/stv/gregory/mod.rs b/src/stv/gregory/mod.rs index eda06df..9528f80 100644 --- a/src/stv/gregory/mod.rs +++ b/src/stv/gregory/mod.rs @@ -241,7 +241,7 @@ where let mut exhausted_ballots = N::new(); let mut exhausted_votes = N::new(); - for parcel in parcels { + for (parcel_num, parcel) in parcels.into_iter().enumerate() { // Count next preferences let result = super::next_preferences(state, parcel.votes); @@ -253,7 +253,14 @@ where exhausted_ballots += &result.exhausted.num_ballots; exhausted_votes += &result.exhausted.num_ballots * &parcel.value_fraction; - parcels_next_prefs.push((parcel.value_fraction, parcel.source_order, result)); + // Determine which column of the transfer table to use + let table_column_num = match opts.round_subtransfers { + RoundSubtransfersMode::ByValueAndSource => Some(parcel.source_order), + RoundSubtransfersMode::ByParcel => Some(parcel_num), + _ => Some(0) + }; + + parcels_next_prefs.push((parcel.value_fraction, table_column_num, result)); } // Calculate and print surplus fraction @@ -320,16 +327,12 @@ where surplus.clone(), surplus_fraction.clone(), surplus_numer.clone(), surplus_denom.clone() ); - for (value_fraction, source_order, result) in parcels_next_prefs { + for (value_fraction, table_column_num, result) in parcels_next_prefs { for (candidate, entry) in result.candidates.into_iter() { // Record transfers transfer_table.add_transfers( &value_fraction, - match opts.round_subtransfers { - RoundSubtransfersMode::ByValueAndSource => Some(source_order), - RoundSubtransfersMode::ByParcel => None, // Force new column per parcel - _ => Some(0) - }, + table_column_num, candidate, &entry.num_ballots ); @@ -366,11 +369,7 @@ where // Record exhausted votes transfer_table.add_exhausted( &value_fraction, - match opts.round_subtransfers { - RoundSubtransfersMode::ByValueAndSource => Some(source_order), - RoundSubtransfersMode::ByParcel => None, // Force new column per parcel - _ => Some(0) - }, + table_column_num, &result.exhausted.num_ballots );