CondorcetElection<TCandidate extends Comparable> constructor
CondorcetElection<TCandidate extends Comparable> (
- List<
RankedBallot< ballots, {TCandidate> > - Iterable<
TCandidate> ? candidates,
Implementation
factory CondorcetElection(
List<RankedBallot<TCandidate>> ballots, {
Iterable<TCandidate>? candidates,
}) {
final ballotCandidates = ballots.expand((b) => b.rank).toSet();
final candidateSet =
candidates == null ? ballotCandidates : candidates.toSet();
assert(
candidates == null || candidateSet.containsAll(ballotCandidates),
'If `candidates` is provided, then every candidate in `ballots` should '
'exist in `candidates`.',
);
final candidateList = candidateSet.toList(growable: false)..sort();
Iterable<CondorcetPair<TCandidate>> iteratePairs() sync* {
for (var i = 0; i < candidateList.length; i++) {
for (var j = i + 1; j < candidateList.length; j++) {
yield CondorcetPair(candidateList[i], candidateList[j], ballots);
}
}
}
final pairs = Set.unmodifiable(iteratePairs());
final places = _calculatePlaces(candidateList, pairs);
return CondorcetElection._internal(
pairs,
places.expand((p) => p).toList(growable: false),
ballots,
places,
);
}