This is the key part of the interface for implementing a new election method in my simulation framework.
/* Use these */
class Voter {
public:
float getPref( int i );
};
class VoterArray {
public:
Voter& operator[]( int i );
};
/* Implement a subclass of this */
class VotingSystem {
public:
virtual void init( const char** envp );
/* return winner in winnerArray[0], second place in [1], and so on */
virtual void runElection( int* winnerArary, const VoterArray& they );
virtual ~VotingSystem();
};
Details here:
Voter.h
VotingSystem.h
A good simple example is the "Pick One" or "plurality" election method.
For those not up on C++, implementing an election method in this form of C would make it easy for me to translate:
void run_election_c( int* winnerArray, float** ratings, int numVoters, int numChoices ) {
/* for example, implementing pick-one/plurality */
int* counts = (int*)malloc( sizeof(int*) * numChoices );
int i, v;
for ( i = 0; i < numChoices; i++ ) { counts[i] = 0; }
for ( v = 0; v < numVoters; v++ ) {
/* for each voter, find the highest rated choice and vote for it */
int maxi = 0;
float tmax = ratings[v][0];
for ( i = 1; i < numChoices; i++ ) {
if ( ratings[v][i] > tmax ) {
tmax = ratings[v][i];
maxi = i;
}
}
counts[maxi]++;
}
{
int maxi = 0;
int max = counts[0];
for ( i = 1; i < numChoices; i++ ) {
if ( counts[i] > max ) {
max = counts[i];
maxi = i;
}
}
/* winnerArary[0] receives the _index_ of the winner. */
winnerArray[0] = maxi;
/* can optionally sort the indecies of all the choices into the outbound argument "winnerArray", but really only the winner at position zero matters */
}
free( counts );
}