function findStv($ballots, $seats) { $candidates = getCandidates($ballots); $num_ballots = count($ballots); $droop = calculateDroop($num_ballots, $seats); $committee = array(); foreach (range(1, 100) as $num) { $logs = tallyBallots($ballots, $candidates, $seats, $droop); $last_log = array_pop($logs); foreach ($last_log['committee'] as $elected) { if (!isset($committee[$elected->eid])) { $committee[$elected->eid] = 0; } $committee[$elected->eid] += 1; } } return $committee; }
$result = array(); foreach (new DirectoryIterator('elections') as $fileInfo) { if ($fileInfo->isDot()) { continue; } else { $fn = 'elections/' . $fileInfo->getFilename(); $elec_data = json_decode(file_get_contents($fn), 1); $seats = $elec_data['ELECTION']['seats']; $year = $elec_data['ELECTION']['id']; if ('2011' != $year) { continue; } $ballots = evenBallotLength($elec_data['BALLOTS']); $candidates = getCandidates($ballots); $num_ballots = count($ballots); $droop = calculateDroop($num_ballots, $seats); $committee = array(); foreach (range(1, 100) as $num) { if (0 == $num % 100) { print $year . " : " . $num . " memory " . memory_get_usage() . "\n"; } $logs = tallyBallots($ballots, $candidates, $seats, $droop); $last_log = array_pop($logs); foreach ($last_log['committee'] as $elected) { if (!isset($committee[$elected->eid])) { $committee[$elected->eid] = 0; } $committee[$elected->eid] += 1; } } $result[$year] = $committee;