Commit a4438ea5 authored by Jason Rhinelander's avatar Jason Rhinelander

Election: finished class/bug fixes

parent 2fa96ee3
......@@ -19,7 +19,7 @@ class Election : public eris::Agent {
public:
/** Creates an election that occurs every `period` simulation periods.
*/
Election(int period);
Election(unsigned int period);
/** Creates an election that occurs every time the given function/lambda returns true. The
* function will be called exactly once per inter-optimization, and, when it returns true,
......@@ -29,7 +29,7 @@ class Election : public eris::Agent {
/** Returns true if the current period is an election period.
*/
bool electionPeriod();
bool electionPeriod() const;
/** Conducts an election at the current positions, returns the winner. Abstract method.
*/
......@@ -42,6 +42,7 @@ class Election : public eris::Agent {
private:
bool election_period_ = false;
unsigned int election_counter_ = 0;
std::function<bool()> election_;
};
......
......@@ -2,11 +2,10 @@
namespace voting {
Election::Election(int period) {
long counter;
election_ = [period,&counter]() -> bool {
if (++counter >= period) {
counter = 0;
Election::Election(unsigned int period) {
election_ = [period,this]() -> bool {
if (++election_counter_ >= period) {
election_counter_ = 0;
return true;
}
return false;
......@@ -15,4 +14,12 @@ Election::Election(int period) {
Election::Election(std::function<bool()> callElection) : election_(callElection) {}
bool Election::electionPeriod() const {
return election_period_;
}
void Election::advance() {
election_period_ = election_();
}
}
......@@ -72,6 +72,7 @@ const std::unordered_map<eris_id_t, int>& FPTP::votes() {
}
void FPTP::advance() {
Election::advance();
winner_ = 0;
votes_.clear();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment