Skip to content

Commit b7bd452

Browse files
stacktraceghostLionqueen94
authored andcommitted
WIP
1 parent afadd45 commit b7bd452

File tree

10 files changed

+421
-328
lines changed

10 files changed

+421
-328
lines changed

backend/apportionment/src/candidate_nomination/mod.rs

Lines changed: 121 additions & 111 deletions
Large diffs are not rendered by default.

backend/apportionment/src/candidate_nomination/structs.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use crate::{
2-
Fraction,
3-
structs::{CandidateNumber, CandidateVotes, ListNumber},
2+
CandidateVotesTrait, Fraction,
3+
structs::{CandidateNumber, ListNumber},
44
};
55

66
/// Contains information about the chosen candidates and
77
/// the candidate list ranking for a specific list.
88
#[derive(Debug, PartialEq)]
9-
pub struct ListCandidateNomination {
9+
pub struct ListCandidateNomination<'a, T: CandidateVotesTrait> {
1010
/// List number for which this nomination applies
1111
pub list_number: ListNumber,
1212
/// The number of seats assigned to this group
1313
pub list_seats: u32,
1414
/// The list of chosen candidates via preferential votes, can be empty
15-
pub preferential_candidate_nomination: Vec<CandidateVotes>,
15+
// TODO: check lifetimes
16+
pub preferential_candidate_nomination: Vec<&'a T>,
1617
/// The list of other chosen candidates, can be empty
17-
pub other_candidate_nomination: Vec<CandidateVotes>,
18+
pub other_candidate_nomination: Vec<&'a T>,
1819
/// The updated ranking of the whole candidate list, can be empty
1920
pub updated_candidate_ranking: Vec<CandidateNumber>,
2021
}
@@ -33,13 +34,13 @@ pub struct PreferenceThreshold {
3334
/// It also contains the preferential nomination of candidates, the remaining
3435
/// nomination of candidates and the final ranking of candidates for each list.
3536
#[derive(Debug, PartialEq)]
36-
pub struct CandidateNominationResult {
37+
pub struct CandidateNominationResult<'a, T: CandidateVotesTrait> {
3738
/// Preference threshold percentage and number of votes
3839
pub preference_threshold: PreferenceThreshold,
3940
/// List of chosen candidates
4041
pub chosen_candidates: Vec<Candidate>,
4142
/// List of chosen candidates and candidate list ranking per list
42-
pub list_candidate_nomination: Vec<ListCandidateNomination>,
43+
pub list_candidate_nomination: Vec<ListCandidateNomination<'a, T>>,
4344
}
4445

4546
#[derive(Debug, PartialEq)]

backend/apportionment/src/int_newtype_macro.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ macro_rules! int_newtype {
99
}
1010
}
1111

12-
impl std::ops::Deref for $identifier {
13-
type Target = u32;
14-
15-
fn deref(&self) -> &Self::Target {
16-
&self.0
17-
}
18-
}
19-
2012
impl std::fmt::Display for $identifier {
2113
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2214
write!(f, "{}", self.0)
@@ -30,6 +22,18 @@ macro_rules! int_newtype {
3022
Ok(Self::from(u32::try_from(value)?))
3123
}
3224
}
25+
26+
impl PartialEq<u32> for $identifier {
27+
fn eq(&self, other: &u32) -> bool {
28+
self.0 == *other
29+
}
30+
}
31+
32+
impl PartialEq<$identifier> for u32 {
33+
fn eq(&self, other: &$identifier) -> bool {
34+
*self == other.0
35+
}
36+
}
3337
};
3438
}
3539

backend/apportionment/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ pub use self::{
1515
use self::{
1616
candidate_nomination::candidate_nomination,
1717
seat_assignment::seat_assignment,
18-
structs::{ApportionmentOutput, CandidateNominationInput, SeatAssignmentInput},
18+
structs::{ApportionmentOutput, as_candidate_nomination_input},
1919
};
2020

21-
pub fn process(input: impl ApportionmentInput) -> Result<ApportionmentOutput, ApportionmentError> {
22-
let seat_assignment = seat_assignment(SeatAssignmentInput::new(&input))?;
23-
let candidate_nomination =
24-
candidate_nomination(CandidateNominationInput::new(&input, &seat_assignment))?;
21+
pub fn process<T: ApportionmentInput>(
22+
input: &T,
23+
) -> Result<ApportionmentOutput<'_, T::List>, ApportionmentError> {
24+
let seat_assignment = seat_assignment(input)?;
25+
let candidate_nomination_input = as_candidate_nomination_input(input, &seat_assignment);
26+
let candidate_nomination = candidate_nomination::<T>(&candidate_nomination_input)?;
2527

2628
Ok(ApportionmentOutput {
2729
seat_assignment,

0 commit comments

Comments
 (0)