-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree_list.cpp
64 lines (56 loc) · 1.8 KB
/
free_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <vector>
#include <bits/stdc++.h>
#include "vote.hpp"
#include "candidate.hpp"
#include "settings.h"
namespace panachage
{
class FreeVote : public Vote
{
std::vector<candidate::id_type> votes;
public:
void count(std::vector<partylist *> lists)
{
for (candidate::id_type cand_id : votes)
{
for (partylist *plist : lists)
{
std::vector<candidate::id_type> candidates = plist->candidateList();
if (!!std::count(candidates.begin(), candidates.end(), cand_id))
{
plist->candidate_votes[cand_id] += this->copies;
}
}
}
}
inline bool validate()
{
return votes.size() <= config.seats;
}
inline bool operator<(const Vote &vote) const
{
const FreeVote *f = dynamic_cast<const FreeVote *>(&vote);
if (!f)
return true;
return this->votes < f->votes;
}
std::string encode() const
{
std::string c = copies == 1 ? "" : std::to_string(copies) + "*";
std::string v = std::accumulate(votes.begin(), votes.end(), std::string(),
[](std::string a, candidate::id_type b)
{ return a + std::to_string(b) + ","; });
v.pop_back();
return c + "b|" + v;
}
inline FreeVote(std::vector<candidate::id_type> votes) : votes(votes) {}
FreeVote(candidate::id_type *votes)
{
while (*votes)
{
candidate::id_type h = *votes++;
this->votes.push_back(h);
}
}
};
}