Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 40e7aa8

Browse files
minor refactoring to Voting.h
1 parent 28a6988 commit 40e7aa8

File tree

1 file changed

+28
-7
lines changed
  • src/eloquentarduino/data_structures

1 file changed

+28
-7
lines changed

‎src/eloquentarduino/data_structures/Voting.h

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ namespace Eloquent {
66
/**
77
* Output a robust value when a given number of votes agree
88
* @tparam votes
9-
* @tparam quorum
109
*/
11-
template<uint8_t votes, uint8_t quorum>
12-
class MajorityVoting {
10+
template<uint8_t votes>
11+
class Voting {
1312
public:
1413

1514
/**
@@ -32,14 +31,16 @@ namespace Eloquent {
3231

3332
/**
3433
* Test if quorum was achieved
34+
* @param quorum how many votes should agree
35+
* @param row how many votes "in row" should agree
3536
*/
36-
bool hasMajority() {
37+
bool agree(uint8_t quorum, uint8_t row = 0) {
3738
_decision = 255;
3839

3940
if (_idx < votes)
4041
return false;
4142

42-
bool majority = false;
43+
bool agree = false;
4344

4445
for (uint8_t i = 0; i < votes - quorum; i++) {
4546
uint8_t vote = _votes[i];
@@ -51,12 +52,32 @@ namespace Eloquent {
5152

5253
if (count >= quorum) {
5354
_decision = vote;
54-
majority = true;
55+
agree = true;
5556
break;
5657
}
5758
}
5859

59-
return majority;
60+
// check votes "in row"
61+
if (row > 0) {
62+
for (uint8_t i = votes - row; i < votes; i++) {
63+
if (_votes[i] != _decision) {
64+
_decision = 255;
65+
agree = false;
66+
break;
67+
}
68+
}
69+
}
70+
71+
return agree;
72+
}
73+
74+
/**
75+
* Test if quorum was achieved
76+
* @param quorum how many votes should agree (in percent)
77+
* @param row how many votes "in row" should agree
78+
*/
79+
bool agree(float quorum, uint8_t row = 0) {
80+
return agree((uint8_t) round(votes * quorum), row);
6081
}
6182

6283
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /