Skip to content

runoff: reuse IRV Numba tally helpers for head-to-head round#46

Draft
endolith wants to merge 1 commit into
masterfrom
cursor/runoff-irv-tally-c8f5
Draft

runoff: reuse IRV Numba tally helpers for head-to-head round#46
endolith wants to merge 1 commit into
masterfrom
cursor/runoff-irv-tally-c8f5

Conversation

@endolith
Copy link
Copy Markdown
Owner

Closes #3.

What changed

The contingent-vote / top-two runoff second round used a Python loop with list.index per ballot. That round is equivalent to “instant runoff among only the two finalists”: skip every other candidate on each ballot, then take first preference among {finalist_0, finalist_1}. That is exactly what _inc_rank_idx + _tally_at_rank_idx do—the same Numba path IRV uses.

Details

  • Build eliminated_cands as all candidate IDs except the two finalists.
  • Call _inc_rank_idx only when that set is non-empty (same Numba empty-set guard as IRV).
  • Tally with _tally_at_rank_idx and read counts at the two finalist indices.

All 215 tests pass locally.

Open in Web Open in Cursor 

Second-round contingent vote compares two finalists by whoever appears
first on each ballot. That matches advancing each voter's rank index past
all non-finalists then tallying at that rank—the same _inc_rank_idx and
_tally_at_rank_idx path IRV uses.

Co-authored-by: endolith <endolith@gmail.com>
@what-the-diff
Copy link
Copy Markdown

what-the-diff Bot commented May 11, 2026

PR Summary

  • New Import Functions Introduced: The code now includes two new import functions called _inc_rank_idx and _tally_at_rank_idx from the _common module. These tools help streamline the process of updating ranks and tallying votes.

  • Improved Vote Counting Process: The logic behind counting votes for two options, finalist_0 and finalist_1, has been refined. It now uses a method from the library NumPy, which simplifies calculations and makes the process more efficient than the former looping method.

  • Introduction of eliminated_cands Set: A new set, eliminated_cands, was added. This serves as a check list to keep track of candidates who are no longer under consideration.

  • Maximized use of New Methods: The newly added functions _inc_rank_idx and _tally_at_rank_idx are now being utilized for updating ranking index and tallying votes respectively. This change was made to make the overall workflow more efficient and reliable.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.33%. Comparing base (8a7213f) to head (f66b32a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #46      +/-   ##
==========================================
+ Coverage   96.31%   96.33%   +0.01%     
==========================================
  Files          19       19              
  Lines         489      491       +2     
==========================================
+ Hits          471      473       +2     
  Misses         18       18              
Flag Coverage Δ
no-numba 95.51% <100.00%> (+0.01%) ⬆️
numba 89.20% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runoff can use the tally functions from IRV

2 participants