コード例 #1
0
VoteProcessorUsingHash::CandidateList VoteProcessorUsingHash::findMaxCandidates(const HashTable& counts) {
	CandidateList maxCandidates;
	int maxCount = 0;

	for (auto& bucket : counts) {
		for (auto& slot : bucket) {
			std::string name = slot.first;
			int count = slot.second;

			if (count < maxCount) {
				continue;
			}

			if (count > maxCount) {
				maxCount = count;
				maxCandidates.clear();
			}

			maxCandidates.push_back(name);
		}
	}

	std::sort(maxCandidates.begin(), maxCandidates.end());
	return maxCandidates;
}
コード例 #2
0
gams::elections::CandidateList
gams::elections::ElectionCumulative::get_leaders(int num_leaders)
{
  madara_logger_ptr_log(gams::loggers::global_logger.get(),
    gams::loggers::LOG_MAJOR,
    "gams::elections::ElectionCumulative:get_leaders" \
    " getting leaders from %s\n", election_prefix_.c_str());

  CandidateList leaders;

  if (knowledge_)
  {
    knowledge::ContextGuard guard(*knowledge_);

    typedef std::map <KnowledgeRecord::Integer, CandidateList> Leaderboard;

    CandidateVotes candidates;
    Leaderboard leaderboard;

    get_votes(candidates);

    // construct the leaderboard
    for (CandidateVotes::iterator i = candidates.begin();
      i != candidates.end(); ++i)
    {
      leaderboard[i->second].push_back(i->first);
    }

    // leaderboard is in ascending order, so grab from the back
    for (Leaderboard::reverse_iterator i = leaderboard.rbegin();
      i != leaderboard.rend() &&(int) leaders.size() < num_leaders; ++i)
    {
      // if it is a tie, we could provide more than num_leaders
      leaders.insert(leaders.end(), i->second.begin(), i->second.end());
    }
  }

  return leaders;
}