Small refactorings in apportionment seat assignment#3336
Conversation
…d_lists. Makes the whole function body less indented.
The only thing assign_remainder did with the slice was cloning it into a vec. Now the information that assign_remainder needs to own the memory is part of the signature. All callers do not need the Vecs themselves right now, so just handing in ownership is no problem.
All the function did was clone the slice into a vector, and all callers no longer need ownership of the value (or passed in an empty slice which can be replaced with an empty Vec without needing allocation either)
… while and a mutable local.
…modifies the element that is folded over. Specifically, the fold was not depending on the previous value but just extending the Vec.
This may enable further refactorings where we can remove the collecting into a container.
…pty: * the for loop just becomes an empty loop * seats_to_reassign is zero, so assign_remainder is called with the same number for total_residual_seats and current_residual_seat_number, resulting in another noop. The overall function result remains the same.
…intermediate allocation.
…ning into a mutable version.
…er to ensure the result is always positive
There should exist exactly one ListStanding with number list_number, so the inner loop always operates on the same element (for each iteration of the outer loop). I.e., full_seat is false until that element's residual_seats is exhausted and then is always false.
…of iterating once per inner loop.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3336 +/- ##
==========================================
- Coverage 90.60% 90.60% -0.01%
==========================================
Files 476 476
Lines 22694 22680 -14
Branches 2390 2386 -4
==========================================
- Hits 20563 20550 -13
Misses 2009 2009
+ Partials 122 121 -1 ☔ View full report in Codecov by Sentry. |
Lionqueen94
left a comment
There was a problem hiding this comment.
Thanks for this awesome refactor and the explanatory commits as well. I have just 2 comments 👍
…ment.rs Fix typo Co-authored-by: Lionqueen94 <lionqueen94@gmail.com>
…an earlier commit
|
@NikoWil I'm afraid there's something we forgot: our repositories require signed commits. Would you be able to sign your commits? |
| list_votes: &[T], | ||
| assigned_residual_seats: u32, | ||
| previous_steps: Vec<SeatChangeStep<T::ListNumber>>, | ||
| mut current_steps: Vec<SeatChangeStep<T::ListNumber>>, |
There was a problem hiding this comment.
iirc we explicitly chose to use immutable arguments, @stacktraceghost @rnijveld @Lionqueen94 do you remember?
|
@Lionqueen94 , of course I can sign the commits :) |
Both are fine! |
I did some small refactorings in the code base.
The only interface change was to save an allocation by returning an Iterator over a Vec. All tests (as performed by 'cargo test') are still green, so I'm reasonably sure the changes are fine :)