This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationalVote.cpp
More file actions
94 lines (78 loc) · 2.58 KB
/
Copy pathRationalVote.cpp
File metadata and controls
94 lines (78 loc) · 2.58 KB
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
#include "RationalVote.h"
#include <ostream>
using namespace std;
// RationalVote - default constructor
RationalVote::RationalVote() : Vote(), preference_order_() {}
// RationalVote
RationalVote::RationalVote( const vector<string>& preference_order ) : Vote(set<string>( preference_order.begin(), preference_order.end() ) ), preference_order_(preference_order) {
//TODO: Some mechanism to verify that candidates_ and preference_order_ are of equal size;
}
// get_preference_order
vector<string> RationalVote::get_preference_order() const {
return preference_order_;
}
// win
set<string> RationalVote::win( const string& candidate ) const {
std::vector<string>::const_iterator i = preference_order_.begin();
while( i != preference_order_.end() && *(i++) != candidate );
return set<string>(i, preference_order_.end());
}
// beats
int RationalVote::beats( const string& c1, const string& c2 ) const {
vector<string>::const_iterator i = preference_order_.begin();
while( i != preference_order_.end() ) {
if( *i == c1 ) {
return 1;
}
else if( *i == c2 ) {
return 0;
}
i++;
}
return -1;
}
// cast to vector<string>
RationalVote::operator vector<string>() const {
return preference_order_;
}
// cast to IrrationalVote
/*RationalVote::operator IrrationalVote() const {
map<pair<string,string>,int> vote;
vector<string>::const_iterator i = preference_order_.begin();
vector<string>::const_iterator j;
while( i != preference_order_.end() ) {
j = i + 1;
while( j != preference_order_.end() ) {
vote[pair<string,string>(*i,*j)] = 1;
vote[pair<string,string>(*j,*i)] = 0;
j++;
}
i++;
}
IrrationalVote ret(candidates_, vote);
return ret;
}*/
// ostream <<
ostream& operator<<( ostream& out, const RationalVote& vote ) {
vector<string> order = vote.get_preference_order();
if( order.size() > 0 ) {
vector<string>::const_iterator i = order.begin();
out << *(i++);
while( i != order.end() ) {
out << " > " << *(i++);
}
}
return out;
}
// RationalVote < RationalVote
int operator<( const RationalVote& v1, const RationalVote& v2 ) {
return v1.get_preference_order() < v2.get_preference_order();
}
// RationalVote > RationalVote
int operator>( const RationalVote& v1, const RationalVote& v2 ) {
return v2 < v1;
}
// RationalVote == RationalVote
int operator==( const RationalVote& v1, const RationalVote& v2 ) {
return v1.get_preference_order() == v2.get_preference_order();
}