-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.h
129 lines (98 loc) · 2.9 KB
/
model.h
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
/*
Copyright (C) 2018-2019 Michelle Blom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _MODEL_H
#define _MODEL_H
#include<vector>
#include<set>
#include<exception>
#include<boost/lexical_cast.hpp>
#include<boost/filesystem.hpp>
#include<boost/tokenizer.hpp>
#include<boost/algorithm/string.hpp>
#include<map>
#ifndef _WIN32
#include<sys/time.h>
#endif
typedef std::vector<int> Ints;
typedef std::vector<double> Doubles;
typedef std::vector<Doubles> Doubles2d;
typedef std::set<int> SInts;
typedef std::set<SInts> SInts2d;
typedef std::set<SInts2d> SInts3d;
typedef std::vector<Ints> Ints2d;
typedef std::vector<SInts> VSInts;
typedef std::vector<std::string> Strings;
struct mytimespec
{
double seconds;
};
void GetTime(struct mytimespec* t);
void Split2Ints(const std::string &line,
const boost::char_separator<char> &sep, Ints &r);
void Split(const std::string &line, const boost::char_separator<char> &sep,
std::vector<std::string> &r);
struct Config
{
int ncandidates;
double totalvotes;
std::map<int,int> id2index;
Config() : ncandidates(0), totalvotes(0) {}
};
class STVException
{
private:
const std::string message;
public:
STVException(const STVException &me) : message(me.what()){}
STVException(const std::string &str) : message(str) {}
const std::string& what() const { return message; }
};
struct Candidate
{
int id;
int index;
double sum_votes;
Ints ballots;
Ints ballots_where_appear;
// For simulation
int sim_votes;
Ints sim_ballots;
int standing;
Candidate() : id(0), index(0), sum_votes(0),
sim_votes(0), standing(1) {}
void Reset(){
standing = 1;
sim_votes = sum_votes;
sim_ballots.clear();
sim_ballots.insert(sim_ballots.end(), ballots.begin(),
ballots.end());
}
};
typedef std::vector<Candidate> Candidates;
struct Ballot
{
int tag;
double votes; // Number of votes present with this signature
Ints prefs;
};
typedef std::vector<Ballot> Ballots;
template <typename T>
T ToType(const std::string &s);
typedef std::map<std::vector<int>,int> I2Map;
void select_random_ballots(int nballots, long seed, Ints &r);
bool ReadReportedBallots(const char *path, Ballots &ballots,
Candidates &candidates, Config &config, double errorp);
bool ReadActualBallots(const char *path, Ballots &ballots,
const Candidates &candidates, const Config &config);
#endif