-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial_list.cpp
143 lines (127 loc) · 4.74 KB
/
partial_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <bits/stdc++.h>
#include "vote.hpp"
#include "partylist.cpp"
#include "candidate.hpp"
#include "settings.h"
namespace panachage
{
class PartialListVote : public Vote
{
partylist *plist;
std::vector<candidate::id_type> struckthrough;
std::vector<candidate::id_type> added;
public:
void count(std::vector<partylist *> lists)
{
for (auto c : plist->candidate_votes)
{
candidate::id_type id = c.first;
if (std::count(struckthrough.begin(), struckthrough.end(), id) == 0)
{
plist->candidate_votes[id] += this->copies;
}
}
for (candidate::id_type id : added)
{
for (partylist *listi : lists)
{
std::vector<candidate::id_type> cs = listi->candidateList();
if (!!std::count(cs.begin(), cs.end(), id))
{
listi->candidate_votes[id] += this->copies;
}
}
}
plist->at_large_votes += (struckthrough.size() - added.size()) * this->copies;
}
/*
* * No candidate can be found in both `struckthrough` and `added`.
* * No candidate can be voted for more than the specified number of times.
* * No candidate can appear more than once in `struckthrough`.
* * `added` cannot be longer than `struckthrough`.
*
*/
bool validate()
{
std::vector<candidate::id_type> res;
std::set_intersection(struckthrough.begin(), struckthrough.end(), added.begin(), added.end(), std::back_inserter(res));
if (res.size() > 0)
{
return false;
}
for (candidate::id_type id : added)
{
int substitutions = std::count(added.begin(), added.end(), id);
std::vector<candidate::id_type> cs = plist->candidateList();
int onlist = !!(std::count(cs.begin(), cs.end(), id));
int votes_for = substitutions + onlist;
if (votes_for > config.max_4_cand)
{
return false;
}
}
for (candidate::id_type id : struckthrough)
{
if (std::count(struckthrough.begin(), struckthrough.end(), id) > 1)
{
return false;
}
}
if (added.size() > struckthrough.size())
{
return false;
}
return true;
}
inline bool operator<(const Vote &vote) const
{
const PartialListVote *f = dynamic_cast<const PartialListVote *>(&vote);
if (!f)
return true;
return this->plist->id < f->plist->id ||
this->struckthrough < f->struckthrough ||
this->added < f->added;
}
std::string encode() const
{
std::string c = copies == 1 ? "" : std::to_string(copies) + "*";
std::string lid = std::to_string(plist->id);
std::string s = std::accumulate(struckthrough.begin(), struckthrough.end(), std::string(),
[](std::string a, candidate::id_type b)
{ return a + std::to_string(b) + ","; });
std::string a = std::accumulate(added.begin(), added.end(), std::string(), [](std::string a, candidate::id_type b)
{ return a + std::to_string(b) + ","; });
s.pop_back();
a.pop_back();
return c + "p|" + lid + "|" + s + "|" + a;
}
inline PartialListVote(partylist *plist,
std::vector<candidate::id_type> struckthrough,
std::vector<candidate::id_type> added = {})
: plist(plist), struckthrough(struckthrough), added(added) {};
PartialListVote(partylist *plist,
const candidate::id_type *struckthrough,
const candidate::id_type *added = nullptr)
: plist(plist)
{
candidate::id_type h;
while (*struckthrough)
{
h = *struckthrough++;
this->struckthrough.push_back(h);
}
if (added == nullptr)
{
this->added;
}
else
{
while (*added)
{
h = *added++;
this->added.push_back(h);
}
}
};
};
}